5 Replies - 1762 Views - Last Post: 22 February 2010 - 07:54 PM Rate Topic: -----

#1 AllboutJava  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 19-February 10

Processing Image

Posted 21 February 2010 - 09:33 PM

Hi guys,

I tried to compute a contrast image with a scale all value by a constant: g'(x,y) = g(x,y) * k

Here is my code; but it doesn't seem run correctly - Can anyone trace what I did wrong?

Cheers

import java.io.*;             //these 6 sentences are for including packages to be used in the program
import java.awt.image.*;
import java.awt.color.ColorSpace;
import javax.swing.*;
import com.pearsoneduc.ip.io.*;
import com.pearsoneduc.ip.gui.*;

public class BrightnessXImage extends JFrame {

    private BufferedImage sourceImage;   // image to be inverted
    private ImageView[] views;           // image display components

    public BrightnessXImage(String imageFile) throws IOException, ImageDecoderException {
        //call the constructor of the "super class" which in this case is JFrame
        //this can be seen at the beginning of the class after the keyword extends
        //the parameter passed to the constructor will be diplayed in the JFrame
        //window title bar
        super("xBrightness: " + imageFile);
        readImage(imageFile);    //call readImage() method (see details later)

        views = new ImageView[2];      //instantiate 2  display windows, this is the place that you can
        //change, to suit your own need. eg, if need more windows
        views[0] = new ImageView(sourceImage);     //display the source image in the first window
        views[1] = new ImageView(xBrightnessInPlace(sourceImage)); //display the output image after
        //InvertXReverse processing

        JTabbedPane tabbedPane = new JTabbedPane();      //instantiate a TabbedPane
        tabbedPane.add(new JScrollPane(views[0]), "inputImage");   //put label that associate to the first display
        //window
        tabbedPane.add(new JScrollPane(views[1]), "Brightness");

        getContentPane().add(tabbedPane);
        addWindowListener(new WindowMonitor());
    }
  /*
   * Read an image from a named file, converting
   * to greyscale if necessary.
   */
    public void readImage(String filename)
    throws IOException, ImageDecoderException {
        ImageDecoder input = ImageFile.createImageDecoder(filename);   //read data from a file
        sourceImage = input.decodeAsBufferedImage();    //decode the data into BufferedImage
        //represenation
        if (sourceImage.getType() != BufferedImage.TYPE_BYTE_GRAY) {  //check image type
            System.err.println("Converting colour image to greyscale image...");
            ColorConvertOp op = new ColorConvertOp(     //convert colour image to grey level
                    ColorSpace.getInstance(ColorSpace.CS_GRAY), null);
            BufferedImage greyImage = op.filter(sourceImage, null);
            sourceImage = greyImage;
        }
    }
  /*
   * Compute and return a inverted version of the source image
   */
    public BufferedImage xBrightnessInPlace(BufferedImage image) {
        //instantiate a new image object
        BufferedImage brighterImage = new BufferedImage(image.getWidth(),image.getHeight(),image.getType());

        //step through the original image pixel by pixel (from up to down
        //and set each pixel of the new brighterImage
        //to an exact copy of the original
        for (int y=0; y<image.getHeight(); ++y) {
            for (int x=0; x<image.getWidth(); ++x) {
                //here is to save the original image
                brighterImage.setRGB(x,y, image.getRGB(x,y));
            }
        }
        int w = brighterImage.getWidth();
        int h = brighterImage.getHeight();


        for (int x=0; x<h; ++x) {
            for (int y=0; y<w; ++y){
                //swap value at (,x) with its invert image()

                int value = (int)(brighterImage.getRGB(x,y)*1.5);    //see how to get a pixel value;
                if (value > 255);
                //brighterImage.setRGB(x,y,brighterImage.getRGB(w-x-1,y));
                brighterImage.setRGB(x,y,value);
            }
        }
        return brighterImage;
    }
    public static void main(String[] argv) {
        if (argv.length > 0) {

            try {
                JFrame frame = new BrightnessXImage(argv[0]);

                frame.pack();
                frame.setVisible(true);
            } catch (Exception e) {
                System.err.println(e);
                System.exit(0);
            }
        } else {
            System.err.println("usage: Java Brightness Image <imagefile> ");
            System.exit(1);
        }
    }
}



