一、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");
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");2、通過FileSaver類
ImagePlus imp = ...
new FileSaver(imp).saveAsTiff("/path/to/image.tif");該FileSaver類有很多其它的選擇:saveAsTiffStack。saveAsJpeg,saveAsPng。saveAsGif ...等。這算是ImageJ入門第一篇,介紹一些基本操作,它的擴張機制和實現方式都非常值得研究。希望很多其它的人參與進來。