MATLAB: Does the SAVEAS function produce an image with an erroneous single grey pixel in the top left corner in MATLAB 6.5 (R13)

bitmapbmpMATLABpixelsaveasscreenshot

I am using the SAVEAS function in MATLAB to save an image to a bitmap file. Upon observing the saved image, however, the first pixel of the image in the top left corner is actually a grey pixel that is not part of the image produced. It is important for this image data to be correct. What can I do to fix this problem?

Best Answer

This bug has been fixed for Release 14 SP1 (R14SP1). For previous releases, please read below for any possible workarounds:
This has been verified as a bug in MATLAB in MATLAB 6.5 (R13) and later versions in the method the SAVEAS function interprets the figure screenshot before writing the figure.
Currently, to work around this issue, try using the IMWRITE function instead. Using the IMWRITE function it is not required to generate individual figures before writing the image file, but can rather simply pass the image matrix itself to be written, as follows:
imwrite(pixelmat, filename, 'bmp')
If this is not desirable and it is necessary to use the format printed out by the SAVEAS function, then the following will also work:
saveas(gcf,filename, 'bmp');
reloadedImage = imread(filename);
% Reassign the first pixel to be the appropriate color
reloadedImage(1,1) = rightColor;
imwrite(reloadedImage,filename, 'bmp')