简体   繁体   中英

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? I'm using this image 10x10 pixel as an image.

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]

I want to change the value of 0 to 1 if there are present color in the pixel other than white?

I want to use python for this problem.

Let's assume the picture is in RGB format, it means the picture has 3 color channels. If you want to map the picture to a mask you could use the following:

img = cv2.imread("picture path")
mask = img[:, :] == [255, 255, 255]
  • cv2 is the opencv-python library
  • imread reads the image from the file
  • np.sum(img, axis=2) == 765 creates an array of boolean values

The sum function adds up the channel values of each pixel. If a pixel is white, then its RGB value is (255, 255, 255). So, if you sum the channel values, then every white pixel will have the value 765.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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