imagej图像类型转换

时间:2016-05-07 19:18:37

标签: netbeans imagej

我是java编程的新手。 我正在尝试使用netbeans编写一个java应用程序,它使用imagej jar来打开一个dicom图像&处理它。我能够使用以下java代码执行此操作:

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                   
}

我可以打开图像并仅使用黑线处理它(在其上绘图)。这是因为图像作为8位灰度图像打开。我不知道如何将图像转换为RGB。 convertToRGB()在图像转换器类的处理文件夹中的ij包中可用。

我该怎么做?

1 个答案:

答案 0 :(得分:1)

的确如你所说,抽象类ImageProcessor有一个方法convertToRGB():

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

它完全符合您的需要:将ByteProcessor(8位)转换为ColorProcessor(24位)。