Android OpenCV MatToBitmap导致黑色图像

时间:2014-05-14 22:06:04

标签: 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);
}

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

原始框架 Original frame

结果框架 Result frame

预期的位图 Intended bitmap

1 个答案:

答案 0 :(得分:0)

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

然后您正在使用具体的破坏性操作来操纵该图像: 1)Canny边缘检测,用图像中存在的边缘的二进制掩码替换原始图像内容; 2)模糊; 3)轮廓查找操作,这也是破坏性的,因为当OpenCV功能发现并遍历它们时“掠过”轮廓。

结果是一个类似Canny边缘的图像,其中缺少许多边缘。当您随后使用图像内容更新控件“img”时,这正是您的输出显示的内容。

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