Skip to main content

[JAI] Speed issues

6 replies [Last post]
Anonymous

Hi,

I'm trying to develop a program in java for calculating some
parameters out of an image. The image I use is around 26MB (3327x4096)
(and it is around the avarage of the images the program needs to
analyse) I've a similar program (not my own), but not in Java. When
running this program, it calculates the information from whole image
in about 2-3 secs

The same image, in Java, it takes about 81 secs to calculate *only one
row* of the image. Is there anything I can do about this? Can I speed
it up somewhere? Are there speedissues in JAI I can use?

The program has to iterate over the whole image, calculating the
average, standard deviation, etc over a roi (eg 170x170 pixels). I use
the following code snippet to calculate these parameters:

int bits = dataset.getInt(Tags.BitsStored, 8);
int size = 1 << bits;
int signed = dataset.getInt(Tags.PixelRepresentation, 0);
int min = dataset.getInt(Tags.SmallestImagePixelValue,
signed == 0 ? 0 : -(size >> 1));
int max = dataset.getInt(Tags.LargestImagePixelValue,
signed == 0 ? size - 1 : (size >> 1) - 1);

int[] bins = {size}; // The number of bins.
double[] low = {(double) min}; // The low value.
double[] high = {(double) max}; // The high value.

public Histogram calculateHistogram(ROI roi) {
ParameterBlock pb = new ParameterBlock();
pb.addSource(image); // The source image
pb.add(roi); // null ROI means whole image
pb.add(1); // check every pixel horizontally
pb.add(1); // check every pixel vertically
pb.add(bins);
pb.add(low);
pb.add(high);

PlanarImage dst = (PlanarImage) JAI.create("histogram", pb);
// Retrieve the histogram data.
Histogram hist = (Histogram) dst.getProperty("histogram");
return hist;
}

Thanks for any information you can provide me.

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

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Bob Deen

>If I calculate the histogram over the whole image (3328x4096) it takes about 3,746 seconds.
>
>When calculating the historgram of a ROI (142x142) of the image, it takes about 1,322 seconds.
>
>This is only a factor of around 2,8 ... while it is a much smaller region...

It could also depend on the tile size. For example if you had 1K square
tiles, you'd have about 3x4=12 full-size tiles in the image. If the ROI
split the corner, you could have to fetch 4 full tiles, which would be 4/12
or a factor of 3 better speed.

More likely, the image reader you're using isn't very efficient in dealing
with tiles (or is even untiled).

-Bob

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

Brian Burkhalter

A priori I don't see anything you are doing wrong.

How are you timing the computation?

Brian

On Mon, 30 May 2005, Tom Deprez wrote:

> Hi,
>
> I'm trying to develop a program in java for calculating some
> parameters out of an image. The image I use is around 26MB (3327x4096)
> (and it is around the avarage of the images the program needs to
> analyse) I've a similar program (not my own), but not in Java. When
> running this program, it calculates the information from whole image
> in about 2-3 secs
>
> The same image, in Java, it takes about 81 secs to calculate *only one
> row* of the image. Is there anything I can do about this? Can I speed
> it up somewhere? Are there speedissues in JAI I can use?
>
> The program has to iterate over the whole image, calculating the
> average, standard deviation, etc over a roi (eg 170x170 pixels). I use
> the following code snippet to calculate these parameters:
>
> int bits = dataset.getInt(Tags.BitsStored, 8);
> int size = 1 << bits;
> int signed = dataset.getInt(Tags.PixelRepresentation, 0);
> int min = dataset.getInt(Tags.SmallestImagePixelValue,
> signed == 0 ? 0 : -(size >> 1));
> int max = dataset.getInt(Tags.LargestImagePixelValue,
> signed == 0 ? size - 1 : (size >> 1) - 1);
>
> int[] bins = {size}; // The number of bins.
> double[] low = {(double) min}; // The low value.
> double[] high = {(double) max}; // The high value.
>
> public Histogram calculateHistogram(ROI roi) {
> ParameterBlock pb = new ParameterBlock();
> pb.addSource(image); // The source image
> pb.add(roi); // null ROI means whole image
> pb.add(1); // check every pixel horizontally
> pb.add(1); // check every pixel vertically
> pb.add(bins);
> pb.add(low);
> pb.add(high);
>
> PlanarImage dst = (PlanarImage) JAI.create("histogram", pb);
> // Retrieve the histogram data.
> Histogram hist = (Histogram) dst.getProperty("histogram");
> return hist;
> }
>
> Thanks for any information you can provide me.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: interest-unsubscribe@jai.dev.java.net
> For additional commands, e-mail: interest-help@jai.dev.java.net
>
>

