Created
September 14, 2011 20:57
-
-
Save tc/1217766 to your computer and use it in GitHub Desktop.
resize and crop using imagej -java version
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package imagej; | |
import ij.*; | |
import ij.io.*; | |
import ij.process.*; | |
import java.io.*; | |
import javax.imageio.*; | |
import javax.imageio.stream.*; | |
import java.awt.image.BufferedImage; | |
public class AppTest{ | |
public static void main(String [] args) throws Exception{ | |
String url = "http://imagej.nih.gov/ij/images/lena-std.tif"; | |
ImagePlus imp = IJ.openImage(url); | |
cropAndResize(imp, 400, 100); | |
} | |
public static void cropAndResize(ImagePlus imp, int targetWidth, int targetHeight) throws Exception{ | |
ImageProcessor ip = imp.getProcessor(); | |
System.out.println("size1: "+ip.getWidth()+"x"+ip.getHeight()); | |
ip.setInterpolationMethod(ImageProcessor.BILINEAR); | |
ip = ip.resize(targetWidth * 2, targetHeight * 2); | |
System.out.println("size2: "+ip.getWidth()+"x"+ip.getHeight()); | |
int cropX = ip.getWidth() / 2; | |
int cropY = ip.getHeight() / 2; | |
ip.setRoi(cropX, cropY, targetWidth, targetHeight); | |
ip = ip.crop(); | |
System.out.println("size3: "+ip.getWidth()+"x"+ip.getHeight()); | |
BufferedImage croppedImage = ip.getBufferedImage(); | |
System.out.println("size4: "+ip.getWidth()+"x"+ip.getHeight()); | |
new ImagePlus("croppedImage", croppedImage).show(); | |
ImageIO.write(croppedImage, "jpg", new File("cropped.jpg")); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi, Do you have full code for the above? I would like to see the class for ImageProcessor & ImagePlus?
Thanks,
Jam