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

How do you get Histogram Mean Value?

Contributor ,
Dec 14, 2011 Dec 14, 2011

Copy link to clipboard

Copied

Well, after learning about Histograms, I'm finding them quite helpful now (thanks to all that helped me with the last thread related to Histograms).  However, I've found another part of the histogram that is proving valuable for optimizing one of my other processes, but I don't know how to incorporate it into the script.  The value I'm looking for is the histogram's mean (displayed when using extended view or all channel view).

I've tried using "var hist = app.activeDocument.histogram.mean;" but this apparently isn't the correct way to get the mean.  Anyone have any ideas on how to go about getting this value?

TOPICS
Actions and scripting

Views

3.3K

Translate

Translate

Report

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 , Dec 15, 2011 Dec 15, 2011

I’m afraid for (Edit: DOM-)Scripting purposes the histogram is just that array of 256 numbers and does not have additional properties.

But as mathematical operations are usually done pretty quickly in a Script you could have the Script calculate the mean, I guess.

I’m not totally sure and there seems to be a slight difference, but something like this might work:

#target photoshop

var histo = app.activeDocument.histogram;

var mean = 0;

var total = 0;

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

total = tot

...

Votes

Translate

Translate
Adobe
Community Expert ,
Dec 15, 2011 Dec 15, 2011

Copy link to clipboard

Copied

I’m afraid for (Edit: DOM-)Scripting purposes the histogram is just that array of 256 numbers and does not have additional properties.

But as mathematical operations are usually done pretty quickly in a Script you could have the Script calculate the mean, I guess.

I’m not totally sure and there seems to be a slight difference, but something like this might work:

#target photoshop

var histo = app.activeDocument.histogram;

var mean = 0;

var total = 0;

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

total = total + histo;

};

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

var thisValue = histo;

mean = mean + (m * thisValue / total);

};

alert (mean);

Votes

Translate

Translate

Report

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
Contributor ,
Dec 15, 2011 Dec 15, 2011

Copy link to clipboard

Copied

PERFECT! That's exactly what I'm looking for!  I do see what you mean by it being a little bit off; but I switched the histogram to luminosity view in photoshop, and the result's still a little off, but only by about a tenth or hundreth; which is well within the bounds I need it to be for what I'm doing (coincidentally; Luminosity provides a slightly better value for what I'm doing as well).  Thanks for the help c.pfaffenbichler!

Votes

Translate

Translate

Report

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 ,
Sep 16, 2020 Sep 16, 2020

Copy link to clipboard

Copied

Gives me "NaN" as alert on PS 2020. 

Votes

Translate

Translate

Report

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
New Here ,
Jul 23, 2018 Jul 23, 2018

Copy link to clipboard

Copied

Could you explain your mean calculation math a bit more? I'm needing to associate a number to a couple of images for comparison and I think finding the mean of each image will do the trick.

Votes

Translate

Translate

Report

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 23, 2018 Jul 23, 2018

Copy link to clipboard

Copied

The number of pixels for any value is multiplied by its value, divided by the total number of pixels and all those are added up.

Votes

Translate

Translate

Report

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
New Here ,
Jul 24, 2018 Jul 24, 2018

Copy link to clipboard

Copied

Thank you much.

Votes

Translate

Translate

Report

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
Advocate ,
Sep 16, 2020 Sep 16, 2020

Copy link to clipboard

Copied

Here is the fixed version...

var histo = app.activeDocument.histogram;  
var mean = 0;  
var total = 0;  
for (var n = 0; n < histo.length; n++) {  
total = total + histo[n];  
};  
for (var m = 0; m < histo.length; m++) {  
var thisValue = histo[m];  
mean = mean + (m * thisValue / total);  
};  
alert (mean); 

Votes

Translate

Translate

Report

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 ,
Sep 16, 2020 Sep 16, 2020

Copy link to clipboard

Copied

LATEST

Wonderful! Thank you so much!

Votes

Translate

Translate

Report

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