Is This A Good Question/Topic? 0
  • +

Replies To: Processing Image

#2 AllboutJava  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 19-February 10

Re: Processing Image

Posted 21 February 2010 - 10:07 PM

Sorry guys, just some amended - the word "brighterImage" should be "contrastImage" - it's just I am mislead between contrast (g'(x,y) = g(x,y) * k) and brightness (g'(x,y) = g(x,y) + k) as both almost the same formula;

But I would like to find the contrast of the image from the code I given - I hope someone can assist me.
Thanks in advance.
Aramatii
Was This Post Helpful? 0
  • +
  • -

#3 javabie  Icon User is offline

  • D.I.C Head

Reputation: 36
  • View blog
  • Posts: 117
  • Joined: 12-February 10

Re: Processing Image

Posted 21 February 2010 - 10:33 PM

From experience.....

when you call x.getRGB(), it returns a int value which is not between 0 to 255.

Try printing out the first 5 values and you will see. It outputs random big integers sumthing like.. -234532
which makes no sense at all. So, if you multiply it by 1.5.. its gonna produce another random useless number.
So instead, what you need to do is... copy a method from java website ;)

following method could be found here:
http://java.sun.com/...xelGrabber.html

and modifying the method we copied from that website, we get the following:

	// pixel - the value that was returned by image.getRGB()
	// bLevel - contrast value... 
	public int handlesinglepixel(int pixel, int bLevel) {
		//don't modify anythign below....
		//extract the alpha, red, green and blue values from the
		//pixel that was passed into this method.
		int alpha = (pixel >> 24) & 0xff;
		int red   = (pixel >> 16) & 0xff;
		int green = (pixel >>  8) & 0xff;
		int blue  = (pixel      ) & 0xff;
		// Deal with the pixel as necessary...
		
		//start modifying
		//
		alpha = alpha * bLevel;
		red = red * bLevel;		
		green = green * bLevel;
		blue = blue * bLevel;
		
		if (alpha > 255)
			alpha = 255; //you might want to change this.
		if (red > 255)
			red = 255;
		if (green > 255)
			green = 255;
		if (blue > 255)
			blue = 255;
			
		//don't modify anything below...
		//put all those alpha, red, green, blue into 1 integer
		return ((alpha<< 24)|(red << 16)|(green << 8)|blue);

	}




and we could use the above method like so...

	public BufferedImage xBrightnessInPlace(BufferedImage image) {
		int w = brighterImage.getWidth();
		int h = brighterImage.getHeight();

		//instantiate a new image object
		BufferedImage brighterImage = new BufferedImage(w, h,image.getType());

		//step through the original image pixel by pixel (from up to down
		//and set each pixel of the new brighterImage
		//to an exact copy of the original
		for (int y=0; y< h; ++y) {
			for (int x=0; x<w; ++x) {
				float val = handlesinglepixel(image.getRGB(x, y), 1.5)
				brighterImage.setRGB(x,y, val);
			}
		}


		return brighterImage;
	}



AGAIN, since I can't run it, I hope I am not pointing you in the wrong direction.
I can't run your code because you are using special packages that are not available to me such as...
import com.pearsoneduc.ip.io.*;
import com.pearsoneduc.ip.gui.*;

so.. try them out.. if it doesn't work.. we will try to make it work.

goodluck.
Was This Post Helpful? 0
  • +
  • -

#4 javabie  Icon User is offline

  • D.I.C Head

Reputation: 36
  • View blog
  • Posts: 117
  • Joined: 12-February 10

Re: Processing Image

Posted 22 February 2010 - 12:09 AM

