opencv 分辨颜色


直方图方法:

I would suggest to try histograms. You can call calcHist and thencalcBackProject. As a result you will have a Mat with pixel values which show the histogram bin index = color ID. Having such Mat it should be easy to understand how many blobs of every color ID do you have, i.e. by contour analysis.


灰度变换:

If the background is completely black you can simply use cvtColor(src,dst,CV_RGB2GRAY) to transfer this image into gray image. Than use findContours on dst, and check number of polygons.

If the background is not black, but at least uniform and you know its color, you can use compare(src,color,dst,CMP_EQ) to determine which pixels are part of the background and which are not. Again use findContours on dst.

If the background is not uniform, the problem became more complicate and will require more complicate solutions. Also note that detection of hue is not something that will help you for blob detection (usually).



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在 OpenCV 中识别轮廓颜色,你可以按照以下步骤进行: 1.首先,将图像转换为 HSV 颜色空间,因为在 HSV 中更容易区分颜色。 2.定义要识别的颜色范围,可以使用 cv2.inRange 函数来创建一个掩膜。 3.使用 cv2.findContours 函数找到图像中的轮廓。 4.对于每个找到的轮廓,计算其质心并检查其颜色是否在你定义的颜色范围内。 5.在符合条件的轮廓周围绘制一个矩形或者一个圆圈。 下面是一个示例代码,它会找到图像中红色和绿色的轮廓,并在它们周围绘制一个圆圈: ``` python import cv2 import numpy as np # Load the image img = cv2.imread('image.jpg') # Convert it to HSV hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV) # Define the range of red and green colors in HSV lower_red = np.array([0,50,50]) upper_red = np.array([10,255,255]) lower_green = np.array([50,50,50]) upper_green = np.array([70,255,255]) # Create masks to only select red or green pixels mask_red = cv2.inRange(hsv, lower_red, upper_red) mask_green = cv2.inRange(hsv, lower_green, upper_green) # Find contours in the masks contours_red, _ = cv2.findContours(mask_red, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) contours_green, _ = cv2.findContours(mask_green, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) # Draw a circle around each red and green contour for cnt in contours_red: (x,y), radius = cv2.minEnclosingCircle(cnt) center = (int(x),int(y)) radius = int(radius) cv2.circle(img,center,radius,(0,0,255),2) for cnt in contours_green: (x,y), radius = cv2.minEnclosingCircle(cnt) center = (int(x),int(y)) radius = int(radius) cv2.circle(img,center,radius,(0,255,0),2) # Show the resulting image cv2.imshow('image',img) cv2.waitKey(0) cv2.destroyAllWindows() ``` 请注意,代码中使用的颜色范围是非常基本的,你需要根据你的图像调整它们的值来获得更准确的结果。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值