Author Topic: How to obtain the raw data from the histogram  (Read 6569 times)

Offline cherodotou

  • Newcomer
  • Posts: 5
How to obtain the raw data from the histogram
« on: 2011 June 30 16:46:00 »
Hi,

I am a mechanical engineering student at the University of Portsmouth UK and I am doing an experiment that I am required to get the raw data from a histogram. Can this be done with this software? If it can be done can someone please show me the steps because I am new to this software.

Thank you

Offline Juan Conejero

  • PTeam Member
  • PixInsight Jedi Grand Master
  • ********
  • Posts: 7111
    • http://pixinsight.com/
Re: How to obtain the raw data from the histogram
« Reply #1 on: 2011 July 01 00:29:21 »
Hi Constandinos,

This can be done very easily in PixInsight. Take a look at the following script:

Code: [Select]
/*
 * A script to obtain raw image histogram data as a text file in PixInsight.
 */

#include <pjsr/StdIcon.jsh>
#include <pjsr/StdButton.jsh>

function HistogramToTextFile( filePath, img, n )
{
   if ( n == undefined || n < 2 )
      n = 65536;
   var H = new Histogram( n );
   H.generate( img );
   var A = H.toArray();

   var f = new File;
   f.createForWriting( filePath );
   for ( var i in A )
      f.outTextLn( A[i].toString() );
   f.close();
}

function main()
{
   var window = ImageWindow.activeWindow;
   if ( window.isNull )
   {
      (new MessageBox( "There is no active image window.",
                       "HistogramAsTextFile Script",
                       StdIcon_Error, StdButton_Ok )).execute();
      return;
   }

   HistogramToTextFile( "/tmp/my-test-histogram.txt", window.currentView.image );
}

main();

To use the script, save it anywhere on your filesystem and load it with PixInsight's Script Editor. First you have to edit the script to specify the output file name you want. On line 34 of the script you have the following:

Code: [Select]
   HistogramToTextFile( "/tmp/my-test-histogram.txt", window.currentView.image );
Just change the text between double quotes to meet your needs. The default file name will work for any Linux/UNIX system, including Mac OS X. On Windows, there is no "/tmp" system folder so you could use something like "C:/histogram.txt".

Open an image and run the script (from the Script Editor window, select Execute > Compile and Run, or press the corresponding key (F9 on Linux/UNIX and Windows; ^R on Mac OS X)). Note that the script generates a text file with as many lines as histogram levels have been defined (65536 by default). Each line contains the number of pixels that have the corresponding value in the image.

The script works for the first image channel only. This is OK for grayscale images but not ideal for color images. The script can be modified very easily to loop over all channels, if required.

Hope this helps.
Juan Conejero
PixInsight Development Team
http://pixinsight.com/

Offline cherodotou

  • Newcomer
  • Posts: 5
Re: How to obtain the raw data from the histogram
« Reply #2 on: 2011 July 01 09:47:41 »
Hi Juan,

I tried this and I get the following error:

Processing script file: C:/Users/Constandinos/Desktop/HistogramAsTextFile.js
*** Error [104]: C:/Users/Constandinos/Desktop/HistogramAsTextFile.js, line 34: SyntaxError: missing ) after argument list
HistogramToTextFile( C:\Users\Constandinos\Desktop\histogram.txt ),( window.currentView.image );
......................^

What can be the problem?

Offline Juan Conejero

  • PTeam Member
  • PixInsight Jedi Grand Master
  • ********
  • Posts: 7111
    • http://pixinsight.com/
Re: How to obtain the raw data from the histogram
« Reply #3 on: 2011 July 01 10:13:42 »
Hi Constandinos,

The correct syntax is:

Code: [Select]
HistogramToTextFile( "C:\\Users\\Constandinos\\Desktop\\histogram.txt", window.currentView.image );
or equivalently, and much better:

Code: [Select]
HistogramToTextFile( "C:/Users/Constandinos/Desktop/histogram.txt", window.currentView.image );
The backslash character '\' has a special meaning in JavaScript. It is used to specify special control characters; for example, '\n' is a newline character. For that reason you must specify two backslashes to tell the JavaScript compiler that you want the backslash character literally. The second option is much better because it uses normal slashes, which don't have this problem. The Windows versions of PixInsight translate '/' into '\' automatically when they occur in file names.
Juan Conejero
PixInsight Development Team
http://pixinsight.com/

Offline cherodotou

  • Newcomer
  • Posts: 5
Re: How to obtain the raw data from the histogram
« Reply #4 on: 2011 July 01 10:30:25 »
Thank you Juan!!

I managed to do it. But the .txt file I get is full of zeros and number that have no meaning. How can I read this data?

thank you.

Offline cherodotou

  • Newcomer
  • Posts: 5
Re: How to obtain the raw data from the histogram
« Reply #5 on: 2011 July 01 10:42:50 »
I managed to extract the data in excel.
It can be done in a few simple steps.

1)open excel and open the .txt file. It will open a window called Text Import Wizard.
2)In the file origin just select MS-DOS (PC-8)
3)Click Finish.

Thank you for all your time and help Juan! I'll totally force the University to buy this software!