在检测到指定颜色的图像中查找坐标
我正在尝试制作一个程序,将图像取进去,并在整个图像中查找以找到一种颜色,比如说蓝色,并给出图像中具有该颜色的该点的坐标.
I'm trying to make a program which takes in an image and looks throughout the image to find a colour, lets say blue, and give out the coordinates of that point in the image which has that colour.
为此,您需要一些信息,包括图像的高度和宽度(以像素为单位)以及图像的色图..之前我已经做过类似的事情,并且我使用了PIL(枕头)来提取每个像素的颜色值.使用此方法,您应该能够将像素颜色值重新格式化为二维数组(array [x] [y],其中x是x坐标,y是y坐标,以便于比较)和比较具有指定RGB值的单个像素值.
In order to do so, you need a few pieces of information, including the height and width of the image in pixels, as well as the colormap of the image. I have done something similar to this before, and I used PIL (Pillow) to extract the color values of each individual pixel. Using this method, you should be able to reformat the pixel colour values into a two-dimensional array (array[x][y], where x is the x-coordinate and y is the y-coordinate, for easy comparison) and compare the individual pixel values with a specified RGB value.
如果图像的高度和宽度未知,则可以执行以下操作获取图像的高度和宽度:
If you have an image of unknown height and width, you could do the following to obtain the image height and width:
from PIL import Image
image = Image.open('path/to/file.jpg')
width, height = image.size
此后,您可以使用以下命令以列表形式获取RGB格式的像素颜色值:
After this, you can use the following to get the pixel color values in RGB format in a list:
pixval = list(image.getdata())
temp = []
hexcolpix = []
for row in range(0, height, 1):
for col in range(0, width, 1):
index = row*width + col
temp.append(pixval[index])
hexcolpix.append(temp)
temp = []
然后您可以进行比较以找到与您指定的颜色匹配的像素
You can then do a comparison to find pixels that match your specified colour