I'm trying to get only the center retangle in this image by using connected cv2.connectedComponentsWithStats. However I don't know how to get only the center image.
My attempt was this:
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
ret, bw = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY_INV+cv2.THRESH_OTSU)
img = cv2.threshold(bw, 127, 255, cv2.THRESH_BINARY)[1] # ensure binary
def undesired_objects(image):
image = image.astype('uint8')
nb_components, output, stats, centroids = cv2.connectedComponentsWithStats(image, connectivity=4)
sizes = stats[:, -1]
max_label = 1
max_size = sizes[1]
for i in range(2, nb_components):
if sizes[i] > max_size:
max_label = i
max_size = sizes[i]
img2 = np.zeros(output.shape)
img2[output == max_label] = 255
cv2.imshow("Biggest component", img2)
cv2.waitKey()
undesired_objects(img)
