Thread Subject: Display image from binary data

Subject: Display image from binary data

From: Jesse Lai

Date: 26 Jan, 2008 17:08:15

Message: 1 of 7

Suppose that I have image data from a file such as a JPG or PNG that is
loaded in a MATLAB variable as the raw binary data from the file like
you would get when you use fread.

I'd like to be able to display this image directly by converting the
binary data into the vectors that MATLAB needs to use the image command.
  For example, using the imread function which loads the 3D matrix and
the image map. Is there a way to use the imread function (or similar
function), point it to the variable with the binary data, then spit out
the image data?

Right now, I write the binary data to a file on the disk, then use the
imread function on that temporary file, but there must be a better solution.

The reason I have the binary data in this format is because I'm
transferring it from various instruments. I store the binary data
directly and it is the desired final product, but I'd also like a way to
display the image at the same time. The temporary file thing isn't a
good solution, so I'd like to hear some other ideas!

Jesse

Subject: Display image from binary data

From: roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson)

Date: 27 Jan, 2008 06:02:53

Message: 2 of 7

In article <h_6dnVFTHN3l9QbanZ2dnUVZ_gednZ2d@comcast.com>,
Jesse Lai <someone@somewhere.com> wrote:
>Suppose that I have image data from a file such as a JPG or PNG that is
>loaded in a MATLAB variable as the raw binary data from the file like
>you would get when you use fread.

>I'd like to be able to display this image directly by converting the
>binary data into the vectors that MATLAB needs to use the image command.
> For example, using the imread function which loads the 3D matrix and
>the image map. Is there a way to use the imread function (or similar
>function), point it to the variable with the binary data, then spit out
>the image data?

image() does not work based upon "vectors": it works upon 2D or 3D
arrays. If it is 2D array, it is either a greyscale array (in which
each array location represents the intensity), or it is an
indexed array (in which each array location represents the index
into the color map.) If it is a 3D array, it is an RGB image
in which (x,y,1) is R, (x,y,2) is G, (x,y,3) is B.

You should be able to image() or imagesc() the original data array,
possibly followed by a colormap() call if the array was 2D.
--
   "Okay, buzzwords only. Two syllables, tops." -- Laurie Anderson

Subject: Display image from binary data

From: Jesse Lai

Date: 27 Jan, 2008 16:05:11

Message: 3 of 7

Yes, I'm aware of how image works. My question was, though, is there a
way to directly display the image data from the raw binary data (as it
would appear if you opened the file in notepad). I would like to not
have to write the binary data to disk first.

Walter Roberson wrote:
> In article <h_6dnVFTHN3l9QbanZ2dnUVZ_gednZ2d@comcast.com>,
> Jesse Lai <someone@somewhere.com> wrote:
>> Suppose that I have image data from a file such as a JPG or PNG that is
>> loaded in a MATLAB variable as the raw binary data from the file like
>> you would get when you use fread.
>
>> I'd like to be able to display this image directly by converting the
>> binary data into the vectors that MATLAB needs to use the image command.
>> For example, using the imread function which loads the 3D matrix and
>> the image map. Is there a way to use the imread function (or similar
>> function), point it to the variable with the binary data, then spit out
>> the image data?
>
> image() does not work based upon "vectors": it works upon 2D or 3D
> arrays. If it is 2D array, it is either a greyscale array (in which
> each array location represents the intensity), or it is an
> indexed array (in which each array location represents the index
> into the color map.) If it is a 3D array, it is an RGB image
> in which (x,y,1) is R, (x,y,2) is G, (x,y,3) is B.
>
> You should be able to image() or imagesc() the original data array,
> possibly followed by a colormap() call if the array was 2D.

Subject: Display image from binary data

From: roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson)

Date: 27 Jan, 2008 17:46:34

Message: 4 of 7

In article <sPKdnV70X6CxNgHanZ2dnUVZ_s3inZ2d@comcast.com>,
Jesse Lai <"jlai.[DELETETHIS]matlab"@gmail[NOSPAM].moc> top-posted:

Please do not post you reply above what you are replying to: it makes
it more difficult to hold a conversation.

>Walter Roberson wrote:
>> In article <h_6dnVFTHN3l9QbanZ2dnUVZ_gednZ2d@comcast.com>,
>> Jesse Lai <someone@somewhere.com> wrote:
>>> Suppose that I have image data from a file such as a JPG or PNG that is
>>> loaded in a MATLAB variable as the raw binary data from the file like
>>> you would get when you use fread.

>Yes, I'm aware of how image works. My question was, though, is there a
>way to directly display the image data from the raw binary data (as it
>would appear if you opened the file in notepad). I would like to not
>have to write the binary data to disk first.

In your initial post you said that you "have image data from a file" (etc),
which is a statement that you have the *image data* as an entity.
It appears from your second posting that you do not have the
image data: that instead you have the binary contents of a JPG or PNG
file, and that content would have to be decoded in order to display
the image. If that is the case, then you should be able to make
a small modification to something such as

