2022-05-10
思考:
与测距传感器结合,越近,图像越白,越远,越黑。
命名交互作品名为:近即远。至近则不见。
用processing臭美一下。
image.png
二值化的原理很简单。按像素点逐个地与设定的阈值比对。如果>阈值,就置为白色,如果<阈值,就置为黑色。
但是要想结果比较合理,选择阈值是一项复杂的工作。
代码参考:
Shiffman, Daniel. Learning Processing: a beginner's guide to programming images, animation, and interaction. Morgan Kaufmann, 2009. P319
PImage source; // Source image
PImage destination; // Destination image
void setup() {
size(1200, 1200);
source = loadImage("zhao1.png");
destination = createImage(source.width,source.height, RGB);
}
void draw() {
float threshold = 120;
// The sketch is going to look at both image's pixels
source.loadPixels();
destination.loadPixels();
for (int x = 0; x < source.width; x++) {
for (int y = 0; y < source.height; y++) {
int loc = x + y*source.width;
// Test the brightness against the threshold
if (brightness(source.pixels[loc]) > threshold){
destination.pixels[loc] = color(255); // White
} else {
destination.pixels[loc] = color(0); // Black
}
}
}
// The pixels in destination changed
destination.updatePixels();
// Display the destination
image(destination, 0, 0);
}
网友评论