How to save image as png or jpg instead of .dat file

7 views (last 30 days)
Following is the code in Simulink's motion sensor and camera model for raspberry pi. When I am running the model on raspberry pi, the images are stored as .dat file. If I change the extension .dat to .png or .jpg, the files do not open. How could I store RGB image as png or jpg instead of .dat file?
Code:
format = ['%d ', 0];
fname = coder.nullcopy(uint8(zeros(1, 32)));
coder.ceval('sprintf', coder.wref(fname), ['/var/www/html/photos/img%d.png', 0], counter);
fd = coder.opaque('FILE *');
fd = coder.ceval('fopen', fname, ['w', 0]);
for i = 1:320
for j = 1:240
coder.ceval('fprintf', fd, format, R(i, j));
end
end
coder.ceval('fclose', fd);
  4 Comments
Walter Roberson
Walter Roberson on 6 Jan 2018
If RGB values are being passed to the code, then why are you only accessing two dimensions, R(i,j) ? Or is R the red component and there are also G and B matrices?
Marium Malik
Marium Malik on 6 Jan 2018
Yes, R is the red component, I have G and B matrices as well. I am getting them as ouput from Raspberry Pi camera.

Sign in to comment.

Answers (1)

Walter Roberson
Walter Roberson on 6 Jan 2018
That code does not even attempt to write an image file at the moment: it writes out space separated decimal numbers, all in one line, and not color. Only the Portable Graymap (PGM) format is close to that; it might be easiest for you to write that; https://en.wikipedia.org/wiki/Netpbm_format#PGM_example
  4 Comments
Marium Malik
Marium Malik on 6 Jan 2018
How could it be written in my case? I am getting R,G and B values from Raspberry pi camera
Walter Roberson
Walter Roberson on 6 Jan 2018
Use that link I posted about PGM format but look at the very next part where it shows PPM format where it writes out RGB triples. The main thing to watch out for is that the header line has width followed by height instead of the more common height followed by width.

Sign in to comment.

Categories

Find more on Raspberry Pi Hardware in Help Center and File Exchange

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!