xvloadimage with imagesc and colormap

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

xvloadimage with imagesc and colormap

Mike Miller-20
I'm finding it tricky to make an imagesc() plot with a particular colormap
and save it in a file (I want PNG but can get to that from some other
format if necessary).  I think maybe imagesc() doesn't respect the
colormap, but if I try to use image(), or imshow(), the image is just
solid black.  My colormap is grayscale.  I could just use
colormap("default"), and I tried it but it didn't work.  The commmand
"imagesc(X,1)" makes a reasonable image but it isn't using my colormap.

If I want to save an imagesc() image to a file, what do I have to do?  It
isn't using gnuplot, so the usual __gnuplot_set__ methods don't work.

In case it matters, I'm running Octave 2.1.71 under Linux with Octave
Forge and I don't have xv or ImageMagick installed, but I do have
xloadimage.

Mike



-------------------------------------------------------------
Octave is freely available under the terms of the GNU GPL.

Octave's home on the web:  http://www.octave.org
How to fund new projects:  http://www.octave.org/funding.html
Subscription information:  http://www.octave.org/archive.html
-------------------------------------------------------------

Reply | Threaded
Open this post in threaded view
|

Re: xvloadimage with imagesc and colormap

Mike Miller-20
Correction: The subject line should have had "xloadimage" not
"xvloadimage".

Mike



-------------------------------------------------------------
Octave is freely available under the terms of the GNU GPL.

Octave's home on the web:  http://www.octave.org
How to fund new projects:  http://www.octave.org/funding.html
Subscription information:  http://www.octave.org/archive.html
-------------------------------------------------------------

Reply | Threaded
Open this post in threaded view
|

Re: xvloadimage with imagesc and colormap

Stéfan van der Walt
In reply to this post by Mike Miller-20
Hi Mika

On Tue, Mar 07, 2006 at 08:13:14PM -0600, Mike Miller wrote:
> I'm finding it tricky to make an imagesc() plot with a particular colormap
> and save it in a file (I want PNG but can get to that from some other
> format if necessary).  I think maybe imagesc() doesn't respect the
> colormap, but if I try to use image(), or imshow(), the image is just
> solid black.  My colormap is grayscale.  I could just use
> colormap("default"), and I tried it but it didn't work.  The commmand
> "imagesc(X,1)" makes a reasonable image but it isn't using my
> colormap.

I find it strange that `imshow' displays a black image.  Imshow tries
to determine what kind of picture you supply, since they come in so
many shapes and forms.  Your image values can be in [0,1], or in
[0,255], or in [0,2^anything].

If the largest value of the image is slightly larger than 255, imshow
will probably assume it is in [0,512].  If `I' is your image, you
should therefore have

min(I(:)) >= 0
max(I(:)) <= 255 or max(I(:)) <= 1

You are right about imagesc: it does look like the colormap specified
is ignored.  A quick workaround is to set the colormap before calling
it, i.e.

colormap(jet(256));
imagesc(...)

> If I want to save an imagesc() image to a file, what do I have to do?  It
> isn't using gnuplot, so the usual __gnuplot_set__ methods don't
> work.

Use the `ind2rgb' function to convert your indexed image to RGB, after
which you can use `imwrite' (`jpgwrite' or `pngwrite') to store the
image to a file.

> In case it matters, I'm running Octave 2.1.71 under Linux with Octave
> Forge and I don't have xv or ImageMagick installed, but I do have
> xloadimage.

Without ImageMagick you will have to rely on `jpgwrite` or `pngwrite`.

Regards
Stéfan



-------------------------------------------------------------
Octave is freely available under the terms of the GNU GPL.

Octave's home on the web:  http://www.octave.org
How to fund new projects:  http://www.octave.org/funding.html
Subscription information:  http://www.octave.org/archive.html
-------------------------------------------------------------

Reply | Threaded
Open this post in threaded view
|

ind2rgb and pngwrite (was "xvloadimage with imagesc and colormap")

Mike Miller-20
On Wed, 8 Mar 2006, Stefan van der Walt wrote:

>Mike Miller wrote:
>
>> If I want to save an imagesc() image to a file, what do I have to do?
>> It isn't using gnuplot, so the usual __gnuplot_set__ methods don't
>> work.
>
> Use the `ind2rgb' function to convert your indexed image to RGB, after
> which you can use `imwrite' (`jpgwrite' or `pngwrite') to store the
> image to a file.
>
>> In case it matters, I'm running Octave 2.1.71 under Linux with Octave
>> Forge and I don't have xv or ImageMagick installed, but I do have
>> xloadimage.
>
> Without ImageMagick you will have to rely on `jpgwrite` or `pngwrite`.


