直方图平均值不循环变化

时间:2018-07-25 03:34:48

标签: javascript histogram photoshop photoshop-script

我编写了一个脚本,将设置为差异混合模式的顶层与其他3个图层进行比较。底层是顶层的精确副本。我查看文档直方图,检查平均值是否<1(以后我要解释一个小的方差,因此为1),将图层调板中的图层涂成红色,然后关闭该图层。...重复。我遇到的问题是每次迭代的平均值保持不变。我已将文件写入到csv中,以便可以调试该问题,但无法弄清楚为什么会发生这种情况。

#target photoshop

//  call the source document
var srcDoc = app.activeDocument;
var check = srcDoc.artLayers.getByName("check");
var layers = srcDoc.layers;
var docHisto = srcDoc.histogram;
//var Name = app.activeDocument.name.replace(/\.[^\.]+$/,'');
//var Ext = decodeURI(app.activeDocument.name).replace(/^.*\./,'');
//var Path=app.activeDocument.path;

startRulerUnits = app.preferences.rulerUnits;
startTypeUnits = app.preferences.typeUnits;

var original_units = app.preferences.rulerUnits;

//app.preferences.rulerUnits = Units.PERCENT;


//width and height of image
var imageWidth = srcDoc.width.as('px');
var imageWidth = srcDoc.width.value;
var imageHeight = srcDoc.height.as('px');
var imageHeight = srcDoc.height.value;

var numPixels = imageWidth*imageHeight;

var layNum = srcDoc.artLayers.length;



srcDoc.activeLayer = check;
check.blendMode = BlendMode.DIFFERENCE;

for (var i = 1; i < layNum; ++i) {
    var meanval = EvalHisto(docHisto, i) ;
   alert(meanval)


    if (meanval < 1){
        alert("same");
        srcDoc.activeLayer = layers[i];
        layercolor("red");
       }


    layers[i].visible = false;

}


 function layercolor(colour) {
         switch (colour.toLocaleLowerCase()){  
        case 'red': colour = 'Rd  '; break;  
        case 'orange' : colour = 'Orng'; break;  
        case 'yellow' : colour = 'Ylw '; break;  
        case 'yellow' : colour = 'Ylw '; break;  
        case 'green' : colour = 'Grn '; break;  
        case 'blue' : colour = 'Bl  '; break;  
        case 'violet' : colour = 'Vlt '; break;  
        case 'gray' : colour = 'Gry '; break;  
        case 'none' : colour = 'None'; break;  
        default : colour = 'None'; break;  
        } 

    var desc = new ActionDescriptor();  
        var ref = new ActionReference();  
        ref.putEnumerated( charIDToTypeID('Lyr '), charIDToTypeID('Ordn'), charIDToTypeID('Trgt') );  
    desc.putReference( charIDToTypeID('null'), ref );  
        var desc2 = new ActionDescriptor();  
        desc2.putEnumerated( charIDToTypeID('Clr '), charIDToTypeID('Clr '), charIDToTypeID(colour) );  
    desc.putObject( charIDToTypeID('T   '), charIDToTypeID('Lyr '), desc2 );  
    executeAction( charIDToTypeID('setd'), desc, DialogModes.NO ); 
 }; 



function EvalHisto(histogram, i) {

        var mean = 0;
        var total = 0;

        var luminosity = srcDoc.histogram;
        var red = srcDoc.channels["Red"].histogram;
        var green = srcDoc.channels["Green"].histogram;
        var blue = srcDoc.channels["Blue"].histogram;

        var datFile = new File("~/Desktop/Histogram" + i + ".csv");

        datFile.open("e");
        datFile.writeln("Level,Luminosity,Red,Green,Blue, Mean\r");

     for (var n = 0; n < histogram.length; n++) {
        total = total + histogram[n];   //calculate total histogram value over the 255 color space
        }

    for (var m = 0; m < histogram.length; m++){
        mean = mean + (m*histogram[m]/total);       //calculate running mean of intensity
        datFile.writeln(m + "," + luminosity[m] + "," + red[m] + "," + green[m] + "," + blue[m] + "," + mean + "\r");
        }

    datFile.close();
    return mean;
};

Layers

1 个答案:

答案 0 :(得分:0)

我发现了错误,我需要搬走

var docHisto = srcDoc.histogram;

进入我的图层循环,否则我只会找到第一层和第二层混合所产生的直方图。