How to save matrix to workspace instead of plotting using imagesc function
You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Show older comments
0 votes
I don't understand how imagesc function works (scales), but my images plotted using via that function look better than using imshow. Is there any way to save the scaled image in the workspace instead of displaying it? I tried to debug built-in imagesc.m, but found nothing that scales data. If anyone knows how it's done, please, let me know.
Accepted Answer
Walter Roberson
on 25 Jun 2012
If M is your image, then
minM = min(M(:));
maxM = max(M(:));
scaledM = (M - minM) ./ (maxM - minM);
This will be in the range 0 to 1, so if you want to work with uint8 then uint8(scaledM .* 255)
8 Comments
Naz
on 25 Jun 2012
Thank's Walter, works well for me.
I need to save the image that shows by imagesc. Walter answer works, but it's giving me the scale of the original image which I don't want. My question is how can I save the image with the new scale shows in imagesc. in this case im-sim is original image, and info.x and info.y is the scale I need. for example the size of im_sim image is 479*616, but info.x and info.y is changing it to 140*180. Then, I need to save the image which include all frames as im-sim does have 54 frames. the image is uint8.
clc
clear
close all
load im_sim
load info
for fr =1:size(im_sim,3);
clf;imagesc(info.x,info.y,im_sim(:,:,fr)),colormap gray, axis image,
hold on; title(num2str(fr));
pause(0.05);
F = getframe ;
end
Thank you very much.
Walter Roberson
on 26 Sep 2017
imresize() the scaled image.
Note: I have found that instead of manually calculating the ranges, that the intensity rescaling can be done by the mat2gray() call.
Thank you very much Walter for your reply. I tried before imresize(), the size is what I need, but the image is totally changed, and I loss many information of that image.
As you recommended I tried I = mat2gray(im_sim); but the size is same as im_sim image which I don't want. In fact, when I execute this line
imagesc(info.x,info.y,im_sim(:,:,fr)),colormap gray, axis image,
it's showing the size I need, but I don't know how can I save this imagesc as a image with all frames. size of im_sim(479*616*54) size info.x = 1*616 size info.y = 1*479 but the size of imagesc result is 140*180 which I need to save the image with this size without losing information. Thanks a lot
Walter Roberson
on 26 Sep 2017
Please attach the data for a sample image that is "totally changed" by the imresize()
Thanks a lot Walter for your reply. Unfortunately, I'm not allow to share the data because of confidentiality. just I can attach the values of info.x and info.y. The size of image with this info.x and info.y turned to 140*180 which I need to save the image with this size. I tried
I = imresize(im_sim,[140 180]);
but as I said I lost information and quality of image changed. In fact this image is my ground truth image, and I need exactly as it is to test my algorithm. I tried
im = imagesc(info.x,info.y,im_sim(:,:,fr)),colormap gray, axis image,
imwrite(im.CData);
G = [im(:).CData];
but again giving the image with 479*616 size. Thank you
Walter Roberson
on 27 Sep 2017
When you use a vector of xdata or ydata for an image() or imagesc() call, all the entries are ignored except for the first and last entries. So your call is equivalent to
imagesc([0, 180.283902162354], [0, 140.292887029289], im_sim(:,:,fr))
Now, that does not tell it to plot into a 140 x 180 area: it tells it to place the lower left pixel center at data units [0, 0] and the upper right pixel center at data units [180.28, 140.29]. The actual number of pixels used on the screen depends upon the size of the axis. If imresize() is losing too much information for your purposes, then you are almost certainly relying on the fact that the displayed image is displaying more than one pixel per data unit.
You need to decide whether you need the final result to be 140 x 180 or if you need the final result to be some intermediate size between 140 x 180 and 479 x 616. Once you decide on the appropriate output size then you can imresize() to it.
Neda
on 28 Sep 2017
Thanks a lot Walter for clarify about info files.
More Answers (0)
Categories
Find more on Convert Image Type in Help Center and File Exchange
Tags
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)