14 Replies - 17780 Views - Last Post: 04 March 2008 - 09:34 PM Rate Topic: -----

#1 CompSciForum854372  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 30
  • Joined: 26-February 08

Mirror an Image

Posted 04 March 2008 - 03:58 PM

I'm trying to take an image and mirror it over the horizontal axis

i have all the code to grab the photo and do a variety of things, like gray scale and negative but i cant quite get it to mirror
here is what ive done so far as in altering the images.



public class DIP {

	public static void grayscale(Picture p) {
		
		for( int i = 0; i < p.getWidth(); i++){
			for (int j = 0; j< p.getHeight(); j++){
				int r = p.getRed(i,j);
				int g = p.getGreen(i,j);
				int b = p.getBlue(i,j);
				
				int c = (r+g+B)/3;
					p.setRed(i, j, c);
					p.setGreen(i, j, c);
					p.setBlue(i,j,c);
				
			}
		}
		
	}

	public static void negative(Picture p){

		for( int i = 0; i < p.getWidth(); i++){
			for (int j = 0; j< p.getHeight(); j++){
				int r = p.getRed(i,j);
				int g = p.getGreen(i,j);
				int b = p.getBlue(i,j);
				
				int rneg = (255-r);
				int gneg = (255-g);
				int bneg = (255-B);
					
				p.setRed(i, j, rneg);
				p.setGreen(i, j, gneg);
				p.setBlue(i,j,bneg);
					
					

	}}}

	public static void horizontalMirror(Picture p){

		for( int i = 0; i < p.getWidth(); i++){
			for (int j = 0; j< p.getHeight(); j++){
		
		 
					
	}}}
	


Is This A Good Question/Topic? 0
  • +

Replies To: Mirror an Image

#2 letthecolorsrumble  Icon User is offline

  • Student of The Sun
  • member icon

Reputation: 27
  • View blog
  • Posts: 555
  • Joined: 07-November 07

Re: Mirror an Image

Posted 04 March 2008 - 04:26 PM

The case that I have thought of first is the picture having odd number of vertical pixels(height).


So you will have to replace the first row with the last one, the second with the second last one and so on.... (see images)

It is same as a matrix being mirrored horizontally.

I hope it helps you :)


Also, try to close the bottom code tag.

Attached image(s)

  • Attached Image
  • Attached Image

Was This Post Helpful? 0
  • +
  • -

#3 CompSciForum854372  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 30
  • Joined: 26-February 08

Re: Mirror an Image

Posted 04 March 2008 - 04:44 PM

Thanks
Do you have any suggestion on how to start me code,
I'm very new at java and can't really think how to get started
Was This Post Helpful? 0
  • +
  • -

#4 letthecolorsrumble  Icon User is offline

  • Student of The Sun
  • member icon

Reputation: 27
  • View blog
  • Posts: 555
  • Joined: 07-November 07

Re: Mirror an Image

Posted 04 March 2008 - 05:29 PM

Could you please post your code of Picture class? I need to know what all goes on in there to find a good solution.

This post has been edited by letthecolorsrumble: 04 March 2008 - 05:30 PM

Was This Post Helpful? 0
  • +
  • -

#5 CompSciForum854372  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 30
  • Joined: 26-February 08

Re: Mirror an Image

Posted 04 March 2008 - 05:36 PM


public class Picture
{
   private File file;
   private BufferedImage bufferedImage;
   private String title;

   /**
	Builds a PixelImage object based off of the file name
	given as a parameter.

	@param fileName The image file to open and read.
   */
   public Picture(String fileName, String title)
   {
	  try 
	  {
		 file = new File(fileName);
		 bufferedImage = ImageIO.read(file);
		 this.title = title;
	  }
	  catch (IOException ex)
	  {
		 System.out.println("Error reading the file " + fileName + ".");
		 System.exit(1);
	  }
   }

   public Picture(int width, int height)
   {
	  bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
	  for (int x = 0; x < width; x++)
		 for (int y = 0; y < height; y++)
			setPixel(x,y,0);
   }

   /**
	Returns the width of the image.

	@return  Returns the width of the image.
   */
   public int getWidth()
   {
	  return bufferedImage.getWidth();
   }

   /**
	Returns the height of the image.

	@return Returns the height of the image.
   */
   public int getHeight()
   {
	  return bufferedImage.getHeight();
   }

