检测 PNG 图像何时在处理中被"擦除透明"?



我正在尝试使用 Kinect 创建一个游戏,您必须使用手部动作擦除图像以使其消失,并在 30 秒内显示其下方的另一个图像。现在我已经完成了丢失条件的代码,如果您在 30 秒内没有擦除图像,则会弹出丢失屏幕。

但是,我不确定如何对零件进行编码以检测整个PNG图像何时被"擦除"。这是否涉及使用 get((?我不知道如何处理这个问题。

想象一下,有2个Pimages月尘.png和月表面.png

Kinect 控制擦除并使 Pimage 月尘.png透明以露出月亮表面.png

void kinect() {
//----------draw kinect------------
// Draw moon surface
image(moonSurface, 0, 0, width, height);
// Draw the moon dirt
image(moonDirt, 0, 0, width, height);
// Threshold the depth image
int[] rawDepth = kinect.getRawDepth();
for (int i=0; i < rawDepth.length; i++) {
if (rawDepth[i] >= minDepth && rawDepth[i] <= maxDepth) {
depthImg.pixels[i] = color(255);
maskingImg.pixels[i] = color(255);
} else {
depthImg.pixels[i] = color(0);
}
}
//moonDirt.resize(640, 480);  //(640, 480);
moonDirt.loadPixels();
for (int i=0; i < rawDepth.length; i++) {
if ( maskingImg.pixels[i] == color(255) ) {
moonDirt.pixels[i] = color( 0, 0, 0, 0 );
}
}
moonDirt.updatePixels();
image(moonDirt, 0, 0, width, height); 
color c = moonDirt.get(width, height);
updatePixels();
//--------timer-----     
if (countDownTimer.complete() == true){
if (timeLeft > 1 ) {
timeLeft--;
countDownTimer.start();
} else {
state = 4;
redraw();
}
}
//show countDown TIMER
String s = "Time Left: " + timeLeft;
textAlign(CENTER);     
textSize(30);
fill(255,0,0);
text(s, 380, 320);
}
//timer
class Timer {
int startTime;
int interval;
Timer(int timeInterval) {
interval = timeInterval;
}
void start() {
startTime = millis();
}
boolean complete() {
int elapsedTime = millis() - startTime;
if (elapsedTime > interval) {
return true;
}else {
return false;
}
}
}

我在本节中看到混乱:

moonDirt.loadPixels();
for (int i=0; i < rawDepth.length; i++) {
if ( maskingImg.pixels[i] == color(255) ) {
moonDirt.pixels[i] = color( 0, 0, 0, 0 );
}
}
moonDirt.updatePixels();
image(moonDirt, 0, 0, width, height); 
color c = moonDirt.get(width, height);

您已经在使用比get()更有效pixels[]这很棒。 完成后不要忘记打电话给updatePixels()。你已经这样做了moonDirt,但不是为了maskingImg

如果您想了解图像是否已清除(在这种情况下,清除表示透明黑色(color(0,0,0,0)(。

看起来您已经熟悉接受参数和返回值的函数。计数函数需要:

  1. 取 2 个参数:要处理的图像和要检查和计数的颜色
  2. 返回总计数
  3. 遍历所有像素:如果任何像素与第二个参数匹配,则总数递增

像这样:

/**
*  countPixels - counts pixels of of a certain colour within an image
* @param image - the PImage to loop through
* @param colorToCount - the colour to count pixels present in the image
* return int - the number of found pixels (between 0 and image.pixels.length)
*/
int countPixels(PImage image,color colorToCount){
// initial transparent black pixel count
int count = 0;
// make pixels[] available
image.loadPixels();
// for each pixel
for(int i = 0 ; i < image.pixels.length; i++){
// check if it's transparent black
if(image.pixels[i] == colorToCount){
// if so, increment the counter
count++; 
}
}
// finally return the count
return count;
}

在你的代码中,你可以像这样使用它: ...

// Threshold the depth image
int[] rawDepth = kinect.getRawDepth();
for (int i=0; i < rawDepth.length; i++) {
if (rawDepth[i] >= minDepth && rawDepth[i] <= maxDepth) {
depthImg.pixels[i] = color(255);
maskingImg.pixels[i] = color(255);
} else {
depthImg.pixels[i] = color(0);
}
}
maskingImg.updatePixels();

//moonDirt.resize(640, 480);  //(640, 480);
moonDirt.loadPixels();
for (int i=0; i < rawDepth.length; i++) {
if ( maskingImg.pixels[i] == color(255) ) {
moonDirt.pixels[i] = color( 0, 0, 0, 0 );
}
}
moonDirt.updatePixels();
image(moonDirt, 0, 0, width, height); 
int leftToReveal = moonDirt.pixels.length;
int revealedPixels = countPixels(moonDirt,color(0,0,0,0));
int percentageClear = round(((float)revealedPixels / leftToReveal) * 100);
println("revealed " + revealedPixels + " of " + leftToReveal + " pixels -> ~" + percentageClear + "% cleared");

您可以选择设置要清除的所有像素的条件或比率/百分比(例如,如果清除了 90% 以上,那就足够了(,然后相应地更改游戏状态。

最新更新