我使用颜色检测(尤其是紫色)和圆形检测来检测下图中的花粉对象(紫色的圆形)。你知道吗
然后我在检测到的物体上写上字母“p”。不幸的是,它没有像我预期的那样工作。你知道吗
我可以修复它,如果我改变半径,但这不是一个好主意,因为我仍然有许多类似的图像与不同的半径处理。我想重点是如何知道这张图片中紫色的确切范围。一般来说,我想知道如何获得图像中任意颜色的范围。有些人给了我一个示例代码,但效果不好。你知道吗
这是我的程序。你知道吗
import cv2
import numpy as np
# In[2]:
path = "./sample.JPG"
font = cv2.FONT_HERSHEY_COMPLEX
# In[3]:
def image_resize(image, width = None, height = None, inter = cv2.INTER_AREA):
# initialize the dimensions of the image to be resized and
# grab the image size
dim = None
(h, w) = image.shape[:2]
# if both the width and height are None, then return the
# original image
if width is None and height is None:
return image
# check to see if the width is None
if width is None:
# calculate the ratio of the height and construct the
# dimensions
r = height / float(h)
dim = (int(w * r), height)
# otherwise, the height is None
else:
# calculate the ratio of the width and construct the
# dimensions
r = width / float(w)
dim = (width, int(h * r))
# resize the image
resized = cv2.resize(image, dim, interpolation = inter)
# return the resized image
return resized
# In[4]:
iml = cv2.imread(path,cv2.IMREAD_COLOR)
img = image_resize(iml,width=960)
# In[5]:
hsv = cv2.cvtColor(img,cv2.COLOR_BGR2HSV)
hsv = cv2.medianBlur(hsv,5)
#mask = cv2.inRange(hsv, (120, 180, 50), (160, 255, 255))
mask = cv2.inRange(hsv, (105, 100,50), (160, 255, 255))
#mask = cv2.inRange(hsv, (126, 142, 57), (145, 255, 255))
#cv2.GaussianBlur(cimg, (9,9),3)
#cimg = cv2.medianBlur(cimg,5)
output = cv2.bitwise_and(hsv, hsv, mask = mask)
#circles = cv2.HoughCircles(mask[:,:,0],cv2.HOUGH_GRADIENT,1,mask.shape[0]/16,param1=15,param2=20,minRadius=18,maxRadius=38)
circles = cv2.HoughCircles(output[:,:,0],cv2.HOUGH_GRADIENT,1,output.shape[0]/16,param1=15,param2=20,minRadius=15,maxRadius=30)
print(len(circles))
circles = np.uint16(np.around(circles))[0,:]
# In[6]:
for i in circles:
cv2.putText(img,'P',(i[0],i[1]), font, 0.5,(0,255,0),1,cv2.LINE_AA)
# In[7]:
cv2.imwrite("./result.jpg",img)
请注意,这个答案并不意味着一个解决方案,但可能是一个新的观点,以实现您的任务。即使它在某些情况下可以工作,它也可能不够健壮,无法实现任何流程的自动化。这就是说,转换为HSV颜色空间的问题是,如果图像(如您的情况)上绘制了类似的颜色对象,则很难用
cv2.inRange()
将一个对象与另一个对象区分开来。我试着修改一下你的代码,并举例说明如何处理这个问题。你知道吗首先,你可以尝试寻找所有轮廓后,大津在图像上,并过滤出最大的(甜甜圈)和其他小的与你选择的标准出来。你知道吗
一旦你有了它,你就可以在轮廓周围做一个ROI。然后我会尝试对每个ROI执行
cv2.inRange()
。你知道吗之后,我会再次搜索轮廓上的每一个投资回报率和计数白色像素或作出一个“圆形”的轮廓标准。如果他们通过,这意味着它有很多像素的范围,并绘制字母T。希望这有点帮助。干杯!你知道吗
示例:
结果:
相关问题 更多 >
编程相关推荐