Author Topic: Corregir distorsion  (Read 5912 times)

Offline samueldl

  • Newcomer
  • Posts: 3
Corregir distorsion
« on: 2020 January 31 14:51:48 »
Soy nuevo en crear script y no se muy bien como hacer lo siguiente:

Tengo una imagen a la cual le quiero corregir la distorsión producida por la óptica y tengo una función que me corrige dicha distorsión. A la función le doy las coordenadas del pixel y me dice las nuevas coordenadas.

Entonces lo que quiero hacer es leer el valor de cada pixel y crearme una nueva imagen con las coordenadas corregidas por la función.

¿Me podéis mostrar algún ejemplo o mostrar algo de luz?

Offline Juan Conejero

  • PTeam Member
  • PixInsight Jedi Grand Master
  • ********
  • Posts: 7111
    • http://pixinsight.com/
Re: Corregir distorsion
« Reply #1 on: 2020 February 03 09:41:51 »
Quote
I am new to creating script and I don't know very well how to do the following:

I have an image to which I want to correct the distortion produced by the optics and I have a function that corrects this distortion. I give the function the pixel coordinates and it tells me the new coordinates.

So what I want to do is read the value of each pixel and create a new image with the coordinates corrected by the function.

Can you show me an example or show some light?

If I interpret you correctly, you need to interpolate an existing image to apply a geometric transformation. In such case here is a script that does something very similar to what you want to do. The script applies a Cartesian-to-polar coordinate transformation to the active image:

Code: [Select]
function cartesianToPolar( image )
{
   // Gather working parameters
   let n = image.numberOfChannels;
   let w = image.width;
   let h = image.height;
   let w2 = 0.5*w;
   let h2 = 0.5*h;
   let r0 = Math.sqrt( w2*w2 + h2*h2 ); // semi-diagonal

   // Create a working image to store one channel of the target image.
   let tmp = new Image( w, h, 1 );

   // Save current channel and region selections.
   image.pushSelections();

   // Reset all selectionsthe rectangular selection to the whole image boundaries.
   image.resetRectSelection();

   // For each channel
   for ( let c = 0; c < n; ++c )
   {
      tmp.fill( 0 ); // initialize working data with zeros

      // For each row of pixels.
      for ( let i = 0; i < h; ++i )
      {
         // Polar angle for the current row.
         let theta = 2*Math.PI*i/h;
         let stheta = Math.sin( theta );
         let ctheta = Math.cos( theta );

         // For each column of pixels.
         for ( let j = 0; j < w; ++j )
         {
            // Radial distance for the current column.
            let r = r0*j/w;

            // Horizontal coordinate on the source image.
            let x = w2 + r*ctheta;

            if ( x >= 0 && x < w )
            {
               // Vertical coordinate on the source image.
               let y = h2 - r*stheta;

               // Copy the source pixel to the polar-transformed location on
               // the working image.
               if ( y >= 0 && y < h )
                  tmp.setSample( image.interpolate( x, y, c ), j, i );
            }
         }
      }

      // Copy our working data to the channel c of image.
      image.selectedChannel = c;
      image.apply( tmp );
   }

   // Deallocate our temporary working image.
   tmp.free();

   // Restore original selections.
   image.popSelections();
}

function main()
{
   let window = ImageWindow.activeWindow;
   let view = window.mainView;
   view.beginProcess();
   cartesianToPolar( view.image );
   view.endProcess();
}

main();

I think you can use this script as a starting point very easily. Let me know if you need more information.
Juan Conejero
PixInsight Development Team
http://pixinsight.com/

Offline samueldl

  • Newcomer
  • Posts: 3
Re: Corregir distorsion
« Reply #2 on: 2020 February 06 04:22:50 »
I think it can be worth it. Thank you!!!