ImageJ是世界上最快的纯Java的图像处理程序。它可以过滤一个2048x2048的图像在0.1秒内(*)。这是每秒40万像素!ImageJ的扩展通过使用内置的文本编辑器和Java编译器的ImageJ的开发插件。500多插件可用。

数据类型:8位灰度或索引色,16位无符号整数,32位浮点和RGB色彩。

文件格式:读写所有支持的数据类型为TIFF(非压缩)或原始数据。打开和保存GIF,JPEG,BMP,PNG,PGM,FITS和ASCII。打开DICOM。使用URL打开的TIFF、GIF文件、JPEG文件、DICOMs和原始数据。也可以扩展许多其他格式 的插件。
图像增强:支持平滑,锐化,边缘检测,中值滤波和阈值的8位灰度和RGB彩色图像。交互方式调整亮度和8位,16位和32位图像的对比度。
几何操作:裁剪,缩放,调整大小和旋转。翻转垂直或水平。
分析:测量面积,平均值,标准偏差,最小的选择和最大或整个图像。测量长度和角度。使用现实世界中的度量单位,如毫米。校准使用密度标准。生成柱状图和剖面图。
色彩处理:分裂一个32位彩色图像转换成RGB或HSV分量。合并8位组件成彩色图像。RGB图像转换为8位索引颜色。应用伪彩色调色板为灰度图像。

基于ImageJ的项目有、CellProfiler(细胞图像分析)、idoimaging(医学影像)、ImageSurfer(3D可视化和分析)、MIPAV(3D成像和可视化)、NITRC(神经影像学工具和资源)等。
下面对ImageJ的编程基础介绍一下。
一、ImageJ的图像剖析
ImageJ的 图像由三个部分组成:
1、ImageProcessor对象实例:持有并提供了访问像素的方法。
2、Image对象实例:即java.awt.Image画在屏幕上。
3、ImagePlus对象实例:包括所有的元数据(标题,属性)、ImageProcessor和Image。
ImageJ的 图像堆栈由四部分组成:
1、ImageStack对象实例:持有像素阵列的阵列(LUT变化通过临时实例StackProcessor)
2、Image对象实例:即java.awt.Image画在屏幕上。
3、ImageProcessor对象实例:提供访问当前切片像素。
4、ImagePlus对象实例:持有ImageStack、ImageProcessor和Image。

使用堆栈时,要谨记以下几点:
1、ImagePlus的ImageProcessor实例在任何调用setSlice(INT)时都由它的像素替换对象。
2、ImageStack.getProcessor(int)在每次调用时都返回一个新的ImageProcessor,是 一个消耗大的操作。
3、ImagePlus的java.awt.Image中在每次调用setSlice(int)时被更新。

当ImagePlus调用updateAndDraw()时重新创建 java.awt.Image对象。如果你想改变被反映当前显示在屏幕上的图像,必须修改的像素之后调用updateAndDraw()。


二、创建图像
    1、创建一个图像(详细)

int width = 400;  
int height = 400;  
ImageProcessor ip = new ByteProcessor(width, height);  
String title = "My new image";  
ImagePlus imp = new ImagePlus(title, ip);  
imp.show();  


    有几个ImageProcessor类,每个都有自己专门的构造函数。ByteProcessor,ShortProcessor,FloatProcessor和ColorProcessor。
    2、创建一个图像(简单方式)

        new ImagePlus("My new image", new ByteProcessor(400, 400)).show(); 

    3、创建任意数量任何类型的
         A、一个简单的8位的400x400像素的灰度图像
ImagePlus imp = IJ.createImage("My new image", "8-bit black", 400, 400, 1);
imp.show();
// or, without getting back a reference:
IJ.newImage("My new image", "8-bit black", 400, 400, 1);

B、堆栈的400×400像素的10色图片
ImagePlus imp = IJ.createImage("My new image", "RGB white", 400, 400, 10);  
imp.show();  
// again, without getting back a reference:  
IJ.newImage("My new image", "RGB white", 400, 400, 10); 

三、销毁图像

调用flush()将释放所使用的ImagePlus所有内存资源。
ImagePlus imp = ...  
imp.flush();  


