MATLAB: Cant write image exactly shown by imshow

digital image processingimage analysisimage processingImage Processing Toolbox

hi every body. i have written a code which the result in imshow is ok but when i want to imwrite it, its different and wrong. could any body help me please?
Echo = imread('echo60%-croped-SRAD.jpg');
Mask = imread('mask60%.jpg');
Echo = mat2gray(Echo(:,:,1));
Mask = mat2gray(Mask(:,:,1));
Echo_Com = imcomplement(Echo);
Echo_Com_Final = (Echo_Com.*Mask)*255;
imshow(Echo_Com_Final, [0 255]);
imwrite(Echo_Com_Final,map,'echo60%-croped-SRAD_complement.jpg')

Best Answer

To write the image exactly as it is shown, you can use getframe() and then imwrite(). Keep in mind that this is like a screenshot so it won't have the same size and values as the original, underlying image, but it will be "exactly shown by imshow" that you requested
displayedImage = getframe();
imwrite(displayedImage, filename);
Or you can use export_fig()