Skip to main content

convert non-byte values to byte

11 replies [Last post]
sportinguista
Offline
Joined: 2010-05-30

Hi, I´m having problems with JAI and converting non-8bit images to byte type.

I have six-banded TIF images that could include double, 16bit, or negative values; and I would like to convert them to byte type just for displaying.

I have been looking at the DisplayDEM.java example in java.net; It looks great! but it only works with one or two banded images. For my 6band images prompts me "No ColorModel is supplied and the image ColorModel is null".

https://jaistuff.dev.java.net/code/display.DisplayDEM.java

How could this code be changed to work with my images?

Thank you very much for your help, I can´t find anything!

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
sportinguista
Offline
Joined: 2010-05-30

Well, it finally worked :D

Thank you very much Bob: after making "subtractconst" (min value) and "multiplyconst" (255/(max-min)), and then "format" to TYPE_BYTE, I just applied:

JAI.create("bandselect", surrogateImage, new int[]{2, 1, 0})

with int[] in various combinations.

Last problem is that 6th band has originally [0.0 - 16.35190643] range values, and scalling it with the formula gives very poor displaying results. (a lot of distinct values in few decimals are now rounded to the same value). Do you think that

(Value - min) * 255
---------------------------
(max -min)

needs max & min total values, or max or min of each band in order to transform one band by one?

Thank you Bob & Rafael!! now I can mark this post as answered.

Bob Deen

It depends on your application of course but it sounds like you should
do different scaling for each band, if the data ranges are very
different. Of course that will change how the bands balance relative to
each other and thus change the displayed color... but with 6 bands you
probably have a pseudo or false color image anyway so that may not matter.

BTW, your previous post had:

JAI.create(...).getAsBufferedImage()

You should not call getAsBufferedImage() unless you really need a BI.
The image returned by JAI.create() is an "active" image that pulls and
computes data a tile at a time as necessary to satisfy requests for
data. This is key to how JAI handles large images. getAsBI forces the
entire image to be computed and stored in memory, so you get one BI
structure containing the entire image. Sometimes that's what you need,
but rarely.

-Bob

jai-interest@javadesktop.org wrote:
> Well, it finally worked :D
>
> Thank you very much Bob: after making "subtractconst" (min value) and "multiplyconst" (255/(max-min)), and then "format" to TYPE_BYTE, I just applied:
>
> JAI.create("bandselect", surrogateImage, new int[]{2, 1, 0})
>
> with int[] in various combinations.
>
> Last problem is that 6th band has originally [0.0 - 16.35190643] range values, and scalling it with the formula gives very poor displaying results. (a lot of distinct values in few decimals are now rounded to the same value). Do you think that
>
> (Value - min) * 255
> ---------------------------
> (max -min)
>
> needs max & min total values, or max or min of each band in order to transform one band by one?
>
> Thank you Bob & Rafael!! now I can mark this post as answered.
> [Message sent by forum member 'sportinguista']
>
> http://forums.java.net/jive/thread.jspa?messageID=477825
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: interest-unsubscribe@jai.dev.java.net
> For additional commands, e-mail: interest-help@jai.dev.java.net
>

---------------------------------------------------------------------
To unsubscribe, e-mail: interest-unsubscribe@jai.dev.java.net
For additional commands, e-mail: interest-help@jai.dev.java.net

sportinguista
Offline
Joined: 2010-05-30

Okey, thanks for the advice. I was using BufferedImages when working with DisplayJAI, because it reloads images better than PlanarImages.

I have noticed that using "...getAsBufferedImage() " gives a ColorModel missing failure, but not with PlanarImages.

Can I ask you one last question please? When calibrating images to radiances, I have to format image data to Type_Double. My process would be:

PlanarImage pi;

double[] subtractThis = new double[nbands];
double[] multiplyBy = new double[nbands];
double[] addThis = new double[nbands];

