简体   繁体   English

如何获取具有特定颜色的图像的像素值?

[英]How to get pixel value of an image with specific color?

Hello can I get the value of an pixel with color?你好,我可以得到一个带颜色的像素的值吗? I'm trying to path this 10x10 pixel image where I want to print and path the value of pixel that are not white?我正在尝试将这个 10x10 像素的图像传送到我想要打印的位置并路径不是白色的像素值? I'm using this image 10x10 pixel as an image.我正在使用这个10x10像素的图像作为图像。

For Example This is a 10x10 pixel [0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0]例如这是一个 10x10 像素 [0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0, 0, 0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0, 0,0,0,0, 0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0, 0, 0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0, 0,0,0,0]

I want to change the value of 0 to 1 if there are present color in the pixel other than white?如果像素中存在白色以外的颜色,我想将 0 的值更改为 1?

I want to use python for this problem.我想使用 python 来解决这个问题。

Let's assume the picture is in RGB format, it means the picture has 3 color channels.假设图片是 RGB 格式,这意味着图片有 3 个颜色通道。 If you want to map the picture to a mask you could use the following:如果您想将 map 图片转换为蒙版,您可以使用以下内容:

img = cv2.imread("picture path")
mask = img[:, :] == [255, 255, 255]
  • cv2 is the opencv-python library cv2是 opencv-python 库
  • imread reads the image from the file imread从文件中读取图像
  • np.sum(img, axis=2) == 765 creates an array of boolean values np.sum(img, axis=2) == 765创建一个 boolean 值数组

The sum function adds up the channel values of each pixel.总和 function 将每个像素的通道值相加。 If a pixel is white, then its RGB value is (255, 255, 255).如果一个像素是白色的,那么它的 RGB 值为 (255, 255, 255)。 So, if you sum the channel values, then every white pixel will have the value 765.所以,如果你对通道值求和,那么每个白色像素的值都是 765。

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

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