Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Access compressed binary image (JPEG) without IMREAD

30 views
Skip to first unread message

Bobane

unread,
Jun 17, 2010, 5:48:03 AM6/17/10
to
I have a Matlab program which simulates transmission of binary data over a wireless communications channel. I need to transmit an image (compressed JPEG) over the channel. When I load a JPEG using IMREAD, the loaded data occupies more memory than the original filesize (due to compression). E.g. the file "football.jpg" (in Matlab root) has a file size of 27130 bytes, but when loaded as:
A=imread('football.jpg'); whos A;
gives A occupying 245760 bytes (256 x 320 x 3). I can easily send the data in A through my channel simulator, but am wondering whether there is a way in which I could send but the 24KB binary data from the original JPEG file, since this is much smaller than the 245KB data in A. I want to take advantage of the image compression to transmit a smaller data stream through the channel. Hence my question as to whether I can access the original 24KB binary data directly without using imread. I am assuming that since the JPEG file is a binary file, I can somehow access that and convert it directly into a binary (1s and 0s) data stream. Can anyone help? Thanks

us

unread,
Jun 17, 2010, 6:01:04 AM6/17/10
to
"Bobane" <boba...@hotmail.com> wrote in message <hvcr0j$ise$1...@fred.mathworks.com>...

> I have a Matlab program which simulates transmission of binary data over a wireless communications channel. I need to transmit an image (compressed JPEG) over the channel. When I load a JPEG using IMREAD, the loaded data occupies more memory than the original filesize (due to compression). E.g. the file "football.jpg" (in Matlab root) has a file size of 27130 bytes, but when loaded as:
> A=imread('football.jpg'); whos A;
> gives A occupying 245760 bytes (256 x 320 x 3). I can easily send the data in A through my channel simulator, but am wondering whether there is a way in which I could send but the 24KB binary data from the original JPEG file, since this is much smaller than the 245KB data in A. I want to take advantage of the image compression to transmit a smaller data stream through the channel. Hence my question as to whether I can access the original 24KB binary data directly without using imread. I am assuming that since the JPEG file is a binary file, I can somehow access that and convert it directly into a binary (1s and 0s) data stream. Can anyone help? Thanks

one of the solutions
- use low-level i/o functions

fp=fopen('football.jpg','rb');
if fp > 0
ibin=fread(fp,inf,'*uint8');
fclose(fp);
whos ibin;
%{
Name Size Bytes Class Attributes
ibin 27130x1 27130 uint8
%}
end

us

Bobane

unread,
Jun 17, 2010, 6:41:05 AM6/17/10
to
"us " <u...@neurol.unizh.ch> wrote in message <hvcrp0$6fg$1...@fred.mathworks.com>...

Many thanks - that's exactly what I wanted! A secondary question if I may: - having read the data from the file using
ibin=fread(...);
if I wanted to display the image in ibn, is there a way of doing this without having to first save the data into a file using imwrite? In other words, can I extract the data in my variable A (A=imread('football.jpg')) from the data in ibn without first saving ibn back into a JPEG file? I've tried
imread (ibn);
but that doesn't work.

Many thanks.

us

unread,
Jun 17, 2010, 7:54:04 AM6/17/10
to
"Bobane" <boba...@hotmail.com> wrote in message <hvcu41$4k3$1...@fred.mathworks.com>...

obviously, nothing of the above will work since you simply slurped byte by byte without any decoding of the information...

us

Steve Amphlett

unread,
Jun 17, 2010, 8:13:04 AM6/17/10
to
"us " <u...@neurol.unizh.ch> wrote in message <hvd2cs$3dn$1...@fred.mathworks.com>...

A quick skim though imread to see if anything could be re-used didn't look too promising either.

Bobane

unread,
Jun 17, 2010, 9:03:04 AM6/17/10
to
"Steve Amphlett" <Firstname...@Where-I-Work.com> wrote in message <hvd3gg$e0t$1...@fred.mathworks.com>...


