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

6 次查看(过去 30 天)
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 个评论
Geoff Hayes
Geoff Hayes 2018-3-16
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 2018-3-16
Ops! Sorry my fault. Actually I is for original image and Ia is for enhanced image.

请先登录,再进行评论。

采纳的回答

Image Analyst
Image Analyst 2018-3-16
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 个评论
Steve Eddins
Steve Eddins 2018-3-16
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 2018-3-17
Hi Steve! It works! I changed double to im2double as you have recommended. Thank you so much for your help.

请先登录,再进行评论。

更多回答(1 个)

Puja Bharti
Puja Bharti 2018-10-4
编辑:Puja Bharti 2018-10-4
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');

类别

Help CenterFile Exchange 中查找有关 Convert Image Type 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by