# Blur Image

• October 2nd, 2009, 01:43 AM
psrkiran
Blur Image

I found some replies thanks

In the Blur Image problem, you are given an image and you have to compute an image blurred as per the given parameters. If radius is r then for a pixel at the data[x][y] in the output image, you would have to take an average of all pixels surrounding data[x][y] in radius r in the source image. For example if the radius 1, then data[3][3] in the output image would be average of following 9 points in the source image ( data [2][2], data [2][3], data [2][4] , data [3][2], data [3][3], data [3][4], data[4][2], data [4][3], data [4][4] ). Similarly, for radius 2, you would taking average of up to 25 points for each pixel (and for radius 3, average of up to 49 points).
Note that averaging would be done individually for each R, G and B component i.e. each of R and G and B components should be separately averaged for calculating the result pixel color
Constraints
• Each data point in the image is in RGB Format (integer value up to 0xFFFFFF), if not return null.
• Radius r has to be less than both rows or column in the source image, otherwise return null.

the code i written is

Code :

```public image blur_image(image i, int radius) {   if(radius<0 || radius>=i.rows || radius>=i.columns) return null; image o=new image(); o.rows=i.rows; o.columns=i.columns; o.data=new int[o.rows][o.columns]; int sr,sg,sb,x,y,d=0; for(int p=0;p<i.rows;p++) for(int q=0;q<i.columns;q++) { if( i.data[p][q]<0x000000 || i.data[p][q]>0xffffff) return null; d=0; sr=0; sg=0; sb=0; for(x=p-radius;x<=p+radius;x++) { if(x<0||x>=i.rows) continue; for(y=q-radius;y<=q+radius;y++) { if(y<0||y>=i.columns) continue; sr=sr+((i.data[x][y] >> 16)&0xff); sg=sg+((i.data[x][y] >> 8)&0xff); sb=sb+((i.data[x][y])&0xff); d++; } } sr=sr/d; sg=sg/d; sb=sb/d; o.data[p][q]=(((sr<<8)|sg)<<8)|sb; } return o;   }```

Format of class image is

Code :

```public class image { public int[][] data; public int rows; public int columns; }```

I want you guys to help me in testing the code whether it is feasible solution for given problem or not

Thanks
• October 2nd, 2009, 03:24 AM
Json
Re: Blur Image
That will be £500 please :D

Jokes aside, what code do you have so far?

// Json
• October 2nd, 2009, 09:37 AM
helloworld922
Re: Blur Image
Here's a simple blurring algorithm:

2. Average the RGB values of all 9 pixels and stick them in your current pixel location
3. Repeat for every pixel

Note: Remember not to store your "averaged" pixels into the same picture as the non-averaged picture, or it won't blur corrrectly. Also, you'll have to figure out a way to handle the edge pixels. You can either keep them the same, or take a smaller box to sample pixels from.
• October 2nd, 2009, 12:17 PM
Json
Re: Blur Image
Interesting, I might give this a go later on for funsies :)

// Json
• October 2nd, 2009, 08:55 PM
helloworld922
Re: Blur Image
Here's code that ignores the edges. It also lets you set the blur level (how many times it's run).

Code :

```public static Color[][] blur(Color[][] image, int blurLevel) { Color[][] blurred = image.clone(); for (int t = 0; t < blurLevel; t++) { for (int i = 1; i < blurred.length - 1; i++) { for (int j = 1; j < blurred[0].length - 1; j++) { int blue = image[i - 1][j - 1].getBlue() + image[i - 1][j].getBlue() + image[i - 1][j + 1].getBlue() + image[i][j - 1].getBlue() + image[i][j].getBlue() + image[i][j + 1].getBlue() + image[i + 1][j - 1].getBlue() + image[i + 1][j].getBlue() + image[i + 1][j + 1].getBlue(); blue /= 9; int red = image[i - 1][j - 1].getRed() + image[i - 1][j].getRed() + image[i - 1][j + 1].getRed() + image[i][j - 1].getRed() + image[i][j].getRed() + image[i][j + 1].getRed() + image[i + 1][j - 1].getRed() + image[i + 1][j].getRed() + image[i + 1][j + 1].getRed(); red /= 9; int green = image[i - 1][j - 1].getGreen() + image[i - 1][j].getGreen() + image[i - 1][j + 1].getGreen() + image[i][j - 1].getGreen() + image[i][j].getGreen() + image[i][j + 1].getGreen() + image[i + 1][j - 1].getGreen() + image[i + 1][j].getGreen() + image[i + 1][j + 1].getGreen(); green /= 9; blurred[i][j] = new Color(red, green, blue); } } image = blurred.clone(); } return blurred; }```
• December 29th, 2009, 07:04 PM
helloworld922
Re: Blur Image
You didn't set the radix (aka. base). To use parseInt on a hex number:

Code :

`Integer.parseInt("FF3a20056",16);`
• December 29th, 2009, 11:10 PM
helloworld922
Re: Blur Image
... I'm slightly confused at what it is you want.

To get a hex String to into an int:

Code :

`Integer.parseInt("1234AB",16);`

To get an integer back into a hex string:

Code :

`Integer.toHexString(123);`

To input a number as a hex, add 0x before it:

Code :

`int num = 0x62; // num = 98`

fyi, 98 is 0x62, not 0x68. Also, on the computer, all numbers are stored in binary because that's all the computer can really understand. The higher-level abstraction is what's displaying that number as decimal or hexadecimal, so it doesn't matter whether you put 0x62 into an int variable or 98 into an int variable they will have the same value.
• December 30th, 2009, 06:19 PM
helloworld922
Re: Blur Image
Sorry, I still don't understand what you want. Do you want a String hex output? The int data type doesn't matter whether it's in decimal, hex, octal, or binary. They all represent the same value. The default action for Integer.toString() is to return the decimal format for that value. You must use Integer.toHexString() in order to get a string representation of the hex value.

Code :

```System.out.println(0xa); // this will print out "10" System.out.println(Integer.toHexString(0xa)); // this will print out "a"```

0xa is understood to be an integer, not a string.
• December 31st, 2009, 12:00 AM
helloworld922
Re: Blur Image
If you want to re-combine the 3 RBG byte values into an int, simply shift the values into the appropriate spots and add. I've said it before, it doesn't matter whether there's a hex or a decimal or any other random base stored inside of an int, they all hold the same value. If you want that int to be displayed as a hex string, you must use the Integer.toHexString() or some sort of equivalent method.

Code :

```// put red byte, green byte, and blue byte into lower 3 bytes of an int int red = 0x62; int green = 0x00; int blue = 0x96; int rgb = (red << 4) + (green << 2) + blue; System.out.println(Integer.toHexString(rgb)); // print out as a hex System.out.println(rbg); // print out the same value as a decimal```
• December 31st, 2009, 06:52 AM
psrkiran
Re: Blur Image
Hey prakash i think u came through this question in

ASPIRATIONS 2020

Man Your code will be evaluated by Server and the test cases provided in main function are just for our verification and server tests ** public image blur_image(image i,radius r) ** function with different inputs

no one ( upto my knowledge ) has not got solution for that problem and im searching for it from approximately 2 months

and also each line of your code will have marks awarded by server so dont get worried about the problem
• December 31st, 2009, 06:57 AM
prakash
Re: Blur Image
helloworld922
Thanku for everything.
Truly belive me. i learned best of java. and it was awesome time to be with u.
all i have to say is THANKUUUUUUUU :-bd
:-bd
:-bd