Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
1

How to get the histogram's Std Dev

Explorer ,
May 14, 2018 May 14, 2018

  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.

TOPICS
Actions and scripting
3.6K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , May 15, 2018 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 hist

...
Translate
Adobe
Community Expert ,
May 15, 2018 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]);

};

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
May 15, 2018 May 15, 2018

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
May 16, 2018 May 16, 2018

Hi c.pfaffenbichler,sorry to disturb you.I want to get value of theMax and theMin in histogram not the the pixels,how to get this? Thanks

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 16, 2018 May 16, 2018

Does this work? (Edit: Otherwise I may have misunderstood your intention.)

// 2018, use at your own risk;

#target "photoshop-120.032"

//#target photoshop

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

var theMinMax = histogramMinMax(activeDocument.histogram);

alert ("min "+theMinMax[0]+"\nmax "+theMinMax[1]);

};

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

function histogramMinMax (theHist) {

// the minimum;

var check = false;

var theMin = 0;

while (check == false) {

if (theHist[theMin] != 0) {

check = true

} else {theMin++}

};

// the maximum;

var check = false;

var theMax = 255;

while (check == false) {

if (theHist[theMax] != 0) {

check = true

} else {theMax--}

};

//

return ([theMin, theMax]);

};

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
May 16, 2018 May 16, 2018

It is that i mean, forgive me for not being clear,because i am not clear about the relationship with them which like "n * theHist" in histogram.Thanks again.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 16, 2018 May 16, 2018

n being the index also indicated the luminance while theHist indicates the number of pixels of that luminance.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
May 16, 2018 May 16, 2018

You got almost full stake! Last time I looked at, you was at 8 lvl. It'll take another century your skull will be finally inpaled 🙂

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 16, 2018 May 16, 2018

I think the last »bar« of those levels takes a lot of points, no hurry …

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
May 16, 2018 May 16, 2018

Ok,thanks

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
May 18, 2018 May 18, 2018

I got it. I have ling54k in list of users I follow, actually he added me first. It's why I was announced of his topic.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
May 16, 2018 May 16, 2018

Will this work on 32 bpc floating point data?  If not, is there a quick workaround?

Thanks,

RONC

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
May 17, 2018 May 17, 2018

It does work on 32 bpc data very well.  Just need to display the histograms.

RONC

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
May 16, 2018 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 😕

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 16, 2018 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?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
May 16, 2018 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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Oct 23, 2020 Oct 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!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jul 17, 2021 Jul 17, 2021

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jul 18, 2021 Jul 18, 2021

I corrected the scripts.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jul 18, 2021 Jul 18, 2021
LATEST

@Kukurykus  – thank you!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines