I have a matrix consisting of True and False values. I want to print this as an image where all the True values are white and the False values are black. The matrix is called indices. I have tried the following:
indices = indices.astype(int) #To convert the true to 1 and false to 0
indices*=255 #To change all the 1's to 255
cv2.imshow('Indices',indices)
cv2.waitKey()
This is printing a fully black image. When I try, print (indices==255).sum()
, it returns a values of 669 which means that there are 669 elements/pixels in the indices matrix which should be white. But I can only see a pure black image. How can I fix this?
Convert Image To Matrix in Python 1 Import Image module from PILLOW library of Python as PIL. 2 Import array module from NUMPY library of Python. 3 These two libraries are for Image extraction from the source file and defining the dimensions of the matrix. More ...
In Python True and False are equivalent to 1 and 0. Use the int () method on a boolean to get its int values. x = True y = False print (int (x)) print (int (y))
Convert a specific string to 1, 0: distutils.util.strtobool () As mentioned above, bool () converts the string 'False' to True. Use distutils.util.strtobool () if you want to convert according to the contents of the string. Convert a string representation of truth to true (1) or false (0).
Use plt.imshow (arr, cmap="gray"), which uses a gray color map rather than a colorful one. When reading the image, and also converting any color to grayscale, you can choose scikit-image or OpenCV. OpenCV has cv.imread (fname, cv.IMREAD_GRAYSCALE). scikit-image offers skimage.io.imread (fname, as_gray=True).
As far as I know, opencv
represents an image as a matrix of floats ranging from 0 to 1, or an integer with values between the minimum and the maximum of that type.
An int
has no bounds (except the boundaries of what can be represented with all available memory). If you however use np.uint8
, that means you are working with (unsigned) bytes, where the minimum is 0 and the maximum 255.
So there are several options. The two most popular would be:
cast to np.uint8
and then multiply with 255:
indices = indices.astype(np.uint8) #convert to an unsigned byte
indices*=255
cv2.imshow('Indices',indices)
cv2.waitKey()
Use a float
representation:
indices = indices.astype(float)
cv2.imshow('Indices',indices)
cv2.waitKey()
Note that you could also choose to use np.uint16
for instance to use unsigned 16-bit integers. In that case you will have to multiply with 65'535. The advantage of this approach is that you can use an arbitrary color depth (although most image formats use 24-bit colors (8 bits per channel), there is no reason not to use 48-bit colors. If you for instance are doing image processing for a glossy magazine, then it can be beneficial to work with more color depth.
Furthermore even if the end result is a 24-bit colorpalette, one can sometimes better use a higher color depth for the different steps in image processing.
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