颜色检测在网络摄像头图像上效率不高 [英] Color Detection not efficient on webcam images

查看:73
本文介绍了颜色检测在网络摄像头图像上效率不高的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在一个项目中,尝试检测特定表面(竞技场)上的绿色和红色圆圈.当我尝试使用该竞技场的数字版本(PNG图像)来执行此操作时,我可以成功检测到两个彩色圆圈.

这是表面的数字图像:

现在,我将这个竞技场打印在flex上(没有这两个彩色圆圈),并在其上手动放置彩色圆形硬币.但是,在通过1.3 MP网络摄像头捕获其图像后,颜色检测无法正常工作,并给出了错误的结果.

这是通过网络摄像头捕获的印刷竞技场:

为什么没有检测到颜色?我需要对摄像头图像进行后处理吗?我已经尝试通过cv2.filter2D来锐化图像,但是它也不起作用.

以下是用于从我的Python代码检测红色圆圈的代码段:

  ip_image = cv2.imread("image.png")内核= np.array([[-1,-1,-1],[-1,9,-1],[-1,-1,-1]])ip_image = cv2.filter2D(ip_image,-1,内核)#cv2.imshow(嗨",ip_image)hsv = cv2.cvtColor(ip_image,cv2.COLOR_BGR2HSV)red_low = np.array([0,255,255])red_up = np.array([10,255,255])mask0 = cv2.inRange(hsv,red_low,red_up)red_low = np.array([170,255,255])red_up = np.array([180,255,255])mask1 = cv2.inRange(hsv,red_low,red_up)mask_red =遮罩1+遮罩0r_img = ip_image.copy()r_img [np.where(mask_red == 0)] = 0gray_img0 = cv2.cvtColor(r_img,cv2.COLOR_BGR2GRAY)ret,thresh = cv2.threshold(gray_img0,0,255,0)M = cv2.moments(脱粒)rX = int(M ["m10"]/M ["m00"])rY = int(M ["m01"]/M ["m00"])cv2.circle(ip_image,(rX,rY),17,(255,255,255),2)cv2.imshow(输出",ip_image) 

此外,当将cv2.threshold()的第四个参数设置为"0"时,它对于数字图像也可以正确使用,而对于网络摄像头图像,它会在行处抛出零除错误:

  rX = int(M ["m10"]/M ["m00"]) 

解决方案

绝对值得尝试改善初始条件(透视,光线,分辨率等).网络摄像头产生的当前结果有些糟糕,因此与其花费大量时间进行修复,不如使用便宜些的硬件可能会更好.

您可以使用一些精美的方法来改善您的图像,但是,最好是输入更多有价值的信息.

无论如何,这是有用的部分.您的标记不是唯一的,因此任何使用颜色的尝试都需要进行额外的形状分析.以下是使用颜色分割的一些结果:

如您所见,某些区域的颜色非常相似.我使用了更高级的颜色相似度功能来处理复杂的情况.基本上,我用一些阈值指定了红色和绿色.

使用这些结果,您可以进行简单的形状分析,或者只是比较区域以找到标记.我希望有一些更独特的颜色和更好的初始条件.

无论如何,任何现实场景都需要您非常小心地使用颜色:

(查看实际操作)

类似的问题:

在不太恶劣的照明环境下通过衣服颜色识别人的问题

I am working on a project where I'm trying to detect green and red circles on a specific surface (arena). When I try to do so with the digital version of that arena (PNG image), I can successfully detect both the colored circles.

Here's the digital image of the surface:

Now, I printed this arena on a flex(without those two colored circles ), and manually placed coloured circular coins on it. But after capturing its image through a 1.3 MP webcam, the color detection didn't work and gave false results.

Here's the printed arena captured through webcam:

Why aren't the colors being detected? Do I need to do post-processing on webcam image? I've tried sharpening the image via cv2.filter2D but it didn't work either.

Here's a snippet for detecting Red circles from my Python code:

ip_image=cv2.imread("image.png")
kernel = np.array([[-1,-1,-1],[-1,9,-1],[-1,-1,-1]])
ip_image=cv2.filter2D(ip_image,-1,kernel)
#cv2.imshow("Hi",ip_image)
hsv=cv2.cvtColor(ip_image,cv2.COLOR_BGR2HSV)
red_low=np.array([0,255,255])
red_up=np.array([10,255,255])
mask0= cv2.inRange(hsv,red_low,red_up)
red_low=np.array([170,255,255])
red_up=np.array([180,255,255])
mask1=cv2.inRange(hsv,red_low,red_up)
mask_red=mask1+mask0
r_img= ip_image.copy()
r_img[np.where(mask_red==0)] = 0
gray_img0 = cv2.cvtColor(r_img,cv2.COLOR_BGR2GRAY)
ret,thresh = cv2.threshold(gray_img0,0,255,0)
M = cv2.moments(thresh)
rX=int(M["m10"] / M["m00"])
rY=int(M["m01"] / M["m00"])
cv2.circle(ip_image,(rX,rY), 17, (255,255,255), 2)
cv2.imshow("Output",ip_image)

Also, the fourth parameter in cv2.threshold() when set to "0" works correctly with digital image while with webcam image it throws zero division error at the line :

rX=int(M["m10"]/M["m00"])

解决方案

It is definitely worth trying to improve initial conditions (perspective, light, resolution, etc). Current result produced by the webcam is somewhat awful, so instead of spending lots of time fixing that it might be better to use less cheaper hardware.

You can use some fancy methods to improve your image, but still, it is better to have more valuable input.

Anyway, here is the useful part. Your markers are not unique, so any attempt to use color will require additional shape analysis. Here are some results using color segmentation:

As you can see some areas have pretty similar colors. I use a bit more advanced color similarity function to handle complex cases. Basically, I specified red and green with some thresholds. Delta E will be the right starting point. Let's see the actual shapes:

With those results you can do a simple shape analysis or just compare areas to find your markers. I'd prefer to have a bit more unique colors and better initial conditions.

Anyway, any real-life scene will require you to be really careful working with colors:

(see in action)

Similar problem:

Issue of the recognize people by their clothes color with not severe illumination environments

这篇关于颜色检测在网络摄像头图像上效率不高的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