⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 colorextraction.java

📁 edge detection in java
💻 JAVA
字号:
package compare;  
   
 import java.awt.BorderLayout;  
 import java.awt.Color;  
 import java.awt.Container;  
 import java.awt.Font;  
 import java.awt.GridLayout;  
 import java.awt.image.RenderedImage;  
 import java.awt.image.renderable.ParameterBlock;  
 import java.io.File;  
 import java.io.IOException;  
    
 import javax.imageio.ImageIO;  
 import javax.media.jai.InterpolationNearest;  
 import javax.media.jai.JAI;  
 import javax.media.jai.iterator.RandomIter;  
 import javax.media.jai.iterator.RandomIterFactory;  
 import javax.swing.JFileChooser;  
 import javax.swing.JFrame;  
 import javax.swing.JLabel;  
 import javax.swing.JOptionPane;  
 import javax.swing.JPanel;  
 import javax.swing.JScrollPane;  
    
 import com.sun.media.jai.widget.DisplayJAI;  

 public class colorExtraction extends JFrame  
   {  
  
   private Color[][] signature;  

   private static final int baseSize = 300;  
  
	   private static final String basePath =   
    	 "/home";  
     

   public colorExtraction(File reference) throws IOException  
     {  
     // Create the GUI  
     super("Color Extraction");  
     Container cp = getContentPane();  
     cp.setLayout(new BorderLayout());  
     // Put the reference, scaled, in the left part of the UI.  
     RenderedImage ref = rescale(ImageIO.read(reference));  
     cp.add(new DisplayJAI(ref), BorderLayout.WEST);  
     // Calculate the signature vector for the reference.  
     signature = calcSignature(ref);  
     // Now we need a component to store X images in a stack, where X is the  
     // number of images in the same directory as the original one.  
     //File[] others = getOtherImageFiles(reference);  
     JPanel otherPanel = new JPanel(new GridLayout(30, 2)); 
 JPanel otherPanel2 = new JPanel(new GridLayout(5, 2)); 


	String s1="Red";
	 String s2="Green";
	String s3="Blue";
	float f1,f2,f3,m1,m2,m3,sd1,sd2,sd3,t1,t2,t3,a1,a2,a3,b1,b2,b3,sk1,sk2,sk3;
	f1=f2=f3=m1=m2=m3=sd1=sd2=sd3=t1=t2=t3=a1=a2=a3=b1=b2=b3=sk1=sk2=sk3=0;
	
     	cp.add(new JScrollPane(otherPanel), BorderLayout.EAST); 
cp.add(new JScrollPane(otherPanel2), BorderLayout.CENTER); 
	JLabel ldist1 = new JLabel("<html>" +  "<br>"  + String.format("%s\t%s\t%s", s1,s2,s3) + "</html>");  
       	ldist1.setFont(new Font(Font.MONOSPACED, Font.PLAIN, 14));  
       	otherPanel.add(ldist1,BorderLayout.SOUTH);  
	for(int u=0;u<5;u++)
		for(int v=0;v<5;v++){
			JLabel ldist = new JLabel("<html>" + String.format("%4d%4d%4d", signature[u][v].getRed(), signature[u][v].getGreen(), signature[u][v].getBlue()) + "</html>");  
       ldist.setFont(new Font(Font.MONOSPACED, Font.PLAIN, 14));  
		f1+=signature[u][v].getRed();
		f2+=signature[u][v].getGreen();
		f3+=signature[u][v].getBlue();
       otherPanel.add(ldist);  
       } 
	m1=f1/25;m2=f2/25;m3=f3/25;	 
   JLabel ldist3 = new JLabel("<html>" + String.format("Mean: Red=%5.2f,Green=%5.2f,Blue=%5.2f", m1,m2,m3) + "</html>");  
 
       ldist3.setFont(new Font(Font.MONOSPACED, Font.PLAIN, 14));  
	
	otherPanel2.add(ldist3);
	
	for(int u=0;u<5;u++)
		for(int v=0;v<5;v++){
			;  
			t1=(signature[u][v].getRed()-m1);
			t2=(signature[u][v].getGreen()-m2);
			t3=(signature[u][v].getBlue()-m3);
			
			a1+=(float)Math.pow(t1,2.0);						
			a2+=(float)Math.pow(t2,2.0);
			a3+=(float)Math.pow(t3,2.0);;

			b1+=(float)Math.pow(t1,3.0);						
			b2+=(float)Math.pow(t2,3.0);
			b3+=(float)Math.pow(t3,3.0);    
       } 
	a1=a1/25F; a2=a2/25F; a3=a3/25F;b1=b1/25F; b2=b2/25F; b3=b3/25F;
  	sd1=(float)Math.sqrt(a1);  sk1=(float)Math.pow(b1,(1F/3F));
  	sd2=(float)Math.sqrt(a2);  sk2=(float)Math.pow(b2,(1F/3F));
  	sd3=(float)Math.sqrt(a3);   sk3=(float)Math.pow(b3,(1F/3F));

  JLabel ldist4 = new JLabel("<html>" + String.format("Variance: Red=%5.2f,Green=%5.2f,Blue=%5.2f", sd1,sd2,sd3) + "</html>");  
       ldist4.setFont(new Font(Font.MONOSPACED, Font.PLAIN, 14));  
	
	otherPanel2.add(ldist4);
  JLabel ldist5 = new JLabel("<html>" + String.format("skewness: Red=%5.2f,Green=%5.2f,Blue=%5.2f", sk1,sk2,sk3) + "</html>");  
       ldist5.setFont(new Font(Font.MONOSPACED, Font.PLAIN, 14));  
	 otherPanel2.add(ldist5);
     pack();  
	setSize(600,800);
     setVisible(true);  
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
     }  
    
     //Normalizes the query image
   private RenderedImage rescale(RenderedImage i)  
     {  
     float scaleW = ((float) baseSize) / i.getWidth();  
     float scaleH = ((float) baseSize) / i.getHeight();  
     // Scales the original image  
     ParameterBlock pb = new ParameterBlock();  
     pb.addSource(i);  
     pb.add(scaleW);  
     pb.add(scaleH);  
     pb.add(0.0F);  
     pb.add(0.0F);  
     pb.add(new InterpolationNearest());  
     // Creates a new, scaled image and uses it on the DisplayJAI component  
     return JAI.create("scale", pb);  
     }  
     
   private Color[][] calcSignature(RenderedImage i)  
     {  
     // Get memory for the signature.  
     Color[][] sig = new Color[5][5];  
     // For each of the 25 signature values average the pixels around it.  
     // Note that the coordinate of the central pixel is in proportions.  
     float[] prop = new float[]  
       {1f / 10f, 3f / 10f, 5f / 10f, 7f / 10f, 9f / 10f};  
     for (int x = 0; x < 5; x++)  
       for (int y = 0; y < 5; y++)  
         sig[x][y] = averageAround(i, prop[x], prop[y]);  
     return sig;  
     }  
    

   private Color averageAround(RenderedImage i, double px, double py)  
     {  
     // Get an iterator for the image.  
     RandomIter iterator = RandomIterFactory.create(i, null);  
     // Get memory for a pixel and for the accumulator.  
     double[] pixel = new double[3];  
     double[] accum = new double[3];  
     // The size of the sampling area.  
     int sampleSize = 15;  
     // Sample the pixels.  
     for (double x = px * baseSize - sampleSize; x < px * baseSize + sampleSize; x++)  
       {  
       for (double y = py * baseSize - sampleSize; y < py * baseSize  
           + sampleSize; y++)  
         {  
         iterator.getPixel((int) x, (int) y, pixel);  
         accum[0] += pixel[0];  
         accum[1] += pixel[1];  
         accum[2] += pixel[2];  
         }  
       }  
     // Average the accumulated values.  
     accum[0] /= sampleSize * sampleSize * 4;  
     accum[1] /= sampleSize * sampleSize * 4;  
     accum[2] /= sampleSize * sampleSize * 4;  
     return new Color((int) accum[0], (int) accum[2], (int) accum[2]);  
     }  
    


    //Entry point of the application

   public static void main(String[] args) throws IOException  
     {  
     JFileChooser fc = new JFileChooser(basePath);  
     fc.setFileFilter(new JPEGImageFileFilter());  
     int res = fc.showOpenDialog(null);  
     // We have an image!  
     if (res == JFileChooser.APPROVE_OPTION)  
       {  
       File file = fc.getSelectedFile();  
       new colorExtraction(file);  
       }  
     // Oops!  
     else  
       {  
       JOptionPane.showMessageDialog(null,  
           "You must select one image to be the reference.", "Aborting...",  
           JOptionPane.WARNING_MESSAGE);  
       }  
     }  
     
   }  

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -