Skip to main content
raxby
Inspiring
May 15, 2018
Answered

How to get the histogram's Std Dev

  • May 15, 2018
  • 3 replies
  • 4005 views

  getRedHistogramStdDev(); 

  function getRedHistogramStdDev() {

      var channels = app.activeDocument.channels;

      var red = channels[0].histogram;

      alert(getStdDev(red));

  }

function getStdDev(arry){

        var length = arry.length; 

        var tarry = new Array(length);

        var si = 0;

        var s = 0;

        for (var i = 0; i < arry.length; i += 1) {

            if (arry[i] > 0) {

                s += i;

                si += arry[i];

            }

        }

        var average = si / s;

        for (var i = 0; i < length; i++) {

            var dev = parseFloat(arry[i]) - parseFloat(average);

            tarry = Math.pow(dev, 2);  

        }

        var powSum = 0;

 

 

        for (var j = 0; j < tarry.length; j++) {

            if (tarry[j].toString() != "" || tarry[j].toString() != null) {

                powSum = parseFloat(powSum) + parseFloat(tarry[j].toString()); 

            }

        }

        return Math.sqrt(parseFloat(powSum) / parseFloat(length)).toFixed(2);

}

This is my code, but the result is inconsistent with the histogram's StdDev data.Please help me, thanks.

This topic has been closed for replies.
Correct answer c.pfaffenbichler

Does this work more consistently? 

// 2018, use at your own risk;

#target "photoshop-120.032"

//#target photoshop

if (app.documents.length > 0 && activeDocument.mode == DocumentMode.RGB) {

var theR = histogramMeanStandardDeviation(activeDocument.channels[0].histogram);

var theG = histogramMeanStandardDeviation(activeDocument.channels[1].histogram);

var theB = histogramMeanStandardDeviation(activeDocument.channels[2].histogram);

alert ((theR[2]+theG[2]+theB[2])/3);

};

////// get mean of histogram //////

function histogramMeanStandardDeviation (theHist) {

// get total number;

var thePixels = 0;

for (var m = 0; m < theHist.length; m++) {

  thePixels = thePixels + theHist[m]

  };

// get mean and median;

var theMean = 0;

var aTotal = 0;

var check = false;

for (var n = 0; n < theHist.length; n++) {

  theMean = theMean + (n * theHist[n] / thePixels);

  aTotal = aTotal + theHist[n];

  if (aTotal >= thePixels / 2 && check == false) {

  theMedian = n;

  check = true;

  }

  };

// get standard deviation;

var theStandDev = 0;

for (var o = 0; o < theHist.length; o++) {

  theStandDev = theStandDev + (Math.pow ((o - theMean), 2) * theHist[o])

  };

theStandDev = Math.sqrt (theStandDev / thePixels);

//

return ([theMean, theMedian, theStandDev]);

};

3 replies

Known Participant
October 23, 2020

Hello dearest people and @c.pfaffenbichler !

I don't get this script working. Gives me a spinning cursor and after that nothing happens. Then i fired it in ESTK and it gave me "theMedian is undefined". I tried to figure it out by myself but the best what i get out of it is "NaN" lol.
Is this again some typo error because of the glorious change of forum software? If yes, i don´t find it,

The first script from @raxby gives me "0.00" all the time btw.
Thanks for your help in advance!

Stephen Marsh
Braniac
July 18, 2021

I can also add that I can't get the script working in later versions either, looking for the Std Dev.

Kukurykus
Braniac
May 16, 2018

That's 3rd topic during last month I'm called constantly to although neither I haven't posted anything on here nor anyone mentioned my forum name :/ That must be some error :/

c.pfaffenbichler
Braniac
May 16, 2018

Well, would not be the first time the Forum software displays »issues«, would it?

Have you tried contacting an Admin or asking about the issue on

Forum comments

yet?

Kukurykus
Braniac
May 16, 2018

No that is no problem for me, it doesn't happen often, especially taking into consideration I forgot 's' at end of 'month' in last post. So once a 1,5 month something causing I'm alerted of new posts in certain topic.

c.pfaffenbichler
c.pfaffenbichlerCorrect answer
Braniac
May 15, 2018

Does this work more consistently? 

// 2018, use at your own risk;

#target "photoshop-120.032"

//#target photoshop

if (app.documents.length > 0 && activeDocument.mode == DocumentMode.RGB) {

var theR = histogramMeanStandardDeviation(activeDocument.channels[0].histogram);

var theG = histogramMeanStandardDeviation(activeDocument.channels[1].histogram);

var theB = histogramMeanStandardDeviation(activeDocument.channels[2].histogram);

alert ((theR[2]+theG[2]+theB[2])/3);

};

////// get mean of histogram //////

function histogramMeanStandardDeviation (theHist) {

// get total number;

var thePixels = 0;

for (var m = 0; m < theHist.length; m++) {

  thePixels = thePixels + theHist[m]

  };

// get mean and median;

var theMean = 0;

var aTotal = 0;

var check = false;

for (var n = 0; n < theHist.length; n++) {

  theMean = theMean + (n * theHist[n] / thePixels);

  aTotal = aTotal + theHist[n];

  if (aTotal >= thePixels / 2 && check == false) {

  theMedian = n;

  check = true;

  }

  };

// get standard deviation;

var theStandDev = 0;

for (var o = 0; o < theHist.length; o++) {

  theStandDev = theStandDev + (Math.pow ((o - theMean), 2) * theHist[o])

  };

theStandDev = Math.sqrt (theStandDev / thePixels);

//

return ([theMean, theMedian, theStandDev]);

};

raxby
raxbyAuthor
Inspiring
May 16, 2018

Yes,It works very well,thank you very much!