简体   繁体   中英

OpenCV detecting largest rectangle yields puzzling results

My aim is to detect the largest rectangle in an image, whether its skewed or not. After some research and googling I came up with a code that theoretically should work, however in half of the cases I see puzzling results.

I used OpenCV for Android, here is the Code:

private void find_parallels() {
    Utils.bitmapToMat(selectedPicture,img);
    Mat temp = new Mat();
    Imgproc.resize(img,temp,new Size(640,480));
    img = temp.clone();

    Mat imgGray = new Mat();
    Imgproc.cvtColor(img,imgGray,Imgproc.COLOR_BGR2GRAY);

    Imgproc.GaussianBlur(imgGray,imgGray,new Size(5,5),0);

    Mat threshedImg = new Mat();
    Imgproc.adaptiveThreshold(imgGray,threshedImg,255,Imgproc.ADAPTIVE_THRESH_GAUSSIAN_C,Imgproc.THRESH_BINARY,11,2);

    List<MatOfPoint> contours = new ArrayList<>();
    Mat hierarchy = new Mat();
    Mat imageContours = imgGray.clone();
    Imgproc.cvtColor(imageContours,imageContours,Imgproc.COLOR_GRAY2BGR);

    Imgproc.findContours(threshedImg,contours,hierarchy,Imgproc.RETR_TREE,Imgproc.CHAIN_APPROX_SIMPLE);
    max_area = 0;
    int num = 0;

    for (int i = 0; i < contours.size(); i++) {
        area = Imgproc.contourArea(contours.get(i));

        if (area > 100) {
            MatOfPoint2f mop = new MatOfPoint2f(contours.get(i).toArray());
            peri = Imgproc.arcLength(mop, true);
            Imgproc.approxPolyDP(mop, approx, 0.02 * peri, true);

            if(area > max_area && approx.toArray().length == 4) {
                biggest = approx;
                num = i;
                max_area = area;
            }

        }

    }

    selectedPicture = Bitmap.createBitmap(640,480, Bitmap.Config.ARGB_8888) ;
    Imgproc.drawContours(img,contours,num,new Scalar(0,0,255));
    Utils.matToBitmap(img, selectedPicture);

    imageView1.setImageBitmap(selectedPicture);}

In some cases it works excellent as can be seen in this image(See the white line between monitor bezel and screen.. sorry for the color): Example that works: 有效的例子

However when in this image, and most images where the screen is greyish it gives crazy result. Example that doesn't work: 例子不起作用

Try use morphology, dilate and then erode with same kernel should make it better. Or use pyrDown + pyrUp, or just blur it.

In short use low-pass filter class of methods, because your object of interest is much larger than noise.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM