Author Topic: PSJR: modifying an image  (Read 5038 times)

Offline Nocturnal

  • PixInsight Jedi Council Member
  • *******
  • Posts: 2727
    • http://www.carpephoton.com
PSJR: modifying an image
« on: 2009 December 08 15:42:28 »

Hi,

I'm trying to figure out why Craig Stark's PHD isn't finding the brightest star in my guide image (well it does sometimes but not always). I figured this was a good opportunity to start experimenting with PSJR.

The first bit is a simple median filter that smooths the image. When I try to modify the image with setSample I get this:

*** Error [000]: C:/PCL/src/scripts/PHD_exp.js, line 164: Image.setSample(): read-only image

so it seems images have an access mode. Do I first have to write to a temporary image and then copy it over the input image? Hmm, that doesn't seem to work either so I'm doing something quite incorrectly.

Code: [Select]
function Median3(img, outp) {
   var ar = new Array(9);
   var xsize = img.width;
   var ysize = img.height;
        for (var y=1; y<ysize-1; y++) {
                for (var x=1; x<xsize-1; x++) {
                        ar[0] = img.sample((x-1),(y-1));
                        ar[1] = img.sample((x),(y-1));
                        ar[2] = img.sample((x+1),+(y-1));
                        ar[3] = img.sample((x-1),+(y));
                        ar[4] = img.sample((x),+(y));
                        ar[5] = img.sample((x+1),+(y));
                        ar[6] = img.sample((x-1),+(y+1));
                        ar[7] = img.sample((x),+(y+1));
                        ar[8] = img.sample((x+1),+(y+1));
                        ar.sort;

                        outp.setSample(ar[4], x, y, 1);
                }
                //Limg.ImageData[(xsize-1)+y*xsize]=img.ImageData[(xsize-1)+y*xsize];  // 1st & Last one in this row -- just grab from orig
                //Limg.ImageData[y*xsize]=img.ImageData[y*xsize];
        }

}

function main()
{
   // Get access to the current active image window.
   var window = ImageWindow.activeWindow;
   if ( window.isNull )
      throw new Error( "No active image" );

   console.show();
   console.writeln( "<end><cbr><br><b>" + window.currentView.fullId + "</b>" );
   console.writeln( "Running PHD code." );
   console.flush();

   console.abortEnabled = true;
   var img = window.currentView.image;
   var outWindow = new ImageWindow(img.width, img.height, 1, 32, true, true, "Filtered");

   var outView =  outWindow.mainView;
   var outImage = outWindow.mainView.image;

   var c = 0;

   console.writeln( "<end><cbr><br>* Channel #", c );
   console.flush();
   img.selectedChannel = c;
   var E = new Median3( img, outImage );

   console.flush();
}

main();


Best,

    Sander
---
Edge HD 1100
QHY-8 for imaging, IMG0H mono for guiding, video cameras for occulations
ASI224, QHY5L-IIc
HyperStar3
WO-M110ED+FR-III/TRF-2008
Takahashi EM-400
PIxInsight, DeepSkyStacker, PHD, Nebulosity

Offline Juan Conejero

  • PTeam Member
  • PixInsight Jedi Grand Master
  • ********
  • Posts: 7111
    • http://pixinsight.com/
Re: PSJR: modifying an image
« Reply #1 on: 2009 December 08 16:33:28 »
Hi Sander,

Welcome to PJSR development!  8)

To modify the image in a view, you first have to "ask for permission" to do so. Otherwise your script has just read-only access to the view's image. The following methods must be used:

Code: [Select]
void View.beginProcess( [uint undoFlags] )

void View.endProcess()

View.beginProcess() tells the core that you are about to modify the image in the calling View object. This is necessary for several reasons. One is that in this way the core can update the view's processing history, generate swap files to store its current state, if necessary, and invalidate internal control structures. Another reason is that PI is a multithreaded environment, so this method implicitly locks the view for read and/or write operations, as necessary to prevent unwanted concurrent accesses.

The undoFlags argument informs about the type of changes that will be made to the view's image. It can be any ORed combination of the flags declared in <pjsr/UndoFlag.jsh>. If undoFlags is not specified, it defaults to UndoFlag_DefaultMode (0), which will save the view's pixel data and its current previews, including their current stored states, if any. You can specify UndoFlag_NoSwapFile if you want to do changes that will not be undoable. Typically, UndoFlag_NoSwapFile is used with temporary working images created in image windows.

View.endProcess() must always be called after a call to View.beginProcess(), when the script has finished doing modifications to the calling view.

If for some reason your script has to abort an ongoing operation in a beginProcess() ... endProcess() sequence, it must call:

Code: [Select]
void View.cancelProcess()
which automatically restores the view's saved state, as if nothing would have happened. However, if the UndoFlag_NoSwapFile flag has been specified, cancelProcess() will leave the view's image "as-is", since there are no swap files to recover a previous state. The core ensures that the view will always contain a valid image in these cases.

Your script should work with the following modifications:

Code: [Select]
   var outView = outWindow.mainView;
   with ( outView )
   {
      beginProcess( UndoFlag_NoSwapFile );
     
      var c = 0;
      console.writeln( "<end><cbr><br>* Channel #", c );
      console.flush();

      image.selectedChannel = c;
      var E = new Median3( img, image );

      endProcess();
   }

Note that I have suppressed your outImage variable, as it is just outView.image. Your script will also require:

Code: [Select]
#include <pjsr/UndoFlag.jsh>
at the top.
Juan Conejero
PixInsight Development Team
http://pixinsight.com/

Offline Nocturnal

  • PixInsight Jedi Council Member
  • *******
  • Posts: 2727
    • http://www.carpephoton.com
Re: PSJR: modifying an image
« Reply #2 on: 2009 December 08 17:25:54 »
Thanks very much Juan. Now I just need to call it PJSR instead of PSJR :)
Best,

    Sander
---
Edge HD 1100
QHY-8 for imaging, IMG0H mono for guiding, video cameras for occulations
ASI224, QHY5L-IIc
HyperStar3
WO-M110ED+FR-III/TRF-2008
Takahashi EM-400
PIxInsight, DeepSkyStacker, PHD, Nebulosity