比较精确的选择区域,而不是边界框

问题描述 投票:0回答:1

有没有办法检测具有相同边界矩形的2个不同选择的不同大小?

使用

app.activeDocument.selection.bounds
将返回包围选区的 矩形 的尺寸。下面的两个选择将返回相同的
bounds
(39px x 38px)。我如何以编程方式区分它们?

photoshop photoshop-script
1个回答
0
投票

当您选择的图像已转换为黑白位图时:

所以诀窍是获取每个像素的颜色样本。我们并不真正关心像素的颜色,而是它是否是选择的一部分。有的话就黑,没有就白。

然后就是循环整个图像,边走边数。

// creates an image of text that looks like
// a punch number on the back of a SNES cart
// The number will be random between 9 and 25;

// Switch off any dialog boxes
displayDialogs = DialogModes.ALL; // OFF

// call the source document
var srcDoc = app.activeDocument;

var imageW = 39; // Custom image width
var imageH = 38; // Custom image height
var count = 0;
var pixelCount = 0;
var imageArea = imageW * imageH;

delete_all_colour_samples(); //just to be safe!

alert("This may take a while!\nBe patient.")

for (var x = 0; x < imageW; x++)
{
  for (var y = 0; y < imageH; y++)
  {
    try
    {
      var pointSample = srcDoc.colorSamplers.add([x,y]);
      count += 1; 
    }
      catch(eek)
    {
      alert("eek!\n" + "x,y\n" + x + ", " + y);
    }

     // Obtain RGB values.
      var r = Math.round(pointSample.color.rgb.red,0);
      var g = Math.round(pointSample.color.rgb.green,0);
      var b = Math.round(pointSample.color.rgb.blue,0);
     
     {
      // if it's a selection pixel (black) count it!
      if (r == 0 && g == 0 && b ==0) pixelCount +=1;
     }

     if (count >9)
     {
      // Photoshop can only have 10 samples at any time
      delete_all_colour_samples();
      // reset teh counter
      count = 0;
    }
   }
}

var selectionArea = pixelCount;
alert("Selection is: " + selectionArea + "\n out of:" + imageArea);
// 1,111/1482
// 813 /1482

// Set Display Dialogs back to normal
displayDialogs = DialogModes.ALL; // NORMAL



// function DELETE ALL COLOUR SAMPLES()
// --------------------------------------------------------
function delete_all_colour_samples()
{
   // Kill all colour samples
   app.activeDocument.colorSamplers.removeAll();
}
© www.soinside.com 2019 - 2024. All rights reserved.