I'm working on a Kuwahara Filter in java for smoothing images while preserving the edges. My current code won't run due to an infinite for-loop, I'm not sure if it's something with the loop itself or an error with a variable or array.
Here's the code:
private static void Processing(BufferedImage img) {
int w = img.getWidth(null);
int h = img.getHeight(null);
int Size = w*h;
int[] ApetureMinX = {-(Size / 2), 0, -(Size / 2), 0};
int[] ApetureMaxX = {0, (Size / 2), 0, (Size / 2)};
int[] ApetureMinY = {-(Size / 2), -(Size / 2), 0, 0};
int[] ApetureMaxY = {0, 0, (Size / 2), (Size / 2)};
for (int x = 0; x < w; x++) {
for (int y = 0; y < h; y++) {
int rgb = img.getRGB(x, y);
int a = (rgb & 0xff000000) >>> 24;
int r = (rgb & 0x00ff0000) >>> 16;
int g = (rgb & 0x0000ff00) >>> 8;
int b = rgb & 0x000000ff;
// KUWAHARA
int[] RValues = {0, 0, 0, 0};
int[] GValues = {0, 0, 0, 0};
int[] BValues = {0, 0, 0, 0};
int[] NumPixels = {0, 0, 0, 0};
int[] MaxRValue = {0, 0, 0, 0};
int[] MaxGValue = {0, 0, 0, 0};
int[] MaxBValue = {0, 0, 0, 0};
int[] MinRValue = {255, 255, 255, 255};
int[] MinGValue = {255, 255, 255, 255};
int[] MinBValue = {255, 255, 255, 255};
for (int i = 0; i < 4; ++i) {
for (int x2 = ApetureMinX[i]; x2 < ApetureMaxX[i]; ++x2) {
int TempX = x + x2;
if (TempX >= 0 && TempX < w) {
for (int y2 = ApetureMinY[i]; y2 < ApetureMaxY[i]; ++y2) {
int TempY = y + y2;
if (TempY >= 0 && TempY < h) {
int TempColor = img.getRGB(TempX, TempY);
int red = (TempColor & 0x00ff0000) >> 16;
int green = (TempColor & 0x0000ff00) >> 8;
int blue = TempColor & 0x000000ff;
RValues[i] += red;
GValues[i] += green;
BValues[i] += blue;
if (red > MaxRValue[i]) {
MaxRValue[i] = red;
} else if (red < MinRValue[i]) {
MinRValue[i] = red;
}
if (green > MaxGValue[i]) {
MaxGValue[i] = green;
} else if (green < MinGValue[i]) {
MinGValue[i] = green;
}
if (blue > MaxBValue[i]) {
MaxBValue[i] = blue;
} else if (blue < MinBValue[i]) {
MinBValue[i] = blue;
}
NumPixels[i]++;
}
}
}
}
}
int j = 0;
int MinDifference = 10000;
for (int i = 0; i < 4; ++i) {
int CurrentDifference = (MaxRValue[i] - MinRValue[i]) + (MaxGValue[i] - MinGValue[i]) + (MaxBValue[i] - MinBValue[i]);
if (CurrentDifference < MinDifference && NumPixels[i] > 0) {
j = i;
MinDifference = CurrentDifference;
b = BValues[j] / NumPixels[j];
g = GValues[j] / NumPixels[j];
r = RValues[j] / NumPixels[j];
}
}
// END KUWAHARA //
int RGB = b | (g << 8) | (r << 16) | (a << 24);
img.setRGB(x, y, RGB);
}
}
I've tested each separate loop with a print statement and it prints infinite times, which leads me to believe there is something wrong with a variable / array / math in the code.
User contributions licensed under CC BY-SA 3.0