Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/opencv/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在图像中间检测颜色(OpenCV,Python)_Python_Opencv_Color Picker_Roi - Fatal编程技术网

在图像中间检测颜色(OpenCV,Python)

在图像中间检测颜色(OpenCV,Python),python,opencv,color-picker,roi,Python,Opencv,Color Picker,Roi,我有这3张图片(考虑“图片”整个广场,而不仅仅是里面的图形-这只是为了演示): 我想做的是检测每一个中间(中间)的颜色。因此,在中心有一个区域(正方形或圆形),并使用OpenCV检测颜色。 像颜色选择器之类的东西 其目的是有3个值,每个图像3个(BGR) 例如: ROI中的颜色是什么 谢谢 编辑 使用此代码,我可以找到图像的中间并应用遮罩 import cv2 import numpy as np img = cv2.imread("im2.png") height, width, de

我有这3张图片(考虑“图片”整个广场,而不仅仅是里面的图形-这只是为了演示):

我想做的是检测每一个中间(中间)的颜色。因此,在中心有一个区域(正方形或圆形),并使用OpenCV检测颜色。 像颜色选择器之类的东西

其目的是有3个值,每个图像3个(BGR)

例如:

ROI中的颜色是什么

谢谢

编辑

使用此代码,我可以找到图像的中间并应用遮罩

import cv2
import numpy as np

img = cv2.imread("im2.png")

height, width, depth = img.shape
circle_img = np.zeros((height, width), np.uint8)

mask = cv2.circle(circle_img, (int(width / 2), int(height / 2)), 20, 1, thickness=-1)
masked_img = cv2.bitwise_and(img, img, mask=circle_img)

cv2.imshow("masked", masked_img)
cv2.waitKey(0)

现在仍然需要找到遮罩区域的BGR(可见区域…

确保图像是一张照片图像,计算中间点并获取颜色:

r,g,b = image.get(x, y)

确保图像是照片图像,计算中间点并获取颜色:

r,g,b = image.get(x, y)
使用枕头:

from PIL import Image

im = Image.open(path_to_image)

width, height = im.size #get image size

#since you do not specify the size of the center area in the question, just replace the below variables with what you want
left = (width - your_width)/2
top = (height - your_height)/2
right = (width + your_width)/2
bottom = (height + your_height)/2

im.crop((left, top, right, bottom)) #crop the center of the image

rgb = im.convert('RGB') # get three R G B values
r, g, b = rgb.getpixel((1, 1))

print(r,g,b)
使用OpenCV将前两行替换为:

im = cv2.imread(path_to_image)
height, width = im.shape[:2]
使用枕头:

from PIL import Image

im = Image.open(path_to_image)

width, height = im.size #get image size

#since you do not specify the size of the center area in the question, just replace the below variables with what you want
left = (width - your_width)/2
top = (height - your_height)/2
right = (width + your_width)/2
bottom = (height + your_height)/2

im.crop((left, top, right, bottom)) #crop the center of the image

rgb = im.convert('RGB') # get three R G B values
r, g, b = rgb.getpixel((1, 1))

print(r,g,b)
使用OpenCV将前两行替换为:

im = cv2.imread(path_to_image)
height, width = im.shape[:2]

您有一个大小为w x h的图像。假设roi大小为每边11个像素,因此偏移量=5

访问从(w/2-宽度/2,h/2-高度/2)到(w/2+宽度/2,h/2+高度/2)的像素

然后计算所有提取像素的平均值(这样您对颜色变化更具鲁棒性)


当然,如果您想在其他空间中更改颜色空间,您可以根据要分析的图片类型更改颜色空间。

您有一个大小为w x h的图像。假设roi大小为每边11个像素,因此偏移量=5

import cv2
import numpy as np

img = cv2.imread('image.png')
height, width = img.shape[:2]
# Change these values to fit the size of your region of interest
roi_size = 10 # (10x10)
roi_values = img[(height-roi_size)/2:(height+roi_size)/2,(width-roi_size)/2:(width+roi_size)/2]
mean_blue = np.mean(roi_values[:,:,0])
mean_green = np.mean(roi_values[:,:,1])
mean_red = np.mean(roi_values[:,:,2])

print("R: {}  G: {}  B: {}").format(mean_red, mean_green, mean_blue)  
访问从(w/2-宽度/2,h/2-高度/2)到(w/2+宽度/2,h/2+高度/2)的像素

然后计算所有提取像素的平均值(这样您对颜色变化更具鲁棒性)


当然,如果您想在其他空间中更改颜色空间,您可以根据要分析的图片类型更改颜色空间。

这将使用OpenCV查找图像中心像素的RGB值

import cv2
import numpy as np

img = cv2.imread('image.png')
height, width = img.shape[:2]
# Change these values to fit the size of your region of interest
roi_size = 10 # (10x10)
roi_values = img[(height-roi_size)/2:(height+roi_size)/2,(width-roi_size)/2:(width+roi_size)/2]
mean_blue = np.mean(roi_values[:,:,0])
mean_green = np.mean(roi_values[:,:,1])
mean_red = np.mean(roi_values[:,:,2])

print("R: {}  G: {}  B: {}").format(mean_red, mean_green, mean_blue)  
import cv2
import numpy as np

img = cv2.imread("image.png")

height, width, depth = img.shape
circle_img = np.zeros((height, width), np.uint8)

mask = cv2.circle(circle_img, (int(width / 2), int(height / 2)), 1, 1, thickness=-1)
masked_img = cv2.bitwise_and(img, img, mask=circle_img)

circle_locations = mask == 1
bgr = img[circle_locations]

rgb = bgr[..., ::-1]

print(rgb)

这将使用OpenCV查找图像中心像素的RGB值

import cv2
import numpy as np

img = cv2.imread("image.png")

height, width, depth = img.shape
circle_img = np.zeros((height, width), np.uint8)

mask = cv2.circle(circle_img, (int(width / 2), int(height / 2)), 1, 1, thickness=-1)
masked_img = cv2.bitwise_and(img, img, mask=circle_img)

circle_locations = mask == 1
bgr = img[circle_locations]

rgb = bgr[..., ::-1]

print(rgb)

您的ROI中每个像素的像素值是多少?你能再解释一下吗?我考虑过遮罩、直方图和hsv,但“每个像素的像素值”是什么意思?同时,谢谢。直方图不会给你任何关于“位置”的信息,所以在你想做的事情中是无用的。我将添加更多内容。@Link对您自己的问题进行回答、修改并将其发布为您自己的问题是不合适的。您的问题中每个像素的像素值是多少?你能再解释一下吗?我考虑过遮罩、直方图和hsv,但“每个像素的像素值”是什么意思?同时,谢谢。直方图不会给你任何关于“位置”的信息,所以在你想做的事情中是无用的。我将添加更多内容。@Link对您自己的问题进行回答、修改并将其发布为您自己的问题是不合适的。他标记为“opencv”。@Moia很容易转换它,加上PIL可能是更好的选择他标记为“opencv”。@Moia很容易转换,加上PIL可能是更好的选择