2016-05-07 22 views
2

Ich bin neu in Java Programmierung. Ich versuche, eine Java-Anwendung mit netbeans zu schreiben, die imagej jar verwendet, um ein dicom-Bild zu öffnen & es zu verarbeiten. Ich war in der Lage zu tun, dass Sie den folgenden Java-Code verwendet:imagej Bild Typ Umwandlung

public class user_interface extends java.awt.Frame { 
/** Creates new form user_interface */ 
public user_interface() { 
    initComponents(); 
} 

private void initComponents() { 

    btn_open_image = new java.awt.Button(); 
    btn_invert_image = new java.awt.Button(); 
    slbl_display = new javax.swing.JLabel(); 

    setBackground(java.awt.Color.orange); 
    addWindowListener(new java.awt.event.WindowAdapter() { 
     public void windowClosing(java.awt.event.WindowEvent evt) { 
      exitForm(evt); 
     } 
    }); 
    setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout()); 

    btn_open_image.setLabel("Open Image"); 
    btn_open_image.addMouseListener(new java.awt.event.MouseAdapter() { 
     public void mouseClicked(java.awt.event.MouseEvent evt) { 
      btn_open_imageMouseClicked(evt); 
     } 
    }); 
    btn_open_image.addActionListener(new java.awt.event.ActionListener() { 
     public void actionPerformed(java.awt.event.ActionEvent evt) { 
      btn_open_imageActionPerformed(evt); 
     } 
    }); 
    add(btn_open_image, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 40, 80, -1)); 

    btn_invert_image.setLabel("Invert Image"); 
    btn_invert_image.addMouseListener(new java.awt.event.MouseAdapter() { 
     public void mouseClicked(java.awt.event.MouseEvent evt) { 
      btn_invert_imageMouseClicked(evt); 
     } 
    }); 
    add(btn_invert_image, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 150, 80, -1)); 

    slbl_display.setBackground(new java.awt.Color(51, 51, 51)); 
    add(slbl_display, new org.netbeans.lib.awtextra.AbsoluteConstraints(120, 60, -1, -1)); 

    pack(); 
}// </editor-fold>       

/** 
* Exit the Application 
*/ 
private void exitForm(java.awt.event.WindowEvent evt) {       
    System.exit(0); 
}       

    private ImagePlus IPL_image; 
    private ImageJ ImageJ_image; 
    private ImageJ CovImageJ_image; 
    private ImageProcessor ip; 
    private Image AWT_image; 
    private ImageIcon AWT_icon; 

private void btn_open_imageMouseClicked(java.awt.event.MouseEvent evt) {            
    // TODO add your handling code here: 
    String MacroName ="C:\\Program Files\\ImageJ\\macros\\RadFz\\DrawGraticule(Worksfine).txt"; 
    String ImgName = "G:\\PV-QA Images\\01-31-2016\\6MV-FS-OF\\RI\\5x5-6MV-MLC.dcm";   
    //(01) open image 
      IPL_image = IJ.openImage(ImgName); 


    int ImgType = IPL_image.getType(); 
    System.out.println("Image Type = " + ImgType); 
      //IJ.runMacroFile(MacroName); 
    //(02) pass it to processor to acess each pixel 
      // ip.convertToColorProcessor(); 
      ip = IPL_image.getProcessor(); 

    //(03) reset the image window & level 
      ip.resetMinAndMax();    

    //get width & height 
    int imgWdth = ip.getWidth(); 
    int imgHgth = ip.getHeight(); 

    // set line color and width 
    ip.setColor(Color.red); 
    ip.setLineWidth(3); 
    ip.drawLine(0, imgHgth/2, imgWdth, imgHgth/2); 
    ip.drawLine(imgWdth/2, 0, imgWdth/2, imgHgth); 

    //IPL_image.show(); // Display image in imagej window 
    //String IIP = IJ.runMacroFile(MacroName); 

    //convert image from imagej format to one that you can 
    //display in image container 
    AWT_image = ip.createImage(); 
    AWT_icon = new ImageIcon(AWT_image); 
    slbl_display.setIcon(AWT_icon);  

    System.out.println("Width = " + imgWdth + " pixels"); 
    System.out.println("Height = " + imgHgth + " pixels"); 

    GetDICOMTagVal("300A,012C"); 

}           

private void btn_invert_imageMouseClicked(java.awt.event.MouseEvent evt) {            
    // TODO add your handling code here: 

    ip.invert(); 
    AWT_image = ip.createImage(); 
    AWT_icon = new ImageIcon(AWT_image); 
    slbl_display.setIcon(AWT_icon); 

}            

private void btn_open_imageActionPerformed(java.awt.event.ActionEvent evt) {            
    // TODO add your handling code here: 
}            


private void GetDICOMTagVal(String DICOMTag) { 

    String imgInfoStr = IPL_image.getInfoProperty(); 
    //"0002,0003" "300A,012C" 

    System.out.println("imgInfoStr = \n"+ imgInfoStr);   
    String InfoLines[]; 
    InfoLines = split(imgInfoStr, "\n"); 
    //System.out.println(" Number of lines = " + InfoLines.length); 
    int i; 
    for (i =0; i<InfoLines.length; i++){ 
     //System.out.println(i+" -->" + InfoLines[i].startsWith(DICOMTag)); 
     if(InfoLines[i].startsWith(DICOMTag)) { 
      String Tag; 
      Tag = InfoLines[i].substring(DICOMTag.length()); 

      System.out.println(DICOMTag + " = " + Tag); 
     } else { 
     }    
    } 
}  


/** 
* @param args the command line arguments 
*/ 
public static void main(String args[]) { 
    java.awt.EventQueue.invokeLater(new Runnable() { 
     public void run() { 
      new user_interface().setVisible(true); 
     } 
    }); 
} 


// Variables declaration - do not modify      
private java.awt.Button btn_invert_image; 
private java.awt.Button btn_open_image; 
private javax.swing.JLabel slbl_display; 
// End of variables declaration     
} 

Ich bin in der Lage, das Bild zu öffnen und bearbeiten sie (auf sie ziehen) nur schwarze Linien. Dies liegt daran, dass das Bild als ein 8-Bit-Graubild geöffnet wird. Ich bin mir nicht sicher, wie man Bild in RGB umwandelt. Das convertToRGB() ist im ij-Paket im Verarbeitungsordner in der Bildkonverterklasse verfügbar.

Wie kann ich das tun?

Antwort

1

der Tat, wie Sie gesagt haben, die abstrakte Klasse ImageProcessor hat eine Methode ConvertToRGB():

public ImageProcessor convertToRGB() 
    { 
    if (type == RGB) return ip ; 
    ImageProcessor ip2 = ip.convertToByte(doScaling) ; 
    return new ColorProcessor(ip2.createImage()) ; 
    } 

Es tut genau das, was Sie brauchen: konvertieren eine ByteProcessor (8 Bit) in eine ColorProcessor (24 Bit).