ImageJ(java)在图像上方绘制图形

时间:2018-09-26 10:36:17

标签: java imagej

我想在imageJ中的图像上画一个圆。实际上,我已经打开了一个虚拟堆栈。通过以下代码,可以看到在调用函数draw_dashed时画出了圆圈。

  public enum Figures {
    RECTANGLE, OVAL, CIRCLE, POLYGON,
    FREEROI, LINE, POLYLINE, FREELINE, ANGLE
}

/**
 * Draws various dashed patterns with different ROI type selections.
 * Adapted from https://imagej.nih.gov/ij/plugins/dotted-line.html
 * 
 * @param imp   The imagePlus for the image
 * @param width   Width line
 * @param dashvalues   [0] Lengths [1] Space
 * 
 * @return void
*/
public static void draw_dashed(ImagePlus imp, boolean first, double width, float[] dashvalues, double x, double y, double w, double h, Figures figure) {
    if (first){
        imp.killRoi();
        imp.copy(); 
    }else{
        imp.paste();
        imp.killRoi();
    }
    ImageProcessor ip = imp.getProcessor();     
    ip = ip.convertToByteProcessor();//ip.convertToRGB();
    BufferedImage bi = ip.getBufferedImage();
    Graphics2D g2 = (Graphics2D)bi.getGraphics();
    g2.clearRect(0, 0, imp.getWidth(), imp.getHeight() );
    g2.drawImage(imp.getBufferedImage(),0,0, null);
    Color c = Toolbar.getForegroundColor();
    Stroke stroke = new BasicStroke((float)width, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL, 0, dashvalues, 0);
    g2.setStroke(stroke);
    g2.setColor(c);

    switch (figure) {
       case RECTANGLE:
          Rectangle2D rect = new Rectangle2D.Double(x, y, w, h);
          g2.draw(rect);
          break;
       case OVAL:
          Ellipse2D ellipse = new Ellipse2D.Double(x, y, w, h);
          g2.draw(ellipse);
          break;
       case CIRCLE:
           Ellipse2D circle = new Ellipse2D.Double(x, y, w, h);
           g2.draw(circle);
           break;
    default:
        System.out.println("figure "+figure.name()+" not implemented yet");
        break;
       }
    System.out.println("Before: "+imp.getNFrames()+" s"+imp.getNSlices());
    imp.setImage(bi);        
    System.out.println("After: "+imp.getNFrames()+" s"+imp.getNSlices());
 }

public void somestuff(boolean first) {
    final ImagePlus imp = WindowManager.getCurrentImage();
    float[] temps1 = {3.f, 4.f};                        
    double[] circle_position = {20.,20.}; 
    double circle_size = 5.;
    draw_dashed(imp , first, 1., temps1, circle_position[0], 
                circle_position[1], circle_size, circle_size,Figures.CIRCLE);
}

我正在执行复制图像并将其粘贴的操作,因为每次调用该函数时,新图像都会覆盖上次修改图像的时间,但我想覆盖原始图像。通过此代码,我可以看到调用函数后,我仍然拥有相同数量的帧,但是如果尝试转到另一帧,则不会更新图像。这就像是否总是被之前绘制的图像覆盖。我该怎么办?

0 个答案:

没有答案