Author Topic: Pixelmath Query  (Read 1485 times)

Offline dhb2206

  • Newcomer
  • Posts: 41
Pixelmath Query
« on: 2020 February 10 04:48:28 »
Danger time - me and pixelmath again! This is purely a learning question!

Background: Okay, I've a really poor image taken during the very bright moon phase we are in at the moment, but I'm itching to get to know my new kit. So, I've run a very aggressive ABE which does the job of getting shot of the moonlight, but does show up the auto-focus banding from my Canon DSLR (which Canon insist isn't a bug and only shows up with heavy processing.....). After I've run the Canon banding reduction I'm left with a series of brightly coloured pixel rows. So, having gotten very bored with a single pixel row clone stamp I though pixelmath would be the key - and it is.

The Question: Bear in mind I'm a pixelmath dummy here, the following expression works for row 370 (I just set it to row 369):

iif(y()==370,pixel($T,x(),y()-1),$T)

Basic read is if the y coordinate is row 370 then replace the data with row 369.

I know the answer to this already I fear (use Javascript), is there a way to prompt for row number?

If I'm right, and do know the answer, I haven't been brave enough to try Javascript yet (I do the vast majority of my programming in Visual Basic) so if I set up an array with all offending rows and iterate the array doing the replacement, what would my execution function look like? Are there some example js files floating about?

Thanks,

David


Offline dave_galera

  • PixInsight Addict
  • ***
  • Posts: 261
    • QDigital Astro
Re: Pixelmath Query
« Reply #1 on: 2020 February 10 05:59:51 »

I know the answer to this already I fear (use Javascript), is there a way to prompt for row number?


Sorry, but you got it in one, yep javascript!!
Dave

Offline dhb2206

  • Newcomer
  • Posts: 41
Re: Pixelmath Query
« Reply #2 on: 2020 February 10 07:09:19 »
I thought that some pigeon javascript might help show how out of my depth I am.....  ;D

function replaceRows()
{
   //I haven't a Scooby Doo how to reference the current view
   var myImage = Parameters.targetView.image;
   //create a simple array of offending rows
   var rowArray = [197,213,267,331,345,794,816];
   //iterate the array
   for (y=0; y<rowArray.count; y++){
      //iterate along the x-axis
      for (x=0; x<myImage.width; x++){
         //total guess at setting this - I notice getPixelValue wants a vector not a point
         myImage.setPixelValue(myImage.getPixelValue(x,rowArray[y]-1));
      };
   };     
}

Any assistance appreciated!

Offline dave_galera

  • PixInsight Addict
  • ***
  • Posts: 261
    • QDigital Astro
Re: Pixelmath Query
« Reply #3 on: 2020 February 10 07:14:04 »
Do you want me to knock up a quick script using that function above?

Dave

Offline dhb2206

  • Newcomer
  • Posts: 41
Re: Pixelmath Query
« Reply #4 on: 2020 February 10 07:17:53 »
That would be brilliant Dave, if you wouldn't mind a distraction!

Offline dhb2206

  • Newcomer
  • Posts: 41
Re: Pixelmath Query
« Reply #5 on: 2020 February 10 07:54:32 »
Hmm, I appear to be getting somewhere after nicking some code from one of Juan's posts...... stumbling along - thought my getPixelValue would come a cropper - my pixel iterator isn't initialised:

function main()
{
   // Get access to the currently active image window.
   var window = ImageWindow.activeWindow;
   var x;
   var y;

   // Do we have one?
   if ( window.isNull )
      throw Error( "There is no active image window!" );

   // Get access to its main view, so we can pick some properties of the image
   // it encapsulates.
   var view = window.mainView;
  // Get the image from the view
   var myImage = view.image;
   //create a simple array of offending rows
   var rowArray = [197,213,267,331,345,794,816];
   //iterate the array
   for (y=0; y<rowArray.length; y++){
      //iterate along the x-axis
      for (x=0; x<myImage.width; x++){
         //total guess at setting this - I notice getPixelValue wants a vector not a point
         myImage.setPixelValue(myImage.getPixelValue(x,rowArray[y]-1));
      };
   };
   view.endProcess();
}