注意:如果你持有一个从ImagePlus.getProcessor()方法获得ImageProcessor。即ImageProcessor的像素数组指针将被设置为null。你应该改为调用ImageProcessor的duplicate(),或直接通过getPixels()得到它的像素,并把它们存储在相同的尺寸的新ImageProcessor。

同样,java.awt.Image中获取自己的flush()方法调用也是如此。
四、打开图像
    所有方法都围绕着ij.io.Opener类展开。
    1、高层次的方式,从文件或URL

ImagePlus imp = IJ.openImage("/path/to/image.tif");  
imp.show();  
  
ImagePlus imp = IJ.openImage("http://www.example.org/path/to/image.tif");  
imp.show();  
  
// Without getting back a pointer, and automatically showing it:  
IJ.open("/path/to/image.tif");  
// Same but from an URL  
IJ.open("http://www.example.org/path/to/image.tif"); 


    2、从文件打开

Opener opener = new Opener();  
ImagePlus imp = opener.openImage("/path/to/image.tif");  
imp.show();  

    3、从URL打开
Opener opener = new Opener();  
ImagePlus imp = opener.openImage("http://www.example.org/path/to/image.tif");  
imp.show(); 


以上注意URL 包含http://如何的自动检测并正确解析。如果需要,可以直接调用:
ImagePlus imp = opener.openURL("http://www.example.org/path/to/image.tif");  


五、编辑像素

1、运行ImageJ命令方式,这是一个高层次的方法,像素可以通过调用ImageJ的命令编辑图像:
ImagePlus imp = ...  
// Making a binary image  
IJ.run(imp, "Convert to Mask", ""); // "" means no arguments  
  
// Resizing, opens a copy in a new window (the 'create' command keyword)  
IJ.run(imp, "Scale...", "x=0.5 y=0.5 width=344 height=345 interpolate create title=[Scaled version of " + imp.getTitle() + "]");  
  ...  

任何ImageJ命令可能被应用。你可以找出哪些命令来使用,哪些参数通过运行插件,并手动调用的ImageJ打开的图像上的菜单命令。
2、中级层次编辑方式:ImageProcessor(ROIs/selections)
在图像上绘制或填充ROI(感兴趣区域):
ImagePlus imp = ...  
ImageProcessor ip = imp.getProcessor();  
  
// Assuming 8-bit image  
  
// fill a rectangular region with 255 (on grayscale this is white color):  
Roi roi = new Roi(30, 40, 100, 100); // x, y, width, height of the rectangle  
ip.setRoi(roi);  
ip.setValue(255);  
ip.fill();  
  
// fill an oval region with 255 (white color when grayscale LUT):  
OvalRoi oroi = new OvalRoi(50, 60, 100, 150); // x, y, width, height of the oval  
ip.setRoi(oroi);  
ip.setValue(255);  
ip.fill(ip.getMask()); // notice different fill method  
                       // regular fill() would fill the entire bounding box rectangle of the OvalRoi  
// The method above is valid at least for PolygonRoi and ShapeRoi as well.  
  
  
// draw the contour of any region with 255 pixel intensity  
Roi roi = ...  
ip.setValue(255);  
ip.draw();  
  
// update screen view of the image  
imp.updateAndDraw();  


3、ROIs的一些事情:
A、有很多selection/ROI类型:Roi(矩形之一,也是所有其它类型的父类),Line, OvalRoi, PolygonRoi, PointRoi, FreehandRoi, ShapeRoi, TextRoi。另外有一些子类型,如PolygonRoi里的POLYGON、POLYLINE 类型。
B、大部分的ROI是用于编辑图像非常有用; 一些用于图像分析(Line,PointRoi,TextRoi)。
C、最强大的ROI是ShapeRoi:java.awt.geom.GeneralPath支持它,它能够存储任意数量的任何形状的不连续区域的。
D、ip.fill(ip.getMask())方法是最安全的,可在各种场合使用,只需要检查ImageProcessor的mask通过getMask()返回的不为null。

旋转,翻转和缩放图像(或者ROI)
ImagePlus imp = ...  
ImageProcessor ip = imp.getProcessor();  
  
ip.flipHorizontal();  
  
ip.flipVertical();  
  
ip.rotateLeft();  
  
ip.rotateRight();  
  