This was excellent advice, but I think I should add something to help
people like me who are dealing with these functions for the first time.
Here are the two help docs:

  - Function File: [R, G, B] = ind2rgb (X, MAP)
      Convert an indexed image to red, green, and blue color components.
      If MAP is omitted, the current colormap is used for the conversion.

pngwrite writes a png file to the disk.
     pngwrite('filename',R,G,B,A) writes the specified file
     using the Red, Green, Blue and Alpha matrices.

     Data must be [0 255] or the high bytes will be lost.


My MAP used [0,1] values, so the R,G,B values were all [0,1], but it seems
that pngwrite requires [0,255] instead.  Also, my X matrix was [0,1], and
it seems that it has to be [1,256].  So I did this and it seemed to work
for me:

[R, G, B] = ind2rgb( round( 255*X ) + 1, round( 255*MAP ) );

But I didn't know what an "Alpha matrix" was for pngwrite, so I tried a
few things and ended up finding that this seemed to work:

A = round( 255 * ones( size(X) ) );

pngwrite( 'file.png', R, G, B, A)

The resulting png file looked good, but I'm not sure that I was following
correct procedure and I would enjoy hearing from someone who knows better.

Thanks again for the helpful tips.

Mike



-------------------------------------------------------------
Octave is freely available under the terms of the GNU GPL.

Octave's home on the web:  http://www.octave.org
How to fund new projects:  http://www.octave.org/funding.html
Subscription information:  http://www.octave.org/archive.html
-------------------------------------------------------------

Reply | Threaded
Open this post in threaded view
|

Re: ind2rgb and pngwrite (was "xvloadimage with imagesc and colormap")

Stéfan van der Walt
Hi Mike

On Thu, Mar 09, 2006 at 05:22:25PM -0600, Mike Miller wrote:

> This was excellent advice, but I think I should add something to help
> people like me who are dealing with these functions for the first time.
> Here are the two help docs:
>
>  - Function File: [R, G, B] = ind2rgb (X, MAP)
>      Convert an indexed image to red, green, and blue color components.
>      If MAP is omitted, the current colormap is used for the conversion.
>
> pngwrite writes a png file to the disk.
>     pngwrite('filename',R,G,B,A) writes the specified file
>     using the Red, Green, Blue and Alpha matrices.
>
>     Data must be [0 255] or the high bytes will be lost.
>
>
> My MAP used [0,1] values, so the R,G,B values were all [0,1], but it seems
> that pngwrite requires [0,255] instead.  Also, my X matrix was [0,1], and
> it seems that it has to be [1,256].  So I did this and it seemed to work
> for me:
>
> [R, G, B] = ind2rgb( round( 255*X ) + 1, round( 255*MAP ) );

The problem you mention is annoying.  In newer versions of Octave you
will therefore find that the imaging functions (well, currently only a
select few, including `imshow') accept an additional image description
format.  Previously, colour images were specified as three MxN
matrices in [0,1] or [0,255] (one for red, one for green and one for
blue).

The new format makes use of the (fairly new) numeric types, and
specifies the image as an MxNx3 matrix of a certain type.  For
example, we could have the following:

Type of image    Matrix size   Numeric type
-------------------------------------------------
Black and White  MxN           logical
Grey-level       MxN           double*, uint8, uint16, ...
Colour           MxNx3         double*, uint8, uint16, ...

* 'double' implies that image is in [0,1]

The new format allows us to specify an image using one parameter only,
i.e.

imshow(X) instead of imshow(r,g,b)

It also prevents wrong interpretations of image depth, since the depth
is specified by the numeric class (uint8 -> 8 bit or 255, uint32 -> 16
bit or 32768 etc.)

The bulk of the imaging functions need to be modified to handle the
new format.  Volunteers are, as always, welcome.

Regards
Stéfan



-------------------------------------------------------------
Octave is freely available under the terms of the GNU GPL.

Octave's home on the web:  http://www.octave.org
How to fund new projects:  http://www.octave.org/funding.html
Subscription information:  http://www.octave.org/archive.html
-------------------------------------------------------------