imread reads PNG files as empty arrays
5 views (last 30 days)
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
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)
Answers (1)
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'));
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!