gpt4 book ai didi

java - 检测 PNG 图像在处理中何时为 "wiped transparent"?

转载 作者:行者123 更新时间:2023-11-30 05:31:05 25 4
gpt4 key购买 nike

我正在尝试使用 Kinect 创建一个游戏,您必须使用手部 Action 删除图像,使其消失,并在 30 秒内露出其下方的另一个图像。现在我已经完成了丢失情况的代码,如果30秒内没有删除图像,就会弹出丢失屏幕。

但是,我不确定如何编写代码来检测整个 PNG 图像何时被“删除”。这涉及到使用 get() 吗?我不知道如何解决这个问题。

假设有 2 张图片 Moondirt.png 和 Moonsurface.png

Kinect控制Pimage Moondirt.png的删除和透明以显示moonsurface.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);

您已经在使用 pixels[],它比 get() 更高效,这很棒。完成后,不要忘记调用 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% 以上,就足够了),然后相应地更改游戏状态。

关于java - 检测 PNG 图像在处理中何时为 "wiped transparent"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57550397/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com