Creating gray scale image from selcted values

Hi, I am having trouble producing an gray scale image from selected values. I tried with the following code to convert my values in gray scale. But it's prodcuing a very tiny image. How can I get a regular size image?
P = [47 61 57; 68 53 39; 60 57 51];
grayimage = imshow(P, [0 255]);
I have also tried this one but having the same output
P = [47 61 57; 68 53 39; 60 57 51];
grayimage = uint8(255 * mat2gray(C));
I = imshow(grayimage);
Could you please help me in this matter? Thank you.

 Accepted Answer

Just create a random 2D matrix
img = rand(1000, 1000);
imshow(img);

9 Comments

Actually, the values will be in that range which I have provided here. So how can I use that value to create a regular size image?
What is the range of values?
The range is between 0 to 80. Thank you.
Use following code to generate values in range [0, 80]
img = randi([0 80], 500, 500, 'uint8');
imshow(img)
Thank you for your reply. I think I did not explain my problem properly. In my case, the number of values are fixed which is a 3x3 matrix. Therefore, the image which is generating is very small. But I need to produce that small image as a bigger one so that it is presentable. Thank you.
Ok, I got it now. Try
P = [47 61 57; 68 53 39; 60 57 51];
img = imresize(P,[400,400],'nearest');
imshow(uint8(img));
Thank you so much. It worked. I have an additional question. What should I do if I want to reverse the color? Right now it shows the darkest color for the minimum value. What should I do to show the darkest color for the maximum value?
There are several way to do this. You just need to subtract the values of matrix from an appropriate number. For example, reverse the entire colorspace
P = 255-[47 61 57; 68 53 39; 60 57 51];
img = imresize(P,[400,400],'nearest');
imshow(uint8(img));
It will make all the values to have a light color, however, the maximum value will have darkest color among them.
Similarly, you can also try
P = [47 61 57; 68 53 39; 60 57 51];
P = max(max(P)) - P;
img = imresize(P,[400,400],'nearest');
imshow(uint8(img));
All the elements are still dark, and maximum value is darkest.
Thank you so much for your help! Now the code is workng as I wanted it to.

Sign in to comment.

More Answers (0)

Categories

Find more on Convert Image Type in Help Center and File Exchange

Products

Release

R2018a

Community Treasure Hunt

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

Start Hunting!