簡體   English   中英

通過處理計算png中的透明像素

[英]Counting transparent pixels in png through Processing

我試圖通過使用 get() 命令或 img.get() 進行處理來獲取 png 中透明的像素數量。 我試過放置白色背景並使用此代碼,但輸出與我測試的圖片的輸出不接近,我知道用它填充了大約 50% 的黑色和其余透明。

我使用此代碼並期望獲得“v”的值也落在 125xxxx 區域,因為我知道大約 50% 的圖片 femtiofemtio.png 是黑色的:

PImage img;
color currentColor;

void setup() {
  size(1176,2172);
  currentColor = color(0);
  img = loadImage("femtiofemtio.png");
}

void draw() {
  int v = 0;
  background(255);
  image(img, 0, 0);
  for (int x = 1; x < 1176; x = x+1) {
  for (int y = 1; y < 2172; y = y+1) {
  currentColor = img.get(x,Y);
  if (alpha(currentColor)==255){
    v=v+1;
    println(v);
   }
}
}
  //print(v/2554272);
  print("end    ");
  exit();
}

問題位於您的代碼片段的第 16 行。 您錯誤地使用了大寫Y而不是小寫y 該行應如下所示:

  currentColor = img.get(x, y);

您的代碼仍然可以編譯和運行的原因是大寫Y是由PConstants中的處理環境定義的現有常量(值為1 )。 它通常與索引結合使用(例如vertex[Y] = 1.0 )。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM