Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 11 of 11

Thread: Blur Image

  1. #1
    Junior Member
    Join Date
    Oct 2009
    Posts
    6
    Thanks
    0
    Thanked 1 Time in 1 Post

    Question Blur Image

    I want Blur Image Program code in java please help

    I found some replies thanks

    Description about this problem is as follows:-

    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

    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

    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
    Last edited by psrkiran; October 2nd, 2009 at 10:22 PM.


  2. #2
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: Blur Image

    That will be 500 please

    Jokes aside, what code do you have so far?

    // Json

  3. #3
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Blur Image

    Here's a simple blurring algorithm:

    1. Take the 8 pixels surrounding your pixel and your pixel
    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.

  4. #4
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: Blur Image

    Interesting, I might give this a go later on for funsies

    // Json

  5. #5
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Blur Image

    Here's code that ignores the edges. It also lets you set the blur level (how many times it's run).

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

  6. #6
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Blur Image

    You didn't set the radix (aka. base). To use parseInt on a hex number:

    Integer.parseInt("FF3a20056",16);

  7. #7
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Blur Image

    ... I'm slightly confused at what it is you want.

    To get a hex String to into an int:

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

    To get an integer back into a hex string:

    Integer.toHexString(123);

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

    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.

  8. #8
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default 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.

    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.

  9. #9
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default 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.

    // 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

  10. The Following User Says Thank You to helloworld922 For This Useful Post:

    prakash (December 31st, 2009)

  11. #10
    Junior Member
    Join Date
    Oct 2009
    Posts
    6
    Thanks
    0
    Thanked 1 Time in 1 Post

    Wink 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

  12. The Following User Says Thank You to psrkiran For This Useful Post:

    prakash (December 31st, 2009)

  13. #11
    Junior Member
    Join Date
    Dec 2009
    Posts
    1
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Smile 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


Similar Threads

  1. Replies: 2
    Last Post: June 29th, 2009, 04:06 PM
  2. GUI problem with image in java
    By Kok in forum AWT / Java Swing
    Replies: 6
    Last Post: May 17th, 2009, 05:17 AM
  3. How images stores in awt.Image class?
    By BharatT in forum AWT / Java Swing
    Replies: 0
    Last Post: February 24th, 2009, 05:10 AM
  4. How to import an .tiff image in JSP?
    By jazz2k8 in forum JavaServer Pages: JSP & JSTL
    Replies: 4
    Last Post: May 12th, 2008, 06:55 AM