Author Topic: Star detection  (Read 904 times)

Offline janez skubic

  • Newcomer
  • Posts: 2
Star detection
« on: 2019 March 16 06:01:28 »
Is there a way to get the list of detected stars (positions in the image, star intensity) as text file from StarDetection algorithm used in StarAlignment?
Maybe there are other easier ways?
/Janez Skubic

Offline Juan Conejero

  • PTeam Member
  • PixInsight Jedi Grand Master
  • ********
  • Posts: 7111
    • http://pixinsight.com/
Re: Star detection
« Reply #1 on: 2019 March 17 03:04:33 »
You'll have to use JavaScript to do this. The StarDetector object is the best option. To get a first grasp of this object, here is an example that will create a mask with all stars detected on the current image:

Code: [Select]
#include <pjsr/StarDetector.jsh>

var S = new StarDetector;
S.test( ImageWindow.activeWindow.mainView.image, true/*createStarmask*/ );

Activate the generated mask on the image and inspect it. You'll see a circle drawn centered on each detected star.

A more interesting example:

Code: [Select]
#include <pjsr/StarDetector.jsh>

var S = new StarDetector;
var stars = S.stars( ImageWindow.activeWindow.mainView.image );
var f = File.createFileForWriting( "/tmp/stars.txt" );
for ( let i = 0; i < stars.length; ++i )
   f.outTextLn( format( "%5d %8.2f %8.2f %8.3f", i, stars[i].pos.x, stars[i].pos.y, stars[i].flux ) );
f.close();

This example will create a new text file where each line corresponds to a detected star. The columns are: star index, X coordinate, Y coordinate, and star flux (accumulated pixel data on the star detection region). Obviously, star detection works best on linear images.

You can also use the StarAlignment process, although it doesn't provide information on star fluxes. The StarAlignment.outputData property contains relevant data for all stars used for alignment after a successful execution of the StarAlignment process. Here is a schematic example:

Code: [Select]
#define NUMBER_OF_PAIR_MATCHES    2
#define REFERENCE_X              29
#define REFERENCE_Y              30
#define TARGET_X                 31
#define TARGET_Y                 32

   let SA = new StarAlignment;
   if ( SA.executeOn( view ) )
   {
      let stars = [];
      let n = SA.outputData[0][NUMBER_OF_PAIR_MATCHES];
      for ( let i = 0; i < n; ++i )
         stars.push( { refX:SA.outputData[0][REFERENCE_X][i],
                       refY:SA.outputData[0][REFERENCE_Y][i],
                       tgtX:SA.outputData[0][TARGET_X][i],
                       tgtY:SA.outputData[0][TARGET_Y][i] } );
   }

This would create an array of objects, where refX,refY are the image coordinates of a reference star and tgtX,tgtY are the corresponding coordinates of the same star matched on the target image (the image in the 'view' object in the example above). Coordinates are in pixels, where 0,0 is the top left corner of the pixel at the top left corner of the image. As noted before, StarAlignment does not provide star fluxes.

Let me know if this helps.

Juan Conejero
PixInsight Development Team
http://pixinsight.com/

Offline janez skubic

  • Newcomer
  • Posts: 2
Re: Star detection
« Reply #2 on: 2019 March 17 08:05:42 »
Thank you! This will do it for my current purposes.
/Janez
« Last Edit: 2019 March 21 08:46:39 by janez skubic »

Offline pk825

  • Newcomer
  • Posts: 22
Re: Star detection
« Reply #3 on: 2019 June 09 02:42:08 »
You'll have to use JavaScript to do this. The StarDetector object is the best option. To get a first grasp of this object, here is an example that will create a mask with all stars detected on the current image:

Code: [Select]
#include <pjsr/StarDetector.jsh>

var S = new StarDetector;
S.test( ImageWindow.activeWindow.mainView.image, true/*createStarmask*/ );

Activate the generated mask on the image and inspect it. You'll see a circle drawn centered on each detected star.

A more interesting example:

Code: [Select]
#include <pjsr/StarDetector.jsh>

var S = new StarDetector;
var stars = S.stars( ImageWindow.activeWindow.mainView.image );
var f = File.createFileForWriting( "/tmp/stars.txt" );
for ( let i = 0; i < stars.length; ++i )
   f.outTextLn( format( "%5d %8.2f %8.2f %8.3f", i, stars[i].pos.x, stars[i].pos.y, stars[i].flux ) );
f.close();

This example will create a new text file where each line corresponds to a detected star. The columns are: star index, X coordinate, Y coordinate, and star flux (accumulated pixel data on the star detection region). Obviously, star detection works best on linear images.

