0

I have two pictures. enter image description here

enter image description here

I want to extract the pixels in the regions marked by their respective color. (The normal distribution of the pixels in yellow and red)

I know opencv supports bitwise operation, however, I've only seen it being done with black/white mask.

I thought about using np.where(), I am curious to see if there's a better solution?

2
  • What sort of answer are you expecting please? The mean of the pixels masked in yellow? The number of pixels masked with red? Commented Feb 1, 2021 at 22:12
  • the number of pixels in the region masked with red/yellow Commented Feb 2, 2021 at 4:00

1 Answer 1

2

You can count the number of red/yellow pixels like this:

#!/usr/bin/env python3 import cv2 import numpy as np # Load image im = cv2.imread('abYaj.png') # Make mask of red pixels - True where red, false elsewhere redMask = (im[:, :, 0:3] == [0,0,255]).all(2) # Count red pixels redTotal = np.count_nonzero(redMask) # redTotal=44158 # Make mask of yellow pixels yellowMask = (im[:, :, 0:3] == [0,255,255]).all(2) # Count yellow pixels yellowTotal = np.count_nonzero(yellowMask) # yellowTotal=356636 

Alternatively, you could just use ImageMagick in the Terminal to count them and write no code at all:

magick identify -verbose abYag.png Image: Filename: abYaj.png Format: PNG (Portable Network Graphics) Mime type: image/png Class: DirectClass Geometry: 1024x1024+0+0 Units: Undefined Colorspace: sRGB ... ... Colors: 3 Histogram: 647782: (0,0,0,0) #00000000 none 44158: (255,0,0,255) #FF0000FF red <--- HERE 356636: (255,255,0,255) #FFFF00FF yellow <--- HERE Rendering intent: Perceptual ... ... 
Sign up to request clarification or add additional context in comments.

6 Comments

Thank you for your answer. However, I should express my question more clearly. It seems like you're counting the number of yellow/red pixels in the "mask". I want to use those color to select the "region of interest" in the original image. As in, what pixels are contained within that region marked by the yellow / red blobs.
That's exactly what you answered in the comments when I asked you to clarify what you wanted! If you want to do something with the pixels in the original image, you can just AND them with the masks I worked out since my masks are Boolean True/False at each pixel location.
Can you explain how you can perform bitwise AND on a tuple (RGB) since that's what the original image is represented as? I know opencv provides the function, but I assume you would have to somehow map the tuples to an integer?
How could the original image be a tuple? You are using OpenCV which stores images as Numpy arrays and you refer to np.where() in your question. Please state clearly exactly what you want as an answer. Thank you.
I am reading the image like this. img = cv2.imread(name, COLOR_BGR2RGB). Since each pixel has three channels, it makes sense that it is a tuple? Printing out img gives something like [[255,255,255],[255,255,255]....]. I want to know how can I perform a bitwise AND using the image and mask above. Thank you
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.