繁体   English   中英

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

[英]Convolution problem that KeyPressed Different Image views for example Sharpen

以不同的视图显示不同的图像。 例如,锐化为一。 我无法显示Sharpen。 我不知道如何完成锐化。 我以教授的代码为例。

还有其他:

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];
                }
            }
        } else if (key == '1') {
            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_sharpen, 3, source);
                    else
                        destination.pixels[loc] = source.pixels[loc];
                }
            }
        }
    }
}    

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);
}

在函数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 ,这将启动图像过滤器:

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);

请参阅示例,在该示例中,我将建议应用于您的问题的代码:

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);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM