Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the most pythonic way of generating a boolean mask of an RGB image based on the colour of the pixels?

I have an image with a missing part that I know has been coloured in green (first image). What is the most pythonic way of generating another "boolean" image that shows white for the missing parts and black for the non-missing parts (second image)?

Image with missing part coloured in green Image that shows the missing part (mask) in white, and the rest in black

Is it possible to do it without a for-loop, but just with array slicing?

My image is a numpy array of shape [height, width, 3]. I would like the following code to assign a two-dimensional array of booleans showing whether the value of each pixel is green ([0, 255, 0]) or not.

mask = image[:, :] == [0, 255, 0]

However, it returns an array of the same shape as the image ([height, width, 3]), showing if red, green, or blue values of the pixels are 0, 255, or 0, respectively. Could I perhaps use any() or all() method here somehow?

like image 280
nim.py Avatar asked Jan 22 '20 18:01

nim.py


People also ask

What is boolean masking?

Boolean masking is typically the most efficient way to quantify a sub-collection in a collection. Masking in python and data science is when you want manipulated data in a collection based on some criteria. The criteria you use is typically of a true or false nature, hence the boolean part.

How do you make a boolean mask in Python?

To create a boolean mask from an array, use the ma. make_mask() method in Python Numpy. The function can accept any sequence that is convertible to integers, or nomask. Does not require that contents must be 0s and 1s, values of 0 are interpreted as False, everything else as True.

What is a logical mask in Python?

The NumPy library in Python is a popular library for working with arrays. Boolean masking, also called boolean indexing, is a feature in Python NumPy that allows for the filtering of values in numpy arrays. There are two main ways to carry out boolean masking: Method one: Returning the result array.

How do you represent image in MxN numerical array form?

There are many different ways you could represent this in physical memory. For example: Using a MxN array where each element is a 24-bit integer. Each integer is formed by the red, green and blue values (each 8-bit integers) for example as so: red<<16 | green<<8 | blue (or equivalently red*256*256 + green*256 + blue ).

What is Boolean masking in Python?

Boolean masking, also called boolean indexing, is a feature in Python NumPy that allows for the filtering of values in numpy arrays. Method one: Returning the result array.

How does masking work in Python?

This would look something like: You can reduce this code to a one-liner using a list comprehension as, The same task can be achieved using the concept of Masking. It essentially works with a list of Booleans (True/False), which when applied to the original array returns the elements of interest.

What is the use of Bool in Python?

Python bool () function The bool () function is one of the functions used for data conversion. This function converts the other data types into boolean type. It gives True if the value is not empty or 0, ele False.

How many values can a Boolean have in Python?

Booleans in Python In Python, the boolean is a data type that has only two values and these are 1.


2 Answers

You have the right idea here. The thing to use would be numpy's alltrue:

mask = np.alltrue(image == [0, 255, 0], axis=2)

like image 110
Lagerbaer Avatar answered Oct 19 '22 10:10

Lagerbaer


The idea is to create a blank mask using np.zeros then color pixels white where the input image pixels are green with np.where. The resulting mask should be of type ndarray.

import cv2
import numpy as np

# Load image, create blank black mask, and color mask pixels 
# white where input image pixels are green
image = cv2.imread('1.png')
mask = np.zeros(image.shape, dtype=np.uint8)
mask[np.where((image == [0,255,0]).all(axis=2))] = [255,255,255]

cv2.imshow('mask', mask)
cv2.waitKey()

enter image description here

like image 1
nathancy Avatar answered Oct 19 '22 08:10

nathancy