gpt4 book ai didi

python - 检测具有特定颜色的圆形物体

转载 作者:太空宇宙 更新时间:2023-11-03 22:07:56 24 4
gpt4 key购买 nike

我的目标是检测下图中所有的紫色花粉,并在其中放入字母“P”。 enter image description here

但结果显示,它总是误认一个黑色区域。

enter image description here

更改圆检测中的半径没有帮助,因为我还有很多相似的图像要处理。那么我应该怎么做才能改善它呢?

这是我的代码:

# coding: utf-8


import cv2
import numpy as np


path = "./sample.JPG"
font = cv2.FONT_HERSHEY_COMPLEX

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

iml = cv2.imread(path,cv2.IMREAD_COLOR)
img = image_resize(iml,width=960)

cimg = cv2.cvtColor(img,cv2.COLOR_BGR2HSV)
#cv2.GaussianBlur(cimg, (9,9),3)
cimg = cv2.medianBlur(cimg,5)

circles = cv2.HoughCircles(cimg[:,:,0],cv2.HOUGH_GRADIENT,1,cimg.shape[0]/16,param1=15,param2=20,minRadius=18,maxRadius=38)
circles = np.uint16(np.around(circles))[0,:]

for i in circles:
cv2.putText(img,'P',(i[0],i[1]), font, 0.5,(0,255,0),1,cv2.LINE_AA)

cv2.imwrite("./output.jpg",img)

此外,我还尝试使用颜色检测,因为我要检测的所有颜色都相同(紫色)。我关注instructions here但还是不行。

最佳答案

我认为如果您仔细选择正确的 hsv 范围,您可以直接在 HSV 颜色空间中检测到紫色。此颜色图取 self 的其他答案。

enter image description here

我为此任务选择Hue(120,160)、Saturation(180, 255)、Value(50, 255)以获取 mask 。

hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
mask = cv2.inRange(hsv, (120, 180, 50), (160, 255, 255))

然后就可以对mask进行处理了。

enter image description here

链接可能有帮助:

  1. How to define a threshold value to detect only green colour objects in an image :Opencv

  2. Choosing the correct upper and lower HSV boundaries for color detection with`cv::inRange` (OpenCV)

  3. RGB range for color red

关于python - 检测具有特定颜色的圆形物体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52591762/

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