How do I extract inner rectangle from a binary image of nested rectangles?

draw bounding boxes around objects in matlab
extract rectangle from image - matlab
matlab draw rectangle on image
matlab binary image edge
regionprops matlab
rectangle matlab
matlab draw rectangle on image matrix
matlab find contour binary image

I am trying to localize an LCD in a set of images but currently stuck at a certain point in the process. This is the binary image of a region of interest, how do I extract the inner rectangle from this -- or coordinates of the inside corner of the boundaries.

I tried the following, Find edges in the image using Canny(), use findContours() to find contours. From a list of contours, I run into two problems,

  1. Which contour to select?
  2. From this image, I got more than 10 contours and only the outside rectangle is a complete rectangle. Rest all contours are disjoint lines that together make the inside rectangle

This is my code,

// threshold
Mat im1_thresh;
threshold(im1, im1_thresh, 100, 200, THRESH_BINARY);        

// 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;
vector<Rect> boundRect(contours.size());
Mat im1_contours = Mat::zeros(im1_thresh.size(), CV_8UC3);
for (int i = 0; i < contours.size(); i++) {
    // draw each contour in a different color
    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());

    // draw bounding boxes around each contour in original image
    boundRect[i] = boundingRect(Mat(contours[i]));
    rectangle(im1, boundRect[i].tl(), boundRect[i].br(), color, 2, 8, 0);

Question: How do I localize the inner rectangle from the above binary image?

To solve the problem of disjoint contours -- I adjusted the threshold value until the contour was smooth. Blurring also helps sometimes. After that, I select the innermost contour.

I used the hierarchy array in the code above to select the relevant contour and create a bounding box using it on the original image. The hierarchy array is an N*4 matrix where N is the number of contours. For each contour, hierarchy contains information on related contours as [previous, next, child, parent]. You can see the value -1 if there is no related contour.

This is the hierarchy matrix for the image in question,

[-1,-1,1,-1] [-1,-1,2,0] [-1,-1,3,1] [-1,-1,-1,2]

I am interested in the last contour. It is childless and has 2 as parent. This is my implementation,

Rect boundRect;

// loop through the contours/hierarchy
for (int i = 0; i<contours.size(); i++) {
    // hierarchy is (previous, next, child, parent)
    // look for innermost -- no child but a parent exists
    if (hierarchy[i][2] == -1 && hierarchy[i][3] != -1) {
        // draw contour
        Scalar color = Scalar(255,0,0);
        drawContours(im1_contours, contours, i, color);

        // draw a bounding rectangle around original image
        boundRect = boundingRect(Mat(contours[i]));
        rectangle(im1,,, color);

// subset the image
Mat im1_roi;
im1_roi = im1(boundRect);

Find distorted rectangle in image (OpenCV), I have a distorted binary image containing a distorted rectangle; I need to find a How do I extract inner rectangle from a binary image of nested rectangles? To get a binary image showing where yellow pixels are you can do simple color detection in RGB color space. Since those look like pure yellow computer graphics regions, you can do this. % Extract the individual red, green, and blue color channels. redChannel = rgbImage (:, :, 1); greenChannel = rgbImage (:, :, 2);

To find the most rectangular contour, you could run minAreaRect to fit a rotated rectangle, then compare the result to the original contour.

The ratio of areas of each might be enough, depending on noise etc.

Once you select the most rectangular contour, using approxPoly to reduce to 4 points should give you a good approximation of the screen rectangle.

Drawing rectangles around binary image objects from region props , Hi, from the binary image above, I've tried drawing boundaries around the object and which is to use the regionprops to extract information of the location of the blobs. But I do not know how to draw a rectangle around them. The code first defines a Rectangle. It uses assignment to convert it into a RectangleF and then uses the RectangleF structure’s Round method to convert it back into a Rectangle. The program then draws all three rectangles on top of each other with different line widths and colors so you can see them all.

From this image, I got more than 10 contours and only the outside rectangle is a complete rectangle. Rest all contours are disjoint lines that together make the inside rectangle

You are using the default Canny apertureSize = 3, try to increase it to 5 or 7 and it will detect the inner contour as one instead of multiple small contours. Try this:

int apertureSize = 5;
Canny(im1_thresh, im1_canny, 50, 100, apertureSize);

I ran into another issue last night that if there is a dead pixel on the screen, it also shows up in the list of contours.

Check the area of each contour to make sure it is larger than the minimum acceptable area. The size of the picture that you provided is 250x450, and the inner rectangle size is ~170x380. So you may set the minArea as 80% of 170x380 (= 0.8 * 64600). For instance, add this check to the loop before drawing.

float minArea = 0.8 * 64600;   
if (boundRect[i].area() > minArea ){
    rectangle(im1, boundRect[i].tl(), boundRect[i].br(), color, 2, 8, 0);

Computer Vision for Beginners: Part 4, From the first series, we saw how to draw a figure on an image. contour lines and reconstructs a full hierarchy of nested contours. We can extract the rectangle with the minimum area with the function cv2. We're going to implement bitwise operation with the masks which should be a binary image. Applying a “test” of some sort to determine if the contour should be removed. Accumulating a mask of contours to be removed. Applying the mask to the original image. To apply this algorithm to your own images you’ll need to take a second and consider Step 3 and determine the criterion you are using to remove contours.

Numpy draw rectangle, Origin point of the inner rectangle, e. Here, in this section, we will OpenCV provides the line() function to draw the line on the image. Rectangles of equal  Python | Draw rectangular shape and extract objects using OpenCV OpenCV is an open source computer vision and machine learning software library. Various image processing operations such as manipulating image and applying tons of filter can be done with the help of it.

CN100501760C - Title extracting device and its method, As the document image with the keyword from the title character rectangle extracted by string rectangle; FIG 77 is a diagram illustrating a pumping M if the inner header; FIG 16 is a diagram illustrating a plurality of nested rectangles. Method and apparatus for recognizing table area formed in binary image of document. My goal then is to examine only those rectangles that are inside the profile, but not contained by another rectangle in that profile. These rectangles were drawn in a slightly thicker line in the left half of Figure 4; the solution rectangle (in red) is one of these interesting rectangles.

Opencv area inside contour, 9 c++, found a contour in an image with findContours and its bounding box:. Extent is the ratio of contour area to bounding rectangle area. convex_hull: the binary image and label them: : Feature extraction and take color histogram (2) And contours can be nested to more than one level (So the structure is like a tree)​. For instance -triangle 60 is an arrow tip specification and inner sep=7pt adjusts the padding between the text in the boxes and the drawn rectangle shape. – Paul Gessler Jul 24 '14 at 15:40 Please see the update.

  • Can you simply shrink the outer contour to use as the inner? Alternately cv::erode the line before making the contour so it is a single thin line and inner and outer are the same
  • @MartinBeckett I get the outer contour using RETR_EXTERNAL parameter in findContours() but I cannot use it because the thinkness of line in the image keeps changing in the images from my dataset. Erosion will also impact the accuracy of the resultant contour. I believe I can use hierarchy of contours here to find a solution but not sure how
  • I ran into another issue last night that if there is a dead pixel on the screen, it also shows up in the list of contours. Then the assertion fails that screen is the minimum area rectangle. I am currently using contourArea() and values from trial and error to find the appropriate rectangle