har*_*sha 0 python opencv image-processing computer-vision opencv-contour
目标是检测大圆圈内的小圆圈。
目前我所做的是将图像转换为灰度并应用阈值(cv2.THRESH_OTSU),从而生成此图像

在此之后,我使用我在 stackoverflow 上找到的椭圆形内核使用 findcontours 应用 Morph open 过滤掉了大对象
有人可以指导我通过正确的路径做什么以及我在哪里出错。
下面是我一直在处理的附加代码
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()
Run Code Online (Sandbox Code Playgroud)
谢谢!
通过将图像转换为灰度,您会丢弃很多有用的信息。
为什么不利用您正在寻找的斑点是唯一红色/橙色的事实?
我将饱和通道与红色通道相乘,这给了我这个图像:
现在找到白色斑点变得微不足道。
为这些通道尝试不同的权重,或者先应用阈值。有很多方法。尝试不同的照明、不同的背景,直到获得理想的图像处理输入。