Tuesday, December 18, 2012
Today I am going to post how you can convert any image to gray scale in Java. First the image is read from a file using javax.imageio.ImageIO class as a BuffertedImage. A function is written that takes in a BufferedImage as parameter and returns a color converted image. An instance of java.awt.image.ColorConvertOp is created which takes in an object of java.awt.color.ColorSpace as first parameter and null as 2nd parameter( since there is no RenderingHints). Then the filter(src,dest) method is called which actually does the work. Then the final image is written to a file in jpeg format as dest.jpg .
How ColorCovertOp works : This class performs a pixel-by-pixel color conversion of the data in the source image. The resulting color values are scaled to the precision of the destination image. Color conversion can be specified via an array of ColorSpace objects or an array of ICC_Profile objects.
ColorSpace class : This is an abstract class whose getInstance(int) method is called to get an instance and CS_GRAY is passed as parameter which defines a built in linear gray scale color space.
Screenshots of images are shown below
--------------------------------------------------------------------------------------------------------------------------
import java.io.File;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.color.ColorSpace;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.image.BufferedImage;
import java.awt.image.ColorConvertOp;
import javax.imageio.ImageIO;
@SuppressWarnings("serial")
public class GrayScale extends JPanel{
@Override
public void paintComponent(Graphics g){
Graphics2D g2d=(Graphics2D)g;
try{ //reading from file
BufferedImage src=ImageIO.read(new File("src.jpg"));
BufferedImage dest=convertToGray(src); //getting destination image
g2d.drawImage(dest,0,0,this); /* drawing the image on panel and then writing to a file */
ImageIO.write(dest,"jpeg",new File("dest.jpg"));
}catch(Exception e){
e.printStackTrace();
}
}
public BufferedImage convertToGray(BufferedImage src){
//creating instance and using in-built linear gray scale
ColorConvertOp grayOp = new ColorConvertOp(
ColorSpace.getInstance(ColorSpace.CS_GRAY), null);
return grayOp.filter(src,null); //converting to gray scale
}
public static void main (String[] args) {
JFrame jf=new JFrame("GRAY_SCALE");
GrayScale obj=new GrayScale();
jf.getContentPane().add(obj);
jf.setVisible(true);
jf.setSize(320,235);
jf.setDefaultCloseOperation(jf.EXIT_ON_CLOSE);
}
}
--------------------------------------------------------------------------------------------------------------------------
DOWNLOAD the source from 4shared
--------------------------------------------------------------------------------------------------------------------------
Reflect or flip an image
Change brightness of image using RescaleOp
Java program to draw partially transparent image using Graphics2D
How ColorCovertOp works : This class performs a pixel-by-pixel color conversion of the data in the source image. The resulting color values are scaled to the precision of the destination image. Color conversion can be specified via an array of ColorSpace objects or an array of ICC_Profile objects.
ColorSpace class : This is an abstract class whose getInstance(int) method is called to get an instance and CS_GRAY is passed as parameter which defines a built in linear gray scale color space.
Screenshots of images are shown below
Original image |
Gray Scale image |
--------------------------------------------------------------------------------------------------------------------------
Java Source Code
--------------------------------------------------------------------------------------------------------------------------import java.io.File;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.color.ColorSpace;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.image.BufferedImage;
import java.awt.image.ColorConvertOp;
import javax.imageio.ImageIO;
@SuppressWarnings("serial")
public class GrayScale extends JPanel{
@Override
public void paintComponent(Graphics g){
Graphics2D g2d=(Graphics2D)g;
try{ //reading from file
BufferedImage src=ImageIO.read(new File("src.jpg"));
BufferedImage dest=convertToGray(src); //getting destination image
g2d.drawImage(dest,0,0,this); /* drawing the image on panel and then writing to a file */
ImageIO.write(dest,"jpeg",new File("dest.jpg"));
}catch(Exception e){
e.printStackTrace();
}
}
public BufferedImage convertToGray(BufferedImage src){
//creating instance and using in-built linear gray scale
ColorConvertOp grayOp = new ColorConvertOp(
ColorSpace.getInstance(ColorSpace.CS_GRAY), null);
return grayOp.filter(src,null); //converting to gray scale
}
public static void main (String[] args) {
JFrame jf=new JFrame("GRAY_SCALE");
GrayScale obj=new GrayScale();
jf.getContentPane().add(obj);
jf.setVisible(true);
jf.setSize(320,235);
jf.setDefaultCloseOperation(jf.EXIT_ON_CLOSE);
}
}
--------------------------------------------------------------------------------------------------------------------------
Download Links
--------------------------------------------------------------------------------------------------------------------------DOWNLOAD the source from 4shared
--------------------------------------------------------------------------------------------------------------------------
Related Posts
--------------------------------------------------------------------------------------------------------------------------Reflect or flip an image
Change brightness of image using RescaleOp
Java program to draw partially transparent image using Graphics2D
Labels:Graphics2D-Images | 5
comments
Monday, December 3, 2012
Today I am going to post a program that will be able to produce all the mColorings of a given graph G.
What is mColoring : The problem statement says that for a given graph G(V,E) and m colors, we have to color all the vertices the graph G with m-colors in all possible ways. This is called m-coloring of a graph G. Suppose for a graph G and 3 colors , we have to show all its 3-colorings.
How to solve the problem : Here we first define a function mColoring(int k) that will use the concept of backtracking. Here in this function we call another function next_color(int k) that will try to color the kth vertex. Now if we are successful then check whether all vertex have been colored. If it is true then we print the solution and then try another solution vector otherwise color the remaining vertex. If we were unsuccessful then we go back (backtrack)) to the last successful situation and try a different path. Our final solution i.e. all the possible m-colorings can be represented as a tree.
--------------------------------------------------------------------------------------------------------------------------
What is mColoring : The problem statement says that for a given graph G(V,E) and m colors, we have to color all the vertices the graph G with m-colors in all possible ways. This is called m-coloring of a graph G. Suppose for a graph G and 3 colors , we have to show all its 3-colorings.
How to solve the problem : Here we first define a function mColoring(int k) that will use the concept of backtracking. Here in this function we call another function next_color(int k) that will try to color the kth vertex. Now if we are successful then check whether all vertex have been colored. If it is true then we print the solution and then try another solution vector otherwise color the remaining vertex. If we were unsuccessful then we go back (backtrack)) to the last successful situation and try a different path. Our final solution i.e. all the possible m-colorings can be represented as a tree.
Graph G to be colored |
Tree for 3-coloring of graph G |
--------------------------------------------------------------------------------------------------------------------------
Java Source Code
--------------------------------------------------------------------------------------------------------------------------
public class MWayGrColor{ /*G is graph's adjacency matrix and x is solution vector */ private int G[][],x[],n,m,soln; public void mColoring(int k){ //backtracking function for(int i=1;i<=n;i++){ next_color(k); //coloring kth vertex if(x[k]==0) return; //if unsuccessful then backtrack if(k==n) //if all colored then show write(); else mColoring(k+1); /* successful but still left to color */ } } private void next_color(int k){ do{ int i=1; x[k]=(x[k]+1)%(m+1); if(x[k]==0) return; for(i=1;i<=n;i++) if(G[i][k]!=0 && x[k]==x[i]) /* checking adjacency and not same color */ break; if(i==n+1) return; //new color found }while(true); } private void write(){ System.out.print("\nColoring(V C) # "+(++soln)+"-->"); for(int i=1;i<=n;i++) System.out.print("\t("+i+" "+x[i]+")"); //solution vector } public void input(){ java.util.Scanner sc=new java.util.Scanner(System.in); System.out.print("Enter no. of vertices : "); n=sc.nextInt(); G=new int[n+1][n+1]; x=new int[n+1]; System.out.print("Enter no. of colors : "); m=sc.nextInt(); System.out.println("Enter adjacency matrix-->"); for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) G[i][j]=sc.nextInt(); } public static void main (String[] args) { MWayGrColor obj=new MWayGrColor(); obj.input(); obj.mColoring(1); if(obj.soln==0) System.out.println("\nNeed more than "+obj.m+" colors"); else System.out.print("\nTOTAL SOLN : "+obj.soln); } }
--------------------------------------------------------------------------------------------------------------------------
Output
--------------------------------------------------------------------------------------------------------------------------
Enter no. of vertices : 4
Enter no. of colors : 3
Enter adjacency matrix-->
0 1 1 1
1 0 1 0
1 1 0 1
1 0 1 0
Coloring(V C) # 1--> (1 1) (2 2) (3 3) (4 2)
Coloring(V C) # 2--> (1 1) (2 3) (3 2) (4 3)
Coloring(V C) # 3--> (1 2) (2 1) (3 3) (4 1)
Coloring(V C) # 4--> (1 2) (2 3) (3 1) (4 3)
Coloring(V C) # 5--> (1 3) (2 1) (3 2) (4 1)
Coloring(V C) # 6--> (1 3) (2 2) (3 1) (4 2)
TOTAL SOLN : 6
--------------------------------------------------------------------------------------------------------------------------
Enter no. of colors : 3
Enter adjacency matrix-->
0 1 1 1
1 0 1 0
1 1 0 1
1 0 1 0
Coloring(V C) # 1--> (1 1) (2 2) (3 3) (4 2)
Coloring(V C) # 2--> (1 1) (2 3) (3 2) (4 3)
Coloring(V C) # 3--> (1 2) (2 1) (3 3) (4 1)
Coloring(V C) # 4--> (1 2) (2 3) (3 1) (4 3)
Coloring(V C) # 5--> (1 3) (2 1) (3 2) (4 1)
Coloring(V C) # 6--> (1 3) (2 2) (3 1) (4 2)
TOTAL SOLN : 6
--------------------------------------------------------------------------------------------------------------------------
Download Links
--------------------------------------------------------------------------------------------------------------------------
DOWNLOAD the source from Mediafire
DOWNLOAD the source from Dropbox
DOWNLOAD the source from 4shared
DOWNLOAD the source from Dropbox
DOWNLOAD the source from 4shared
--------------------------------------------------------------------------------------------------------------------------
Related Posts
--------------------------------------------------------------------------------------------------------------------------
To color a graph with least number of colors visit
Labels:Algorithms | 4
comments
Subscribe to:
Posts
(Atom)
Total Pageviews
Followers
Labels
- Algorithms (7)
- Annotation (3)
- Files (6)
- Generics (3)
- Graphics2D (5)
- Graphics2D-Images (7)
- Inheritance (2)
- J2EE (9)
- Java 8 (4)
- Java FAQs (19)
- JDBC (3)
- Networking (2)
- Packages (1)
- Reflection (4)
- Security (7)
- Sorting (2)
- Swing (3)
- Threads (3)
- Utils (3)
Popular Posts
-
Today I will show you how you can implement Bankers algorithm in Java. The Banker's algorithm is a resource allocation and deadlock a...
-
------------------------- UPDATE ------------------------- I have updated the code on request of some followers so that they can directly...
-
Today I am going to show how to convert a postfix expression to an infix expression using stack in Java. In an earlier post here we ...
-
Today in this article I will tell you how to convert an infix expression to postfix expression using stack. This is an important applicat...
-
--------------------UPDATE------------------- I have updated my post so that now it can detect IE 11. This modification was necessary as t...
-
Today I am going to show you how you can generate and validate captcha. A CAPTCHA (an acronym for "Completely Automated Public Turin...
-
Today I am going to post a program that will be able to produce all the mColorings of a given graph G. What is mColoring : The problem st...
-
Today in this article I will show you how to create or develop a Tower of Hanoi game in Java. The Tower of Hanoi is a famous problem tha...