   /**
	Returns the Alpha-Red-Green-Blue (ARGB) value for 
	the pixel at (x,y).

	@param x An x coordinate in the image.
	@param y An y coordinate in the image.
	@return  Returns the ARGB value for the pixel at (x,y).
   */
   public int getPixel(int x, int y)
   {
	  return bufferedImage.getRGB(x,y);
   }

   /**
	Returns the Alpha value for the pixel at (x,y). 

	@param x  An x coordinate in the image.
	@param y  An y coordinate in the image.
	@return   Returns the alpha value for the pixel at (x,y).
   */
   public int getAlpha(int x, int y)
   {
	  int rgb = bufferedImage.getRGB(x,y);

	  return (rgb >> 24) & 0xFF;
   }

   /**
	Returns the Red value for the pixel at (x,y).

	@param x An x coordinate in the image.
	@param y An y coordinate in the image.
	@return  Returns the red value for the pixel at (x,y).
   */
   public int getRed(int x, int y)
   {
	  int rgb = bufferedImage.getRGB(x,y);

	  return (rgb >> 16) & 0xFF;
   }

   /**
	Returns the Green value for the pixel at (x,y).

	@param x An x coordinate in the image.
	@param y An y coordinate in the image.
	@return  Returns the green value for the pixel at (x,y).
   */
   public int getGreen(int x, int y)
   {
	  int rgb = bufferedImage.getRGB(x,y);

	  return (rgb >> 8) & 0xFF;
   }

   /**
	Returns the Blue value for the pixel at (x,y).

	@param x An x coordinate in the image.
	@param y An y coordinate in the image.
	@return  Returns the blue value for the pixel at (x,y).
   */
   public int getBlue(int x, int y)
   {
	  int rgb = bufferedImage.getRGB(x,y);

	  return rgb & 0xFF;
   }

   /**
	Sets the Alpha-Red-Green-Blue value for the pixel at (x,y).

	@param x An x coordinate in the image.
	@param y An y coordinate in the image.
	@param value The ARGB value for the pixel at (x,y).
   */
   public void setPixel(int x, int y, int value)
   {
	  bufferedImage.setRGB(x,y,value);
   }

   /**
	Sets the Alpha value for the pixel at (x,y).

	@param x An x coordinate in the image.
	@param y An y coordinate in the image.
	@param value The alpha value for the pixel at (x,y).
   */
   public void setAlpha(int x, int y, int value)
   {
	  int rgb = bufferedImage.getRGB(x,y);

	  rgb = (rgb & 0xFFFFFF) | (value << 24);
	  bufferedImage.setRGB(x,y,rgb);
   }

   /**
	Sets the Red value for the pixel at (x,y).

	@param x An x coordinate in the image.
	@param y An y coordinate in the image.
	@param value The red value for the pixel at (x,y).
   */
   public void setRed(int x, int y, int value)
   {
	  int rgb = bufferedImage.getRGB(x,y);

	  rgb = (rgb & 0xFF00FFFF) | (value << 16);
	  bufferedImage.setRGB(x,y,rgb);
   }

   /**
	Sets the Green value for the pixel at (x,y).

	@param x An x coordinate in the image.
	@param y An y coordinate in the image.
	@param value The green value for the pixel at (x,y).
   */
   public void setGreen(int x, int y, int value)
   {
	  int rgb = bufferedImage.getRGB(x,y);

	  rgb = (rgb & 0xFFFF00FF) | (value << 8);
	  bufferedImage.setRGB(x,y,rgb);
   }

   /**
	Sets the Blue value for the pixel at (x,y).

	@param x An x coordinate in the image.
	@param y An y coordinate in the image.
	@param value The blue value for the pixel at (x,y).
   */
   public void setBlue(int x, int y, int value)
   {
	  int rgb = bufferedImage.getRGB(x,y);

	  rgb = (rgb & 0xFFFFFF00) | value;
	  bufferedImage.setRGB(x,y,rgb);
   }

   /**
	Displays the image in a separate window.
   */
   public void displayImage()
   {
	  JFrame frame = new JFrame();
	  ImageIcon imageIcon = new ImageIcon();
	  JLabel label = new JLabel(imageIcon);

	  imageIcon.setImage(bufferedImage);
	  frame.getContentPane().add(label);
	  frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
	  frame.pack();
	  
	  frame.setTitle(this.title);

	  frame.setVisible(true);
   }

