我正在學習java中的高斯,并試圖為我的png檔案添加一個模糊。但是,在編譯時會拋出錯誤訊息“IllegalArgumentException:顏色引數超出預期范圍:紅綠藍”并指向
這一行:
answer.setRGB(x, y, new Color(getWeightedColorValue(distributedColorRed), getWeightedColorValue(distributedColorGreen), getWeightedColorValue(distributedColorBlue)).getRGB());
從這個方法:
public BufferedImage createGaussiImage(BufferedImage source_image, double weights[][], int radius) {
BufferedImage answer = new BufferedImage(source_image.getWidth(), source_image.getHeight(), BufferedImage.TYPE_INT_RGB);
for(int x = 0; x < source_image.getWidth() - radius; x ) {
for(int y = 0 ; y < source_image.getHeight() - radius; y ) {
double[][] distributedColorRed = new double[radius][radius];
double[][] distributedColorGreen = new double[radius][radius];
double[][] distributedColorBlue = new double[radius][radius];
for(int weightX = 0; weightX < weights.length; weightX ) {
for(int weightY = 0; weightY < weights[weightX].length; weightY ) {
try {
int sampleX = x weightX - (weights.length/2);
int sampleY = y weightY - (weights.length/2);
double currentWeight = weights[weightX][weightY];
Color sampledColor = new Color(source_image.getRGB(sampleX, sampleY));
distributedColorRed[weightX][weightY] = currentWeight * sampledColor.getRed();
distributedColorGreen[weightX][weightY] = currentWeight * sampledColor.getGreen();
distributedColorBlue[weightX][weightY] = currentWeight * sampledColor.getBlue();
} catch (Exception ex) {
System.out.println("Out of bounds");
}
}
}
answer.setRGB(x, y, new Color(getWeightedColorValue(distributedColorRed), getWeightedColorValue(distributedColorGreen), getWeightedColorValue(distributedColorBlue)).getRGB());
}
}
return answer;
}
獲取加權顏色值:
private int getWeightedColorValue(double[][] weightedColor) {
double summation = 0;
for (int i = 0; i < weightedColor.length; i ) {
for(int j = 0; j<weightedColor[i].length; j ) {
summation = weightedColor[i][j];
}
}
return(int) summation;
}
我一直在嘗試除錯代碼,但我被卡住了,無法弄清楚出了什么問題。這個錯誤到底是什么意思,為什么我的代碼會拋出它?
輸出:
run:
0.007372423150128975 0.020040323880706385 0.02796852501066331 0.020040323880706385 0.007372423150128975
0.020040323880706385 0.054475248241358035 0.07602633330528842 0.054475248241358035 0.020040323880706385
0.02796852501066331 0.07602633330528842 0.10610329539459691 0.07602633330528842 0.02796852501066331
0.020040323880706385 0.054475248241358035 0.07602633330528842 0.054475248241358035 0.020040323880706385
0.007372423150128975 0.020040323880706385 0.02796852501066331 0.020040323880706385 0.007372423150128975
--------------------------------------------------
Summation: 0.007372423150128975
Out of bounds
Out of bounds
Out of bounds
Out of bounds
Out of bounds
Out of bounds
Out of bounds
Out of bounds
Out of bounds
Out of bounds
Out of bounds
Out of bounds
RGB values check:
2058.0440016064354
1986.084421130686
1928.5167567500864
Current weight:
14.391916095149899
Radius:
5
RGB values check:
1505.589741193562
1454.0284486869334
1412.7794146816302
Current weight:
10.312258501325768
Radius:
5
RGB values check:
550.0818447290609
531.113505255645
515.9388336769123
Current weight:
3.7936678946831783
Radius:
5
Out of bounds
Out of bounds
RGB values check:
1433.4039316842818
1361.2181221750013
1278.7200541643954
Current weight:
10.312258501325768
Radius:
5
RGB values check:
1004.9116294545687
975.3554050588461
908.8539001684702
Current weight:
7.389056098930652
Radius:
5
RGB values check:
372.4046104988892
361.53148318505305
337.06694672892166
Current weight:
2.7182818284590455
Radius:
5
Out of bounds
Out of bounds
RGB values check:
519.7325015715954
478.00215473008046
455.24014736198137
Current weight:
3.7936678946831783
Radius:
5
RGB values check:
380.5594559842664
356.09491952813494
337.06694672892166
Current weight:
2.7182818284590455
Radius:
5
RGB values check:
139.0
130.0
123.0
Current weight:
1.0
Radius:
5
Exception in thread "main" java.lang.IllegalArgumentException: Color parameter outside of expected range: Red Green Blue
at java.desktop/java.awt.Color.testColorValueRange(Color.java:310)
at java.desktop/java.awt.Color.<init>(Color.java:395)
at java.desktop/java.awt.Color.<init>(Color.java:369)
at gaussianblur.GaussianBlur.createGaussiImage(GaussianBlur.java:134)
at gaussianblur.main.main(main.java:27)
C:\Users\MarkC\AppData\Local\NetBeans\Cache\12.6\executor-snippets\run.xml:111: The following error occurred while executing this line:
C:\Users\MarkC\AppData\Local\NetBeans\Cache\12.6\executor-snippets\run.xml:68: Java returned: 1
BUILD FAILED (total time: 1 second)
uj5u.com熱心網友回復:
回答:
我的高級過濾器實作有兩個問題。第一個是超出界限的 RGB 值(必須 >=0 和 <=255)。我通過將 x 和 y 起始值從 0 更改為當前半徑來解決這個問題:
for(int x = radius; x < source_image.getWidth() - radius; x ) {
for(int y = radius ; y < source_image.getHeight() - radius; y )
這會將 0,0 坐標從左上角移動到死點。現在不是圍繞 0,0 進行迭代,因為這是影像的左上角位置,它會從中心像素向外迭代。
第二個問題是我處理影像的運行時間超過 30 分鐘。這就像從最里面的 for 回圈中洗掉 System.out.printlns 一樣簡單。這是一個問題的原因是因為緩沖 IO 是它自己的執行緒,控制臺將與其他執行緒競爭輸出(競爭條件)。當前運行時間減少到 10 秒!對于某些演算法來說,處理每個像素需要一點時間確實很常見,并且存在更快的像素(請在此處查看: Fastest Gaussian blur implementation)。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/437730.html
