【问题标题】:How to detect the colors of detected shapes OpenCV如何检测检测到的形状OpenCV的颜色
【发布时间】:2019-07-04 19:19:18
【问题描述】:

我编写了下面的代码来检测图像中的 3D 形状,它可以正常工作。

现在我需要检测形状内部的颜色并计算它们。

谁能指出我应该从哪里开始进行颜色检测?

下面的形状检测代码,也许会有用:

import cv2
import numpy as np

cv2.imshow('Original Image',rawImage) 
cv2.waitKey(0)

hsv = cv2.cvtColor(rawImage, cv2.COLOR_BGR2HSV)
cv2.imshow('HSV Image',hsv)
cv2.waitKey(0)

hue ,saturation ,value = cv2.split(hsv)
cv2.imshow('Saturation Image',saturation)
cv2.waitKey(0)

retval, thresholded = cv2.threshold(saturation, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
cv2.imshow('Thresholded Image',thresholded)
cv2.waitKey(0)

medianFiltered = cv2.medianBlur(thresholded,5)
cv2.imshow('Median Filtered Image',medianFiltered)
cv2.waitKey(0) 

cnts, hierarchy = cv2.findContours(medianFiltered, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)

for c in cnts:
# compute the center of the contour
M = cv2.moments(c)
cX = int(M["m10"] / M["m00"])
cY = int(M["m01"] / M["m00"])


first = cv2.drawContours(rawImage, [c], -1, (0, 255, 0), 2)
second =cv2.circle(rawImage, (cX, cY),1 , (255, 255, 255), -1)


cv2.imshow('Objects Detected',rawImage)
cv2.waitKey(0)

【问题讨论】:

  • calculate them 是什么意思?将它们表示为 RGB 值,作为 HTML 十六进制代码?
  • 我认为您的意思是计算主色。 This answer 展示了一个实现。
  • @SzymonMaszke 我的意思是,例如;如果在输入图像中总共有 5 个具有三种不同颜色的立方体,我需要输出 2 个立方体是红色的,2 个立方体是黄色的,1 个是红色的。
  • @J.D.我的意思是计算形状内部的颜色。例如,如果我们有一个由树颜色(蓝色、白色、黄色)组成的 6 个圆圈的图像,我需要识别颜色并得到输出,比如 2 个圆圈是蓝色的,2白色和 2 个黄色。
  • 我不禁觉得如果我们能看到你的照片会有所帮助...

标签: python opencv colors object-detection color-detection


【解决方案1】:

基本思路:

(1) Convert the image to HSV color space;
(2) Threahold the `S` to find color regions;
(3) Calculate average hsv for each color-region-maskin HSV, then convert into BGR.

图片:

(1) Convert the image to HSV color space:

(2) Threahold the `S` to find color regions:

(3) Calculate average hsv for each color-region-maskin HSV, then convert into BGR.


一些链接可能有用:

1. just detect color regions: 

(1)How to detect colored patches in an image using OpenCV?

(2) OpenCV C++/Obj-C: Detecting a sheet of paper / Square Detection

2. detect specific color in HSV:

(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) 取红的HHow to find the RED color regions using OpenCV?

3. If you want to crop polygon mask:

(1) Cropping Concave polygon from Image using Opencv python

【讨论】:

  • 谢谢你的解释,不过这个方法对我来说也不是没用。我必须用上面的代码和步骤进行检测。我更新了主要代码和图像。最后一步我找到了形状的中心,所以现在我得到了每个形状的中心坐标。现在我需要使用这些坐标访问像素。你知道吗,我该如何访问?再次感谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-03-15
  • 2021-08-20
  • 2023-03-13
  • 1970-01-01
  • 1970-01-01
  • 2016-06-01
  • 1970-01-01
相关资源
最近更新 更多