Contrast control with java Buffered image class..

11 07 2011

Hi, in this post I’m going to tell you how to change the contrast of an buffered image. And it will be very easy to you if you read my previous post on Brightness control with java Buffered image class... Here I’m going to explain only the key methods and others will be same as Brightness control.

In this program also here are two methods I’ve written.

1). public void rescale() {}

2). private void contrastChange() {}

Let’s consider them, one by one

An Image before change the Contrast...

        public void rescale() {

        rescale = new RescaleOp(scaleFactor,20.0f, null);
bufferedImage=rescale.filter(bufferedImage,null);//(sourse,destination)

       }//end rescale
In here, I’ve created RescaleOp object, and should pass parameters as in order sacleFactors, offsets,  hints .

By changing offsets value we can adjust the brightness of an image, and by changing sacleFactors value we can adjust contrast of an image and hints for the specified RenderingHints or can be kept as null. In my program scaleFactor is set to 1.0 in float for default image and for enhanced image it is set to 3.6 in float.

An Image after increasing the Contrast...

private void contrastChange(){

         rescale();
         icon = new ImageIcon(bufferedImage);          
         picLabel.setIcon(icon);
         System.out.println(“scaleFactor : “+scaleFactor  );

    }//end  contrastChange()

In the filter method filter(sourse_bufferedImage, destination_bufferedImage); should be given as like this, and it returns filtered buffered image. Then in the contrastChange() the rescale() method is called and as I mentioned in my previous post, the buffered image is printed on a JLabel. :D

Here is the source code of it and if you want you can DOWNLOAD my Netbeans project. And it has two classes Contrast.java and Main.java which is included the main method.

Main.java

/*

* Main.java

*/
package contrast;

/**
 *
 * @author gihan
 */
public class Main {
     public static void main(String[] args) {
        Contrast obj=new Contrast();
    }//end main()

}//end class Main

Contrast.java

/*
 * Contrast.java
 */
package contrast;

import java.awt.image.BufferedImage;
import java.awt.image.RescaleOp;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

/**
 *
 * @author gihan
 */
public class Contrast extends JFrame{
       //globel variables
        BufferedImage bufferedImage;
        String path=”/home/gihan/Pictures/nfs_bmw.jpg”;// image file path     
        float scaleFactor=3.6f;//change scaleFactor to change contrast        
        /*keep the value scaleFactor = 1.0f; as for a normal image*/
        RescaleOp rescale;
        ImageIcon icon;
        JLabel picLabel=new JLabel();

     public Contrast() {

         JFrame jf=new JFrame();
         JPanel jp=new JPanel();        

         jf.add(jp);
         jp.add(picLabel);

            jf.setVisible(true);
            jf.setSize(740, 487);
            jf.setLocation(200,100);
            jf.setTitle(“Gihan’s Image Processing Test Area.. “);
            jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

            File file = new File(path);

        try {           
            bufferedImage = ImageIO.read(file); // create a buffered image
        } catch (IOException ex) {
            Logger.getLogger(Contrast.class.getName()).log(Level.SEVERE, null, ex);
        }

         contrastChange();   
         icon = new ImageIcon(bufferedImage);          
         picLabel.setIcon(icon);       

    }

     private void contrastChange(){

         rescale();
         icon = new ImageIcon(bufferedImage);          
         picLabel.setIcon(icon);
         System.out.println(“scaleFactor : “+scaleFactor  );

    }//contrastChange()

     public void rescale() {

        rescale = new RescaleOp(scaleFactor,20.0f, null);
        bufferedImage=rescale.filter(bufferedImage,null);//(sourse,destination)

       }//end rescale

}//end class contrast

If you have any doubt, feel free to ask. :D
Thank you

Gihan Malan De Silva @ gihansblog.wordpress.com


Actions

Information

One response

22 07 2011
car games for kids

Admirable blog! I’ll almost certainly be referencing some of this facts in my next speech. I would appreciate it should you visited my blog at

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s




%d bloggers like this: