Why does the imshow function display a white image despite the image matrix consisting of elements ranging from 0-255?
59 views (last 30 days)
Show older comments
So, I wrote this program where I m supposed convert an RBG image to a Grayscale image without using the inbuilt functions. I decided to use loops for this purpose. My original matrix did get converted to a gray scale matrix, but upon displaying the grayscale image using the imshow function, it displayed a white image. How do I fix it?
This is the code:
size_org = size(org_img); % size of original matrix
gray_img = zeros(size_org(1), size_org(2)); % a zero matrix that will be manipulated to become grayscale image
for i = 1:1:size_org(1)
for j = 1:1:size_org(2)
gray_img(i,j) = (0.2126*org_img(i,j,1)) + (0.7152*org_img(i,j,2)) + (0.0722*org_img(i,j,3));
end
end
imshow(gray_img)
2 Comments
Stephen23
on 14 Nov 2020
Edited: Stephen23
on 14 Nov 2020
So far no one has actually answered your original question: "Why does the imshow function display a white image despite the image matrix consisting of elements ranging from 0-255?"
Answer: because when you store an image as floating point (i.e. double or single) then by default imshow assumes that the image data is in the range 0..1. For integer types, the assumed range is the range of the integer class.
This is explained in the documentation:
If you store image data which has some other range, then the simplest solution is to use Ameer Hamza's answer. Otherwise any out-of-range values will simply saturate to black or white (which is what you are seeing).
Answers (2)
Ameer Hamza
on 14 Nov 2020
You can let imshow() automatically scale the intensity range. Replace your last line with
imshow(gray_img, [])
2 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!