I have the following input image:
and I do like to crop the inner max rectangle like this (Expected Output):
but when I do contour detection I get the external rectangle (Current Result):
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.
I have solved this issue by: