猿问

KeyPressed不同图像视图的卷积问题,例如锐化

以不同的视图显示不同的图像。例如,锐化就是其中之一。我无法让它显示锐化。我不知道如何完成锐化。我以教授的代码为例。


还有其他的:


blur 3 x 3 

blur 5 x 5

edge detection

grayscale 

RGB > GRB

Zoom in

Zoom 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];

                }

            }

        } 

慕森卡
浏览 87回答 1
1回答

森栏

在函数中计算图像计划中像素的索引时存在问题convolution。一个像素的索引是 x + y * width而不是x * width:int loc = xLoc * yLoc * img.width;</s>int loc = xLoc + yLoc * img.width;将source图像复制到destination启动时并不断绘制destination 图像:void setup() {&nbsp; &nbsp; size(200, 200);&nbsp; &nbsp; source = loadImage("C:/temp/flower.jpg"); //source = loadImage("sunflower.jpg");&nbsp;&nbsp; &nbsp; destination = createImage(source.width, source.height, RGB);&nbsp; &nbsp; destination.copy(source, 0, 0, source.width, source.height, 0, 0, source.width, source.height);}void draw() {&nbsp; &nbsp; // [...]&nbsp; &nbsp; image(destination, 0, 0);}&nbsp;&nbsp;使用 keyPressed()事件来识别是否key按下了 a,这将启动图像过滤器:boolean average = key == '0';boolean sharpen = key == '1';void keyPressed() {&nbsp; &nbsp; average = key == '0';&nbsp; &nbsp; 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++) {&nbsp; &nbsp; for (int y = 0; y < source.height; y++) {&nbsp; &nbsp; &nbsp; &nbsp; int loc = x + y * source.width;&nbsp; &nbsp; &nbsp; &nbsp; if (x > xStart && x < xEnd && y > yStart && y < yEnd) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ( average )&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; destination.pixels[loc] = convolution(x, y, matrix_3_3_average, 3, source);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; destination.pixels[loc] = convolution(x, y, matrix_3_3_sharpen, 3, source);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}source.copy(destination, 0, 0, source.width, source.height, 0, 0, source.width, source.height);请参阅示例,其中我将建议应用于您的问题代码:PImage source;PImage destination;int w = 80;float[][] matrix_3_3_average = {&nbsp;&nbsp; &nbsp; {1.0/9.0, 1.0/9.0, 1.0/9.0 },&nbsp;&nbsp; &nbsp; {1.0/9.0, 1.0/9.0, 1.0/9.0 },&nbsp;&nbsp; &nbsp; {1.0/9.0, 1.0/9.0, 1.0/9.0 }};float[][] matrix_3_3_sharpen =&nbsp;&nbsp; &nbsp; { { -1, -1, -1 },&nbsp;&nbsp; &nbsp; { -1, 9, -1 },&nbsp;&nbsp; &nbsp; { -1, -1, -1 } };void setup() {&nbsp; &nbsp; size(200, 200);&nbsp; &nbsp; source = loadImage("C:/temp/flower.jpg"); //source = loadImage("sunflower.jpg");&nbsp;&nbsp; &nbsp; destination = createImage(source.width, source.height, RGB);&nbsp; &nbsp; destination.copy(source, 0, 0, source.width, source.height, 0, 0, source.width, source.height);}boolean average = key == '0';boolean sharpen = key == '1';&nbsp;void keyPressed() {&nbsp; &nbsp; average = key == '0';&nbsp; &nbsp; sharpen = key == '1';&nbsp;}void draw() {&nbsp; &nbsp; int xStart = constrain(mouseX - w/2, 0, width);&nbsp; &nbsp; int xEnd = constrain(mouseX + w/2, 0, width);&nbsp; &nbsp; int yStart = constrain(mouseY - w/2, 0, height);&nbsp; &nbsp; int yEnd = constrain(mouseY + w/2, 0, height);&nbsp; &nbsp; println(xStart, xEnd, yStart, yEnd);&nbsp; &nbsp; if (average || sharpen) {&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; destination.copy(source, 0, 0, source.width, source.height, 0, 0, source.width, source.height);&nbsp; &nbsp; &nbsp; &nbsp; for (int x = 0; x < source.width; x++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (int y = 0; y < source.height; y++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int loc = x + y * source.width;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (x > xStart && x < xEnd && y > yStart && y < yEnd) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ( average )&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; destination.pixels[loc] = convolution(x, y, matrix_3_3_average, 3, source);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; destination.pixels[loc] = convolution(x, y, matrix_3_3_sharpen, 3, source);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; source.copy(destination, 0, 0, source.width, source.height, 0, 0, source.width, source.height);&nbsp; &nbsp; &nbsp; &nbsp; average = sharpen = false;&nbsp;&nbsp; &nbsp; }&nbsp; &nbsp; image(destination, 0, 0);}&nbsp;&nbsp;color convolution(int x, int y, float[][] matrix, int matrixSize, PImage img) {&nbsp; &nbsp; int offset = (matrixSize - 1)/2;&nbsp; &nbsp; float r = 0;&nbsp; &nbsp; float g = 0;&nbsp; &nbsp; float b = 0;&nbsp; &nbsp; for (int i = 0; i < matrixSize; i++) {&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; for (int j = 0; j < matrixSize; j++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int xLoc = x + i - offset;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int yLoc = y + j - offset;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int loc = xLoc + yLoc * img.width;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; loc = constrain(loc, 0, img.pixels.length-1);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; r = r + matrix[i][j] * red(img.pixels[loc]);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; g = g + matrix[i][j] * green(img.pixels[loc]);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; b = b + matrix[i][j] * blue(img.pixels[loc]);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; return color(r, g, b);}
随时随地看视频慕课网APP

相关分类

Java
我要回答