Loading in png file that is black
이전 댓글 표시
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
Guillaume
2017년 5월 5일
What is
class(ToCrop)
and what is
min(ToCrop(:))
max(ToCrop(:))
Dries Weytjens
2017년 5월 5일
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.
채택된 답변
추가 답변 (4개)
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
Image Analyst
2017년 5월 5일
Why don't you convert it to an RGB image?
rgbImage = ind2rgb(CropIm, storedColorMap);
imwrite(rgbImage, 'iteration31crop.png');
Then your problems will go away, since you're not so used to dealing with indexed images.
Don't use image as the name of a variable since that is the name of a very important built in function. I don't know why you decided to change the nice descriptive names I gave them. First of all, I looks too much like 1 (one) and l (lower case L), and secondly image is a very important reserved function name.
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
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
2017년 5월 5일
0 개 추천
The problem might be due to improper scaling or the type of image.
Use imagesc
댓글 수: 1
Dries Weytjens
2017년 5월 5일
편집: Dries Weytjens
2017년 5월 5일
KSSV
2017년 5월 5일
ToCrop = imread('iteration 31.png','BackgroundColor',[1 1 1]);
imshow(ToCrop);
댓글 수: 2
Dries Weytjens
2017년 5월 5일
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
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);
카테고리
도움말 센터 및 File Exchange에서 Convert Image Type에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!