Advertisement
Guest User

Untitled

a guest
Feb 27th, 2017
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 13.60 KB | None | 0 0
  1. import java.awt.EventQueue;
  2.  
  3. import javax.swing.JFrame;
  4. import javax.swing.JButton;
  5.  
  6. import java.awt.event.ActionListener;
  7. import java.awt.event.ActionEvent;
  8. import java.io.EOFException;
  9. import java.io.FileInputStream;
  10. import java.io.FileNotFoundException;
  11. import java.io.FileOutputStream;
  12. import java.io.IOException;
  13. import java.io.ObjectInputStream;
  14. import java.io.ObjectOutputStream;
  15.  
  16. import javax.swing.JTextField;
  17.  
  18. import ij.io.Opener;
  19. import ij.ImagePlus;
  20. import ij.process.ImageProcessor;
  21.  
  22.  
  23.  
  24. public class SwinApp implements ActionListener{
  25.  
  26.     private JFrame frame;
  27.     private JButton button3;
  28.     private ImagePlus imgPlus;
  29.     private ObjectInputStream ois;
  30.     private FileInputStream fis;
  31.     private FileOutputStream fos;
  32.     private ObjectOutputStream oos;
  33.     private ImageProcessor ip;
  34.     private JButton button4;
  35.     private JButton button5;
  36.    
  37.  
  38.     /**
  39.      * Launch the application.
  40.      */
  41.     public static void main(String[] args) {
  42.         EventQueue.invokeLater(new Runnable() {
  43.             public void run() {
  44.                 try {
  45.                     SwinApp window = new SwinApp();
  46.                     window.frame.setVisible(true);
  47.                 } catch (Exception e) {
  48.                     e.printStackTrace();
  49.                 }
  50.             }
  51.         });
  52.     }
  53.  
  54.     /**
  55.      * Create the application.
  56.      */
  57.     public SwinApp() {
  58.         initialize();
  59.     }
  60.  
  61.     /**
  62.      * Initialize the contents of the frame.
  63.      */
  64.     private void initialize() {
  65.         frame = new JFrame();
  66.         frame.setBounds(100, 100, 450, 300);
  67.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  68.         frame.getContentPane().setLayout(null);
  69.        
  70.         button3 = new JButton("Bild \u00F6ffnen");
  71.         button3.setBounds(87, 34, 89, 23);
  72.         frame.getContentPane().add(button3);
  73.        
  74.         button4 = new JButton("Merkmale speichern");
  75.         button4.setBounds(56, 82, 146, 23);
  76.         frame.getContentPane().add(button4);
  77.        
  78.         button5 = new JButton("Merkmale anzeigen");
  79.         button5.setBounds(56, 128, 136, 23);
  80.         frame.getContentPane().add(button5);
  81.         button3.addActionListener(this);
  82.         button4.addActionListener(this);
  83.         button5.addActionListener(this);
  84.     }
  85.    
  86.     public void actionPerformed(ActionEvent e) {
  87.        
  88.         if(e.getSource() == button3){
  89.             Opener fo = new Opener(); //Opener Objekt erstellen
  90.             imgPlus = new ImagePlus();
  91.             imgPlus = fo.openImage("C:/Users/Valentin/Desktop/Aufgabenstellung BV/Texturen/Texturen/1.1.04.tiff");
  92.             ip = (ImageProcessor) imgPlus.getProcessor();
  93.         }
  94.         if(e.getSource() == button4){
  95.             merkmaleSpeichern(ip);
  96.         }
  97.         if(e.getSource() == button5){
  98.             merkmaleAnzeigen();
  99.         }
  100.     }
  101.    
  102.     public void merkmaleSpeichern(ImageProcessor ip){
  103.         int w = ip.getWidth();
  104.         int h = ip.getHeight();
  105.  
  106.         double[][] P0   = new double[256][256];     //Co-Occurrence-Matrix mit alpha = 0°
  107.         double[][] P45  = new double[256][256];     //Co-Occurrence-Matrix mit alpha = 45°
  108.         double[][] P90  = new double[256][256];     //Co-Occurrence-Matrix mit alpha = 90°
  109.         double[][] P135 = new double[256][256];     //Co-Occurrence-Matrix mit alpha = 135°
  110.      
  111.         double second_angular_moment0   = 0;        //Haralick-Merkmale      
  112.         double second_angular_moment45  = 0;  
  113.         double second_angular_moment90  = 0;  
  114.         double second_angular_moment135 = 0;
  115.         double contrast0   = 0;                          
  116.         double contrast45  = 0;
  117.         double contrast90  = 0;  
  118.         double contrast135 = 0;
  119.         double entropy0   = 0;                          
  120.         double entropy45  = 0;  
  121.         double entropy90  = 0;  
  122.         double entropy135 = 0;
  123.         double homogeneity0   = 0;                      
  124.         double homogeneity45  = 0;
  125.         double homogeneity90  = 0;
  126.         double homogeneity135 = 0;
  127. /*      double correlation0 = 0;    //fehlt
  128.         double correlation45 = 0;   //fehlt
  129.         double correlation90 = 0;   //fehlt
  130.         double correlation135 = 0;  //fehlt
  131. */      double inverse_difference_moment0   = 0;        
  132.         double inverse_difference_moment45  = 0;  
  133.         double inverse_difference_moment90  = 0;  
  134.         double inverse_difference_moment135 = 0;
  135.              
  136.         double [] m0   = new double [5];            //Merkmalsvektor mit alpha = 0°**Korrelation fehlt**
  137.         double [] m45  = new double [5];            //Merkmalsvektor mit alpha = 45°**Korrelation fehlt**
  138.         double [] m90  = new double [5];            //Merkmalsvektor mit alpha = 90°**Korrelation fehlt**
  139.         double [] m135 = new double [5];            //Merkmalsvektor mit alpha = 135°**Korrelation fehlt**
  140.  
  141. //--------------------------------------------------------------------------------
  142.      
  143.          //Merkmalsextraktion
  144.      
  145.         //Bestimmung der verschiedenen Co-Occurrence-Matrizen(für direkte Pixelnachbarn)
  146.         //------------------------------------------------------------------------------
  147.      
  148.     //Bestimmung von P0
  149.         for(int u=0; u<w-1; u++){
  150.             for (int v=0; v<h; v++){
  151.                 int p = ip.getPixel(u, v);
  152.                 int q = ip.getPixel(u+1, v);
  153.                 P0[p][q]++;
  154.             }
  155.         }
  156.     //Bestimmung von P45
  157.         for(int u=0; u<w-1; u++){
  158.             for (int v=0; v<h-1; v++){
  159.                 int p = ip.getPixel(u, v);
  160.                 int q = ip.getPixel(u+1, v+1);
  161.                 P45[p][q]++;
  162.             }
  163.         }
  164.     //Bestimmung von P90
  165.         for(int u=0;u<w; u++){
  166.             for (int v=0; v<h-1; v++){
  167.                 int p = ip.getPixel(u, v);
  168.                 int q = ip.getPixel(u, v+1);
  169.                 P90[p][q]++;
  170.             }
  171.         }
  172.     //Bestimmung von P135
  173.         for(int u=1; u<w; u++){
  174.             for (int v = 0; v < h-1; v++){
  175.                 int p = ip.getPixel(u, v);
  176.                 int q = ip.getPixel(u-1, v+1);
  177.                 P135[p][q]++;
  178.             }
  179.         }
  180.     //Normieren der Matrizen auf die Gesamtanzahl der Pixel
  181.         for(int u=0; u<256; u++){
  182.             for(int v=0; v<256; v++){
  183.                 P0[u][v]     = P0[u][v]     / (w*h);
  184.                 P45[u][v]   = P45[u][v]   / (w*h);
  185.                 P90[u][v]   = P90[u][v]   / (w*h);
  186.                 P135[u][v] = P135[u][v] / (w*h);
  187.             }
  188.         }
  189.     //----------------------------------------------------------
  190.      
  191.      
  192.     // Bestimmung der Momente der jeweiligen Co-Occurrence-Matrix
  193.     //------------------------------------------------------------
  194.      
  195.              
  196. for (int i = 0; i <= 255; i++) {            //Bestimmung des Merkmales Energie(second angular moment)  
  197.           for (int j = 0; j <= 255; j++) {               
  198.               second_angular_moment0     =  second_angular_moment0     + Math.pow(P0[i][j], 2);
  199. second_angular_moment45   =  second_angular_moment45   + Math.pow(P45[i][j], 2);
  200. second_angular_moment90   =  second_angular_moment90   + Math.pow(P90[i][j], 2);
  201.               second_angular_moment135 =  second_angular_moment135 + Math.pow(P135[i][j], 2);
  202.               }
  203.       }      
  204.       for (int i = 0; i <= 255; i++) {           //Bestimmung des Merkmales Kontrast
  205.           for (int j = 0; j <= 255; j++) {           
  206.               contrast0     = contrast0     + (Math.pow((i-j), 2) * P0[i][j]);
  207.           contrast45   = contrast45   + (Math.pow((i-j), 2) * P45[i][j]);
  208.           contrast90   = contrast90   + (Math.pow((i-j), 2) * P90[i][j]);
  209.           contrast135 = contrast135 + (Math.pow((i-j), 2) * P135[i][j]);
  210.               }
  211.       }                              
  212. for (int i = 0; i <= 255; i++) {            //Bestimmung des Merkmales Entropie
  213.           for (int j = 0; j <= 255; j++) {               
  214.             if(P0[i][j] != 0){
  215.                     entropy0   = entropy0   - (P0[i][j]   * Math.log(P0[i][j])  );
  216.             }
  217.             if(P45[i][j] != 0){
  218.                     entropy45  = entropy45  - (P45[i][j]  * Math.log(P45[i][j]) );
  219.             }
  220.              if(P90[i][j] != 0){
  221.                     entropy90  = entropy90  - (P90[i][j]  * Math.log(P90[i][j]) );
  222.             }
  223.             if(P135[i][j] != 0){
  224.                     entropy135 = entropy135 - (P135[i][j] * Math.log(P135[i][j]));
  225.             }
  226.               }
  227.       }
  228.       for (int i = 0; i <= 255; i++) {              //Bestimmung des Merkmales Homogenität
  229.           for (int j = 0; j <= 255; j++) {               
  230.               homogeneity0     = homogeneity0     + ((P0[i][j])     / (1 + Math.abs(i-j)));
  231.           homogeneity45   = homogeneity45   + ((P45[i][j])   / (1 + Math.abs(i-j)));
  232.           homogeneity90   = homogeneity90   + ((P90[i][j])   / (1 + Math.abs(i-j)));
  233.           homogeneity135 = homogeneity135 + ((P135[i][j]) / (1 + Math.abs(i-j)));
  234.  
  235.               }
  236.       }
  237.      
  238. /*                       
  239.       for (int i = 0; i <= 255; i++) {              //Bestimmung des Merkmales Korrelation
  240.           for (int j = 0; j <= 255; j++) {
  241.              
  242.               correlation = correlation + (...);
  243.               }
  244.       }
  245. */      
  246.        
  247.       for (int i = 0; i <= 255; i++) {               //Bestimmung des Merkmales "Inverse Differenz Moment"
  248.           for (int j = 0; j <= 255; j++) {               
  249.               inverse_difference_moment0     = inverse_difference_moment0      + (Math.pow(P0[i][j], 2)     / (1 +       Math.pow(i-j, 2)));
  250. inverse_difference_moment45   = inverse_difference_moment45   + (Math.pow(P45[i][j], 2)   / (1 + Math.pow(i-j, 2)));
  251. inverse_difference_moment90   = inverse_difference_moment90   + (Math.pow(P90[i][j], 2)   / (1 + Math.pow(i-j, 2)));
  252. inverse_difference_moment135 = inverse_difference_moment135 + (Math.pow(P135[i][j], 2) / (1 + Math.pow(i-j, 2)));
  253.               }
  254.       }
  255.     //-----------------------------------------------------------------------------------------------------
  256.  
  257.     //Merkmales Vektoren **Korrelation fehlt noch**
  258.     //---------------------------------------------------------------------
  259.      
  260.     m0[0] = second_angular_moment0;
  261.     m0[1] = contrast0;
  262.     m0[2] = entropy0;
  263.     m0[3] = homogeneity0;
  264.     m0[4] = inverse_difference_moment0;
  265.      
  266.     m45[0] = second_angular_moment45;
  267.     m45[1] = contrast45;
  268.     m45[2] = entropy45;
  269.     m45[3] = homogeneity45;
  270.     m45[4] = inverse_difference_moment45;
  271.      
  272.     m90[0] = second_angular_moment90;
  273.     m90[1] = contrast90;
  274.     m90[2] = entropy90;
  275.     m90[3] = homogeneity90;
  276.     m90[4] = inverse_difference_moment90;
  277.      
  278.     m135[0] = second_angular_moment135;
  279.     m135[1] = contrast135;
  280.     m135[2] = entropy135;
  281.     m135[3] = homogeneity135;
  282.     m135[4] = inverse_difference_moment135;
  283.  
  284.    
  285.     try {
  286.         fos = new FileOutputStream ("test0.ser");
  287.         oos = new ObjectOutputStream (fos);
  288.         for(int i=0; i<5; i++){
  289.         oos.writeDouble(m0[i]);
  290.         }
  291.         oos.close();
  292.  
  293.     } catch (FileNotFoundException e1) {
  294.         // TODO Auto-generated catch block
  295.         e1.printStackTrace();
  296.     } catch (IOException e1) {
  297.         // TODO Auto-generated catch block
  298.         e1.printStackTrace();
  299.     }
  300.     try {
  301.         fos = new FileOutputStream ("test45.ser");
  302.         oos = new ObjectOutputStream (fos);
  303.         for(int i=0; i<5; i++){
  304.         oos.writeDouble(m45[i]);
  305.         }
  306.         oos.close();
  307.  
  308.     } catch (FileNotFoundException e1) {
  309.         // TODO Auto-generated catch block
  310.         e1.printStackTrace();
  311.     } catch (IOException e1) {
  312.         // TODO Auto-generated catch block
  313.         e1.printStackTrace();
  314.     }
  315.     try {
  316.         fos = new FileOutputStream ("test90.ser");
  317.         oos = new ObjectOutputStream (fos);
  318.         for(int i=0; i<5; i++){
  319.         oos.writeDouble(m90[i]);
  320.         }
  321.         oos.close();
  322.  
  323.     } catch (FileNotFoundException e1) {
  324.         // TODO Auto-generated catch block
  325.         e1.printStackTrace();
  326.     } catch (IOException e1) {
  327.         // TODO Auto-generated catch block
  328.         e1.printStackTrace();
  329.     }
  330.     try {
  331.         fos = new FileOutputStream ("test135.ser");
  332.         oos = new ObjectOutputStream (fos);
  333.         for(int i=0; i<5; i++){
  334.         oos.writeDouble(m135[i]);
  335.         }
  336.         oos.close();
  337.  
  338.     } catch (FileNotFoundException e1) {
  339.         // TODO Auto-generated catch block
  340.         e1.printStackTrace();
  341.     } catch (IOException e1) {
  342.         // TODO Auto-generated catch block
  343.         e1.printStackTrace();
  344.     }
  345.     }
  346.    
  347.     public void merkmaleAnzeigen(){
  348.         try{
  349.         fis = new FileInputStream ("test0.ser");
  350.         ois = new ObjectInputStream (fis);
  351.         double[] double0 = new double[5];
  352.         for(int i = 0; i<5; i++){
  353.             double0[i] = ois.readDouble();
  354.         }
  355.         ois.close();
  356.  
  357.         for(int i=0;i<5;i++){
  358.             System.out.println(double0[i]);
  359.         }
  360.         } catch (FileNotFoundException e1) {
  361.         // TODO Auto-generated catch block
  362.         e1.printStackTrace();
  363.         System.out.println("File not found!");
  364.     } catch (IOException e1) {
  365.         // TODO Auto-generated catch block
  366.         e1.printStackTrace();
  367.     }
  368.        
  369.         try{
  370.             fis = new FileInputStream ("test45.ser");
  371.             ois = new ObjectInputStream (fis);
  372.             double[] double45 = new double[5];
  373.             for(int i = 0; i<5; i++){
  374.                 double45[i] = ois.readDouble();
  375.             }
  376.             for(int i=0;i<5;i++){
  377.                 System.out.println(double45[i]);
  378.             }
  379.               } catch (FileNotFoundException e1) {
  380.             // TODO Auto-generated catch block
  381.             e1.printStackTrace();
  382.             System.out.println("File not found!");
  383.         } catch (IOException e1) {
  384.             // TODO Auto-generated catch block
  385.             e1.printStackTrace();
  386.         }
  387.        
  388.         try{
  389.             fis = new FileInputStream ("test90.ser");
  390.             ois = new ObjectInputStream (fis);
  391.             double[] double90 = new double[5];
  392.             for(int i = 0; i<5; i++){
  393.                 double90[i] = ois.readDouble();
  394.             }
  395.             for(int i=0;i<5;i++){
  396.                 System.out.println(double90[i]);
  397.             }
  398.               } catch (FileNotFoundException e1) {
  399.             // TODO Auto-generated catch block
  400.             e1.printStackTrace();
  401.             System.out.println("File not found!");
  402.         } catch (IOException e1) {
  403.             // TODO Auto-generated catch block
  404.             e1.printStackTrace();
  405.         }
  406.        
  407.         try{
  408.             fis = new FileInputStream ("test135.ser");
  409.             ois = new ObjectInputStream (fis);
  410.             double[] double135 = new double[5];
  411.             for(int i = 0; i<5; i++){
  412.                 double135[i] = ois.readDouble();
  413.             }
  414.             ois.close();
  415.             for(int i=0;i<5;i++){
  416.                 System.out.println(double135[i]+" ");
  417.             }
  418.               } catch (FileNotFoundException e1) {
  419.             // TODO Auto-generated catch block
  420.             e1.printStackTrace();
  421.             System.out.println("File not found!");
  422.         } catch (IOException e1) {
  423.             // TODO Auto-generated catch block
  424.             e1.printStackTrace();
  425.         }
  426.     }
  427. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement