亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

KeyPressed不同圖像視圖的卷積問題,例如銳化

KeyPressed不同圖像視圖的卷積問題,例如銳化

慕森卡 2022-10-07 16:34:50
以不同的視圖顯示不同的圖像。例如,銳化就是其中之一。我無法讓它顯示銳化。我不知道如何完成銳化。我以教授的代碼為例。還有其他的:blur 3 x 3 blur 5 x 5edge detectiongrayscale RGB > GRBZoom inZoom out我嘗試將 1 添加到 for 循環而不是零。這是代碼:PImage source;PImage destination;int w = 80;float[][] matrix_3_3_average = {     {1.0/9.0, 1.0/9.0, 1.0/9.0 },     {1.0/9.0, 1.0/9.0, 1.0/9.0 },     {1.0/9.0, 1.0/9.0, 1.0/9.0 }};float[][] matrix_3_3_sharpen =     { { -1, -1, -1 },     { -1, 9, -1 },     { -1, -1, -1 } };void setup() {    size(200, 200);    source = loadImage("sunflower.jpg");              destination = createImage(source.width, source.height, RGB);}void draw() {    destination.loadPixels(); // Tell Processing that we want to read the pixels of the output window    source.loadPixels();     int xStart = constrain(mouseX - w/2, 0, width);    int xEnd = constrain(mouseX + w/2, 0, width);    int yStart = constrain(mouseY - w/2, 0, height);    int yEnd = constrain(mouseY + w/2, 0, height);    if (keyPressed) {        if (key == '0') {            image(source, 0, 0);            for (int x = 1; x < source.width; x++) {                for (int y = 1; y < source.height; y++) {                    int loc = x + y * source.width;                    if ((x > xStart) && (x < xEnd) && (y > yStart) && (y < yEnd))                         destination.pixels[loc] = convolution(x, y, matrix_3_3_average, 3, source);                    else                         // set the color of the corresponding pixel to the output window to the color of the pixel to the input image                        destination.pixels[loc] = source.pixels[loc];                }            }        } 
查看完整描述

1 回答

?
森欄

TA貢獻1810條經驗 獲得超5個贊

在函數中計算圖像計劃中像素的索引時存在問題convolution。一個像素的索引是 x + y * width而不是x * width:


int loc = xLoc * yLoc * img.width;</s>

int loc = xLoc + yLoc * img.width;


將source圖像復制到destination啟動時并不斷繪制destination 圖像:


void setup() {


    size(200, 200);

    source = loadImage("C:/temp/flower.jpg"); //source = loadImage("sunflower.jpg"); 

    destination = createImage(source.width, source.height, RGB);

    destination.copy(source, 0, 0, source.width, source.height, 0, 0, source.width, source.height);

}


void draw() {


    // [...]


    image(destination, 0, 0);

}  

使用 keyPressed()事件來識別是否key按下了 a,這將啟動圖像過濾器:


boolean average = key == '0';

boolean sharpen = key == '1';


void keyPressed() {

    average = key == '0';

    sharpen = key == '1';

}

當執行圖像過濾器時,將圖像復制source到destination圖像中。更新過濾區域中的像素。最后將更改后destination的圖像復制到源圖像,為下一次過濾操作做準備:


destination.copy(source, 0, 0, source.width, source.height, 0, 0, source.width, source.height);

for (int x = 0; x < source.width; x++) {

    for (int y = 0; y < source.height; y++) {

        int loc = x + y * source.width;


        if (x > xStart && x < xEnd && y > yStart && y < yEnd) {

            if ( average )

                destination.pixels[loc] = convolution(x, y, matrix_3_3_average, 3, source);

            else

                destination.pixels[loc] = convolution(x, y, matrix_3_3_sharpen, 3, source);

        }

    }

}


source.copy(destination, 0, 0, source.width, source.height, 0, 0, source.width, source.height);

請參閱示例,其中我將建議應用于您的問題代碼:

http://img1.sycdn.imooc.com//633fe4d70001eebd01920195.jpg

PImage source;

PImage destination;

int w = 80;

float[][] matrix_3_3_average = { 

    {1.0/9.0, 1.0/9.0, 1.0/9.0 }, 

    {1.0/9.0, 1.0/9.0, 1.0/9.0 }, 

    {1.0/9.0, 1.0/9.0, 1.0/9.0 }

};

float[][] matrix_3_3_sharpen = 

    { { -1, -1, -1 }, 

    { -1, 9, -1 }, 

    { -1, -1, -1 } };


void setup() {


    size(200, 200);

    source = loadImage("C:/temp/flower.jpg"); //source = loadImage("sunflower.jpg"); 

    destination = createImage(source.width, source.height, RGB);

    destination.copy(source, 0, 0, source.width, source.height, 0, 0, source.width, source.height);

}


boolean average = key == '0';

boolean sharpen = key == '1'; 


void keyPressed() {

    average = key == '0';

    sharpen = key == '1'; 

}


void draw() {


    int xStart = constrain(mouseX - w/2, 0, width);

    int xEnd = constrain(mouseX + w/2, 0, width);

    int yStart = constrain(mouseY - w/2, 0, height);

    int yEnd = constrain(mouseY + w/2, 0, height);

    println(xStart, xEnd, yStart, yEnd);


    if (average || sharpen) { 


        destination.copy(source, 0, 0, source.width, source.height, 0, 0, source.width, source.height);

        for (int x = 0; x < source.width; x++) {

            for (int y = 0; y < source.height; y++) {

                int loc = x + y * source.width;


                if (x > xStart && x < xEnd && y > yStart && y < yEnd) {

                    if ( average )

                        destination.pixels[loc] = convolution(x, y, matrix_3_3_average, 3, source);

                    else

                        destination.pixels[loc] = convolution(x, y, matrix_3_3_sharpen, 3, source);

                }                   

            }

        }


        source.copy(destination, 0, 0, source.width, source.height, 0, 0, source.width, source.height);

        average = sharpen = false; 

    }


    image(destination, 0, 0);

}  


color convolution(int x, int y, float[][] matrix, int matrixSize, PImage img) {

    int offset = (matrixSize - 1)/2;

    float r = 0;

    float g = 0;

    float b = 0;

    for (int i = 0; i < matrixSize; i++) {  

        for (int j = 0; j < matrixSize; j++) {

            int xLoc = x + i - offset;

            int yLoc = y + j - offset;

            int loc = xLoc + yLoc * img.width;


            loc = constrain(loc, 0, img.pixels.length-1);


            r = r + matrix[i][j] * red(img.pixels[loc]);

            g = g + matrix[i][j] * green(img.pixels[loc]);

            b = b + matrix[i][j] * blue(img.pixels[loc]);

        }

    }

    return color(r, g, b);

}


查看完整回答
反對 回復 2022-10-07
  • 1 回答
  • 0 關注
  • 80 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號