   public void saveImage(String fileName)
   {
	  try
	  {
		 File file = new File(fileName);

		 ImageIO.write(bufferedImage,"png",file);
	  }
	  catch (Exception ex)
	  {
		 System.out.println("Save error");
		 System.exit(1);
	  }
   }
}



Was This Post Helpful? 0
  • +
  • -

#6 letthecolorsrumble  Icon User is offline

  • Student of The Sun
  • member icon

Reputation: 27
  • View blog
  • Posts: 555
  • Joined: 07-November 07

Re: Mirror an Image

Posted 04 March 2008 - 05:52 PM

Oops you did it again! ;) Please edit your post using correct code tags... makes it more readable. Thanks
Was This Post Helpful? 0
  • +
  • -

#7 letthecolorsrumble  Icon User is offline

  • Student of The Sun
  • member icon

Reputation: 27
  • View blog
  • Posts: 555
  • Joined: 07-November 07

Re: Mirror an Image

Posted 04 March 2008 - 06:22 PM

I am guessing that you could use the two methods getPixel() and setPixel() to mirror the image. I haven't tested it though.
Was This Post Helpful? 0
  • +
  • -

#8 CompSciForum854372  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 30
  • Joined: 26-February 08

Re: Mirror an Image

Posted 04 March 2008 - 06:23 PM

public class Picture
{
   private File file;
   private BufferedImage bufferedImage;
   private String title;

   /**
	Builds a PixelImage object based off of the file name
	given as a parameter.

	@param fileName The image file to open and read.
   */
   public Picture(String fileName, String title)
   {
	  try 
	  {
		 file = new File(fileName);
		 bufferedImage = ImageIO.read(file);
		 this.title = title;
	  }
	  catch (IOException ex)
	  {
		 System.out.println("Error reading the file " + fileName + ".");
		 System.exit(1);
	  }
   }

   public Picture(int width, int height)
   {
	  bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
	  for (int x = 0; x < width; x++)
		 for (int y = 0; y < height; y++)
			setPixel(x,y,0);
   }

   /**
	Returns the width of the image.

	@return  Returns the width of the image.
   */
   public int getWidth()
   {
	  return bufferedImage.getWidth();
   }

   /**
	Returns the height of the image.

	@return Returns the height of the image.
   */
   public int getHeight()
   {
	  return bufferedImage.getHeight();
   }

   /**
	Returns the Alpha-Red-Green-Blue (ARGB) value for 
	the pixel at (x,y).

	@param x An x coordinate in the image.
	@param y An y coordinate in the image.
	@return  Returns the ARGB value for the pixel at (x,y).
   */
   public int getPixel(int x, int y)
   {
	  return bufferedImage.getRGB(x,y);
   }

   /**
	Returns the Alpha value for the pixel at (x,y). 

	@param x  An x coordinate in the image.
	@param y  An y coordinate in the image.
	@return   Returns the alpha value for the pixel at (x,y).
   */
   public int getAlpha(int x, int y)
   {
	  int rgb = bufferedImage.getRGB(x,y);

	  return (rgb >> 24) & 0xFF;
   }

   /**
	Returns the Red value for the pixel at (x,y).

	@param x An x coordinate in the image.
	@param y An y coordinate in the image.
	@return  Returns the red value for the pixel at (x,y).
   */
   public int getRed(int x, int y)
   {
	  int rgb = bufferedImage.getRGB(x,y);

	  return (rgb >> 16) & 0xFF;
   }

   /**
	Returns the Green value for the pixel at (x,y).

	@param x An x coordinate in the image.
	@param y An y coordinate in the image.
	@return  Returns the green value for the pixel at (x,y).
   */
   public int getGreen(int x, int y)
   {
	  int rgb = bufferedImage.getRGB(x,y);

	  return (rgb >> 8) & 0xFF;
   }

   /**
	Returns the Blue value for the pixel at (x,y).

	@param x An x coordinate in the image.
	@param y An y coordinate in the image.
	@return  Returns the blue value for the pixel at (x,y).
   */
   public int getBlue(int x, int y)
   {
	  int rgb = bufferedImage.getRGB(x,y);

	  return rgb & 0xFF;
   }

   /**
	Sets the Alpha-Red-Green-Blue value for the pixel at (x,y).

	@param x An x coordinate in the image.
	@param y An y coordinate in the image.
	@param value The ARGB value for the pixel at (x,y).
   */
   public void setPixel(int x, int y, int value)
   {
	  bufferedImage.setRGB(x,y,value);
   }

   /**
	Sets the Alpha value for the pixel at (x,y).

	@param x An x coordinate in the image.
	@param y An y coordinate in the image.
	@param value The alpha value for the pixel at (x,y).
   */
   public void setAlpha(int x, int y, int value)
   {
	  int rgb = bufferedImage.getRGB(x,y);

	  rgb = (rgb & 0xFFFFFF) | (value << 24);
	  bufferedImage.setRGB(x,y,rgb);
   }

   /**
	Sets the Red value for the pixel at (x,y).

	@param x An x coordinate in the image.
	@param y An y coordinate in the image.
	@param value The red value for the pixel at (x,y).
   */
   public void setRed(int x, int y, int value)
   {
	  int rgb = bufferedImage.getRGB(x,y);

	  rgb = (rgb & 0xFF00FFFF) | (value << 16);
	  bufferedImage.setRGB(x,y,rgb);
   }

   /**
	Sets the Green value for the pixel at (x,y).

	@param x An x coordinate in the image.
	@param y An y coordinate in the image.
	@param value The green value for the pixel at (x,y).
   */
   public void setGreen(int x, int y, int value)
   {
	  int rgb = bufferedImage.getRGB(x,y);

	  rgb = (rgb & 0xFFFF00FF) | (value << 8);
	  bufferedImage.setRGB(x,y,rgb);
   }

   /**
	Sets the Blue value for the pixel at (x,y).

	@param x An x coordinate in the image.
	@param y An y coordinate in the image.
	@param value The blue value for the pixel at (x,y).
   */
   public void setBlue(int x, int y, int value)
   {
	  int rgb = bufferedImage.getRGB(x,y);

	  rgb = (rgb & 0xFFFFFF00) | value;
	  bufferedImage.setRGB(x,y,rgb);
   }

   /**
	Displays the image in a separate window.
   */
   public void displayImage()
   {
	  JFrame frame = new JFrame();
	  ImageIcon imageIcon = new ImageIcon();
	  JLabel label = new JLabel(imageIcon);

	  imageIcon.setImage(bufferedImage);
	  frame.getContentPane().add(label);
	  frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
	  frame.pack();
	  
	  frame.setTitle(this.title);

	  frame.setVisible(true);
   }

   public void saveImage(String fileName)
   {
	  try
	  {
		 File file = new File(fileName);

		 ImageIO.write(bufferedImage,"png",file);
	  }
	  catch (Exception ex)
	  {
		 System.out.println("Save error");
		 System.exit(1);
	  }
   }
}

Was This Post Helpful? 0
  • +
  • -

#9 letthecolorsrumble  Icon User is offline

  • Student of The Sun
  • member icon

Reputation: 27
  • View blog
  • Posts: 555
  • Joined: 07-November 07

Re: Mirror an Image

Posted 04 March 2008 - 06:54 PM

I came up with something... don't know if it works because couldn't test it yet. Thought of first posting it.

So here goes:
public static void horizontalMirror(Picture p) {

		for( int i=0, k=p.getWidth()-1;  i != k; i++,k--) {
			for (int j = 0 ,l = p.getHeight()-1; j != l ; j++,l--) {		
				int x = p.getPixel(i,j);  //get the pixel above the horizontal axis
				int y = p.getPixel(k, l); //get the symmetrical pixel below the axis
				//exchange the pixel values
p.setPixel(i, j, y);  
				p.setPixel(k, l, x);
			}			
		}
	}



Tell me if it worked. I will be testing it now.

Oops that will just turn the image by 180 ... lol or something even worse :)

New version:
public static void horizontalMirror(Picture p) {

		for( int i=0, k = 0; i < p.getWidth(); i++, k++) {
			for (int j = 0 ,l = p.getHeight()-1; j != l ;j++, l--) {		
				int x = p.getPixel(i, j);   //get the pixel above the horizontal axis
				int y = p.getPixel(k, l);  //get symmetrical pixel below the horizontal axis
				//exchange the pixels' ARGB values.
p.setPixel(i, j, y);
				p.setPixel(k, l, x);
			}			
		}
	}



This should work.

This post has been edited by letthecolorsrumble: 04 March 2008 - 07:25 PM

Was This Post Helpful? 2

#10 CompSciForum854372  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 30
  • Joined: 26-February 08

Re: Mirror an Image

Posted 04 March 2008 - 07:30 PM

That works but it just turns the picture upside down
and i need it to reflect over the horizontal axis

would it work to possibly make the height half when defining it and then repeat it?
Was This Post Helpful? 0
  • +
  • -

#11 letthecolorsrumble  Icon User is offline

  • Student of The Sun
  • member icon

Reputation: 27
  • View blog
  • Posts: 555
  • Joined: 07-November 07

Re: Mirror an Image

Posted 04 March 2008 - 07:38 PM

Oh I might have misunderstood the "horizontal axis". You just need to change the setting in the loop and you will be done.

public static void horizontalMirror(Picture p) {

		for( int i = 0, k = p.getWidth()-1; i != k; i++, k--) {
			for (int j = 0 ,l = 0; j < p.getHeight() ;j++, l++) {		
				int x = p.getPixel(i, j);
				int y = p.getPixel(k, l);
				p.setPixel(i, j, y);
				p.setPixel(k, l, x);
			}			
		}
	}



try this one.

This post has been edited by letthecolorsrumble: 04 March 2008 - 07:44 PM

Was This Post Helpful? 0
  • +
  • -

#12 CompSciForum854372  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 30
  • Joined: 26-February 08

Re: Mirror an Image

Posted 04 March 2008 - 07:46 PM

it didnt seem to work
it said the coordinates were out of data base,
I'm trying to debug it but have come up empty so far

This post has been edited by Lambeau86: 04 March 2008 - 08:29 PM

Was This Post Helpful? 0
  • +
  • -

#13 letthecolorsrumble  Icon User is offline

  • Student of The Sun
  • member icon

Reputation: 27
  • View blog
  • Posts: 555
  • Joined: 07-November 07

Re: Mirror an Image

Posted 04 March 2008 - 08:10 PM

Did you check the first for-loop for( int i = 0, k = p.getWidth()-1; i != k; i++, k--) . It works when I tested it.

or

for( int i = 0, k = p.getWidth()-1; i < k; i++, k--)

This post has been edited by letthecolorsrumble: 04 March 2008 - 08:13 PM

Was This Post Helpful? 0
  • +
  • -

#14 letthecolorsrumble  Icon User is offline

  • Student of The Sun
  • member icon

Reputation: 27
  • View blog
  • Posts: 555
  • Joined: 07-November 07

Re: Mirror an Image

Posted 04 March 2008 - 08:21 PM

So the final version would be :

	public static void horizontalMirror(Picture p) {
		//for even width pixels
		if(p.getWidth()%2 == 0) {
			
			for( int i = 0, k = p.getWidth()-1; i < k; i++, k--) {
				for (int j = 0 ,l = 0; j < p.getHeight() ;j++, l++) {		
					int x = p.getPixel(i, j);
					int y = p.getPixel(k, l);
					p.setPixel(i, j, y);
					p.setPixel(k, l, x);
				}			
			}
		}
		//for odd width pixels
		else {
			for( int i = 0, k = p.getWidth()-1; i != k; i++, k--) {
				for (int j = 0 ,l = 0; j < p.getHeight() ;j++, l++) {		
					int x = p.getPixel(i, j);
					int y = p.getPixel(k, l);
					p.setPixel(i, j, y);
					p.setPixel(k, l, x);
				}			
			}
		}		
	}




Glad I could help! :)
Was This Post Helpful? 1
  • +
  • -

#15 CompSciForum854372  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 30
  • Joined: 26-February 08

Re: Mirror an Image

Posted 04 March 2008 - 09:34 PM

got it to work thanks for you're help

for( int i=0, k = 0; i < p.getWidth(); i++, k++) {  
	for (int j = 0 ,l = p.getHeight()-1; j != l ;j++, l--) {          
			                 
			int x = p.getPixel(i, j);   
			 int y = p.getPixel(k, l);    
			                 
			 p.setPixel(k, l, y);
			 p.setPixel(k, l, x);  


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1