Detect boundaries of a document in an image using opencv java

Ankit Aggarwal picture Ankit Aggarwal · Nov 20, 2014 · Viewed 8.9k times · Source

I want to detect the 4 corners of the document in android. Iam using opencv library.

Iam using the following approach- (1) grayscale the image (2) Apply median blur (3) Apply adaptive threshold (4) Canny Edge detection (5) find contours (6) find the largest contour (7) get the edges and corners of the largest contour

My code is

srcImg = Utils.loadResource(this, R.drawable.test1, Highgui.CV_LOAD_IMAGE_COLOR);
Imgproc.cvtColor(srcImg, srcImg, Imgproc.COLOR_BGR2GRAY);
Imgproc.medianBlur(srcImg, srcImg, 9);
Imgproc.adaptiveThreshold(srcImg, srcImg, 255, Imgproc.ADAPTIVE_THRESH_MEAN_C, Imgproc.THRESH_BINARY_INV, 11, 2);
Imgproc.Canny(srcImg, srcImg, 50, 80);


Imgproc.findContours(srcImg,contours , hierarchy, Imgproc.RETR_TREE, Imgproc.CHAIN_APPROX_SIMPLE);

After canny edge detection, the outlining boundary at many points are broken due to which the largest contour detected in the image is not the actual boundary of the document. In some images the largest contour is showing some part of the boundary but in some images it is pointing to the center of the document or completely outside the document

(A)How do I proceed further in this condition ?

Another approach i applied was to apply houghes line transform and calculate the point of intersection of the lines which are intersecting at 90 degree angle but in that case am not getting exactpoints. when I put 88

(B)How to handle that much number of points

(C)or do I need to follow completely different approach ?

Answer