It seems that your image has only 4 black squares, so you need to do the following:
- Convert Image to Gray
- At the doorstep
- Find black outlines (before doing this in OpenCV, you must invert your image, because by default, OpenCV finds white outlines)
- Scroll through these paths and find the bounding box.
Make a check:
A) The rectangle area is larger than some constant (in my solution it was 100 )
B) The width / height of the rectangle is about 1.0 (in my soul it was [0.9, 1.1] )
The code:
Mat img = imread("test.jpg"), gray; vector<Vec4i> hierarchy; vector<vector<Point2i> > contours; cvtColor(img, gray, CV_BGR2GRAY); threshold(gray, gray, 100, 255, THRESH_BINARY); bitwise_not(gray, gray); findContours(gray, contours, hierarchy, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE); for(size_t i=0; i<contours.size(); i++) { Rect rect = boundingRect(contours[i]); double k = (rect.height+0.0)/rect.width; if (0.9<k && k<1.1 && rect.area()>100) { drawContours(img, contours, i, Scalar(0,0,255)); } } imshow("result", img); waitKey();
Result:
Also read this discussion. - you do not need these 4 squares to detect paper.
source share