简体   繁体   English

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

[英]Detect color in the middle of image (OpenCV, Python)

I have these 3 images (consider "image" the whole square not only the figure inside - this is just for demonstration purposes): 我有以下3张图片(不仅仅考虑内部图片,还考虑“图片”整个正方形-这仅是出于演示目的):

im1 im2 im3

What I want to do is detect the colour in the middle (center) of each one. 我想要做的是检测每个颜色的中间(中间)的颜色。 So, having an area (square or circle) in the center and with OpenCV detect which is the colour. 因此,在中心有一个区域(正方形或圆形),并使用OpenCV可以检测出哪种颜色。 Something like a color picker... 有点像拾色器...

The purpose is to have 3 values, 3 for each image (BGR). 目的是具有3个值,每个图像3个(BGR)。

Example: 例:

im4

What is the colour in the ROI ? ROI中的颜色是什么?

Thanks 谢谢

EDIT 编辑

Using this code I can find the middle of an image and apply a mask. 使用此代码,我可以找到图像的中间并应用蒙版。

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)

Now it remains to find BGR of the masked area (visible one...) 现在仍然可以找到被遮罩区域的BGR(可见一个...)

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)  

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

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

Using Pillow: 使用枕头:

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)

With OpenCV replace the two first lines with: 使用OpenCV时,将前两行替换为:

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

You have an image of size wx h. 您的图像尺寸为wx h。 Let's assume a roi size of 11 pixel per side, so offset = 5 假设每侧的roi大小为11像素,所以offset = 5

Access the pixel from (w/2 - width/2, h/2 - height/2) to (w/2 + width/2, h/2 + height/2). 从(w / 2-宽度/ 2,h / 2-高度/ 2)到(w / 2 +宽度/ 2,h / 2 +高度/ 2)访问像素。

Then calculate the mean of all extracted pixel (so you're more robust to color variation). 然后计算所有提取像素的均值(因此您对颜色变化更鲁棒)。

You can of course change the color space if you want it in other space color depending of the kind of picture are you going to analyze. 当然,如果您要在其他空间中使用其他颜色,则可以更改颜色空间,具体取决于您要分析的图片类型。

This will find the RGB value of the pixel in the center of image using OpenCV. 这将使用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)

Source 资源

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM