imread reads PNG files as empty arrays

Hello all,
I have read previous posts about people explaining why they get black image when trying to display their image files. But none of them helped me out, because I believe I have a problem with my source file (or reading it) in the first place and I cannot figure out what.
The story is that I have a set stimuli that I created on Inkscape and saved them as PNG files (all of them black lines on white background in 500x500px). This is how I try to display them (at least one of them) and get a black image:
sti=imread('18_1b.png')
image(sti)
%same result with imagesc and imshow
When I look at the way that the file is arrayed, I could get the following information:
class(sti) % ans = uint8
min(sti(:)) % ans = 0
max(sti(:)) % ans = 0
More information on the PNG file:
imfinfo('18_1b.png') % FileSize: 25478
% Format: 'png'
% Width: 500
% Height: 500
% BitDepth: 24
% ColorType: 'truecolor'
% FormatSignature: [137 80 78 71 13 10 26 10]
% Transparency: 'alpha'
% XResolution: 3779
% YResolution: 3779
% ResolutionUnit: 'meter'
I do not think that my problem is related to image type or how I display them (as opposed to other previous posts). I have also tried converting my PNG files into BMP files and they seem to be working just fine (although a few artifacts are visible due to conversion). I feel like I have something wrong with my PNG files (resolutionunit in meters seems somehow suspicious), but I cannot figure out what and wonder if I am missing something else here.
I would be really pleased if you could help me figure this out.
Thanks!

7 Comments

Agreed - your code for loading and viewing works with a png. If your max value is 0, then you are seeing what is in your image - all black.
What do you see when you open the png file on your computer (any image viewing software)? Do you see anything? Can you confirm that a png exported from Inkscape matches what you created in Inkscape?
Yes - I can open the PNG file on my computer and it matches with what I created in Inkspace. I'll attach the image here, just for your information.
I open the image in an image editor, and I see a transparent background with black lines. I haven't tried to work with images with transparent backgrounds in MATLAB before. Any chance you can check how to export the image with a white background instead of a transparent one?
When I fill the background, it loads as expected.
I = imread("18_1b_white.png");
imagesc(I)
I guess another alternative is to change your line color to something other than black.
I was thinking (by making the background transparent) that the lines would be displayed on top of the background color of my screen that I am creating during my experiment. I was not aware that MATLAB does not support this (or my idea cannot be implemented as simple as what I had in mind). Filling the background of PNG file is one way to go - yes, thank you!
Based on Jan's response, this code works with the original image in 20b
[I,m,a] = imread("18_1b.png");
image(I,'AlphaData',a)
% this creates a black image
figure
image(I)
This works! It really is simple as much as I thought then, just needed to change it to transparent by importing the image with further arguments. Thank you!

Sign in to comment.

Answers (1)

Jan
Jan on 9 Jan 2021
Edited: Jan on 9 Jan 2021
When I run your code in Matlab 2018b I get a different result:
img = imread('18_1b.png');
size(img) % 500 x 500 x 3, not empty
min(img(:)) % 0
max(img(:)) % 255, not 0
The color map and the transparency by be obtained by further output arguments:
[img, map, alpha] = imread('18_1b.png');
In your case the imported data cannot be an "empty array", because min and max would not reply values in this case. So maybe the problem is, that you do not import the file you assume you are importing, because it is one with the same name in a different folder. Prefer to use absolute path names in every case:
folder = 'D:\MyImages';
img = imread(fullfile(folder, '18_1b.png'));

1 Comment

That is very strange. This could only be explained by difference in releases then (I am using 20b), because I do not have any other file named the same (nevertheless I tried importing the file by given its full path and nothing changed)

Sign in to comment.

Categories

Find more on Convert Image Type in Help Center and File Exchange

Products

Release

R2020b

Asked:

on 9 Jan 2021

Commented:

on 9 Jan 2021

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!