ImageJ 学习第一篇
ImageJ是世界上最快的纯Java的图像处理程序。它可以过滤一个2048x2048的图像在0.1秒内(*)。这是每秒40万像素!ImageJ的扩展通过使用内置的文本编辑器和Java编译器的ImageJ的开发插件。500多插件可用。
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()。
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();
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()方法调用也是如此。
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");
Opener opener = new Opener();
ImagePlus imp = opener.openImage("/path/to/image.tif");
imp.show();
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");2、通过FileSaver类
ImagePlus imp = ...
new FileSaver(imp).saveAsTiff("/path/to/image.tif");该FileSaver类有更多的选择:saveAsTiffStack,saveAsJpeg,saveAsPng,saveAsGif ...等。这算是ImageJ入门第一篇,介绍一些基本操作,它的扩张机制和实现方式都很值得研究。希望更多的人参与进来。
ImageJ 学习第一篇的更多相关文章
- Java图像处理最快技术:ImageJ 学习第一篇
ImageJ是世界上最快的纯Java的图像处理程序. 它能够过滤一个2048x2048的图像在0.1秒内(*). 这是每秒40万像素!ImageJ的扩展通过使用内置的文本编辑器和Java编译器的Ima ...
- LINQ to XML LINQ学习第一篇
LINQ to XML LINQ学习第一篇 1.LINQ to XML类 以下的代码演示了如何使用LINQ to XML来快速创建一个xml: public static void CreateDoc ...
- 从.Net到Java学习第一篇——开篇
以前我常说,公司用什么技术我就学什么.可是对于java,我曾经一度以为“学java是不可能的,这辈子不可能学java的.”结果,一遇到公司转java,我就不得不跑路了,于是乎,回头一看N家公司交过社保 ...
- Java并发包下锁学习第一篇:介绍及学习安排
Java并发包下锁学习第一篇:介绍及学习安排 在Java并发编程中,实现锁的方式有两种,分别是:可以使用同步锁(synchronized关键字的锁),还有lock接口下的锁.从今天起,凯哥将带领大家一 ...
- JVM学习第一篇思考:一个Java代码是怎么运行起来的-上篇
JVM学习第一篇思考:一个Java代码是怎么运行起来的-上篇 作为一个使用Java语言开发的程序员,我们都知道,要想运行Java程序至少需要安装JRE(安装JDK也没问题).我们也知道我们Java程序 ...
- Golang学习-第一篇 Golang的简单介绍及Windows环境下安装、部署
序言 这是本人博客园第一篇文章,写的不到位之处,希望各位看客们谅解. 本人一直从事.NET的开发工作,最近在学习Golang,所以想着之前学习的过程中都没怎么好好的将学习过程记录下来.深感惋惜! 现在 ...
- Python学习第一篇
好久没有来博客园了,今天开始写自己学习Python和Hadoop的学习笔记吧.今天写第一篇,Python学习,其他的环境部署都不说了,可以参考其他的博客. 今天根据MachineLearning里面的 ...
- Android基础学习第一篇—Project目录结构
写在前面的话: 1. 最近在自学Android,也是边看书边写一些Demo,由于知识点越来越多,脑子越来越记不清楚,所以打算写成读书笔记,供以后查看,也算是把自己学到所理解的东西写出来,献丑,如有不对 ...
- H264学习第一篇(编码结构分析)
学习H264之前,最好阅读一下维基百科中有关H264的相关介绍,里面包含了其的发展历程.主要特点.参考文献.参考网站等. 研究H264的主要文件包括两份参考手册(一份是语法结构参考手册,一份是JM开发 ...
随机推荐
- mysql max_allowed_packet
系统linux > show global max_allowed_packet; >set global max_allowed_packet=1024*1024: 退出mysql,重启 ...
- li标签的点击范围
<div class="login_menu_choose"> <ul class="cursor"> & ...
- PHP memcache 安装问题
第一步:下载 memcached.exe 第二步:c:\window\system32\cmd.exe 管理员启动 第三步: memcached.exe -d start (错误) failed ...
- 转载《Android-TabHost 选项卡功能用法详解》
一. TabHost介绍 TabHost组件可以在界面中存放多个选项卡, 很多软件都使用了改组件进行设计; 1. TabHost常用组件 TabWidget : 该组件就是TabHost标签页中上部 ...
- Java和C++的虚函数的异同
参考博客:点我 要点:Java中的普通函数默认为虚函数,因此动态绑定的行为是默认的,而C++必须将方法声明为虚函数(virtual关键字),执行时才会进行动态绑定,详细区别可参考代码以及注释. 代码大 ...
- 关于Cookie的 工具类
import java.io.UnsupportedEncodingException; import java.net.URLDecoder; import java.net.URLEncoder; ...
- 用C#,SQL Server编写的音乐播放软件
主界面代码 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data ...
- javaIO-学习笔记
package IOTest; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream ...
- HDU 5964 平行四边形
假设直线L和L'相交于原点O.假设S ={s1,s2,...,sn}是平面上的n个点.你打 算找四个点满足如下条件:1. A ∈ L 而 A' ∈ L'.2. B,B'都属于S;即 B∈S 且 ...
- Thread系列之Thread.Sleep(0)
线程这一概念,可以理解成进程中的一个小单元.这个单元是一个独立的执行单元,但是与进程中的其他线程共享进程中的内存单元. 由于Cpu资源是有限的,所以进程中的多个线程要抢占Cpu,这也导致进程中的多个线 ...