----------------
Brian Burkhalter
Advanced Development, Graphics and Media
Software Chief Technology Office
Sun Microsystems, Inc.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
This email message is for the sole use of the intended recipient(s)
and may contain confidential and privileged information. Any
unauthorized review, use, disclosure or distribution is prohibited.
If you are not the intended recipient, please contact the sender by
reply email and destroy all copies of the original message.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

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

zifnabbe
Offline
Joined: 2005-05-26
Points: 0

very simple:

before calculating the roi:
long startTime = System.currentTimeMillis();

after calculating the roi:
elapsedTime = System.currentTimeMillis()-startTime;
out.write(elapsedTime/(1000F)+ " (s)"); out.write("\t");

zifnabbe
Offline
Joined: 2005-05-26
Points: 0

I don't understand something:

If I calculate the histogram over the whole image (3328x4096) it takes about 3,746 seconds.

When calculating the historgram of a ROI (142x142) of the image, it takes about 1,322 seconds.

This is only a factor of around 2,8 ... while it is a much smaller region...

Is this normal behavior?

Brian Burkhalter

You are probably not timing the Histogram operation alone.

If you have this code

String fileName;
PlanarImage source = JAI.create("ImageRead", fileName);
ParameterBlock pb;
// ParameterBlock initialization omitted
long startTime = System.currentTimeMillis();
PlanarImage histo = JAI.create("Histogram", pb);
long elapsedTime = System.currentTimeMillis() - startTime;

then 'elapsedTime' will include the time required to compute the histogram
over the ROI as well as the time to read all the tiles which overlap the ROI.
This is because of deferred excution as discussed recently in other threads:
JAI.create() merely adds a node to a graph but pixel computation is deferred
until needed.

If you want to isolate the Histogram calculation you need to force the
computation of everything in the chain before that point:

String fileName;
PlanarImage source = JAI.create("ImageRead", fileName);
ParameterBlock pb;
// ParameterBlock initialization omitted
source.getTiles(); // Force image read
long startTime = System.currentTimeMillis();
PlanarImage histo = JAI.create("Histogram", pb);
long elapsedTime = System.currentTimeMillis() - startTime;

You also need to ensure that the TileCache is big enough so that tiles of
'source' will not be flushed and therefore need to be re-read.

Brian

On Wed, 1 Jun 2005 jai-interest@javadesktop.org wrote:

> I don't understand something:
>
> If I calculate the histogram over the whole image (3328x4096) it takes about 3,746 seconds.
>
> When calculating the historgram of a ROI (142x142) of the image, it takes about 1,322 seconds.
>
> This is only a factor of around 2,8 ... while it is a much smaller region...
>
> Is this normal behavior?
> ---
> [Message sent by forum member 'zifnabbe' (Tom Deprez)]
>
> http://www.javadesktop.org/forums/thread.jspa?messageID=86283&#86283

----------------
Brian Burkhalter
Advanced Development, Graphics and Media
Software Chief Technology Office
Sun Microsystems, Inc.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
This email message is for the sole use of the intended recipient(s)
and may contain confidential and privileged information. Any
unauthorized review, use, disclosure or distribution is prohibited.
If you are not the intended recipient, please contact the sender by
reply email and destroy all copies of the original message.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

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

zifnabbe
Offline
Joined: 2005-05-26
Points: 0

Hi,

Sorry to re-init this question, but can somebody give me an idea on:

1) this is normal behavior (ie it can't go quicker)
2) my code is incorrect and there is a much better way to handle it. At the moment I perform this the following way:

// somewhere in the beginning of the code

int[] bins = {size}; // The number of bins.
double[] low = {(double) min}; // The low value.
double[] high = {(double) max}; // The high value.

pb = new ParameterBlock();
pb.addSource(image); // The source image
pb.add(null); // null ROI means whole image
pb.add(1);
pb.add(1);
pb.add(bins);
pb.add(low);
pb.add(high);

//For each 1cm block on the image:

r = new Rectangle(x,
y,
w,
h);
roi = new ROIShape(r);
hist = calculateHistogram(roi);

public Histogram calculateHistogram(ROI roi) {
pb.set(roi, 0);
PlanarImage dst = (PlanarImage) JAI.create("histogram", pb);
Histogram hist = (Histogram) dst.getProperty("histogram");
return hist;
}

Is there another way to do this?

Thanks in advance!

Message was edited by: zifnabbe