2

I have the following input image:

input

and I do like to crop the inner max rectangle like this (Expected Output):

res

but when I do contour detection I get the external rectangle (Current Result):

res2

import cv2
import numpy as np

res = cv2.imread("input.png", 0)

k0 = 5
dgauss = cv2.GaussianBlur(res, (k0, k0), 0)

op = cv2.MORPH_CLOSE
morph_elem = cv2.MORPH_RECT
morph_size = 51
element = cv2.getStructuringElement(morph_elem, (2*morph_size + 1, 2*morph_size+1), (morph_size, morph_size))
mph = cv2.morphologyEx(dgauss, op, element)

contours = cv2.findContours(mph, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)[0]

c_th = 10000

for idx, cnt in enumerate(contours):
    if(cv2.contourArea(cnt)>c_th):
        x,y,w,h = cv2.boundingRect(cnt)
        print(x,y,w,h)
        cv2.rectangle(res,(x,y),(x+w,y+h),128,1)

cv2.imshow("final", res)
cv2.waitKey(0)

Can you please tell me how can I get the inner rectangle without hard-coding the contour coordinates (x, y, w, h)? thanks in advance.

Jeru Luke
  • 20,118
  • 13
  • 80
  • 87
Bilal
  • 3,191
  • 4
  • 21
  • 49
  • Can you repeat the contour detection just on the ROI (i.e. the outer rectangle)? – Pam Mar 14 '22 at 09:31
  • 2
    I think you will need some kind of score-function that gives a rectangle a higher score if there are less black pixels/blocks at the border of of that rectangle. E.g. +1 score for every white pixel in the chosen rectangle and -10 for every black pixel that is connected by black pixels to the rectangle border. – Micka Mar 14 '22 at 09:31
  • input data please, before it was thresholded. and some context too! what do we see? how arbitrary can this shape be? – Christoph Rackwitz Mar 14 '22 at 10:18
  • @ChristophRackwitz [original_depth](https://cloud.univ-grenoble-alpes.fr/index.php/s/fyyqaRP6AQCWqCA) obtained from `L515 realsense` camera, the inner rectangle is a table that I want to detect. – Bilal Mar 14 '22 at 10:27
  • 1
    that data looks usable but the segmentation doesn't. the segmentation step must be corrected. I find this situation tiresome. some changes to the environment would make this easier. there's no (depth) separation between the square blocks on the corners and the flat area between them. further, the flat area extends beyond the right edge between the corner blocks, which gives you the problem in the boundingRect step. -- just take a convex hull, approxPolyDP it a little, and pick the four extreme corners of it (NE, NW, SE, SW directions). now you have the corner blocks. the rest is subtraction – Christoph Rackwitz Mar 14 '22 at 10:36

1 Answers1

0

I have solved this issue by:

  1. Aligning depth to the color.
  2. Segmenting the green table using HSV information as explained here
  3. Cropped the desired rectangle (ROI) using color information.
Bilal
  • 3,191
  • 4
  • 21
  • 49