How to save the output image into the folder as shown in matlab?

3 views (last 30 days)
Hi there! I'm facing a problem when saving the output image that I need. I've successfully saved the image into the file with my desired location. However, the image saved is not the same as shown while running in the MATLAB. The following is my coding.
"imshow(blackMaskedOriImage,[])", when showing the image in MATLAB, I need to include '[]' to show the image that I've needed. If '[]' is not included, there's the only binary image that I'll get.
Therefore, I supposed to have this kind of image saved as shown in A. Instead I get the image as shown in B that saved in my folder.
Very much appreciate if there's a solution for this. Thank you.
A
B
% MAsk the original image and display it.
blackMaskedOriImage =bw.*double(Ia);
figure
imshow(blackMaskedOriImage,[]);%',[]'to display the crop area with dark background
blackMaskedOriImage(~mask) = NaN;
axis on;
title('Masked Outside Region');
path=strcat('C:\Users\Chuah\Desktop\FYP\Aperio_ImageScope\histopathologicalimage\B1_score2\mask+ori(MATLAB)\',srcFile(j).name);
imwrite(bw.*double(I),path);
  2 Comments
Geoff Hayes
Geoff Hayes on 16 Mar 2018
Your code is showing the image
blackMaskedOriImage =bw.*double(Ia)
but you are saving to file
bw.*double(I)
What is the difference between I and Ia?
matlab noob
matlab noob on 16 Mar 2018
Ops! Sorry my fault. Actually I is for original image and Ia is for enhanced image.

Sign in to comment.

Accepted Answer

Image Analyst
Image Analyst on 16 Mar 2018
Use fullfile(), not strcat().
Don't use path as the name of your variable - you'll destroy a very important built-in variable.
Don't have the image be double. Make it uint8.
Don't set pixels to nan.
% Mask image:
blackMaskedOriImage = Ia; % Initialize
blackMaskedOriImage(bw) = 0; % Blacken
% Construct full filename.
folder = 'C:\Users\Chuah\Desktop\FYP\Aperio_ImageScope\histopathological image\B1_score2\mask+ori(MATLAB)\';
fullFileName = fullfile(folder, srcFile(j).name);
% Write to disk.
imwrite(blackMaskedOriImage, fullFileName);
  3 Comments
Steve Eddins
Steve Eddins on 16 Mar 2018
I like Image Analyst's recommendation to work with uint8 images. If you need the mask for later computations, then keep it as a separate variable or file. However, if you really want to convert to double so you can set pixels to NaN, then convert to double using the function im2double instead of double. The function im2double scales your uint8 values in the range [0,255] to the range [0,1], which is what MATLAB and Image Processing Toolbox assume is the grayscale range when the type is double. The range mismatch is why you weren't getting the output image file you wanted.
matlab noob
matlab noob on 17 Mar 2018
Hi Steve! It works! I changed double to im2double as you have recommended. Thank you so much for your help.

Sign in to comment.

More Answers (1)

Puja Bharti
Puja Bharti on 4 Oct 2018
Edited: Puja Bharti on 4 Oct 2018
My ques is: if I save the enhanced image then using imshow(NSS_Img,[]) I get the same enhanced image. But if I use imshow(NSS_Img) the image is not the saved enhanced image. Please help. Actually, I want to enhance image X and save it and then crop a small portion from the saved enhanced image. But, the values of NSS_img and the image after saving is not same.
my code is given below:
X = imread(imfile(image).name);
filename = imfile(image).name;
full_file = fullfile('C:\Users\Puja Bharti\Desktop\matlab\',filename);
[pathname, name, ext] = fileparts(full_file);
s1 = 'En';
s2 = filename;
s = strcat(s1,s2);
new_name = fullfile(pathname, s)
if size(X,3)==3
X = rgb2gray(X);
end
grayImage= im2double(X);
figure('Name','Original image'), imshow(grayImage)
NSS_img = NSS(grayImage); %NSS_img is enhanced image
whos NSS_img
figure('Name','final image'), imshow(NSS_img,[])
imwrite(NSS_img,new_name,'png','Mode','lossless');

Community Treasure Hunt

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

Start Hunting!