-2

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

BF Mohtedi
  • 53
  • 6
  • 2
    You are simply writing the values with `fopen`. That's not a valid raw picture format. See for example [here](https://en.wikipedia.org/wiki/Raw_image_format#File_contents) – Luis Mendo Jul 22 '15 at 14:52
  • 1
    You are writing the raw image data, that's something else then writing a .raw file. The later contains additional information like resolution at the beginning of the file. – Daniel Jul 22 '15 at 14:54
  • So how is it possible to store one channel image from matlab (Luminance channel) in a .raw file ? – BF Mohtedi Jul 22 '15 at 15:03
  • 2
    You'd need to know the specification of the raw format. There's not a single raw format – Luis Mendo Jul 22 '15 at 15:09
  • @BFMohtedi: What do you really need? To export data in a loseless format, I would use TIFF – Daniel Jul 22 '15 at 15:34
  • What I need is to have a one channel image (luminance channel) out from an RGB image and that the one channel image is a stand alone file. – BF Mohtedi Jul 22 '15 at 20:00

1 Answers1

0

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')
Daniel
  • 36,610
  • 3
  • 36
  • 69
  • thank you ! it is much easier like this. However there is one more change to make: `out(i,j) = uint8(0.2126*m(i,j,1) + 0.7152*m(i,j,2) + 0.0722*m(i,j,3))` to get correct output – BF Mohtedi Jul 23 '15 at 07:43