python画轮廓,Python OpenCV:在特定轮廓内绘制外部轮廓

I'm new to OpenCV and I'm trying to draw the outer contours inside a specific contour. Here's the image I'm using to clarify (already grayscaled, thresholded, etc.)

C03eN.jpg

What I want is to find all the contours of the circles (120 in total), inside the outer rectangle.

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

So I basically used RETR_EXTERNAL for this but it only returns the outer rectangle. I tried using RETR_TREE but in that case it's returning me way more contours than there are circles, for some reason I don't understand. To clarify: I just want 1 contour per circle.

How can I use RETR_EXTERNAL and ignore the outer contour (rectangle), so that it only returns the circles?

解决方案

Filter the contours by area:

I filtered the contours by area to isolate the circles. I think you may need to work on thresholding the image a bit more to help delineate the circles from the border in the image. I used the following code:

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)

Result:

NTYUY.jpg

Edit:

If you just want to extract the portion of the image that is inside the larger outer rectangle, using cv2.RETR_EXTERNAL, allowing you to focus on the inner circles you can do something like the following:

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)

Result:

7Gwiu.jpg

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值