user1538798
user1538798

Reputation: 1135

How to detect for a disjointed rectangle

I am trying to detect some box like symbol and post processed image looks such as this.

preprocessed image

I have tried using morphological operation to try to close the gap but the gap seem too big

 cv::Mat element = cv::getStructuringElement(cv::MORPH_ELLIPSE, cv::Size(5,5));
 cv::morphologyEx(frame, frame, cv::MORPH_DILATE, element);
 cv::morphologyEx(frame, frame, cv::MORPH_ERODE, element);

After using findcontour the picture looks as this

std::vector<std::vector<cv::Point> > contours;
std::vector< cv::Vec4i > hierarchy;

cv::findContours(frame, contours, hierarchy, cv::RETR_CCOMP, cv::CHAIN_APPROX_SIMPLE);

contour

Not really unexpected here as there are a number of gaps which are not close When i try to approxPolyDP, the contour upon testing for vertices = 4 and isContourConvex function test fails... (again somewhat within expectation)

I could only used minRectArea to force the whole contour to be a Rectangle.

 tempCandidate.push_back(cv::minAreaRect(cv::Mat(contours[i])));

It seem to work for this example but i have noisier image to handle.

  1. what other preprocessing strategy could i take to close the border?
  2. how could i make certain the area which i enclose using miniRectArea is rectangle? I check using contour length meeting minimal requirement and the RotatedRect must be of certain angles, breath and width

  3. Is my use of findContour correct?

PS: I do not know if the post constitutes one too many questions. If it does, I will spilt the questions up into a few posts.

Thanks in advance

Upvotes: 0

Views: 131

Answers (2)

L.C.
L.C.

Reputation: 1145

An alternative is to remove the thin lines that are not part of the box (for example by scaling down the image as the lines seem 1px thin, thinner than the box sides) then compute the convex hull of what's left. See https://docs.opencv.org/2.4/doc/tutorials/imgproc/shapedescriptors/hull/hull.html

Upvotes: 1

Florian Echtler
Florian Echtler

Reputation: 2513

I would suggest using a line detection algorithm, such as the Hough transform, on your contour picture. This will give you a set of lines, which you can then analyze on a higher level than the individual contour pixels (e.g. by looking at clusters of line intersections etc.).

Upvotes: 1

Related Questions