Why does the imshow function display a white image despite the image matrix consisting of elements ranging from 0-255?

68 次查看(过去 30 天)
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 个评论
Stephen23
Stephen23 2020-11-14
编辑:Stephen23 2020-11-14
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).

请先登录,再进行评论。

回答(2 个)

Ameer Hamza
Ameer Hamza 2020-11-14
You can let imshow() automatically scale the intensity range. Replace your last line with
imshow(gray_img, [])

KSSV
KSSV 2020-11-14
Convert the image into uint8 or uint16 and then use imshow.

标签

Community Treasure Hunt

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

Start Hunting!

Translated by