// rotate WITHOUT enlarging the canvas to fit  
double angle = 45;  
ip.setInterpolate(true); // bilinear  
ip.rotate(45);  
  
// rotate ENLARGING the canvas and filling the new areas with background color  
double angle = 45;  
IJ.run(imp, "Arbitrarily...", "angle=" + angle + " grid=1 interpolate enlarge");  
  
// scale WITHOUT modifying the canvas dimensions  
ip.setInterpolate(true); // bilinear  
ip.scale(2.0, 2.0); // in X and Y  
  
// scale ENLARGING or SHRINKING the canvas dimensions  
double sx = 2.0;  
double sy = 0.75;  
int new_width = (int)(ip.getWidth() * sx);  
int new_height = (int)(ip.getHeight() * sy);  
ip.setInterpolate(true); // bilinear  
ImageProcesor ip2 = ip.resize(new_width, new_height); // of the same type as the original  
imp.setProcessor(imp.getTitle(), ip2); // UPDATE the original ImagePlus  
  
// update screen view of the image  
imp.updateAndDraw();  

ImageProcessor类提供了绘制线条、文字和点等。看看在ImageProcessor的API。

4、低层次的编辑方式:像素数组
ImagePlus imp = ...  
ImageProcessor ip = imp.getProcessor();  
  
// Editing the pixel array  
if (imp.getType() == ImagePlus.GRAY8) {  
    byte[] pixels = (byte[])ip.getPixels();  
    // ... do whatever operations directly on the pixel array  
}  
  
// Replacing the pixel array: ONLY if same size  
if (imp.getType() == ImagePlus.GRAY8) {  
    int width = ip.getWidth();  
    int height = ip.getHeight();  
    byte[] new_pixels = new byte[width * height];  
    // set each pixel value to whatever, between -128 and 127  
    for (int y=0; y<height; y++) {  
        for (int x=0; x<width; x++) {  
            // Editing pixel at x,y position  
            new_pixels[y * width + x] = ...;  
        }  
    }  
    // update ImageProcessor to new array  
    ip.setPixels(new_pixels);  
}  
  
// Replacing the pixel array but of different length: for example, to resize 2.5 times in width and height  
int new_width = (int)(ip.getWidth() * 2.5);  
int new_height = (int)(ip.getHeight() * 2.5);  
ImageProcessor ip2 = ip.createProcessor(new_width, new_height); // of same type  
imp.setProcessor(imp.getTitle(), ip2);  
  
if (imp.getType() == ImagePlus.GRAY8) {  
    byte[] pix = (byte[])imp.getProcessor().getPixels(); // or ip2.getPixels();  
    // .. process pixels ...  
    for (int y=0; y<height; y++) {  
        for (int x=0; x<width; x++) {  
            // Editing pixel at x,y position  
            new_pixels[y * width + x] = ...;  
        }  
    }  
}  
  
// DON'T forget to update the screen image!  
imp.updateAndDraw();  

如果要显示的ImagePlus,更新图像只有必须的,

六、保存图像

1、高层次的方式
ImagePlus imp = ...  
IJ.saveAs(imp, "tif", "/path/to/image.tif");  
  
// or by using the file format extension:  
IJ.save(imp, "/path/to/image.tif");

    
很多格式都支持。在IJ类里搜索方法"saveAs"
2、通过FileSaver类
ImagePlus imp = ...  
new FileSaver(imp).saveAsTiff("/path/to/image.tif"); 

该FileSaver类有更多的选择:saveAsTiffStack,saveAsJpeg,saveAsPng,saveAsGif ...等。


这算是ImageJ入门第一篇,介绍一些基本操作,它的扩张机制和实现方式都很值得研究。希望更多的人参与进来。


