android 边界框裁剪

6uxekuva  于 2023-06-28  发布在  Android
关注(0)|答案(1)|浏览(101)

我做了物体探测。我把它放在边界框里了。我在边界框内裁剪。但它不起作用在调试模式下,它卡在if行上。谢谢!

result.setLocation(location);
         mappedRecognitions.add(result);
 
                                    if ((int) location.left > 0 && (int) location.top > 0 && ((int) location.left + (int) location.width()) < xyBitmap.getWidth()
                                          && ((int) location.top + (int) location.height()) < xyBitmap.getHeight()) {
                                     mycroppedBitmap = Bitmap.createBitmap(xyBitmap,
                                            (int) location.left,
                                            (int) location.top,
                                            (int) location.width(),
                                            (int) location.height()
                                    );                                      
 mycroppedBitmap = Bitmap.createScaledBitmap(mycroppedBitmap, 224, 224, true);

坐标

DetectorActivity.java 文件
https://github.com/usertttwm/CropAndClassificationModel/blob/main/DetectorActivity.java
对于修剪后的分类:TFLiteObjectDetectionAPIModel.java
https://github.com/usertttwm/CropAndClassificationModel/blob/main/TFLiteObjectDetectionAPIModel
NEW!!!!!

if (1==1) {

mycroppedBitmap = Bitmap.createBitmap(croppedBitmap,
1,1,224,224

);

final String resultLabel = detector1.recognizeImage1(mycroppedBitmap);

结果裁剪位图


mycroppedBitmap

DETECTIVE(我正在尝试裁剪边界框)

更新!!!!!!!!!!!!!!!!!!!!!!

mappedRecognitions.add(result);//TRAY DETECTIVE
                            Bitmap copyOfOriginalBitmap = rgbFrameBitmap.copy(rgbFrameBitmap.getConfig(), true);
                            Bitmap secondBitmap = Bitmap.createBitmap(copyOfOriginalBitmap,
                                    (int)  location.left, (int)location.top, (int)location.right, (int)location.bottom);
                            Bitmap secondScaledBitmap = Bitmap.createScaledBitmap(secondBitmap, 224, 224, true);

                            final String resultLabel = detector1.recognizeImage1(secondScaledBitmap);

作物工作更新

result.setLocation(location);
                        mappedRecognitions.add(result);
                        Bitmap copyOfOriginalBitmap = rgbFrameBitmap.copy(rgbFrameBitmap.getConfig(), true);
                        Bitmap secondBitmap = Bitmap.createBitmap(copyOfOriginalBitmap,
                                (int)location.left, (int)location.top, (int)location.right-(int)location.left, (int)location.bottom-(int)location.top);
                        Bitmap secondScaledBitmap = Bitmap.createScaledBitmap(secondBitmap, 224, 224, true);

                        final String resultLabel = detector1.recognizeImage1(secondScaledBitmap);

CROP

------------------资源-------------
https://github.com/AarohiSingla/TFLite-Object-Detection-Android-App-Tutorial-Using-YOLOv5我用它来检测yolo的物体。
https://www.youtube.com/watch?v=ZepT3N6aNFM&t=881shttps://github.com/ivangrov/TensorFlow-Lite-Age-Gender-Estimation-on-Android/blob/main/tflite/TFLiteObjectDetectionAPIModel.java
我用它来通过作物和分类模型。

rsl1atfo

rsl1atfo1#

如果没有你提供的代码和信息,调试这个有点困难,但是这里有一些关于我如何解决这个问题的清晰步骤,也许你可以检查一下你的bug在哪里。我能看到的是,你试图从第一个裁剪的图像中裁剪第二个图像,这是错误的,你应该尝试从原始图像中裁剪第二个解释器的图像,所以这个过程应该看起来像这样:
1.首先创建一个原始位图的副本(因为您正在缩小它)

Bitmap originalBitmap = ...; // your original bitmap here
Bitmap copyOfOriginalBitmap = originalBitmap.copy(originalBitmap.getConfig(), true);

1.在你运行第一个解释器(使用originalBitmap)之后,你会得到坐标的范围是从0到解释器输入的宽度或高度(这可以改变,在你的日志中,我看到坐标被放大了,这是我们需要的)

Recognition myObject = ...; // you can get the detection that you want here, you can do this with a simple for loop
// now create second bitmap by cropping the original and using the scaled up coordinates
Bitmap secondBitmap = Bitmap.createBitmap(copyOfOriginalBitmap, myObject.location.left, myObject.location.top, myObject.location.right, myObject.location.bottom)
// now create the second scaled bitmap for your second Interpreter with the width and height of the interpreter size
Bitmap secondScaledBitmap = Bitmap.createScaledBitmap(secondBitmap, width, height, true)

你应该可以走了。

相关问题