如何使用鼠标坐标将效果应用于图像?
我编码了一个程序,以处理屏幕上的所有像素都扰乱但在光标周围。该代码通过用0和像素之间的随机像素替换循环目前正在打开的像素来替换像素。为了找到像素,我使用了代码(y*width+x)-1。但是,该代码正在从整个屏幕上取出像素。我希望代码可以从鼠标坐标周围40m平方的像素中取出像素。我该怎么做?
import processing.video.*;
Capture video;
void setup() {
size(640, 480);
video = new Capture(this, 640, 480);
video.start();
}
void draw() {
loadPixels();
if (video.available()){
video.read();
video.loadPixels();
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
pixels[y*width+x] = video.pixels[y*video.width+(width-x-1)];
// the code should only be applied 20 pixels around the mouse
if (dist(mouseX, mouseY, x, y) < 20){
int d = int(random(0, y*width+x-1));
pixels[y*width+x] = video.pixels[d];
}
}
}
}
updatePixels();
}
I coded a program on Processing where all the pixels on the screen are scrambled, but around the cursor. The code works by replacing the pixels with a random pixel between 0 and the pixel the loop is currently on. To find that pixel, I used the code (y*width+x)-1. This code, however, is taking pixels from the entire screen. I want the code to instead take the pixels from a 40m square around the mouse coordinates. How can I do this?
import processing.video.*;
Capture video;
void setup() {
size(640, 480);
video = new Capture(this, 640, 480);
video.start();
}
void draw() {
loadPixels();
if (video.available()){
video.read();
video.loadPixels();
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
pixels[y*width+x] = video.pixels[y*video.width+(width-x-1)];
// the code should only be applied 20 pixels around the mouse
if (dist(mouseX, mouseY, x, y) < 20){
int d = int(random(0, y*width+x-1));
pixels[y*width+x] = video.pixels[d];
}
}
}
}
updatePixels();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您无需迭代所有像素即可更改一些。
幸运的是,您的草图与网络摄像头feed的大小相同,因此您使用
x +(y + width)
算术在正确的轨道上,从2D阵列索引转换为1D像素[]
索引。请记住,您是从当前的一维数组中采样(随机0,坐标)。即使您将启动/结束索引上升至仍将跨越几个完整图像行的范围,这意味着效果选择的左右像素。我建议在2D中选择随机X,Y索引,然后将这些随机值转换为1D(而不是从1D数组中的单个索引)。这是我的意思:(
请注意,以上未测试,但希望这一点是)
You don't need to iterate through all the pixels to only change a few.
Luckily your sketch is the same size as the webcam feed, so you're on the right track using the
x + (y + width)
arithmetic to convert from a 2D array index to the 1Dpixels[]
index. Remember that you're sampling from a 1D array currently (random 0, coords). Even if you upate the start/end index that's still a range that will span a few full image rows which means pixels to the left and right of the effect selection. I recommend picking the random x, y indices in 2D, then converting these random values to 1D (as opposed to a single index from the 1D array).Here's what I mean:
(Note that the above isn't tested, but hopefully the point gets across)