繁体   English   中英

RGB模糊算法

[英]Blur algorithm with RGB

我试图根据用户输入模糊图像。 如果是1那么模糊图像平均为3X3平方并将其放入中间平方。

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

我想我已经提出了一个正确的解决方案,它进入边缘,但图像变成了完全黑色。 任何人都可以发现此代码的问题或是另一个问题?

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

是。 像素数不是

double num_pixels = x_range_max * y_range_max;

你没有减去范围的最小值,所以你假设太多的像素并除以一个太大的值。

更正:

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

@andy发现代码中的另一个问题可能不会导致黑色像素,但会导致您只取中间像素而不是平方像素的平均值。

您的代码中还有另一个问题导致它占用的区域小于您的预期。

您应该将for循环更改为:

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

并计算像素数:

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

根据您当前的实现,您错过了正确和最底部的像素。 也就是说你正在这样做(在你应用@ andy的固定之后 - 在你刚刚接受'S'之前):

P P
P S

我想这个

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

应该

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

暂无
暂无

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

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