Search code examples
imagematlabrgbluminance

How to convert RGB image to Luminance image and store the image as .raw file in Matlab


i am trying to convert an RGB image to a Luminance image and save it as a .raw image to use it in another software. I am using the following code

m = imread('20x20-alpha1-1.jpg');
out = zeros(1942,2588);
for i=1:1942
   for j=1:2588
    out(i,j) = 0.2126*m(i,j,1) + 0.7152*m(i,j,2) + 0.0722*m(i,j,3);
   end
end
fileID = fopen('20x20-alpha1-1.raw');
fwrite(fileID,out);
fclose(fileID);

However, when I try to open the image with IrfanViewer, the file is said to be corrupted. Is it a problem in my code ? If so how can I convert this image to Luminance image and save it ? Thank you :)


Solution

  • There is no need to mess around with .raw files in this case. Write a tiff file instead:

    imwrite(out,'20x20-alpha1-1.tiff','tiff')