imread reads PNG files as empty arrays
Show older comments
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
Cris LaPierre
on 9 Jan 2021
Edited: Cris LaPierre
on 9 Jan 2021
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?
Mert Can
on 9 Jan 2021
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)
Cris LaPierre
on 9 Jan 2021
I guess another alternative is to change your line color to something other than black.
Mert Can
on 9 Jan 2021
Cris LaPierre
on 9 Jan 2021
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)
Mert Can
on 9 Jan 2021
Answers (1)
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'));
Categories
Find more on Convert Image Type in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!