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)?
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?
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.
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.
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.
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 ).
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.
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.
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.
Booleans in Python In Python, the boolean is a data type that has only two values and these are 1.
You have the right idea here. The thing to use would be numpy's alltrue
:
mask = np.alltrue(image == [0, 255, 0], axis=2)
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()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With