Highlighted

## How do you get Histogram Mean Value?

Contributor ,
Dec 14, 2011

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?

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 photoshopvar 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);`
TOPICS
Actions and scripting

Views

2.4K

Likes

Report

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

## How do you get Histogram Mean Value?

Contributor ,
Dec 14, 2011

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?

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 photoshopvar 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);`
TOPICS
Actions and scripting

Views

2.4K

Likes

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Dec 14, 2011 0
8 Replies 8
LEGEND ,
Dec 15, 2011

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 photoshopvar 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);`

Likes

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Dec 15, 2011 1
Contributor ,
Dec 15, 2011

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!

Likes

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Dec 15, 2011 0
Explorer ,
Sep 16, 2020

Copied

Gives me "NaN" as alert on PS 2020.

Likes

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Sep 16, 2020 0
New Here ,
Jul 23, 2018

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.

Likes

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Jul 23, 2018 0
LEGEND ,
Jul 23, 2018

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.

Likes

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Jul 23, 2018 0
New Here ,
Jul 24, 2018

Copied

Thank you much.

Likes

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Jul 24, 2018 0
Sep 16, 2020

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);
};

Likes

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Sep 16, 2020 2
LATEST
Explorer ,
Sep 16, 2020

Copied

Wonderful! Thank you so much!

Likes

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Sep 16, 2020 0
Resources
Get Started