// Create 3 nband-array to substract different values to each band, it is neccesary to make 3
//operations. I don´t know if it works this way¿¿??
...
(parameterblock sources are "pi", including corresponding arrays)
pi=JAI.create("subtractconst", pbSub, null);
pi=JAI.create("multiplyconst", pbMult, null);
pi=JAI.create("AddConst", pbSum, null);
...
pbConvert.add(DataBuffer.TYPE_DOUBLE);
pi=JAI.create("format", pbConvert);

As you can see, It is not a complete code. I don´t know if format has to be made after o before the operations; also if the way to change band to band values is correct.

What do you think?

Thank you!

Bob Deen

You might want to look at JadeDisplay as a display component; it's a lot
more sophisticated than DisplayJAI.

http://www.openchannelfoundation.org/projects/JadeDisplay/

It's open source with a BSD-style license. (disclaimer: I wrote it,
but it's free).

I think there are some bugs in the infrastructure that require
ColorModels where they are not strictly necessary. Also it should be
easier (as in, trivial) to attach or replace a CM when doing the
display. But, it is what it is.

As for the data type thing, for your application you should almost
certainly convert to double first thing... so that you don't lose
precision in any of your operations. Especially if you're converting to
radiances, whose data range has nothing to do with 0-255. You can do
that by putting format first, or you might be able to attach a
RenderingHint to the first operator that tells it to output double.

Good luck...

-Bob

jai-interest@javadesktop.org wrote:
> Okey, thanks for the advice. I was using BufferedImages when working with DisplayJAI, because it reloads images better than PlanarImages.
>
> I have noticed that using "...getAsBufferedImage() " gives a ColorModel missing failure, but not with PlanarImages.
>
> Can I ask you one last question please? When calibrating images to radiances, I have to format image data to Type_Double. My process would be:
>
> PlanarImage pi;
>
> double[] subtractThis = new double[nbands];
> double[] multiplyBy = new double[nbands];
> double[] addThis = new double[nbands];
>
> // Create 3 nband-array to substract different values to each band, it is neccesary to make 3
> //operations. I don´t know if it works this way¿¿??
> ...
> (parameterblock sources are "pi", including corresponding arrays)
> pi=JAI.create("subtractconst", pbSub, null);
> pi=JAI.create("multiplyconst", pbMult, null);
> pi=JAI.create("AddConst", pbSum, null);
> ...
> pbConvert.add(DataBuffer.TYPE_DOUBLE);
> pi=JAI.create("format", pbConvert);
>
> As you can see, It is not a complete code. I don´t know if format has to be made after o before the operations; also if the way to change band to band values is correct.
>
> What do you think?
>
> Thank you!
> [Message sent by forum member 'sportinguista']
>
> http://forums.java.net/jive/thread.jspa?messageID=477862
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: interest-unsubscribe@jai.dev.java.net
> For additional commands, e-mail: interest-help@jai.dev.java.net
>

---------------------------------------------------------------------
To unsubscribe, e-mail: interest-unsubscribe@jai.dev.java.net
For additional commands, e-mail: interest-help@jai.dev.java.net

sportinguista
Offline
Joined: 2010-05-30

Thanks again, I have changed to JadeDisplay without problems and It looks fine, good Job!

Bob Deen

I would recommend using the "bandselect" operator to pick 1 or 3 bands
for display. This also lets you control which bands are selected.

-Bob

jai-interest@javadesktop.org wrote:
> Hi, I´m having problems with JAI and converting non-8bit images to byte type.
>
> I have six-banded TIF images that could include double, 16bit, or negative values; and I would like to convert them to byte type just for displaying.
>
> I have been looking at the DisplayDEM.java example in java.net; It looks great! but it only works with one or two banded images. For my 6band images prompts me "No ColorModel is supplied and the image ColorModel is null".
>
> https://jaistuff.dev.java.net/code/display.DisplayDEM.java
>
> How could this code be changed to work with my images?
>
> Thank you very much for your help, I can´t find anything!
> [Message sent by forum member 'sportinguista']
>
> http://forums.java.net/jive/thread.jspa?messageID=477764
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: interest-unsubscribe@jai.dev.java.net
> For additional commands, e-mail: interest-help@jai.dev.java.net
>