main();

Offline dave_galera

  • PixInsight Addict
  • ***
  • Posts: 261
    • QDigital Astro
Re: Pixelmath Query
« Reply #6 on: 2020 February 10 08:23:07 »
Give this a try

This also gives you a nice script template that you can use to build any script you want
Dave

Offline dhb2206

  • Newcomer
  • Posts: 41
Re: Pixelmath Query
« Reply #7 on: 2020 February 10 09:28:51 »
Thanks Dave. I managed to get mine to run (but obvioulsy in a very slow way as I set it off about ten minues ago to do 10 rows and its still running). By the look of things I've done the same SetSample from a sample (though I merged the function lines - no v= line I lobbed that in with setsample!). I did take a butchers at the CanonBanding routine and it makes use of a rectangle (albeit a single row rectangle), but I couldn't see where the data was coming from / going to thanks to it being embedded in a number of classes and me just reading it on a flat web view. That, I reckon is a cuter way of achieving the same - if only I could find the read / write element of the code! When, or possibly, if PI comes back to life after executing my script I'll paste it into another reply.

Offline dhb2206

  • Newcomer
  • Posts: 41
Re: Pixelmath Query
« Reply #8 on: 2020 February 10 09:51:29 »
This was mine, though I made a hash of changing my row iterator's name from y to row and left;    for (var row=0; row<rowArray.length; y++){
So all rows were being replaced lots of time. what an eejit! ;D Oh how I love a JIT debugger as I make these stupid errors a lot!

I kept getting an out of range error with x spanning 0 to less than or equal to width, so I switched to 1 to less than width and it ran.

Typically having crashed PI, thankjs to my almost infinite iteration, I started the process from scratch to get my dodgy lines, but did a crop first and no dodgy lines!! Typical.....
Code: [Select]
function main()
{
   // Get access to the currently active image window.
   var window = ImageWindow.activeWindow;

   // is there an active window
   if ( window.isNull )
      throw Error( "There is no active image window!" );

   // Get access to its main view, so we can pick some properties of the image
   // it encapsulates.
   var view = window.mainView;
   var myImage = view.image;
   if (!myImage.isColor)
      throw Error( "Needs to be a colour image!" );
   //stick the view in a changeable mode
   view.beginProcess()
   //create a simple array of offending rows
   var rowArray = [197,213];
   //iterate the array
   for (var row=0; row<rowArray.length; row++){
      var y=rowArray[row];
      console.writeln(format("<end><cbr>The row is=%d",y))
      //iterate along the x-axis
      for (var x=1; x<myImage.width; x++){
         //output value from row-1 and set in row
         myImage.setSample(myImage.sample(x, y-1, 0 ), x, y, 0);
         myImage.setSample(myImage.sample(x, y-1, 1 ), x, y, 1);
         myImage.setSample(myImage.sample(x, y-1, 2 ), x, y, 2);
      };
   };
   //stop changing the view
   view.endProcess();
}

main();

Offline dave_galera

  • PixInsight Addict
  • ***
  • Posts: 261
    • QDigital Astro
Re: Pixelmath Query
« Reply #9 on: 2020 February 10 09:59:14 »
This works perfectly

      targetView.beginProcess(UndoFlag_NoSwapFile);

      var w = targetView.image.width;
      var h = targetView.image.height;
      
      // Create a simple array of offending rows
      var x,y;      
      var rowArray = [197,198,199,200,201,202,203];
      
      // Iterate the array
      var v = 0;
      for (key in rowArray) {
        console.writeln( "key: " + key + ", value" + rowArray[key]);
         // Iterate along the x-axis
         for (x=0; x < w; x++){
            v = targetView.image.sample(x, (rowArray[key] - 1), 0);
            targetView.image.setSample(v, x, rowArray[key], 0);
         };
      };
      targetView.endProcess()
Dave