如何检测检测到的形状颜色OpenCV

2024-10-03 21:24:31 发布

您现在位置:Python中文网/ 问答频道 /正文

我写了下面的代码来检测图像中的三维形状,它工作正常。在

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

有谁能告诉我从哪里开始颜色检测?在

下面的形状检测代码,可能有用:

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)

Centers of shapes founded as we can see, when we do print(second) we get a output of all pixels But I need output just from the pixel inside contour,with this method is that imposible to get values from pixel inside in contour?


Tags: 代码imageimport颜色cv2hsv形状imshow
1条回答
网友
1楼 · 发布于 2024-10-03 21:24:31

基本思路:

(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.

对于图像:

enter image description here

^{pr2}$

enter image description here

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

enter image description here

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

enter image description here


一些链接可能有用:

1. just detect color regions: 

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

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

2. detect specific color in HSV:

{绿色:^ 1}

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

(3)拿红色的H为例:How to find the RED color regions using OpenCV?

3. If you want to crop polygon mask:

(一) Cropping Concave polygon from Image using Opencv python

相关问题 更多 >