http://www.mathworks.com/matlabcentral/fileexchange/loadFile.do?objectId=10476&objectType=File
"JPEG Codec"

I do not think that Matlab itself provides a method to decode an
image file from a binary array.

--
  "There are some ideas so wrong that only a very intelligent person
  could believe in them." -- George Orwell

Subject: Display image from binary data

From: Jesse Lai

Date: 27 Jan, 2008 19:03:26

Message: 5 of 7

Walter Roberson wrote:
> In article <sPKdnV70X6CxNgHanZ2dnUVZ_s3inZ2d@comcast.com>,
> Jesse Lai <"jlai.[DELETETHIS]matlab"@gmail[NOSPAM].moc> top-posted:
>
> Please do not post you reply above what you are replying to: it makes
> it more difficult to hold a conversation.
>
>> Walter Roberson wrote:
>>> In article <h_6dnVFTHN3l9QbanZ2dnUVZ_gednZ2d@comcast.com>,
>>> Jesse Lai <someone@somewhere.com> wrote:
>>>> Suppose that I have image data from a file such as a JPG or PNG that is
>>>> loaded in a MATLAB variable as the raw binary data from the file like
>>>> you would get when you use fread.
>
>> Yes, I'm aware of how image works. My question was, though, is there a
>> way to directly display the image data from the raw binary data (as it
>> would appear if you opened the file in notepad). I would like to not
>> have to write the binary data to disk first.
>
> In your initial post you said that you "have image data from a file" (etc),
> which is a statement that you have the *image data* as an entity.
> It appears from your second posting that you do not have the
> image data: that instead you have the binary contents of a JPG or PNG
> file, and that content would have to be decoded in order to display
> the image. If that is the case, then you should be able to make
> a small modification to something such as
>
> http://www.mathworks.com/matlabcentral/fileexchange/loadFile.do?objectId=10476&objectType=File
> "JPEG Codec"
>
> I do not think that Matlab itself provides a method to decode an
> image file from a binary array.
>

Yes, that sounds more like what I want.

In my first post I did say:
"...from a file such as a JPG or PNG that is
loaded in a MATLAB variable as the raw binary data from the file like
you would get when you use fread."

It sounds like for each image type (JPG, PNG, etc) a specific decoder
would be required, is that correct? That's the nice thing about the
function imread is that it already has all of those decoders built in
for the most common image formats.

I'll research further, thanks for the link.

Subject: Display image from binary data

From: roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson)

Date: 27 Jan, 2008 19:22:43

Message: 6 of 7

In article <aoGdnS7qS_BrSQHanZ2dnUVZ_rSrnZ2d@comcast.com>,
Jesse Lai <"jlai.[DELETETHIS]matlab"@gmail[NOSPAM].moc> wrote:

>It sounds like for each image type (JPG, PNG, etc) a specific decoder
>would be required, is that correct? That's the nice thing about the
>function imread is that it already has all of those decoders built in
>for the most common image formats.

Yes, that is right.

I do not know how you are obtaining your images from the instruments.
Perhaps for some TCP/IP connected devices, you might be able to
use imread() specifying a URL on the device in order to fetch the
data and decode it in one step ?
--
  "I will speculate that [...] applications [...] could actually see a
  performance boost for most users by going dual-core [...] because it
  is running the adware and spyware that [...] are otherwise slowing
  down the single CPU that user has today" -- Herb Sutter

Subject: Display image from binary data

From: Jesse Lai

Date: 27 Jan, 2008 20:03:37

Message: 7 of 7

Walter Roberson wrote:
> In article <aoGdnS7qS_BrSQHanZ2dnUVZ_rSrnZ2d@comcast.com>,
> Jesse Lai <"jlai.[DELETETHIS]matlab"@gmail[NOSPAM].moc> wrote:
>
>> It sounds like for each image type (JPG, PNG, etc) a specific decoder
>> would be required, is that correct? That's the nice thing about the
>> function imread is that it already has all of those decoders built in
>> for the most common image formats.
>
> Yes, that is right.
>
> I do not know how you are obtaining your images from the instruments.
> Perhaps for some TCP/IP connected devices, you might be able to
> use imread() specifying a URL on the device in order to fetch the
> data and decode it in one step ?

I use a binary block transfer from the various types of devices to
transfer the raw binary data.

You're right, for the TCP/IP connected devices, it is possible to use
imread directly. However, some of the instruments are Windows, some are
not. In the Windows cases, you have to have permission to access
folders on the instruments. I try to bypass all of the potential
Windows problems by just doing a binary transfer using the instrument
drivers. Additionally, I like it to be flexible to support GPIB
connected instruments as well.

The end product desired is the binary data because in an automated test,
the binary data is what is stored. However, in manual mode, its nice to
just be able to pop up the image to the user.

Your suggestion was a good one though, and I'll look for decoders for
the various formats. I'm primarily interested in BMP, JPG, and PNG, so
I'm sure there must be lots of information out there on implementing a
decoder.

Tags for this Thread

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

rssFeed for this Thread

Public Submission Policy

NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Disclaimer prior to use.

Contact us at files@mathworks.com