You can also use the StarAlignment process, although it doesn't provide information on star fluxes. The StarAlignment.outputData property contains relevant data for all stars used for alignment after a successful execution of the StarAlignment process. Here is a schematic example:

Code: [Select]
#define NUMBER_OF_PAIR_MATCHES    2
#define REFERENCE_X              29
#define REFERENCE_Y              30
#define TARGET_X                 31
#define TARGET_Y                 32

   let SA = new StarAlignment;
   if ( SA.executeOn( view ) )
   {
      let stars = [];
      let n = SA.outputData[0][NUMBER_OF_PAIR_MATCHES];
      for ( let i = 0; i < n; ++i )
         stars.push( { refX:SA.outputData[0][REFERENCE_X][i],
                       refY:SA.outputData[0][REFERENCE_Y][i],
                       tgtX:SA.outputData[0][TARGET_X][i],
                       tgtY:SA.outputData[0][TARGET_Y][i] } );
   }

This would create an array of objects, where refX,refY are the image coordinates of a reference star and tgtX,tgtY are the corresponding coordinates of the same star matched on the target image (the image in the 'view' object in the example above). Coordinates are in pixels, where 0,0 is the top left corner of the pixel at the top left corner of the image. As noted before, StarAlignment does not provide star fluxes.

Let me know if this helps.

Great code, it's near perfect to my needs. Is it possible to get also RA and DEC coordinates?

Offline Juan Conejero

  • PTeam Member
  • PixInsight Jedi Grand Master
  • ********
  • Posts: 7111
    • http://pixinsight.com/
Re: Star detection
« Reply #4 on: 2019 June 09 10:28:18 »
Quote
Is it possible to get also RA and DEC coordinates?

Yes with the latest 1.8.6 versions of PixInsight:

Code: [Select]
#include <pjsr/StarDetector.jsh>

var window = ImageWindow.activeWindow;
var S = new StarDetector;
var stars = S.stars( window.mainView.image );
var f = File.createFileForWriting( "/tmp/stars.txt" );
f.outTextLn( "Star      X        Y      Flux       R.A.         Dec.    " );
f.outTextLn( "===== ======== ======== ======== ============ ============" );
for ( let i = 0; i < stars.length; ++i )
{
   let q = window.imageToCelestial( stars[i].pos );
   f.outTextLn( format( "%5d %8.2f %8.2f %8.3f %12.8f %+12.8f", i, stars[i].pos.x, stars[i].pos.y, stars[i].flux, q.x, q.y ) );
}
f.close();

Of course, this script requires the active image to have a valid astrometric solution. The computed right ascension and declination are ICRS/J2000.0 coordinates in degrees for the baycenter of each detected star.
Juan Conejero
PixInsight Development Team
http://pixinsight.com/

Offline pk825

  • Newcomer
  • Posts: 22
Re: Star detection
« Reply #5 on: 2019 June 10 10:32:40 »
Yes with the latest 1.8.6 versions of PixInsight:

Code: [Select]
#include <pjsr/StarDetector.jsh>

var window = ImageWindow.activeWindow;
var S = new StarDetector;
var stars = S.stars( window.mainView.image );
var f = File.createFileForWriting( "/tmp/stars.txt" );
f.outTextLn( "Star      X        Y      Flux       R.A.         Dec.    " );
f.outTextLn( "===== ======== ======== ======== ============ ============" );
for ( let i = 0; i < stars.length; ++i )
{
   let q = window.imageToCelestial( stars[i].pos );
   f.outTextLn( format( "%5d %8.2f %8.2f %8.3f %12.8f %+12.8f", i, stars[i].pos.x, stars[i].pos.y, stars[i].flux, q.x, q.y ) );
}
f.close();

Of course, this script requires the active image to have a valid astrometric solution. The computed right ascension and declination are ICRS/J2000.0 coordinates in degrees for the baycenter of each detected star.

It is perfect: I got all the data from the image! Thank you very much.
I have just modified the output format in order to have a csv file.

Offline pk825

  • Newcomer
  • Posts: 22
Re: Star detection
« Reply #6 on: 2019 July 27 06:13:44 »
I use this function in order to get the objects in a photo and it works enough well, but it doesn't get some objects.
In the attachment (forum_1.jpg) there is a 21.59 galaxy that is visible, but the script doesn't show it, it's the same with Dynamic PSF.
Is there a way to modify the stars extraction in order to get more objects?
Here is the photo https://drive.google.com/open?id=1x5ZGrl1YqjQII5mvqP-YdbsSGU9rnM9Y

The idea is to match this data with astronomy catalogs in order to check exactly the objects in the photo.