---------------------------------------------------------------------
To unsubscribe, e-mail: interest-unsubscribe@jai.dev.java.net
For additional commands, e-mail: interest-help@jai.dev.java.net

sportinguista
Offline
Joined: 2010-05-30

Hi, thank you Bob.

I don´t understand what do you mean with the "bandselect" operator. My problem is that I can´t use 3 bands for displaying the image, I don´t have a colormodel.

I´m using the "DisplayCompositionsApp" example (http://jaistuff.dev.java.net...) for visualizing my 8bit images, and wanted to keep using it. I think it wouldn´t be a problem to pass a 8 bit sampled bufferedImage from the non-byte image, display the "adapted" and working with the non-byte one.

how could I insert a colormodel to the formated image?

Thank you again!

Bob Deen

I don't recall the details offhand but basically, you create the kind of
ColorModel you want (typically grayscale or rgb color) and attach it to
the image via a RenderingHints. You add the ColorMap to a
RenderingHints object and then pass that in when you create the
bandselect operator. The thing is, these color models expect 1 or 3
bands only of data, so you have to trim the number of bands first.

Hopefully that gives you a place to look, anyway.

-Bob

jai-interest@javadesktop.org wrote:
> Hi, thank you Bob.
>
> I don´t understand what do you mean with the "bandselect" operator. My problem is that I can´t use 3 bands for displaying the image, I don´t have a colormodel.
>
> I´m using the "DisplayCompositionsApp" example (http://jaistuff.dev.java.net...) for visualizing my 8bit images, and wanted to keep using it. I think it wouldn´t be a problem to pass a 8 bit sampled bufferedImage from the non-byte image, display the "adapted" and working with the non-byte one.
>
> how could I insert a colormodel to the formated image?
>
> Thank you again!
> [Message sent by forum member 'sportinguista']
>
> http://forums.java.net/jive/thread.jspa?messageID=477776
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: interest-unsubscribe@jai.dev.java.net
> For additional commands, e-mail: interest-help@jai.dev.java.net
>

---------------------------------------------------------------------
To unsubscribe, e-mail: interest-unsubscribe@jai.dev.java.net
For additional commands, e-mail: interest-help@jai.dev.java.net

sportinguista
Offline
Joined: 2010-05-30

Thanks again Bob,

I appreciate your help but would need more detailed info because I am a begginer in JAI.

I´m looking for some piece of code to create a basic colormodel and insert it to the image. I find JAI very difficult to understand because its very opaque with blockparameters (like "renderinghints") and operators.

So my question is still open. Please help!

Rafael Santos

Hi there,

Please check http://www.lac.inpe.br/JIPCookbook/index.jsp , some answers
are there, as soon as I get some time I'll return to this thread.

Rafael

> I appreciate your help but would need more detailed info because I am a begginer in JAI.
>
> I´m looking for some piece of code to create a basic colormodel and insert it to the image. I find JAI very difficult to understand because its very opaque with blockparameters (like "renderinghints") and operators.
>
> So my question is still open. Please help!
> [Message sent by forum member 'sportinguista']
>
> http://forums.java.net/jive/thread.jspa?messageID=477779
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: interest-unsubscribe@jai.dev.java.net
> For additional commands, e-mail: interest-help@jai.dev.java.net
>
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: interest-unsubscribe@jai.dev.java.net
For additional commands, e-mail: interest-help@jai.dev.java.net

sportinguista
Offline
Joined: 2010-05-30

Hi Rafael, thanks for your reply.

Despite having tryed all examples, I haven´t found what I´m looking for. In the DisplayNBImageWithLUTs example a colormodel is created:

ColorModel colorModel = new IndexColorModel(8, 256, reds, greens, blues);

But pausing the execution I can observe the colormodel in the newSurrogateImage is set to NULL.

I understand all the steps given until the colormodel operations. Please, I would need help at this point!!

To Bob: did you mean this? ->
JAI.create("bandselect", surrogateImage, new int[]{0, 1, 2}).getAsBufferedImage();

Thank you everyone :)