简体   繁体   English

RGB模糊算法

[英]Blur algorithm with RGB

Im trying to blur an image depending on a user input. 我试图根据用户输入模糊图像。 If it is 1 then the blur image will average a 3X3 square and put it into the middle one. 如果是1那么模糊图像平均为3X3平方并将其放入中间平方。

P P P       All the pixels will be averaged and put into S.
P S P
P P P

I think I have come up with a correct solution that takes into the edges but the image is turning completely black. 我想我已经提出了一个正确的解决方案,它进入边缘,但图像变成了完全黑色。 Can anyone find a problem with this code or is it another problem? 任何人都可以发现此代码的问题或是另一个问题?

int range = blur_slider.getValue();

for (int x = 0; x < source.getWidth(); x++) {
    for (int y = 0; y < source.getHeight(); y++) {
        double red_sum = 0.0;
        double green_sum = 0.0;
        double blue_sum = 0.0;
        // finds the min x and y values and makes sure there are no out of bounds exceptions

        int x_range_min = x - range;
        if (x_range_min < 0) {
            x_range_min = 0;
        }

        int x_range_max = x + range;
        if (x_range_max >= new_frame.getWidth()) {
            x_range_max = new_frame.getWidth() - 1;
        }

        int y_range_min = y - range;
        if (y_range_min < 0) {
            y_range_min = 0;
        }

        int y_range_max = y + range;
        if (y_range_max >= new_frame.getHeight()) {
            y_range_max = new_frame.getHeight() - 1;
        }
        // averages the pixels within the min and max values and puts it into the pixel at the center. copy new frame is the frame that has previous
        // work done on it, it is used so that new blur pixels that are set do not affect later pixels. new_frame is the main frame that will be set.

        for (int k = x_range_min; k < x_range_max; k++) {
            for (int j = y_range_min; j < y_range_max; j++) {
                Pixel p = copy_new_frame.getPixel(x, y);
                red_sum += p.getRed();
                green_sum += p.getGreen();
                blue_sum += p.getBlue();
            }
        }
        double num_pixels = x_range_max * y_range_max;
        ColorPixel tempPixel = new ColorPixel(red_sum / num_pixels, green_sum / num_pixels, blue_sum / num_pixels);
        new_frame.setPixel(x, y, tempPixel);
    }

}
frame_view.setFrame(new_frame);

Yes. 是。 The number of pixels is not 像素数不是

double num_pixels = x_range_max * y_range_max;

You didn't subtract the minimum of the range so you're assuming far too many pixels and dividing by a value that's too large. 你没有减去范围的最小值,所以你假设太多的像素并除以一个太大的值。

Corrected: 更正:

double num_pixels = (x_range_max - x_range_min) * (y_range_max - y_range_min);

@andy identified another problem in the code that is probably not causing black pixels but will cause you to just take the middle pixel instead of the average of the pixels in a square. @andy发现代码中的另一个问题可能不会导致黑色像素,但会导致您只取中间像素而不是平方像素的平均值。

There is yet another problem in your code that causes it to take an area that's smaller than you intended. 您的代码中还有另一个问题导致它占用的区域小于您的预期。

You should change your for loops to: 您应该将for循环更改为:

for (int k = x_range_min; k <= x_range_max; k++) {
    for (int j = y_range_min; j <= y_range_max; j++) {

And your calculation for the number of pixels to: 并计算像素数:

double num_pixels = (x_range_max - x_range_min + 1) * (y_range_max - y_range_min + 1);

With your current implementation, you're missing the right and bottom-most pixels. 根据您当前的实现,您错过了正确和最底部的像素。 Ie you're doing this (after you have applied @andy's fixed - before that you're just taking 'S'): 也就是说你正在这样做(在你应用@ andy的固定之后 - 在你刚刚接受'S'之前):

P P
P S

I think this one 我想这个

Pixel p = copy_new_frame.getPixel(x,y);

should be 应该

Pixel p = copy_new_frame.getPixel(k,j);

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

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