我对图像处理很陌生。我正在使用 opencv 和 python。 我熟悉查找轮廓的常规方法,但在这种情况下,我想忽略每个正方形作为轮廓,但想要在每个所述形状上绘制轮廓,如图所示。
目前,在阈值化之后,我得到了每个正方形的轮廓。 opencv 库中是否有任何简单的函数来检测这些完整的大形状而不是小方 block 。
最佳答案
因为要提取黄色矩形区域,所以:
1. Read
2. Exrtact green channel
3. Do morph-close-op and threshold
4. Findcontours and filter by Area
#!/usr/bin/python3
# 2018.01.07 14:04:18 CST
# 2018.01.07 14:20:15 CST
import cv2
## 1. Read
img = cv2.imread("img01.png")
## 2. Exrtact green channel
g = img[...,1]
## 3. Do morph-close-op and Threshold
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (5,5))
morphed = cv2.morphologyEx(g,cv2.MORPH_CLOSE, kernel)
th, threshed = cv2.threshold(morphed, 100, 255, cv2.THRESH_OTSU)
## 4. Findcontours and filter by Area
cnts = cv2.findContours(threshed, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)[-2]
canvas = img.copy()
AREA = img.shape[0]*img.shape[1]/20
for cnt in cnts:
if cv2.contourArea(cnt) < AREA:
cv2.drawContours(canvas, [cnt], -1, (0,255,0), 2, cv2.LINE_AA)
##
cv2.imwrite("res.png", canvas)
关于python - 如何检测由较小轮廓组成的较大轮廓?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48134525/