Home > OS >  Bluring images with gaussian: Color parameter outside of expected range: Red Green Blue error
Bluring images with gaussian: Color parameter outside of expected range: Red Green Blue error

Time:03-03

I'm learning about gaussian in java and am trying to add a blur to my png file. However, when compiling it throws the error message "IllegalArgumentException: Color parameter outside of expected range: Red Green Blue" and points to

This line:

answer.setRGB(x, y, new Color(getWeightedColorValue(distributedColorRed), getWeightedColorValue(distributedColorGreen), getWeightedColorValue(distributedColorBlue)).getRGB());

From this method:

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

getWeightedColorValue:

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

I've been trying to debug the code for a bit but I'm stuck and can't figure out what's wrong. What exactly does this error mean and why is my code throwing it?

Output:

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)

CodePudding user response:

Answer:

There are two issues with my implementation of the advanced filter. The first is the RGB values exceeding the bounds (must be >=0 and <=255). I resolved this by changing the x and y starting values from 0 to the current radius:

for(int x = radius; x < source_image.getWidth() - radius; x  ) {
    for(int y = radius ; y < source_image.getHeight() - radius; y  )

This moves the 0,0 coordinate from the top left to dead center. Now instead of iterating around 0,0 which goes out of bounds as that is the top left position of the image, it'll iterate from the center pixel and outwards.

The second issue was my run time being over 30 minutes to process the image. This was as simple as removing the System.out.printlns from the inner most for loops. The reason this is an issue is because buffered IO is it's own thread and the console will compete with other threads to output (race condition). Current run time reduced to 10 seconds! It is indeed common for some of algorithms to take a little time as to process every pixel and faster ones exists (Please check here for that: Fastest Gaussian blur implementation).

  • Related