ImageJ 学习第一篇的更多相关文章

  1. Java图像处理最快技术:ImageJ 学习第一篇

    ImageJ是世界上最快的纯Java的图像处理程序. 它能够过滤一个2048x2048的图像在0.1秒内(*). 这是每秒40万像素!ImageJ的扩展通过使用内置的文本编辑器和Java编译器的Ima ...

  2. LINQ to XML LINQ学习第一篇

    LINQ to XML LINQ学习第一篇 1.LINQ to XML类 以下的代码演示了如何使用LINQ to XML来快速创建一个xml: public static void CreateDoc ...

  3. 从.Net到Java学习第一篇——开篇

    以前我常说,公司用什么技术我就学什么.可是对于java,我曾经一度以为“学java是不可能的,这辈子不可能学java的.”结果,一遇到公司转java,我就不得不跑路了,于是乎,回头一看N家公司交过社保 ...

  4. Java并发包下锁学习第一篇:介绍及学习安排

    Java并发包下锁学习第一篇:介绍及学习安排 在Java并发编程中,实现锁的方式有两种,分别是:可以使用同步锁(synchronized关键字的锁),还有lock接口下的锁.从今天起,凯哥将带领大家一 ...

  5. JVM学习第一篇思考:一个Java代码是怎么运行起来的-上篇

    JVM学习第一篇思考:一个Java代码是怎么运行起来的-上篇 作为一个使用Java语言开发的程序员,我们都知道,要想运行Java程序至少需要安装JRE(安装JDK也没问题).我们也知道我们Java程序 ...

  6. Golang学习-第一篇 Golang的简单介绍及Windows环境下安装、部署

    序言 这是本人博客园第一篇文章,写的不到位之处,希望各位看客们谅解. 本人一直从事.NET的开发工作,最近在学习Golang,所以想着之前学习的过程中都没怎么好好的将学习过程记录下来.深感惋惜! 现在 ...

  7. Python学习第一篇

    好久没有来博客园了,今天开始写自己学习Python和Hadoop的学习笔记吧.今天写第一篇,Python学习,其他的环境部署都不说了,可以参考其他的博客. 今天根据MachineLearning里面的 ...

  8. Android基础学习第一篇—Project目录结构

    写在前面的话: 1. 最近在自学Android,也是边看书边写一些Demo,由于知识点越来越多,脑子越来越记不清楚,所以打算写成读书笔记,供以后查看,也算是把自己学到所理解的东西写出来,献丑,如有不对 ...

  9. H264学习第一篇(编码结构分析)

    学习H264之前,最好阅读一下维基百科中有关H264的相关介绍,里面包含了其的发展历程.主要特点.参考文献.参考网站等. 研究H264的主要文件包括两份参考手册(一份是语法结构参考手册,一份是JM开发 ...

随机推荐

  1. mysql max_allowed_packet

    系统linux > show global max_allowed_packet; >set global max_allowed_packet=1024*1024: 退出mysql,重启 ...

  2. li标签的点击范围

    <div class="login_menu_choose"> <ul class="cursor">                & ...

  3. PHP memcache 安装问题

    第一步:下载  memcached.exe 第二步:c:\window\system32\cmd.exe  管理员启动 第三步: memcached.exe -d start (错误) failed ...

  4. 转载《Android-TabHost 选项卡功能用法详解》

    一. TabHost介绍 TabHost组件可以在界面中存放多个选项卡, 很多软件都使用了改组件进行设计; 1. TabHost常用组件 TabWidget : 该组件就是TabHost标签页中上部 ...

  5. Java和C++的虚函数的异同

    参考博客:点我 要点:Java中的普通函数默认为虚函数,因此动态绑定的行为是默认的,而C++必须将方法声明为虚函数(virtual关键字),执行时才会进行动态绑定,详细区别可参考代码以及注释. 代码大 ...

  6. 关于Cookie的 工具类

    import java.io.UnsupportedEncodingException; import java.net.URLDecoder; import java.net.URLEncoder; ...

  7. 用C#,SQL Server编写的音乐播放软件

    主界面代码 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data ...

  8. javaIO-学习笔记

    package IOTest; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream ...

  9. HDU 5964 平行四边形

    假设直线L和L'相交于原点O.假设S ={s1,s2,...,sn}是平面上的n个点.你打 算找四个点满足如下条件:1.   A ∈ L 而 A' ∈ L'.2.   B,B'都属于S;即 B∈S 且 ...

  10. Thread系列之Thread.Sleep(0)

    线程这一概念,可以理解成进程中的一个小单元.这个单元是一个独立的执行单元,但是与进程中的其他线程共享进程中的内存单元. 由于Cpu资源是有限的,所以进程中的多个线程要抢占Cpu,这也导致进程中的多个线 ...