这是打开图像,反转图像并将其保存回来的示例代码:
import ij.ImagePlus;
import ij.io.FileSaver;
import ij.process.ImageProcessor;
ImagePlus imgPlus = new ImagePlus("path-to-sample.jpg");
ImageProcessor imgProcessor = imgPlus.getProcessor();
imgProcessor.invert();
FileSaver fs = new FileSaver(imgPlus);
fs.saveAsJpeg("path-to-inverted.jpg");
这是一个示例代码,显示了如何处理图像以使其成为灰度图像:
BufferedImage bufferedImage = imgProcessor.getBufferedImage();
for(int y=0;y<bufferedImage.getHeight();y++)
{
for(int x=0;x<bufferedImage.getWidth();x++)
{
Color color = new Color(bufferedImage.getRGB(x, y));
int grayLevel = (color.getRed() + color.getGreen() + color.getBlue()) / 3;
int r = grayLevel;
int g = grayLevel;
int b = grayLevel;
int rgb = (r<<16) | (g<<8) | b;
bufferedImage.setRGB(x, y, rgb);
}
}
ImagePlus grayImg = new ImagePlus("gray", bufferedImage);
fs = new FileSaver(grayImg);
fs.saveAsJpeg("path-to-gray.jpg");
我希望它可以帮助您入门:)
0
在常规的Java应用程序中,我有一个BufferedImage,我想使用ImageJ对其进行操作。我有一个正是我需要执行的宏。我怀疑第一步是制作一个ImagePlus对象,但是我不确定如何从Java内部在ImagePlus对象上运行宏。在此处找到的ImageJ教程的7.3节说:
但是并没有说明如何这样做。如果有人可以解释如何做,或将我指向有帮助的资源,我将非常感激。