If you copied the above code, you would prolly have an error... becuz i forgot about double and int....

so here is the revised version...

public int hsp(int pixel, double bLevel) {
		//don't modify anything below....
		//extract the alpha, red, green and blue values from the
		//pixel that was passed into this method.
		int alpha = (pixel >> 24) & 0xff;
		int red   = (pixel >> 16) & 0xff;
		int green = (pixel >>  8) & 0xff;
		int blue  = (pixel      ) & 0xff;
		// Deal with the pixel as necessary...

		//start modifying
		alpha = (int)(alpha * bLevel);
		red = (int)(red * bLevel);             
		green = (int)(green * bLevel);
		blue = (int)(blue * bLevel);

		if (alpha > 255)
			alpha = 255; //you might want to change this.
		if (red > 255)
			red = 255;
		if (green > 255)
			green = 255;
		if (blue > 255)
			blue = 255;

		//don't modify anything below...
		//put all those alpha, red, green, blue into 1 integer
		return ((alpha<< 24)|(red << 16)|(green << 8)|blue);

	}


Was This Post Helpful? 0
  • +
  • -

#5 AllboutJava  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 19-February 10

Re: Processing Image

Posted 22 February 2010 - 11:05 AM

View Postjavabie, on 21 February 2010 - 11:09 PM, said:

If you copied the above code, you would prolly have an error... becuz i forgot about double and int....

so here is the revised version...

public int hsp(int pixel, double bLevel) {
		//don't modify anything below....
		//extract the alpha, red, green and blue values from the
		//pixel that was passed into this method.
		int alpha = (pixel >> 24) & 0xff;
		int red   = (pixel >> 16) & 0xff;
		int green = (pixel >>  8) & 0xff;
		int blue  = (pixel      ) & 0xff;
		// Deal with the pixel as necessary...

		//start modifying
		alpha = (int)(alpha * bLevel);
		red = (int)(red * bLevel);             
		green = (int)(green * bLevel);
		blue = (int)(blue * bLevel);

		if (alpha > 255)
			alpha = 255; //you might want to change this.
		if (red > 255)
			red = 255;
		if (green > 255)
			green = 255;
		if (blue > 255)
			blue = 255;

		//don't modify anything below...
		//put all those alpha, red, green, blue into 1 integer
		return ((alpha<< 24)|(red << 16)|(green << 8)|blue);

	}



Hi thanks for the ideas; Yes i did run them and found the error either int or float. So what I did is I placed like this in xBrightnessInPlace method:

for (int y = 0; y < h; ++y) {
            for (int x = 0; x < w; ++x) {
                float val = handlesinglePixel(image.getRGB(x, y),1.5);
                brighterImage.setRGB(x, y,(int) val);
            }
        } 


And I was wonder if we can place all the code which is in hsp method put in XBrightnessInPlace method instead? So instead of having two method we only have one. How I can do that?

Cheers
Was This Post Helpful? 0
  • +
  • -

#6 javabie  Icon User is offline

  • D.I.C Head

Reputation: 36
  • View blog
  • Posts: 117
  • Joined: 12-February 10

Re: Processing Image

Posted 22 February 2010 - 07:54 PM

Personally, i prefer to have it as a method. Why? Because if I were to read over my code, I wouldn't have so much things happening in the for-loop. I know hsp method will handle all those. But anyway, its easy to make it into one.

		double bLevel = 2.9;
		for (int x=0; x<h; ++x) 
		{
			for (int y=0; y<w; ++y)
			{
				int pixel = image.getRGB(x, y); 
				//copy and paste the method here... you should know which lines to omit and which to keep.
                               
                                pixel = ((alpha<< 24)|(red << 16)|(green << 8)|blue); //we don't need the return statement but instead
                                                      //assign it to the pixel
				brighterImage.setRGB(x,y,(int)pixel); //set the pixel here...
                        }
               }



Was This Post Helpful? 0
  • +
  • -

Page 1 of 1