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

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,

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()

edit retag close merge delete

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.