KeyPressed不同图像视图(例如,锐化

时间:2019-04-14 23:03:00

标签: java image processing

以不同的视图显示不同的图像。例如,锐化为一。我无法显示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);
}

1 个答案:

答案 0 :(得分:0)

在函数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);
}