Thanks guys - I guess I will just have to write the data back to a file and read it back to display:
fid=fopen('myfile.jpg','wb');
fwrite(fid,ibin);
fclose(fid);
A=imread('myfile.jpg');
imshow(A);

As I have to do this several times, I was hoping to avoid the repeated file write and read, but seems that can't be helped.
Thanks all the same.

Bobane

unread,
Jun 17, 2010, 10:58:04 AM6/17/10
to
Seems that the code for loading JPEGS (and other images) are in function_handles - imread() simply calls these handles through imformats() function. For JPEGs, need @readjpg to find out what goes on. Does anyone know how to access (edit) these - if it is at all possible? Surely the binary data in the JPEG file has to 'slurped' in first before being manipulated into whatever form - would be helpful to know if this is the case, and what is then done to the data.

"Bobane" <boba...@hotmail.com> wrote in message <hvd6e8$k1o$1...@fred.mathworks.com>...

Walter Roberson

unread,
Jun 17, 2010, 11:32:18 AM6/17/10
to
Bobane wrote:
> Seems that the code for loading JPEGS (and other images) are in
> function_handles - imread() simply calls these handles through
> imformats() function. For JPEGs, need @readjpg to find out what goes on.
> Does anyone know how to access (edit) these - if it is at all possible?
> Surely the binary data in the JPEG file has to 'slurped' in first before
> being manipulated into whatever form - would be helpful to know if this
> is the case, and what is then done to the data.

Matlab does not provide any documented mechanism to display JPEG
byte-steams that are in memory.

You can probably find a Java JPEG displayer and edit it to accept byte
streams.

Steve Eddins

unread,
Jun 17, 2010, 2:51:55 PM6/17/10
to
On 6/17/2010 11:32 AM, Walter Roberson wrote:
> Bobane wrote:
>> Seems that the code for loading JPEGS (and other images) are in
>> function_handles - imread() simply calls these handles through
>> imformats() function. For JPEGs, need @readjpg to find out what goes
>> on. Does anyone know how to access (edit) these - if it is at all
>> possible? Surely the binary data in the JPEG file has to 'slurped' in
>> first before being manipulated into whatever form - would be helpful
>> to know if this is the case, and what is then done to the data.
>
> Matlab does not provide any documented mechanism to display JPEG
> byte-steams that are in memory.
>

MATLAB does not provide any undocumented mechanism, either. :-)

---
Steve Eddins
http://blogs.mathworks.com/steve/


Walter Roberson

unread,
Jun 17, 2010, 3:11:14 PM6/17/10
to

Sometimes it would be useful if Matlab provided a way to "fopen" a memory
buffer and do I/O operations on it. This is similar in some ways to mmap'ing a
file, except that no disk store would be used.

For writing, probably the first version should return a write error if the end
of the buffer was reached; extending the memory might perhaps not be the most
consistent behaviour... ah, unless perhaps the extended buffer became an
additional (optional) output argument.

Amongst other things, it would be useful if writing a plot (e.g., saveas,
imwrite) could be done to memory; likewise for audio files and for movies.

rado

unread,
Feb 2, 2013, 5:35:08 PM2/2/13
to
can you tell me how you can convert you compressed image to binary data for sending it throw the channel ..please

Gabriela Mogollón

unread,
Jul 21, 2016, 1:15:09 PM7/21/16
to
% decode image stream using Java
jImg = javax.imageio.ImageIO.read(java.io.ByteArrayInputStream(ibin));
h = jImg.getHeight;
w = jImg.getWidth;

% convert Java Image to MATLAB image
p = reshape(typecast(jImg.getData.getDataStorage, 'uint8'), [3,w,h]);
img = cat(3, ...
transpose(reshape(p(3,:,:), [w,h])), ...
transpose(reshape(p(2,:,:), [w,h])), ...
transpose(reshape(p(1,:,:), [w,h])));

% show
imshow(img)

"Bobane" wrote in message <hvcu41$4k3$1...@fred.mathworks.com>...
0 new messages