如何使用鼠标坐标将效果应用于图像?

发布于 2025-02-04 21:18:11 字数 828 浏览 3 评论 0原文

我编码了一个程序,以处理屏幕上的所有像素都扰乱但在光标周围。该代码通过用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

孤星 2025-02-11 21:18:11

您无需迭代所有像素即可更改一些。

幸运的是,您的草图与网络摄像头feed的大小相同,因此您使用x +(y + width)算术在正确的轨道上,从2D阵列索引转换为1D 像素[]索引。请记住,您是从当前的一维数组中采样(随机0,坐标)。即使您将启动/结束索引上升至仍将跨越几个完整图像行的范围,这意味着效果选择的左右像素。我建议在2D中选择随机X,Y索引,然后将这些随机值转换为1D(而不是从1D数组中的单个索引)。

这是我的意思:(

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];
    //    }
    //  }
    //}
    // mouse x, y shorthand
    int mx = mouseX;
    int my = mouseY;
    // random pixels effect size
    int size = 40;
    // half of size
    int hsize = size / 2;
    // 2D pixel coordinates of the effect's bounding box
    int minX = mx - hsize;
    int maxX = mx + hsize;
    int minY = my - hsize;
    int maxY = my + hsize;
    // apply the effect only where the bounding can be applied
    // e.g. avoid a border (of hsize) around edges of the image
    if (mx >= hsize && mx < width - hsize &&
        my >= hsize && my < height - hsize) {
      for(int y = minY; y < maxY; y++){
        for(int x = minX; x < maxX; x++){
          // pick random x,y coordinates to sample a pixel from
          int rx = (int)random(minX, maxX);
          int ry = (int)random(minY, maxY);
          // convert the 2D random coordinates to a 1D pixel[] index
          int ri = rx + (ry * width);
          // replace current pixel with randomly sampled pixel (within effect bbox)
          pixels[x + (y * width)] = video.pixels[ri];
        }
      }
    }
    
  }
  updatePixels();
}

请注意,以上未测试,但希望这一点是)

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 1D pixels[] 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:

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];
    //    }
    //  }
    //}
    // mouse x, y shorthand
    int mx = mouseX;
    int my = mouseY;
    // random pixels effect size
    int size = 40;
    // half of size
    int hsize = size / 2;
    // 2D pixel coordinates of the effect's bounding box
    int minX = mx - hsize;
    int maxX = mx + hsize;
    int minY = my - hsize;
    int maxY = my + hsize;
    // apply the effect only where the bounding can be applied
    // e.g. avoid a border (of hsize) around edges of the image
    if (mx >= hsize && mx < width - hsize &&
        my >= hsize && my < height - hsize) {
      for(int y = minY; y < maxY; y++){
        for(int x = minX; x < maxX; x++){
          // pick random x,y coordinates to sample a pixel from
          int rx = (int)random(minX, maxX);
          int ry = (int)random(minY, maxY);
          // convert the 2D random coordinates to a 1D pixel[] index
          int ri = rx + (ry * width);
          // replace current pixel with randomly sampled pixel (within effect bbox)
          pixels[x + (y * width)] = video.pixels[ri];
        }
      }
    }
    
  }
  updatePixels();
}

(Note that the above isn't tested, but hopefully the point gets across)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文