Author Topic: PJSR: Wish list: Image.getSamples(array) performance  (Read 4212 times)

Offline mschuster

  • PTeam Member
  • PixInsight Jedi
  • *****
  • Posts: 1087
PJSR: Wish list: Image.getSamples(array) performance
« on: 2014 December 29 11:04:41 »
I would like to see better performance for Image.getSamples(array).

On Win7 1123, SSD disk, laptop, time required for one 8 megapixel 16-bit image not in the OS file cache:

ImageWindow.open()0.15 seconds
Image.getSamples(array)1.7 seconds

As a result opening 32 images takes about 60 seconds, most of it in getSamples(). Also, the script's UI is not responsive nor abortable during the majority of this time, which makes the overhead more annoying.

Thanks,
Mike


« Last Edit: 2014 December 29 11:17:39 by mschuster »

Offline Juan Conejero

  • PTeam Member
  • PixInsight Jedi Grand Master
  • ********
  • Posts: 7111
    • http://pixinsight.com/
Re: PJSR: Wish list: Image.getSamples(array) performance
« Reply #1 on: 2014 December 30 10:05:42 »
Yes, Image.getSamples()/Image.setSamples() are quite inefficient methods. These methods are intended for occasional use. Unfortunately, most of the overhead in these routines is caused by the task of creating a new Array element (See the documentation for the JS_SetElement() API call on MDN). This task, which is not trivial, has to be executed once for each pixel. Dynamic languages are very nice and powerful, but often the price to pay is performance problems like this one.

Instead of calling these methods, you can convert your image into a Matrix object by calling:

Matrix Image.toMatrix( [Rect rect=0[, int channel=-1]] )

and work directly on the Matrix object. To get a matrix element, use:

Number Matrix.at( int row, int col )

and to set elements:

void Matrix.at( int row, int col, Number value )

Once you are done working with the matrix, you can convert back to an image with:

Image Matrix.toImage()

Both methods are extremely efficient because they are fully implemented as native C++ code.
Juan Conejero
PixInsight Development Team
http://pixinsight.com/