在检测到指定颜色的图像中查找坐标 [英] Find the coordinates in an image where a specified colour is detected

查看:66
本文介绍了在检测到指定颜色的图像中查找坐标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试制作一个程序,将图像取进去,并在整个图像中查找以找到一种颜色,比如说蓝色,并给出图像中具有该颜色的该点的坐标.

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

这篇关于在检测到指定颜色的图像中查找坐标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