我想使用霍夫变换来检测该图像中的紫色圆圈(紫色花粉)。
但是结果表明,它也检测出其他因素。
我该怎么做才能改善此圈子检测?
这是我的代码:
import os, sys
import numpy as np
import cv2
from PIL import Image
def circle_detect(path, filename):
iml = cv2.imread(path,cv2.IMREAD_COLOR)
img = image_resize(iml,height=960)
img_array = np.array(img)
cimg = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
cimg = cv2.medianBlur(cimg,5)
#Circle detection to detect pollen in big images, return the center's coordinates and radius of circles in array
circles = cv2.HoughCircles(cimg,cv2.HOUGH_GRADIENT,1,10,param1=15,param2=20,minRadius=10,maxRadius=25)
circles = np.uint16(np.around(circles))[0,:]
for j in range(len(circles)):
cv2.circle(img,(circles[i][0],circles[i][1]),circles[i][2],(0,255,0),2)
cv2.imwrite("export_circle/"+"output"+filename,img)
答案 0 :(得分:0)
每当您播放彩色图像时,最好选择HSV色彩空间。对于提供的图像,我将图像转换为HSV并获得了色相通道。在此色调频道上查找圈子。
代码:
import cv2
im1 = cv2.imread(r'C:\Users\Jackson\Desktop\pollen.jpg')
#--- convert to hsv color space ---
hsv = cv2.cvtColor(im1, cv2.COLOR_BGR2HSV)
im2 = im1.copy()
#--- find circles in the hue channel ---
circles = cv2.HoughCircles(hsv[:,:,0],cv2.HOUGH_GRADIENT, 1, 75, param1=15, param2=30, minRadius=0,maxRadius=400)
circles = np.uint16(np.around(circles))
for i in circles[0,:]:
# draw the outer circle
cv2.circle(im2,(i[0],i[1]),i[2],(0,255,0),2)
# draw the center of the circle
cv2.circle(im2,(i[0],i[1]),2,(0,0,255),3)
cv2.imshow("im2", im2)
结果:
您必须调整参数才能正确找到圆