问题
I am working on a project where I need to recognize colors in sequential order from an Image
I am new to openCv. Help needed. Image here
回答1:
since I think that you have to recognize colors inside the squares, not all colours in the image, you should first of all detect all squares.
After that for each square it's so easy to recognize the colour since you can have the pixel value information in different color spaces (rgb, hsv and so on).
I can suggest you to read tutorials that you can find in the official documentation before to start, they can be really useful.
回答2:
You may try this (your image is saved to color_strip.jpg
), basically you load the file and cut the image into the pieces of the same color, then average over the piece to get the average color:
>>> import cv2
>>> img = cv2.imread( 'color_strip.jpg' )
>>> img.shape
(3677, 235, 3)
>>> square_size = 2640 / 10
>>> for i in range(11) :
... top = i * square_size + 10
... bottom = top + 160
... data = img[top:bottom,80:210,:]
... _ = cv2.imwrite( 'data_%02d.jpg' % i, data )
... print 'mean color', data.mean(axis=0).mean(axis=0)
...
mean color [ 92.55783654 127.716875 143.74230769]
mean color [ 95.17754808 126.11514423 157.42605769]
mean color [ 84.09365385 151.56105769 190.28004808]
mean color [ 83.29528846 148.21403846 165.08956731]
mean color [ 50.76451923 140.88158654 211.09413462]
mean color [ 19.91221154 150.03350962 221.26485577]
mean color [ 41.71350962 150.38677885 200.61456731]
mean color [ 114.19682692 155.68245192 190.50230769]
mean color [ 106.44120192 160.234375 194.67211538]
mean color [ 106.43980769 148.12759615 102.86701923]
mean color [ 117.02735577 151.62211538 171.19259615]
>>>
And you may check the data_XX.jpg
files to make sure they actually contain the color stripes, and not something else.
A minor detail, the printed results are in BGR format used by OpenCV, you may reorder them if you need RGB or any specific order.
来源:https://stackoverflow.com/questions/57743213/detecting-colors-in-an-image-in-sequence-using-opencv