Mike G
Mike G

Reputation: 33

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: 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: Example that doesn't work

Upvotes: 0

Views: 313

Answers (1)

Andrey  Smorodov
Andrey Smorodov

Reputation: 10852

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.

Upvotes: 1

Related Questions