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

#image #saving #output #false imageImage Processing Toolbox

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);

Best Answer

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);