[英]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.