Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.EventQueue;
- import javax.swing.JFrame;
- import javax.swing.JButton;
- import java.awt.event.ActionListener;
- import java.awt.event.ActionEvent;
- import java.io.EOFException;
- import java.io.FileInputStream;
- import java.io.FileNotFoundException;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.ObjectInputStream;
- import java.io.ObjectOutputStream;
- import javax.swing.JTextField;
- import ij.io.Opener;
- import ij.ImagePlus;
- import ij.process.ImageProcessor;
- public class SwinApp implements ActionListener{
- private JFrame frame;
- private JButton button3;
- private ImagePlus imgPlus;
- private ObjectInputStream ois;
- private FileInputStream fis;
- private FileOutputStream fos;
- private ObjectOutputStream oos;
- private ImageProcessor ip;
- private JButton button4;
- private JButton button5;
- /**
- * Launch the application.
- */
- public static void main(String[] args) {
- EventQueue.invokeLater(new Runnable() {
- public void run() {
- try {
- SwinApp window = new SwinApp();
- window.frame.setVisible(true);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- });
- }
- /**
- * Create the application.
- */
- public SwinApp() {
- initialize();
- }
- /**
- * Initialize the contents of the frame.
- */
- private void initialize() {
- frame = new JFrame();
- frame.setBounds(100, 100, 450, 300);
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- frame.getContentPane().setLayout(null);
- button3 = new JButton("Bild \u00F6ffnen");
- button3.setBounds(87, 34, 89, 23);
- frame.getContentPane().add(button3);
- button4 = new JButton("Merkmale speichern");
- button4.setBounds(56, 82, 146, 23);
- frame.getContentPane().add(button4);
- button5 = new JButton("Merkmale anzeigen");
- button5.setBounds(56, 128, 136, 23);
- frame.getContentPane().add(button5);
- button3.addActionListener(this);
- button4.addActionListener(this);
- button5.addActionListener(this);
- }
- public void actionPerformed(ActionEvent e) {
- if(e.getSource() == button3){
- Opener fo = new Opener(); //Opener Objekt erstellen
- imgPlus = new ImagePlus();
- imgPlus = fo.openImage("C:/Users/Valentin/Desktop/Aufgabenstellung BV/Texturen/Texturen/1.1.04.tiff");
- ip = (ImageProcessor) imgPlus.getProcessor();
- }
- if(e.getSource() == button4){
- merkmaleSpeichern(ip);
- }
- if(e.getSource() == button5){
- merkmaleAnzeigen();
- }
- }
- public void merkmaleSpeichern(ImageProcessor ip){
- int w = ip.getWidth();
- int h = ip.getHeight();
- double[][] P0 = new double[256][256]; //Co-Occurrence-Matrix mit alpha = 0°
- double[][] P45 = new double[256][256]; //Co-Occurrence-Matrix mit alpha = 45°
- double[][] P90 = new double[256][256]; //Co-Occurrence-Matrix mit alpha = 90°
- double[][] P135 = new double[256][256]; //Co-Occurrence-Matrix mit alpha = 135°
- double second_angular_moment0 = 0; //Haralick-Merkmale
- double second_angular_moment45 = 0;
- double second_angular_moment90 = 0;
- double second_angular_moment135 = 0;
- double contrast0 = 0;
- double contrast45 = 0;
- double contrast90 = 0;
- double contrast135 = 0;
- double entropy0 = 0;
- double entropy45 = 0;
- double entropy90 = 0;
- double entropy135 = 0;
- double homogeneity0 = 0;
- double homogeneity45 = 0;
- double homogeneity90 = 0;
- double homogeneity135 = 0;
- /* double correlation0 = 0; //fehlt
- double correlation45 = 0; //fehlt
- double correlation90 = 0; //fehlt
- double correlation135 = 0; //fehlt
- */ double inverse_difference_moment0 = 0;
- double inverse_difference_moment45 = 0;
- double inverse_difference_moment90 = 0;
- double inverse_difference_moment135 = 0;
- double [] m0 = new double [5]; //Merkmalsvektor mit alpha = 0°**Korrelation fehlt**
- double [] m45 = new double [5]; //Merkmalsvektor mit alpha = 45°**Korrelation fehlt**
- double [] m90 = new double [5]; //Merkmalsvektor mit alpha = 90°**Korrelation fehlt**
- double [] m135 = new double [5]; //Merkmalsvektor mit alpha = 135°**Korrelation fehlt**
- //--------------------------------------------------------------------------------
- //Merkmalsextraktion
- //Bestimmung der verschiedenen Co-Occurrence-Matrizen(für direkte Pixelnachbarn)
- //------------------------------------------------------------------------------
- //Bestimmung von P0
- for(int u=0; u<w-1; u++){
- for (int v=0; v<h; v++){
- int p = ip.getPixel(u, v);
- int q = ip.getPixel(u+1, v);
- P0[p][q]++;
- }
- }
- //Bestimmung von P45
- for(int u=0; u<w-1; u++){
- for (int v=0; v<h-1; v++){
- int p = ip.getPixel(u, v);
- int q = ip.getPixel(u+1, v+1);
- P45[p][q]++;
- }
- }
- //Bestimmung von P90
- for(int u=0;u<w; u++){
- for (int v=0; v<h-1; v++){
- int p = ip.getPixel(u, v);
- int q = ip.getPixel(u, v+1);
- P90[p][q]++;
- }
- }
- //Bestimmung von P135
- for(int u=1; u<w; u++){
- for (int v = 0; v < h-1; v++){
- int p = ip.getPixel(u, v);
- int q = ip.getPixel(u-1, v+1);
- P135[p][q]++;
- }
- }
- //Normieren der Matrizen auf die Gesamtanzahl der Pixel
- for(int u=0; u<256; u++){
- for(int v=0; v<256; v++){
- P0[u][v] = P0[u][v] / (w*h);
- P45[u][v] = P45[u][v] / (w*h);
- P90[u][v] = P90[u][v] / (w*h);
- P135[u][v] = P135[u][v] / (w*h);
- }
- }
- //----------------------------------------------------------
- // Bestimmung der Momente der jeweiligen Co-Occurrence-Matrix
- //------------------------------------------------------------
- for (int i = 0; i <= 255; i++) { //Bestimmung des Merkmales Energie(second angular moment)
- for (int j = 0; j <= 255; j++) {
- second_angular_moment0 = second_angular_moment0 + Math.pow(P0[i][j], 2);
- second_angular_moment45 = second_angular_moment45 + Math.pow(P45[i][j], 2);
- second_angular_moment90 = second_angular_moment90 + Math.pow(P90[i][j], 2);
- second_angular_moment135 = second_angular_moment135 + Math.pow(P135[i][j], 2);
- }
- }
- for (int i = 0; i <= 255; i++) { //Bestimmung des Merkmales Kontrast
- for (int j = 0; j <= 255; j++) {
- contrast0 = contrast0 + (Math.pow((i-j), 2) * P0[i][j]);
- contrast45 = contrast45 + (Math.pow((i-j), 2) * P45[i][j]);
- contrast90 = contrast90 + (Math.pow((i-j), 2) * P90[i][j]);
- contrast135 = contrast135 + (Math.pow((i-j), 2) * P135[i][j]);
- }
- }
- for (int i = 0; i <= 255; i++) { //Bestimmung des Merkmales Entropie
- for (int j = 0; j <= 255; j++) {
- if(P0[i][j] != 0){
- entropy0 = entropy0 - (P0[i][j] * Math.log(P0[i][j]) );
- }
- if(P45[i][j] != 0){
- entropy45 = entropy45 - (P45[i][j] * Math.log(P45[i][j]) );
- }
- if(P90[i][j] != 0){
- entropy90 = entropy90 - (P90[i][j] * Math.log(P90[i][j]) );
- }
- if(P135[i][j] != 0){
- entropy135 = entropy135 - (P135[i][j] * Math.log(P135[i][j]));
- }
- }
- }
- for (int i = 0; i <= 255; i++) { //Bestimmung des Merkmales Homogenität
- for (int j = 0; j <= 255; j++) {
- homogeneity0 = homogeneity0 + ((P0[i][j]) / (1 + Math.abs(i-j)));
- homogeneity45 = homogeneity45 + ((P45[i][j]) / (1 + Math.abs(i-j)));
- homogeneity90 = homogeneity90 + ((P90[i][j]) / (1 + Math.abs(i-j)));
- homogeneity135 = homogeneity135 + ((P135[i][j]) / (1 + Math.abs(i-j)));
- }
- }
- /*
- for (int i = 0; i <= 255; i++) { //Bestimmung des Merkmales Korrelation
- for (int j = 0; j <= 255; j++) {
- correlation = correlation + (...);
- }
- }
- */
- for (int i = 0; i <= 255; i++) { //Bestimmung des Merkmales "Inverse Differenz Moment"
- for (int j = 0; j <= 255; j++) {
- inverse_difference_moment0 = inverse_difference_moment0 + (Math.pow(P0[i][j], 2) / (1 + Math.pow(i-j, 2)));
- inverse_difference_moment45 = inverse_difference_moment45 + (Math.pow(P45[i][j], 2) / (1 + Math.pow(i-j, 2)));
- inverse_difference_moment90 = inverse_difference_moment90 + (Math.pow(P90[i][j], 2) / (1 + Math.pow(i-j, 2)));
- inverse_difference_moment135 = inverse_difference_moment135 + (Math.pow(P135[i][j], 2) / (1 + Math.pow(i-j, 2)));
- }
- }
- //-----------------------------------------------------------------------------------------------------
- //Merkmales Vektoren **Korrelation fehlt noch**
- //---------------------------------------------------------------------
- m0[0] = second_angular_moment0;
- m0[1] = contrast0;
- m0[2] = entropy0;
- m0[3] = homogeneity0;
- m0[4] = inverse_difference_moment0;
- m45[0] = second_angular_moment45;
- m45[1] = contrast45;
- m45[2] = entropy45;
- m45[3] = homogeneity45;
- m45[4] = inverse_difference_moment45;
- m90[0] = second_angular_moment90;
- m90[1] = contrast90;
- m90[2] = entropy90;
- m90[3] = homogeneity90;
- m90[4] = inverse_difference_moment90;
- m135[0] = second_angular_moment135;
- m135[1] = contrast135;
- m135[2] = entropy135;
- m135[3] = homogeneity135;
- m135[4] = inverse_difference_moment135;
- try {
- fos = new FileOutputStream ("test0.ser");
- oos = new ObjectOutputStream (fos);
- for(int i=0; i<5; i++){
- oos.writeDouble(m0[i]);
- }
- oos.close();
- } catch (FileNotFoundException e1) {
- // TODO Auto-generated catch block
- e1.printStackTrace();
- } catch (IOException e1) {
- // TODO Auto-generated catch block
- e1.printStackTrace();
- }
- try {
- fos = new FileOutputStream ("test45.ser");
- oos = new ObjectOutputStream (fos);
- for(int i=0; i<5; i++){
- oos.writeDouble(m45[i]);
- }
- oos.close();
- } catch (FileNotFoundException e1) {
- // TODO Auto-generated catch block
- e1.printStackTrace();
- } catch (IOException e1) {
- // TODO Auto-generated catch block
- e1.printStackTrace();
- }
- try {
- fos = new FileOutputStream ("test90.ser");
- oos = new ObjectOutputStream (fos);
- for(int i=0; i<5; i++){
- oos.writeDouble(m90[i]);
- }
- oos.close();
- } catch (FileNotFoundException e1) {
- // TODO Auto-generated catch block
- e1.printStackTrace();
- } catch (IOException e1) {
- // TODO Auto-generated catch block
- e1.printStackTrace();
- }
- try {
- fos = new FileOutputStream ("test135.ser");
- oos = new ObjectOutputStream (fos);
- for(int i=0; i<5; i++){
- oos.writeDouble(m135[i]);
- }
- oos.close();
- } catch (FileNotFoundException e1) {
- // TODO Auto-generated catch block
- e1.printStackTrace();
- } catch (IOException e1) {
- // TODO Auto-generated catch block
- e1.printStackTrace();
- }
- }
- public void merkmaleAnzeigen(){
- try{
- fis = new FileInputStream ("test0.ser");
- ois = new ObjectInputStream (fis);
- double[] double0 = new double[5];
- for(int i = 0; i<5; i++){
- double0[i] = ois.readDouble();
- }
- ois.close();
- for(int i=0;i<5;i++){
- System.out.println(double0[i]);
- }
- } catch (FileNotFoundException e1) {
- // TODO Auto-generated catch block
- e1.printStackTrace();
- System.out.println("File not found!");
- } catch (IOException e1) {
- // TODO Auto-generated catch block
- e1.printStackTrace();
- }
- try{
- fis = new FileInputStream ("test45.ser");
- ois = new ObjectInputStream (fis);
- double[] double45 = new double[5];
- for(int i = 0; i<5; i++){
- double45[i] = ois.readDouble();
- }
- for(int i=0;i<5;i++){
- System.out.println(double45[i]);
- }
- } catch (FileNotFoundException e1) {
- // TODO Auto-generated catch block
- e1.printStackTrace();
- System.out.println("File not found!");
- } catch (IOException e1) {
- // TODO Auto-generated catch block
- e1.printStackTrace();
- }
- try{
- fis = new FileInputStream ("test90.ser");
- ois = new ObjectInputStream (fis);
- double[] double90 = new double[5];
- for(int i = 0; i<5; i++){
- double90[i] = ois.readDouble();
- }
- for(int i=0;i<5;i++){
- System.out.println(double90[i]);
- }
- } catch (FileNotFoundException e1) {
- // TODO Auto-generated catch block
- e1.printStackTrace();
- System.out.println("File not found!");
- } catch (IOException e1) {
- // TODO Auto-generated catch block
- e1.printStackTrace();
- }
- try{
- fis = new FileInputStream ("test135.ser");
- ois = new ObjectInputStream (fis);
- double[] double135 = new double[5];
- for(int i = 0; i<5; i++){
- double135[i] = ois.readDouble();
- }
- ois.close();
- for(int i=0;i<5;i++){
- System.out.println(double135[i]+" ");
- }
- } catch (FileNotFoundException e1) {
- // TODO Auto-generated catch block
- e1.printStackTrace();
- System.out.println("File not found!");
- } catch (IOException e1) {
- // TODO Auto-generated catch block
- e1.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement