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?
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
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);
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!
Copy link to clipboard
Copied
Gives me "NaN" as alert on PS 2020.
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.
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.
Copy link to clipboard
Copied
Thank you much.
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);
Copy link to clipboard
Copied
Wonderful! Thank you so much!