为了账号安全,请及时绑定邮箱和手机立即绑定

我正在尝试对正在处理的图像进行马赛克效果,但图像保持不变?

我正在尝试对正在处理的图像进行马赛克效果,但图像保持不变?

潇潇雨雨 2021-12-10 15:00:46
我对处理很陌生。我正在尝试创建一个对普通图像应用马赛克效果的程序。我想要实现的是为图像创建过滤器大小的块(例如 30 像素)并将其替换为该块的 r、g、b 颜色的平均值这是我到目前为止所做的:class ME {  PImage image;  ME(String imagename) {    this.image = loadImage(imagename);  }  void display(int length, int height ) {    image.resize(length, height);    image(this.image, 0, 0);  }  void effect(int filterationSize) {  print("smth");    image.loadPixels();     float r, g, b;    for (int v = 0; v < (width*height ); v += filterationSize*width)     {      for (int h = 0; h < width; h+=filterationSize)      {        r = g = b = 0;        for (int bH = 0; bH<filterationSize; bH++)        {           for (int bV = 0; bV<filterationSize; bV++)        {        int p = v+h+bH+bV*width;        if ( p < width*width)          {            r += (red(this.image.pixels[p]) / (filterationSize*filterationSize));            g += (green(this.image.pixels[p]) / (filterationSize*filterationSize));            b += (blue(this.image.pixels[p]) / (filterationSize*filterationSize));          }        }        }         for (int blockH = 0; blockH<filterationSize; blockH++)      {        for (int blockV = 0; blockV<filterationSize; blockV++)        {          int p = v+h+blockH+blockV*width;          if ( p < width*width)          {            this.image.pixels[p] = color(r, g, b);          }        }      }      }    }    this.image.updatePixels();  }}这是我的主要课程:ME img ;void setup(){  size(500 ,500);  img = new ME("image.png");  img.display(width , height);}void draw(){   img.effect(30);}但最终图像与一开始的图像相同。
查看完整描述

1 回答

?
喵喔喔

TA贡献1735条经验 获得超5个赞

将效果应用于图像后,您错过了显示图像:


void draw(){

    img.effect(30);

    img.display(width , height);

}

但可能您想在图像加载后应用一次效果:


    ME img;


void setup(){

   size(500 ,500);

   img = new ME("image.png");

   img.display(width , height);

   img.effect(30);

}


void draw(){

    img.effect(30);

    img.display(width, height);

}

此外,您可以改进effect算法。


计算瓷砖的数量,但请注意,行或列中的最后一个瓷砖可能会被剪裁:


int tiles_x = width / filterationSize;

if ( width % filterationSize > 0 ) 

    tiles_x += 1;


int tiles_y = height / filterationSize;

if ( height % filterationSize > 0 ) 

    tiles_y += 1;

计算开始结束坐标和循环内图块的“大小”:


int start_x = tile_x*filterationSize;

int start_y = tile_y*filterationSize;

int end_x   = min(start_x+filterationSize, width); 

int end_y   = min(start_y+filterationSize, height);

int size    = (end_x-start_x) * (end_y-start_y); 

现在很容易计算一个瓦片的像素的平均值。完整的算法可能如下所示:


void effect(int filterationSize) {

    image.loadPixels(); 


    int tiles_x = width / filterationSize;

    if ( width % filterationSize > 0 ) 

        tiles_x += 1;

    int tiles_y = height / filterationSize;

    if ( height % filterationSize > 0 ) 

        tiles_y += 1;


    print( tiles_x, tiles_y );

    for ( int tile_y = 0; tile_y < tiles_x; tile_y ++ ) {

        for ( int tile_x = 0; tile_x < tiles_y; tile_x ++ ) {


            int start_x = tile_x*filterationSize;

            int start_y = tile_y*filterationSize;

            int end_x   = min(start_x+filterationSize, width); 

            int end_y   = min(start_y+filterationSize, height);

            int size    = (end_x-start_x) * (end_y-start_y);


            float r = 0, g = 0, b = 0;

            for (int by = start_y; by < end_y; by++ ) {

                for (int bx = start_x; bx < end_x; bx++ ) {

                    int p = by * width + bx;

                    r += red(this.image.pixels[p])   / size;

                    g += green(this.image.pixels[p]) / size;

                    b += blue(this.image.pixels[p])  / size;

                }

            }


            for (int by = start_y; by < end_y; by++ ) {

                for (int bx = start_x; bx < end_x; bx++ ) {

                    int p = by * width + bx;

                    this.image.pixels[p] = color(r, g, b);

                }

            }

        }

    }

    this.image.updatePixels();

}

查看应用于 256*256 图像和 32 块长度的效果:

//img1.sycdn.imooc.com//61b2fb3c000170c802510252.jpg//img1.sycdn.imooc.com//61b2fb430001648c02510255.jpg

查看完整回答
反对 回复 2021-12-10
  • 1 回答
  • 0 关注
  • 223 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号