gpt4 book ai didi

Python OpenCV : draw outer contours inside a specific contour

转载 作者:太空宇宙 更新时间:2023-11-03 21:41:17 25 4
gpt4 key购买 nike

我是 OpenCV 的新手,我正在尝试在特定轮廓内绘制外部轮廓。这是我用来澄清的图像(已经灰度化、阈值化等)

enter image description here

我想要的是找到外矩形内的所有圆圈(总共 120 个)的轮廓。

contours =  cv2.findContours(image, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

所以我基本上使用了 RETR_EXTERNAL 但它只返回外矩形。我尝试使用 RETR_TREE 但在那种情况下,它返回给我的轮廓比圆圈多,出于某种我不明白的原因。澄清一下:我只想要每个圆 1 个轮廓。

如何使用 RETR_EXTERNAL 并忽略外部轮廓(矩形),以便它只返回圆圈?

最佳答案

按区域过滤轮廓:

我按区域过滤轮廓以隔离圆圈。我认为您可能需要处理 thresholding图像多一点,以帮助从图像中的边界描绘圆圈。我使用了以下代码:

import cv2
import numpy as np

img = cv2.imread("/your/path/C03eN.jpg")

def find_contours_and_centers(img_input):

img_gray = cv2.cvtColor(img_input, cv2.COLOR_BGR2GRAY)
img_gray = cv2.bilateralFilter(img_gray, 3, 27,27)
#(T, thresh) = cv2.threshold(img_input, 0, 100, 0)
_, contours_raw, hierarchy = cv2.findContours(img_gray, cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
contours = [i for i in contours_raw if cv2.contourArea(i) > 20]
contour_centers = []

for idx, c in enumerate(contours):
M = cv2.moments(c)
cX = int(M["m10"] / M["m00"])
cY = int(M["m01"] / M["m00"])
samp_bounds = cv2.boundingRect(c)
contour_centers.append(((cX,cY), samp_bounds))

print("{0} contour centers and bounds found".format(len(contour_centers)))

contour_centers = sorted(contour_centers, key=lambda x: x[0])

return (contours, contour_centers)

conts, cents = find_contours_and_centers(img.copy())

circles = [i for i in conts if np.logical_and((cv2.contourArea(i) > 650),(cv2.contourArea(i) < 4000))]

cv2.drawContours(img, circles, -1, (0,255,0), 2)

cv2.imwrite("/your/path/tester.jpg", img)

结果:

enter image description here

编辑:

如果您只想提取较大外部矩形内的图像部分,使用 cv2.RETR_EXTERNAL,让您专注于内部圆圈,您可以执行如下操作:

import cv2
import numpy as np

img = cv2.imread("/your/path/C03eN.jpg")

def find_contours_and_centers(img_input):

img_gray = cv2.cvtColor(img_input, cv2.COLOR_BGR2GRAY)
img_gray = cv2.bilateralFilter(img_gray, 3, 27,27)
#(T, thresh) = cv2.threshold(img_input, 0, 100, 0)
#_, contours_raw, hierarchy = cv2.findContours(img_gray, cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
_, contours_raw, hierarchy = cv2.findContours(img_gray, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
contours = [i for i in contours_raw if cv2.contourArea(i) > 20]
contour_centers = []

for idx, c in enumerate(contours):
M = cv2.moments(c)
cX = int(M["m10"] / M["m00"])
cY = int(M["m01"] / M["m00"])
samp_bounds = cv2.boundingRect(c)
contour_centers.append(((cX,cY), samp_bounds))

print("{0} contour centers and bounds found".format(len(contour_centers)))

contour_centers = sorted(contour_centers, key=lambda x: x[0])

return (contours, contour_centers)

conts, cents = find_contours_and_centers(img.copy())

x,y,w,h = cv2.boundingRect(conts[0])

cropped = img[y+10:y+(h-10),x+10:x+(w-10)]

cv2.imwrite("/your/path/cropped.jpg", cropped)

结果:

enter image description here

关于Python OpenCV : draw outer contours inside a specific contour,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51919841/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com