Author Topic: PJSR getting view id of image created  (Read 5651 times)

Offline bitli

  • PTeam Member
  • PixInsight Guru
  • ****
  • Posts: 513
PJSR getting view id of image created
« on: 2009 December 19 03:44:23 »
Hello,

In the following code:
    var pm = new PixelMath;

    with (pm) {
        expression  = data.flatFrame1.id + "+"+data.flatFrame2.id;
        useSingleExpression = true;
        ...
        createNewImage = true;
        newImageId = 'MCCD_F_SUM';
    }

    data.flatFrame1.beginProcess(UndoFlag_NoSwapFile);
    pm.executeOn(data.flatFrame1);
    data.flatFrame1.endProcess();


If an image with the same id already exist, a new one with a sequential number is appended is created. Is there a way to find the effective id of the image returned?  Is there a way to have the returned image not being visible?

Also is it possible to make a pixelmath operation on (source) views? If not, can I convert the view to non visible images?

I have some ideas for work arounds, but I am interested to know if there is an elegant way to achieve this.

-- bitli

Offline Juan Conejero

  • PTeam Member
  • PixInsight Jedi Grand Master
  • ********
  • Posts: 7111
    • http://pixinsight.com/
Re: PJSR getting view id of image created
« Reply #1 on: 2009 December 19 04:28:49 »
Hi Bitli,

Quote
If an image with the same id already exist, a new one with a sequential number is appended is created. Is there a way to find the effective id of the image returned?

Not specifically. Although a "tricky" method could be relatively easy to implement, the best way to manage this is to first check if the desired view id is already in use:

Code: [Select]
function isViewIdInUse( id )
{
   return !View.viewById( id ).isNull;
}

Then a routine to find a unique view id (by suffixing a base id with a zero-padded growing counter, in the same fashion the PI core application does) is immediate:

Code: [Select]
function uniqueViewId( baseId )
{
   var id = baseId;
   for ( var count = 0; isViewIdInUse( id ); )
      id = baseId + format( "%02d", ++count );
   return id;
}

With this routine, you can create a PixelMath instance:

Code: [Select]
var myUniqueImageId = uniqueViewId( "MCCD_F_SUM" );
var pm = new PixelMath;
with ( pm )
{
   expression = data.flatFrame1.id + "+" + data.flatFrame2.id;
   useSingleExpression = true;
   // ...
   createNewImage = true;
   newImageId = myUniqueImageId;
}

and be sure your result image is uniquely identified as you expect it to be.

Quote
Is there a way to have the returned image not being visible?

Nope. PixelMath will make the output image window explicitly visible. There is no way to avoid this. You can explicitly hide the window (as you know its identifier), but with the current responsiveness of PI's GUI, there is no way to avoid an ugly flashing window...

Having explained all that, you don't need PixelMath to carry out this task. You can create images as private objects in your script and operate with them. For example:

Code: [Select]
var flatView1 = View.viewById( data.flatFrame1.id );
var flatView2 = View.viewById( data.flatFrame2.id );

var resultWindow = new ImageWindow( 1, 1, 1, // width, height, channels
                                    32, // bits per sample
                                    true, // float sample
                                    false, // true=RGB, false=grayscale
                                    myUniqueImageId ); // your unique identifier
var resultView = resultWindow.mainView;

with ( resultView )
{
   beginProcess( UndoFlag_NoSwapFile );
   image.assign( flatView1.image ); // assign the first image
   image.apply( flatView2.image, ImageOp_Add ); // add both images
   image.rescale();     // either rescale result to [0,1] (if necessary)
   //image.truncate();  // or truncate it to [0,1]
   endProcess();
}

resultWindow.show();
resultWindow.zoomToOptimalFit();

Hope this helps. Let me know how it goes.

Edited: fixed a small syntax error (disclaimer: code not tested -> can be errors!)
Juan Conejero
PixInsight Development Team
http://pixinsight.com/

Offline bitli

  • PTeam Member
  • PixInsight Guru
  • ****
  • Posts: 513
Re: PJSR getting view id of image created
« Reply #2 on: 2009 December 19 10:14:55 »
Thanks for the fast and effective answer. Naturally I am more interested in the second solution. The issue is that  I was not able to find any information on the parameters of apply (or of any other functions), except by looking in some examples. In some case one may guess looking at the PCL documentation.

I was using the "real" images also because at some time I need to crop it (I make a preview and extract it, as I found example code somewhere). There are methods as crop and cropTo in Image, but I was not able to find the parameters, neither to guess if the crop the target image or return a new one.  Any place I should have looked that I missed ?

-- bitli

Offline bitli

  • PTeam Member
  • PixInsight Guru
  • ****
  • Posts: 513
Re: PJSR getting view id of image created
« Reply #3 on: 2009 December 19 12:09:54 »
Trying more to do the crop.

Code: [Select]
  var cropped1 = new Image( targetRect.width, targetRect.height, image1.numberOfChannels, image1.colorSpace );
   image1.selectedRect = targetRect;
   cropped1.apply( image1);
   ShowIntermediateResult(cropped1, 'MCDD_C1');

function ShowIntermediateResult(image, name)
{
   var resultWindow = new ImageWindow( image.width, image.height, // width, height
                                    1, image.bitsPerSample, //  channels, bits per sample
                                    image.sampleType == SampleType_Real, // float sample
                                    false, // true=RGB, false=grayscale
                                    name ); // your unique identifier
   var resultView = resultWindow.mainView;

   with ( resultView )
   {
      beginProcess( UndoFlag_NoSwapFile );
      image.assign( image ); // assign the first image
      endProcess();
   }

   resultWindow.show();
   resultWindow.zoomToOptimalFit();
}

However the result is pathetic. The size is correct, but the image is largely black, with 0 or very large negative numbers.  The original images are 16 bits grayscale (flats). Rectangle is [590.000000,462.000000,690.000000,562.000000], and the source image large enough. After a few tests, pixinsight crashes.

-- bitli



Offline bitli

  • PTeam Member
  • PixInsight Guru
  • ****
  • Posts: 513
Re: PJSR getting view id of image created
« Reply #4 on: 2009 December 19 14:03:19 »
OK, got it. The name image as the parameter of the function was colliding with the name image in the view itself.

With the function rewritten as follow (change in parameter name) it works fine:

Code: [Select]
function ShowIntermediateResult(newImage, name)
{
   var resultWindow = new ImageWindow( 1, 1, // width, height
                                    1, newImage.bitsPerSample, //  channels, bits per sample
                                    newImage.sampleType == SampleType_Real, // float sample
                                    false, // true=RGB, false=grayscale
                                    name ); // your unique identifier
   var resultView = resultWindow.mainView;

   with ( resultView )
   {
      beginProcess( UndoFlag_NoSwapFile );
      image.assign( newImage ); // assign the first image
      endProcess();
   }

   resultWindow.show();
   resultWindow.zoomToOptimalFit();
}

-- bitli