简体   繁体   中英

How to find filled rectangles

I am trying to find some steps to determine using OpenCV the following rectangle is filled with dark gray and have a border colored with white. The image background is black. The text doesn't matter.

What I have tried so far is: blur, convert to grayscale, otsu thresholding and finally findContours. After that I use arcLength and aproxPolyDP and boundingRect to find the box but I only find the outer box only which it is classified as filled. I need to find the color of contour also.

Here is the image that I use:

图片

I tried connectedcommponects and its giving the desired output.

import cv2
import numpy as np
from matplotlib import pyplot as plt

img = cv2.imread("/content/4RihR.png")
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
_, img_bin = cv2.threshold(gray, 150, 255, cv2.THRESH_BINARY)
cv2_imshow("pre_processed", img_bin)
img_bin_bk = ~img_bin
_, labels, stats, _ = cv2.connectedComponentsWithStats(img_bin_bk, connectivity=4, ltype=cv2.CV_32S)
for x, y, w, h, area in stats[2:]:
  print(area)
  cv2.rectangle(img, (x, y), (x + w, y + h), (255, 255, 0), 2)
cv2.imshow("out_img", img)

Output:

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM