How to save image as png or jpg instead of .dat file
7 views (last 30 days)
Show older comments
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
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?
Answers (1)
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
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.
See Also
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!