0

I have a mask image which is technically a matrix full of True/False values. I would like to view this as an image. First, I converted it into a binary array with (astype(np.uint8))

print('Part Mask', p['masks'][class_id].astype(np.uint8))

but I still can't view it as an image under Python notebook. OpenCV goes crazy and crashes the kernel.

Does anyone know how to view such a structure as an image on Python notebook without crashing the kernel?

    [[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]]

or this would work as well ():

[[False False False ... False False False]
 [False False False ... False False False]
 [False False False ... False False False]
 ...
 [False False False ... False False False]
 [False False False ... False False False]
 [False False False ... False False False]]

Thanks in advance.

EDIT: I cannot copy paste the whole code here but basically I have a prediction tensor p, and p[masks][class_id] is what I want to visualize (showing the mask of each class).

enumerator = 0
# run through the instances
for class_id in p['class_ids']:
    #print('Image:', image) # the original input image
    #print('Mask:', merged_mask) # whole masked image
    print('ID: ', class_names[class_id] + str(enumerator))
    #print('Outline Poses: ', ) # mask boundary coordinates
    #print('Pose:',) # mask center coordinates
    print('Part Mask', p['masks'][class_id].astype(np.uint8)) # how to visualize this as an image?
    print('Confidence: ', p['scores'][class_id])
    print('BB: ', p['rois'][class_id]) # get the BB
    print('--------------------------')
    enumerator = enumerator + 1

enter image description here

P.S: Matplotlib does not work either. This is the kind of image I get when I try to print:

enter image description here

6
  • Have you tried matplotlib? Commented Nov 26, 2018 at 8:34
  • Got another error with that, but perhaps it was because of something else, could you suggest a way with matplotlib? Commented Nov 26, 2018 at 8:36
  • Please do not even think of pasting the whole code here. Create a minimal reproducible example instead. Commented Nov 26, 2018 at 9:30
  • Not sure if this was a bot answer or not @Goyo. I said myself I cannot paste the code here, you don't need to link me that page. Commented Nov 26, 2018 at 9:36
  • 1
    No, it's not. But you do not need to care, just think whether the advice makes sense or not. You have an example of matplotlib working with sample data. It does not work with your data but we don't know why. Knowing that it comes from a masked prediction tensor does not help. Having an actual hardcoded sample does. Commented Nov 26, 2018 at 9:49

2 Answers 2

4

Matplotlib should work for you:

import numpy as np
from matplotlib import pyplot as plt


image = np.eye(10)
binary = image > 0
plt.imshow(binary)
plt.show()

With result:

enter image description here

Edit:
Your image is of shape (510,7), what you got above is exactly what you should expect:

import numpy as np
from matplotlib import pyplot as plt


image = np.eye(510)[:,:7]
binary = image > 0
plt.imshow(binary)
plt.show()

Results:
enter image description here

Sign up to request clarification or add additional context in comments.

9 Comments

Post your code, This seems like a problem with the notebook your using, the figure does not get the size it should get.
I don't think what you created withnp.eye() there is the same with my data though. Mine is (510, 7) <class 'numpy.ndarray'>
Probaly not, but binary = image > 0 create a binary array which I am using to plot. The content of the array does not matter, as long as it is Binary.
@Schütze your image looks ok to me for an array of that shape. What else do you expect?
You have an image of (510,7), it will look like a very narrow rectangle, I don't know if it is messed up, as we do not know what is the expected result.
|
0

Maybe you may use PIL

from PIL import Image

data = [...]

width = len(data[0])
height = len(data)

output_image = Image.new(mode='1', size=(width, height))

for x in range(height):
    for y in range(width):
        pixel_value = data[x][y]
        output_image.putpixel((x,y), pixel_value)


output_image

Output is such this image ->Click and zoom the image to pixels<- Click and zoom the image to pixels using the following data:

data = [[0,0,0,0], [0,0,1,1], [1,1,1,0], [0,1,1,0]]

4 Comments

Gave an error at output_image.putpixel((x,y), pixel_value) saying SystemError: new style getargs format but argument is not a tuple
What version of PIL you use ?
Version I have is 5.1.0
I have version 5.3.0 and python3.6 , and all works. Try to update PIL version

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.