Results 1 to 4 of 4
  1. #1
    jude87 is offline Member
    Join Date
    Feb 2009
    Posts
    3
    Rep Power
    0

    Exclamation can someone help me extend my code so that it can enhance grey scale images

    Hi,
    can someone help me extend my code so that it can be used enhance grey scale images using the histogram equalisation technique.I have been using this code for manipulating the mean, the median, enhancing the grey scale ,histogram of an image.But i am stack.i want to use it to enhance grey scale images using the histogram equalisation technique.....

    Java Code:
    import java.io.*;
    
    public class JPEGHist {
        /*
          Example of how to use the JPEGImage class
         */
    
        public static void main (String args[]) {
    	/* Check that the user has provided the right number of arguments */
    	if (args.length != 2) {
    	    System.out.println("Usage: java JPEGCopy <source JPEG file> " + 
    			       "<target JPEG file>");
    	    System.exit(1);
    	}
    
    	/* Create an empty image. We will read an image from a file into
    	   this object */
    	JPEGImage imageOne = new JPEGImage();
    
    	/* Try to open the file. This may cause an exception if the name 
    	   given is not a valid JPEG file so we need to catch the exceptions */
    	try {
    	    imageOne.read(args[0]);
    	} catch (Exception e) {
    	    /* An exception has been thrown. This is usually because the file
    	       either does not exist, or is not a JPEG image */
    	    System.out.println("Error reading file " + args[0]);
    	    System.out.println(e.getMessage());
    	    System.exit(1);
    	}
    
    	/* Make a new image the same size as the one that was read in */
    	JPEGImage imageTwo = new JPEGImage(256,
    					   350);
    
            int greyvalue[]= new int[256];
    
    
    	/* Copy the pixel information from image that was read in to the 
    	   new image */
    	for (int x = 0; x < imageOne.getWidth(); x++) {
    	    for (int y = 0; y < imageOne.getHeight(); y++) {
    		/* Get the values from imageOne */
    		int red = imageOne.getRed(x,y);
    		int green = imageOne.getGreen(x,y);
    		int blue = imageOne.getBlue(x,y);
    
                    int grey =(int)(red + green + blue)/3;
    
    		/* Put these values into imageTwo */
    		//imageTwo.setRGB(x, y, grey, grey, grey);
    
                    greyvalue [grey]+=1;
    
    	    }
    	}
            //find max value of grey
            int maxvalue = greyvalue[0];
            for (int i = 0; i < 256; i++){
                if (greyvalue[i] > maxvalue)
                    maxvalue = greyvalue[i];
            }
            //white image
            for (int j=0; j < 256; j++){
                for (int k=0; k < 350; k++){
                   imageTwo.setRGB(j, k, 255, 255, 255);
                }
            }
            //draw histogram
            for (int m=0; m <256; m++){
                double height = 256*((double)greyvalue[m]/maxvalue);
                for (int n=(int)height-1; n>0; n--){
                    imageTwo.setRGB(m, 350-n, 0, 0, 0);
                }
            }
    
    	/* Write the new image out to a file. Again exceptions might occur */
    	try {
    	    imageTwo.write(args[1]);
    	} catch (Exception e) {
    	    System.out.println("Error writing file " + args[1]);
    	    System.out.println(e.getMessage());
    	    System.exit(1);
    	}
    
        }
    }
    Moderator edit: code tags added
    Last edited by Fubarable; 04-06-2010 at 01:01 AM. Reason: Moderator edit: code tags added

  2. #2
    Webuser is offline Senior Member
    Join Date
    Dec 2008
    Posts
    526
    Rep Power
    0

    Default

    could provide us the problem code block?

  3. #3
    jude87 is offline Member
    Join Date
    Feb 2009
    Posts
    3
    Rep Power
    0

    Default

    import com.sun.image.codec.jpeg.*;
    import java.awt.image.*;
    import java.io.*;

    /**
    <p>
    This class provides basic image manupulation routines.
    This class is essentially a wrapper around a <tt>BufferedImage</tt>, and
    provides simple methods for reading and writing images in JPEG format,
    accessing pixel values, and creating new images.
    </p>
    <p>
    There is no range checking either on image coordinates or on pixel
    colour values. Entering values outside of the allowed ranges may
    generate runtime exceptions, or result in garbled images.
    </p>

    @author <a href="mailto:smx@cs.nott.ac.uk">Steven Mills</a>
    @version 1.0 (Jan 8 2003)
    */
    public class JPEGImage {

    private BufferedImage img;


    /**
    Default image constructor.

    Creates an empty JPEGImage. Since this object does not actually
    contain an image, any attempts to access the image (through get
    or set methods or write) will result in runtime exceptions.

    */
    public JPEGImage() {

    }

    /**
    Blank image constructor.

    Creates a new image with the given dimensions, and
    all pixels in the image are set to be black. The dimensions
    should be positive integers.

    @param width the width of the image in pixels
    @param height the height of the image in pixels
    */
    public JPEGImage(int width, int height) {
    img = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
    for (int x = 0; x < width; x++) {
    for (int y = 0; y < height; y++) {
    img.setRGB(x,y,0xff000000);
    }
    }
    }

    /**
    Reads a JPEG from a file into an existing JPEGImage.

    Reading from a file
    that does not exist, or which does not contain a JPEG image will
    generate an exception

    @param filename the file containing the image
    @throws IOException indicates a general problem reading the file
    @throws ImageFormatException indicates that the file is not a JPEG image
    */
    public void read(String filename)
    throws IOException, ImageFormatException {
    InputStream istream = new FileInputStream(filename);
    JPEGImageDecoder jpegDec = JPEGCodec.createJPEGDecoder(istream);
    img = jpegDec.decodeAsBufferedImage();
    }

    /**
    Writes an image to a file in JPEG format.

    Writing to a file that does not exist, or which is protected
    will generate an exception

    @param filename the file to write the image to
    @throws IOException indicates a general problem reading the file
    */
    public void write(String filename)
    throws IOException {
    OutputStream ostream = new FileOutputStream(filename);
    JPEGImageEncoder jpegEnc = JPEGCodec.createJPEGEncoder(ostream);
    jpegEnc.encode(img);
    }

    /**
    Returns the height of the JPEGImage.

    @return the height of the image in pixels
    */
    public int getHeight() {
    return img.getHeight();
    }

    /**
    Returns the width of the JPEGImage.

    @return the width of the image in pixels
    */
    public int getWidth() {
    return img.getWidth();
    }

    /**
    Returns the red value of the image at the given coordinates.

    The coordinates should be non-negative and less than the width (x)
    or height (y) of the image. The red value is returned as an
    integer in the range [0,255].

    @param x the horizontal coordinate of the pixel
    @param y the vertical coordinate of the pixel
    @return the red value at the given coordinates
    */
    public int getRed(int x, int y) {
    return (img.getRGB(x, y) & 0x00ff0000) >> 16;
    }

    /**
    Returns the green value of the image at the given coordinates.

    The coordinates should be non-negative and less than the width (x)
    or height (y) of the image. The green value is returned as an
    integer in the range [0,255].

    @param x the horizontal coordinate of the pixel
    @param y the vertical coordinate of the pixel
    @return the green value at the given coordinates
    */
    public int getGreen(int x, int y) {
    return (img.getRGB(x, y) & 0x0000ff00) >> 8;
    }

    /**
    Returns the blue value of the image at the given coordinates.

    The coordinates should be non-negative and less than the width (x)
    or height (y) of the image. The blue value is returned as an
    integer in the range [0,255].

    @param x the horizontal coordinate of the pixel
    @param y the vertical coordinate of the pixel
    @return the blue value at the given coordinates
    */
    public int getBlue(int x, int y) {
    return (img.getRGB(x, y) & 0x000000ff);
    }

    /**
    Sets the red value of the image at the given coordinates.

    The coordinates should be non-negative and less than the width (x)
    or height (y) of the image. The red value should be an
    integer in the range [0,255].

    @param x the horizontal coordinate of the pixel
    @param y the vertical coordinate of the pixel
    @param value the new red value at the given coordinates
    */
    public void setRed(int x, int y, int value) {
    img.setRGB(x, y, (img.getRGB(x, y) & 0xff00ffff) | (value << 16));
    }

    /**
    Sets the green value of the image at the given coordinates.

    The coordinates should be non-negative and less than the width (x)
    or height (y) of the image. The green value should be an
    integer in the range [0,255].

    @param x the horizontal coordinate of the pixel
    @param y the vertical coordinate of the pixel
    @param value the new green value at the given coordinates
    */
    public void setGreen(int x, int y, int value) {
    img.setRGB(x, y, (img.getRGB(x, y) & 0xffff00ff) | (value << 8));
    }

    /**
    Sets the blue value of the image at the given coordinates.

    The coordinates should be non-negative and less than the width (x)
    or height (y) of the image. The blue value should be an
    integer in the range [0,255].

    @param x the horizontal coordinate of the pixel
    @param y the vertical coordinate of the pixel
    @param value the new blue value at the given coordinates
    */
    public void setBlue(int x, int y, int value) {
    img.setRGB(x, y, (img.getRGB(x, y) & 0xffffff00) | value);
    }

    /**
    Sets the red, green, and blue values of the image at the
    given coordinates.

    The coordinates should be non-negative and less than the width (x)
    or height (y) of the image. The colour values should be
    integers in the range [0,255].

    @param x the horizontal coordinate of the pixel
    @param y the vertical coordinate of the pixel
    @param r the new red value at the given coordinates
    @param g the new green value at the given coordinates
    @param b the new blue value at the given coordinates
    */
    public void setRGB(int x, int y, int r, int g, int b) {
    img.setRGB(x, y, 0xff000000 | (r << 16) | (g << 8) | b);
    }

    }

  4. #4
    Webuser is offline Senior Member
    Join Date
    Dec 2008
    Posts
    526
    Rep Power
    0

    Default

    Oh, it is too long as for algorythm problem ;)
    What exactly you want to achieve? Grey color or what?

Similar Threads

  1. Replies: 3
    Last Post: 03-03-2011, 05:59 PM
  2. How to extend two classes
    By man4ish in forum AWT / Swing
    Replies: 4
    Last Post: 12-31-2009, 03:31 PM
  3. Trying to extend class
    By ribbs2521 in forum New To Java
    Replies: 4
    Last Post: 10-29-2009, 07:28 PM
  4. using J-classes or extend them ?
    By itaipee in forum AWT / Swing
    Replies: 1
    Last Post: 01-01-2009, 08:58 PM
  5. Grey Box
    By mja229 in forum New To Java
    Replies: 2
    Last Post: 12-24-2008, 08:24 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •