MATLAB Answers

imread reads PNG files as empty arrays

30 views (last 30 days)
Mert Can
Mert Can on 9 Jan 2021
Commented: Mert Can on 9 Jan 2021
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

Show 4 older comments
Mert Can
Mert Can on 9 Jan 2021
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!
Cris LaPierre
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
Mert Can on 9 Jan 2021
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

Mert Can
Mert Can on 9 Jan 2021
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.

Community Treasure Hunt

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

Start Hunting!

Translated by