How to represent a 2D matrix as a grayscale image
18 views (last 30 days)
Show older comments
i have a 2D matrix and i would like to visualize it as a grayscale image. i used the fuction mat2gray(x) and then imshow. However, i had got black and whites squares as shown in the first image attached below and i want to merge those squares and to get the image as the second one.

8 Comments
Rik
on 17 Feb 2022
You could also simply edit your previous question instead of deleting it and posting a new one.
Image Analyst
on 17 Feb 2022
Your first, second, and third posts, each showing an image, are all different images. I'm not sure if or why they're supposed to be different, but they are.
A matrix already IS an image. You can display any 2-D image with
imshow(M, []);
Answers (1)
Walter Roberson
on 17 Feb 2022
Look at your top left square of your "black and white" image. It is clearly not the same shade as the square to its right, and neither of them is the same shade as the square under the top left square. Therefore you do not have a "black and white" image, you have a grayscale image in which adjacent locations happen to have notable differences.
If we create some data with notable differences between adjacent locations:
data = randn(8,8);
scaled_data = mat2gray(data);
figure(); imshow(scaled_data)
Now for the purposes of demonstration we sort the data:
figure(); imshow(sort(sort(scaled_data,1,'ascend'),2,'descend'))
and we see something that looks more like what you are asking for. Both cases are using imshow(), so it is not imshow() that is making the difference: the difference is that the data happens to be arranged differently so that there are not high contrast between neighbours.
In short, the problem is not with imshow(): the problem is with your data happening to have high contrast between adjacent locations.
Maybe you want interpolation to reduce the contrast?
figure(); pcolor(scaled_data); colormap(gray)
0 Comments
See Also
Categories
Find more on Blue in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!


