Loading in png file that is black

48 ビュー (過去 30 日間)
Dries Weytjens
Dries Weytjens 2017 年 5 月 5 日
コメント済み: Zhifei Deng 2018 年 11 月 16 日
Hello,
I am trying to load in a large png file in Matlab with purposes of cropping it (I have a bunch of them so am trying to automate the cropping). Unfortunately, when I load in the images, I first get the message that the image is too large to display, hence Matlab displays at 67% which does not seem to be a problem for the quality of the images. The bigger problem, however, is that the image shows up in black.
I using the following code:
ToCrop = imread('iteration 31.png','png');
imshow(ToCrop,[]);
Thanks!
  3 件のコメント
Dries Weytjens
Dries Weytjens 2017 年 5 月 5 日
The class is uint8, min(ToCrop(:)) = 0; max(ToCrop(:)) = 255.
Guillaume
Guillaume 2017 年 5 月 5 日
There's definitively at least one non-black pixels in that image. Can you attach it to your question?
If not, what is the output of
imfinfo('iteration 31.png')
Note that you did not need the [] in your call to imshow. However, as it has no effect in your case, removing it won't solve the problem.

サインインしてコメントする。

採用された回答

Santhana Raj
Santhana Raj 2017 年 5 月 5 日
By saying that the image appears in black, do you mean that the image is still visible but the image is in grey scale?? The below code works fine for me in that case:
[I,m]=imread('iteration 31.png');
imshow(I,'Colormap',m);
  2 件のコメント
Dries Weytjens
Dries Weytjens 2017 年 5 月 5 日
Thank you, that code worked! The image was in grey scale I think.
Guillaume
Guillaume 2017 年 5 月 5 日
No! The image was not in greyscale. The problem was that you were displaying the indexes into a colour map instead of the colours themselves. Read up on indexed images (or convert the image to RGB as suggested by Image Analyst)

サインインしてコメントする。

その他の回答 (4 件)

Image Analyst
Image Analyst 2017 年 5 月 5 日
This works just fine for your indexed image. You just have to apply the correct colormap:
filename = 'iteration 31.png'
[indexedImage, storedColorMap] = imread(filename);
imshow(indexedImage, storedColorMap)
  3 件のコメント
Guillaume
Guillaume 2017 年 5 月 5 日
"Don't use image as the name of a variable". I'm not sure where you've seen image used as a variable name. In the question, the OP uses ToCrop which is a good name in my opinion.
Image Analyst
Image Analyst 2017 年 5 月 5 日
Guillaume, between my Answer and immediately below was a response by Dries with some code where he used my code but changed the variable names, including making image variable names of "image" and "I". He deleted that comment after I advised him to not use those names and use descriptive names instead. I agree - it's now confusing since he did that.

サインインしてコメントする。


Santhana Raj
Santhana Raj 2017 年 5 月 5 日
The problem might be due to improper scaling or the type of image.
Use imagesc
  1 件のコメント
Dries Weytjens
Dries Weytjens 2017 年 5 月 5 日
編集済み: Dries Weytjens 2017 年 5 月 5 日
When using imagesc I obtain the image in blue. Maybe some background, the images were all generated in ppt and then converted to pdf. Since Matlab does not support pdf reading I used an online pdf to png converter. Maybe all these conversions cause the issue?

サインインしてコメントする。


KSSV
KSSV 2017 年 5 月 5 日
ToCrop = imread('iteration 31.png','BackgroundColor',[1 1 1]);
imshow(ToCrop);
  2 件のコメント
Dries Weytjens
Dries Weytjens 2017 年 5 月 5 日
The image still appears in black. I have added it in the attachments, maybe something is inherently wrong with the file.
Zhifei Deng
Zhifei Deng 2018 年 11 月 16 日
I had the same problem with SW, but it seems that this code works for my case, great thanks!

サインインしてコメントする。


Guillaume
Guillaume 2017 年 5 月 5 日
Your description was not very accurate. The image is not black, it simply is the wrong colour, with the background showing black instead of white.
That is because, this is an indexed image. The pixel values are indices into a colour map that you're not loading. The white colour in the colour map is at index 0, so if you use 0 as intensity instead of an index in the colour map it is going to look black.
To fix this you need to load the colour map and pass it to imshow:
[ToCrop, map] = imread('iteration 31.png');
imshow(ToCrop, map);
Note: use imfinfo to check the colour type of the image.

カテゴリ

Help Center および File ExchangeConvert Image Type についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by