gpt4 book ai didi

python - 计算机视觉 : Opencv Counting small circles inside big circle

转载 作者:行者123 更新时间:2023-12-02 16:45:10 26 4
gpt4 key购买 nike

这是我一直在处理的图像
enter image description here
目标是检测大圆圈内的小圆圈。
目前我所做的是将图像转换为灰度并应用阈值(cv2.THRESH_OTSU),从而产生此图像
enter image description here
在此之后,我使用我在stackoverflow上找到的椭圆形内核应用了Morph open使用findcontours过滤掉了大对象
结果图像是这样的
enter image description here
有人可以指导我通过正确的路径做什么以及我哪里出错了。
下面是我一直在处理的附加代码

import cv2
import numpy as np

# Load image, grayscale, Otsu's threshold
image = cv2.imread('01.jpg')
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]
#cv2.imwrite('thresh.jpg', thresh)

# Filter out large non-connecting objects
cnts = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
for c in cnts:
area = cv2.contourArea(c)
#print(area)
if area < 200 and area > 0:
cv2.drawContours(thresh,[c],0,0,-1)

# Morph open using elliptical shaped kernel
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (3,3))
opening = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, kernel, iterations=3)

# Find circles
cnts = cv2.findContours(opening, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
for c in cnts:
area = cv2.contourArea(c)
if area > 20 and area < 50:
((x, y), r) = cv2.minEnclosingCircle(c)
cv2.circle(image, (int(x), int(y)), int(r), (36, 255, 12), 2)

cv2.namedWindow('orig', cv2.WINDOW_NORMAL)
cv2.imshow('orig', thresh)
cv2.namedWindow('image', cv2.WINDOW_NORMAL)
cv2.imshow('image', image)
cv2.waitKey()
谢谢!

最佳答案

通过将图像转换为灰度,您会丢弃许多有用的信息。
为什么不使用您正在寻找的 Blob 是唯一红色/橙色的事实呢?
我将饱和 channel 与红色 channel 相乘,得到了这个图像:
enter image description here
现在找到白色 Blob 变得微不足道。
对这些 channel 尝试不同的权重,或先应用阈值。有很多方法。尝试不同的照明、不同的背景,直到获得图像处理的理想输入。

关于python - 计算机视觉 : Opencv Counting small circles inside big circle,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62793937/

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