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 24 of 24

Thread: Convert color image to grayscale and then to sepia

  1. #1
    Junior Member
    Join Date
    Apr 2014
    Posts
    29
    Thanks
    23
    Thanked 0 Times in 0 Posts

    Question Convert color image to grayscale and then to sepia

    I have the program working where the image is converted to grayscale but I get the following error when trying to convert from grayscale to sepia.

    java:36 error: '. class' expected
    red = Math.min(int(red * 1.08), 255);
    java:36 error: illegal start of expression
    red = Math.min(int(red * 1.08), 255);

     
    import images.APImage;
    import images.Pixel;
    import java.util.Scanner;
     
    public class ImageProgram {
     
    	public static void main(String[] args) {
     
    		//Use scanner to accept input from user
    		Scanner reader = new Scanner(System.in);
     
    		//Create and draw the image
    		APImage image = new APImage("smokey.jpg");
    		image.draw();
     
    		//Set image to grayscale
    		for (Pixel p: image) {
    			int red = p.getRed();
    			int green = p.getGreen();
    			int blue = p.getBlue();
    			int average = (red + green + blue) / 3;
    			p.setRed(average);
    			p.setGreen(average);
    			p.setBlue(average);
     
    			//set image from grayscale to sepia
    			if(red < 63) {
    				red = (int)(red * 1.1);
    				blue = (int)(blue *0.9);
    			}
    			else if (red < 192) {
    				red = (int)(red * 1.15);
    				blue = (int)(blue * 0.85);
    			}
    			else {
    				red = Math.min(int(red * 1.08), 255);
    				blue = (int)(blue * 0.93);
    			}
     
     
    		}
     
    		//Causes program to wait to draw the image
    		System.out.print("Press return to continue:");
    		reader.nextLine();
     
    		//Draw the image again
    		image.draw();
    	}
     
     
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Convert color image to grayscale and then to sepia

    red = Math.min(int(red * 1.08), 255);
    int is coded here with method call syntax.
    int is a keyword and can't be the name of a method.
    Casting is done by enclosing the type in ()s: (int)
    You have several correct casts:
    red = (int)(red * 1.1);
    If you don't understand my answer, don't ignore it, ask a question.

  3. The Following User Says Thank You to Norm For This Useful Post:

    sjdganc (September 15th, 2014)

  4. #3
    Junior Member
    Join Date
    Apr 2014
    Posts
    29
    Thanks
    23
    Thanked 0 Times in 0 Posts

    Default Re: Convert color image to grayscale and then to sepia

    Thank you that cleared the errors. So now the image appears in color and then in grayscale but the sepia effect is not working. What am I missing?

  5. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Convert color image to grayscale and then to sepia

    Do you have the coding rules for creating those kinds of images? Does your code follow those rules?
    If you don't understand my answer, don't ignore it, ask a question.

  6. The Following User Says Thank You to Norm For This Useful Post:

    sjdganc (September 15th, 2014)

  7. #5
    Junior Member
    Join Date
    Apr 2014
    Posts
    29
    Thanks
    23
    Thanked 0 Times in 0 Posts

    Default Re: Convert color image to grayscale and then to sepia

    I thought the second if statement and elseif statements is the code for sepia.

  8. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Convert color image to grayscale and then to sepia

    Is the results from when those statements correct? Have you tried debugging the code to see if the values are right?
    If you don't understand my answer, don't ignore it, ask a question.

  9. The Following User Says Thank You to Norm For This Useful Post:

    sjdganc (September 15th, 2014)

  10. #7
    Junior Member
    Join Date
    Apr 2014
    Posts
    29
    Thanks
    23
    Thanked 0 Times in 0 Posts

    Default Re: Convert color image to grayscale and then to sepia

    Hmmm. I'm not sure how to do that. I'll keep researching.

  11. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Convert color image to grayscale and then to sepia

    One debugging technique is to use println() statements to print out the values of variables as they are used and changed.
    If you don't understand my answer, don't ignore it, ask a question.

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

    sjdganc (September 16th, 2014)

  13. #9
    Junior Member
    Join Date
    Apr 2014
    Posts
    29
    Thanks
    23
    Thanked 0 Times in 0 Posts

    Default Re: Convert color image to grayscale and then to sepia

    I put in a println() statement and the values are all the same:
    Pixel R: 62 G: 62 B: 62

    which is the grayscale. So according to this the R value is less than 63 as in the below if statement. The first part of the below if statement should work, shouldn't it?
    if(red < 63) {
    				red = (int)(red * 1.1);
    				blue = (int)(blue *0.9);
    			}
    			else if (red < 192) {
    				red = (int)(red * 1.15);
    				blue = (int)(blue * 0.85);
    			}
    			else {
    				red = Math.min((int)(red * 1.08), 255);
    				blue = (int)(blue * 0.93);
    			}

  14. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Convert color image to grayscale and then to sepia

    Add some more println() statements so you can see what statements are being executed and what the values of the variables are as they are changed. The code in post#9 doesn't have any print statements. Add some so you know what code is being executed.
    If you don't understand my answer, don't ignore it, ask a question.

  15. The Following User Says Thank You to Norm For This Useful Post:

    sjdganc (September 16th, 2014)

  16. #11
    Junior Member
    Join Date
    Apr 2014
    Posts
    29
    Thanks
    23
    Thanked 0 Times in 0 Posts

    Default Re: Convert color image to grayscale and then to sepia

    I put in a for statement to run through all of the pixels. The red pixel is below 63 and when I run it through the second if statement they are below 192.

    if(red < 63) {
    				red = (int)(red * 1.1);			
    				blue = (int)(blue *0.9);
     
    			for(int y = 0; y < image.getHeight(); y++)
    				for(int x = 0; x < image.getWidth(); x++)
    				System.out.println(image.getPixel(x, y));
     
     
    			}
    			else if (red < 192) {
    				red = (int)(red * 1.15);
    				blue = (int)(blue * 0.85);	
     
    			}
    			else {
    				red = Math.min((int)(red * 1.08), 255);
    				blue = (int)(blue * 0.93);			
     
    			}

  17. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Convert color image to grayscale and then to sepia

    Is that what they are supposed to be? Is the code doing what you expect? If not, explain what it is not doing correctly.

    The for loop is inside the if() test for red < 63. Why is there a loop at that location? What is it supposed to show?

    --- Update ---

    The code changes the values of red and blue but never uses the new values.
    Why does it change them if it doesn't use the new values?
    If you don't understand my answer, don't ignore it, ask a question.

  18. The Following User Says Thank You to Norm For This Useful Post:

    sjdganc (September 16th, 2014)

  19. #13
    Junior Member
    Join Date
    Apr 2014
    Posts
    29
    Thanks
    23
    Thanked 0 Times in 0 Posts

    Default Re: Convert color image to grayscale and then to sepia

    Ok. That makes sense. What is printing out as the pixel values follows the if statements, so maybe what I am missing is the code after the if() statements to get or set what the pixels should be. Is that right?

  20. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Convert color image to grayscale and then to sepia

    I think the code should use the new values to change the image.
    If you don't understand my answer, don't ignore it, ask a question.

  21. The Following User Says Thank You to Norm For This Useful Post:

    sjdganc (September 16th, 2014)

  22. #15
    Junior Member
    Join Date
    Apr 2014
    Posts
    29
    Thanks
    23
    Thanked 0 Times in 0 Posts

    Default Re: Convert color image to grayscale and then to sepia

    Just saw your update. I put the for loop in just to see the pixel values. So I'll try putting it after the if () statements and get the values.

    --- Update ---

    So here is the whole code. I put in another for statement at the that should get the new values red and blue as stated in the if statement. I am getting the following errors:

    java:56 : error: cannot find symbol
    p.setRed(red);
    symbol: variable red
    The same error for blue as well
    import images.APImage;
    import images.Pixel;
    import java.util.Scanner;
     
    public class ImageProgram {
     
    	public static void main(String[] args) {
     
    		//Use scanner to accept input from user
    		Scanner reader = new Scanner(System.in);
     
    		//Create and draw the image
    		APImage image = new APImage("Convict Lake.jpg");
    		image.draw();
     
     
    		//Set image to grayscale
    		for (Pixel p: image) {
    			int red = p.getRed();
    			int green = p.getGreen();
    			int blue = p.getBlue();
    			int average = (red + green + blue) / 3;
    			p.setRed(average);
    			p.setGreen(average);
    			p.setBlue(average);	
     
     
    		//System.out.println(image.getPixel(0, 0));			
     
    			if(red < 63) {
    				red = (int)(red * 1.1);			
    				blue = (int)(blue *0.9);		
     
    			}
    			else if (red < 192) {
    				red = (int)(red * 1.15);
    				blue = (int)(blue * 0.85);	
     
    			}
    			else {
    				red = Math.min((int)(red * 1.08), 255);
    				blue = (int)(blue * 0.93);			
     
    			}
     
    			for(int y = 0; y < image.getHeight(); y++)
    				for(int x = 0; x < image.getWidth(); x++)	
     
    		}		
    		for (Pixel p: image) {
    			int red = p.getRed();
    			int green = p.getGreen();	
    			int blue = p.getBlue();
    			p.setRed(red);
    			p.setGreen(average);
    			p.setBlue(blue);
     
    		}	
    		//Causes program to wait to draw the image
    		System.out.print("Press return to continue:");
    		reader.nextLine();
     
     
    		//Draw the image again
    		image.draw();
    	}
     
     
    }

  23. #16
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Convert color image to grayscale and then to sepia

    The variable: red is defined 3 statements before that statement in the posted code.
    Are you sure you posted the code with the error?
    	                int red = p.getRed();     //<<<<<<< Defined here
    			int green = p.getGreen();	
    			int blue = p.getBlue();
    			p.setRed(red);              // used here
    If you don't understand my answer, don't ignore it, ask a question.

  24. The Following User Says Thank You to Norm For This Useful Post:

    sjdganc (September 16th, 2014)

  25. #17
    Junior Member
    Join Date
    Apr 2014
    Posts
    29
    Thanks
    23
    Thanked 0 Times in 0 Posts

    Default Re: Convert color image to grayscale and then to sepia

    Yes I'm sure but now I'm having trouble recreating it. I keep getting an illegal start of statement at line 56 which is the curly brace right above the comment //Causes program to wait to draw the image. I checked and have matching curly braces. Can you see what I'm missing?

  26. #18
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Convert color image to grayscale and then to sepia

    Please copy the full text of the error message and paste it here and the full java code that has the errors.
    If you don't understand my answer, don't ignore it, ask a question.

  27. The Following User Says Thank You to Norm For This Useful Post:

    sjdganc (September 16th, 2014)

  28. #19
    Junior Member
    Join Date
    Apr 2014
    Posts
    29
    Thanks
    23
    Thanked 0 Times in 0 Posts

    Default Re: Convert color image to grayscale and then to sepia

    I found the error, it was the curly brace, ugh. So the program is compiling without errors but not doing what I want it to do. Now only the color image displays. The grayscale image is not showing which it was before and I think it still should but maybe not because now I'm asking for the new set color. Do I have the for statement in the right place? Am I correct in saying p.setRed(red) below this for statement as well?
    import images.APImage;
    import images.Pixel;
    import java.util.Scanner;
     
    public class ImageProgram {
     
    	public static void main(String[] args) {
     
    		//Use scanner to accept input from user
    		Scanner reader = new Scanner(System.in);
     
    		//Create and draw the image
    		APImage image = new APImage("Convict Lake.jpg");
    		image.draw();
     
     
    		//Set image to grayscale
    		for (Pixel p: image) {
    			int red = p.getRed();
    			int green = p.getGreen();
    			int blue = p.getBlue();
    			int average = (red + green + blue) / 3;
    			p.setRed(average);
    			p.setGreen(average);
    			p.setBlue(average);	
     
     
    		//System.out.println(image.getPixel(0, 0));
     
     
    				if(red < 63) {
    					red = (int)(red * 1.1);			
    					blue = (int)(blue *0.9);		
    					p.setRed(red);
    					p.setBlue(blue);			
     
    				}
    				else if (red < 192) {
    					red = (int)(red * 1.15);
    					blue = (int)(blue * 0.85);	
    					p.setRed(red);
    					p.setBlue(blue);
     
    				}
    				else {
    					red = Math.min((int)(red * 1.08), 255);
    					blue = (int)(blue * 0.93);			
    					p.setRed(red);
    					p.setBlue(blue);
     
    				}	
     
    				for(int y = 0; y < image.getHeight(); y++)
    					for(int x = 0; x < image.getWidth(); x++)
     
    					p.setRed(red);
    					p.setGreen(average);
    					p.setBlue(blue);	
     
    		}
    		//Causes program to wait to draw the image
    		System.out.print("Press return to continue:");
    		reader.nextLine();
     
     
    		//Draw the image again
    		image.draw();
    	}
     
     
    }

  29. #20
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Convert color image to grayscale and then to sepia

    The for statements are missing {}s that should enclose the code that is inside of the loop. Never leave off the {}s. Indenting a statement does NOT put it inside of a for loop. Use {}s

    This for loop does nothing. The only values that change in the loop are the loop indexes: y and x
                            for(int y = 0; y < image.getHeight(); y++)
    				for(int x = 0; x < image.getWidth(); x++)
     
    					p.setRed(red);
    					p.setGreen(average);
    					p.setBlue(blue);
    If you don't understand my answer, don't ignore it, ask a question.

  30. The Following User Says Thank You to Norm For This Useful Post:

    sjdganc (September 16th, 2014)

  31. #21
    Junior Member
    Join Date
    Apr 2014
    Posts
    29
    Thanks
    23
    Thanked 0 Times in 0 Posts

    Default Re: Convert color image to grayscale and then to sepia

    I put the {} around the for statements but the outcome did not change. Still only getting the color image.

    for(int y = 0; y < image.getHeight(); y++) { 
    				for(int x = 0; x < image.getWidth(); x++)
     
    					p.setRed(red);
    					p.setGreen(average);
    					p.setBlue(blue);	
     
    			}

  32. #22
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Convert color image to grayscale and then to sepia

    Nothing is changed inside that loop. The set methods are called with the same values everytime around the loop.

    I put the {} around the for statements
    Only one. The inner one isn't in {}s
    But that makes no difference. The loop is useless. See above.
    If you don't understand my answer, don't ignore it, ask a question.

  33. The Following User Says Thank You to Norm For This Useful Post:

    sjdganc (September 17th, 2014)

  34. #23
    Junior Member
    Join Date
    Apr 2014
    Posts
    29
    Thanks
    23
    Thanked 0 Times in 0 Posts

    Default Re: Convert color image to grayscale and then to sepia

    I'm stumped. Am I on the right track with the below code? Or do I need to redefine red, green, and blue?
     
    				if(red < 63) {
    					red = (int)(red * 1.1);			
    					blue = (int)(blue *0.9);								
     
    				}
    				else if (red < 192) {
    					red = (int)(red * 1.15);
    					blue = (int)(blue * 0.85);	
     
    				}
    				else {
    					red = Math.min((int)(red * 1.08), 255);
    					blue = (int)(blue * 0.93);			
     
    				}
     
     
    			for(int y = 0; y <= image.getHeight(); y++) { 
    				for(int x = 0; x <= image.getWidth(); x++) {
    					image.setPixel(x, y, new Pixel(red, 0, blue));
     
    				}	
    			}

  35. #24
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Convert color image to grayscale and then to sepia

    What is the for loop at the end of the posted code supposed to do?
    The values of red and blue do not change in the loop.
    What will the image look like if all the pixels are set to the same values of red and blue?

    You need to stop making random changes to the code and work on the design for the steps the program needs to take.
    Write some pseudo code describing what you want the code to do.
    When the design is worked out, then write the code to do what the design says to do.

    An example of pseudo code:
    initialize variables
    begin loop through all the pixels in image
    get next pixel
    change value of colors for pixel
    set the updated pixel
    end loop
    show new image with changed pixels
    If you don't understand my answer, don't ignore it, ask a question.

  36. The Following User Says Thank You to Norm For This Useful Post:

    sjdganc (September 17th, 2014)

Similar Threads

  1. how to convert rgb to a grayscale image??
    By Mumpy Zinu in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 8th, 2013, 09:30 AM
  2. removing image background color using java??
    By game06 in forum Java Theory & Questions
    Replies: 2
    Last Post: March 23rd, 2013, 07:19 AM
  3. Replies: 7
    Last Post: January 23rd, 2013, 09:04 PM
  4. how to get gray scale value from 16- bit grayscale image
    By div111 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: January 16th, 2013, 08:51 AM
  5. get a pixel value from 16- bit grayscale image
    By div111 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: January 11th, 2013, 02:44 PM