android - Android OpenCV MatToBitmap导致黑色图像

标签 android opencv bitmap mat

我在Android上使用OpenCV。我尝试检测最大的矩形并从图像中提取此矩形。我在下面使用此代码。拍摄照片后,将进行一些图像处理。我的目标是,我想检测一个类似于纸边框的矩形,并且如果其面积大于threshold_area,我想从原始位图中裁剪出来并在imageview上绘制。

Mat cropped_mat;

PictureCallback _jpgCallBack = new PictureCallback(){
    @Override
    public void onPictureTaken(byte[] data, Camera camera) {

        mCamera.startPreview();

        bitmap = BitmapFactory.decodeByteArray(data , 0, data .length); 
        Mat ImageMat = new Mat();
        Utils.bitmapToMat(cropped_mat, ImageMat); 
        double area = findLargestRectangle(ImageMat);

        if(area > THRESHOLD_AREA){

            mCamera.stopPreview();

            try{
                bitmap=Bitmap.createBitmap(cropped_mat.cols(), cropped_mat.rows(),Bitmap.Config.ARGB_8888);
                Utils.matToBitmap(cropped_mat, bitmap);
            }
            catch(Exception e){}

            img.setImageBitmap(bitmap);

        }
        else{
            takePicture();
        }

    }
};

这是我的findLargestRectangle方法。
private double findLargestRectangle(Mat imgSource) {


    Imgproc.Canny(imgSource, imgSource, 50, 50);


    Imgproc.GaussianBlur(imgSource, imgSource, new Size(5, 5), 5);


    List<MatOfPoint> contours = new ArrayList<MatOfPoint>();
    Imgproc.findContours(imgSource, contours, new Mat(), Imgproc.RETR_LIST, Imgproc.CHAIN_APPROX_SIMPLE);

    double maxArea = -1;
    int maxAreaIdx = -1;        

    MatOfPoint temp_contour = contours.get(0); //the largest is at the index 0 for starting point
    MatOfPoint2f approxCurve = new MatOfPoint2f();
    Mat largest_contour = contours.get(0);

    List<MatOfPoint> largest_contours = new ArrayList<MatOfPoint>();
    for (int idx = 0; idx < contours.size(); idx++) {

        temp_contour = contours.get(idx);
        double contourarea = Imgproc.contourArea(temp_contour);

        //compare this contour to the previous largest contour found
        if (contourarea > maxArea) {
            //check if this contour is a square
            MatOfPoint2f new_mat = new MatOfPoint2f( temp_contour.toArray() );
            int contourSize = (int)temp_contour.total();
            Imgproc.approxPolyDP(new_mat, approxCurve, contourSize*0.05, true);

            if (approxCurve.total() == 4) {
                maxArea = contourarea;
                maxAreaIdx = idx;
                largest_contours.add(temp_contour);
                largest_contour = temp_contour;
            }
        }
    }

    MatOfPoint temp_largest = largest_contours.get(largest_contours.size()-1);
    Rect rect = Imgproc.boundingRect(temp_largest);

    largest_contours = new ArrayList<MatOfPoint>();
    largest_contours.add(temp_largest);

    Core.rectangle(imgSource, new Point(rect.x,rect.y), new Point(rect.x+rect.width,rect.y+rect.height),new Scalar(0,0,255));
    cropped_mat= imgSource.submat(rect.y , rect.y + rect.height, rect.x, rect.x + rect.width);
    return Imgproc.contourArea(temp_largest);
}

但是结果如下。我哪里错了?

原始框架

结果框架

预期的位图

最佳答案

您正在onPictureTaken中处理的图像对象的引用已通过值传递到findLargestRectangle中。

然后,您将使用破坏性操作来专门处理该图像:
1)Canny边缘检测,它将原始图像内容替换为图像中存在的边缘的二进制掩码;
2)模糊;
3)轮廓查找操作,这也具有破坏性,因为此OpenCV功能在发现并遍历轮廓时会“吃掉”轮廓。

结果是Canny边缘状图像,其中许多边缘缺失。这就是您随后使用图像内容更新控件“img”时输出所显示的内容。

您可以通过在findLargestRectangle中创建图像对象的本地副本并处理该副本来解决此问题,以解决此问题。

关于android - Android OpenCV MatToBitmap导致黑色图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23665978/

相关文章:

iphone - 如何在 iphone 中找到图像的颜色边缘?

python - 如何通过 OpenCV 和 Python 通过索引从视频中获取帧?

c - 如何将单色 xbm 位图数据转换为 1 channel 8bpp 图像?

android - 调试服务主线程

android - 从同一应用程序内调用时内容提供者的权限被拒绝

android - 从后台进程更新 TabActivity 的子 Activity 的 View

Android位图形菱形

android - 我如何知道我的 Android 设备上打开了哪些端口以及如何关闭它们?

c++ - OpenCV 3.0.0 MSER 二进制掩码

java - android/graphics/bitmapfactory 上的 NoClassDefFoundError