Canny Edge shows a clear rectangle, how do I make that ROI?

asked 2018-11-01 08:17:31 -0500

zscore gravatar image

Hi,

I am trying to make the rectangular grid of pixels on LCD my ROI, for this

  1. Threshold image
  2. Detect edges using Canny()
  3. Try to find Contours

These are the resultant images I have, image description

How can I make the inner rectangle my ROI and subset the image to see only that? I tried findContours() but it gives multiple broken contours. You can see in the image below -- different color represents different contour. I used the following code to generate this,

```C++

 // find edges
Mat im1_canny;
Canny(im1_thresh, im1_canny, 50, 100);

// find contours
vector<vector<Point> > contours;
vector<Vec4i> hierarchy;
findContours(im1_canny, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, Point(0, 0));

// draw contours
RNG rng;
Mat im1_contours = Mat::zeros(im1_canny.size(), CV_8UC3);
for (int i = 0; i < contours.size(); i++) {
    Scalar color = Scalar(rng.uniform(0, 255), rng.uniform(0, 255), rng.uniform(0, 255));
    drawContours(im1_contours, contours, i, color, 2, 8, hierarchy, 0, Point());
}

```

Result of findContours() image description

edit retag flag offensive close merge delete

Comments

no, your Canny image is already "broken". it does not have a rectangle, just line segments.

you could try morphology operations to close the holes, or use findContours() on the inverted threshold() image (so it will find the "inner" contours)

berak gravatar imageberak ( 2018-11-01 08:44:32 -0500 )edit

I once had a similar problem and solved it with at first using morphological operations. Since you only have these single lines I would try rectilinear kernels e.g. cv:: Mat horizontal_element = cv:: getStructuringElement(cv::MORPH_RECT, cv:: Size(1, 5)); Then I used a closing and after that the Douglas-Peucker-Algorithm. I iteratively checked if my resulting contour could be approximated with only 4 points. I could get quite good results for my 4 rectangle corners with approxPolyDP.

Grillteller gravatar imageGrillteller ( 2018-11-02 03:51:47 -0500 )edit