Download: ij152.zip
1. Image Create and Save
1 2 3 4 5 6 7 8 9 10 11 12 13 | public static void createJpegImage(int width, int height) { ImagePlus imp = IJ.createImage("created.jpg", "jpg", width, height, 32); ImageProcessor ip = imp.getProcessor(); ip.setColor(Color.BLUE); ip.setLineWidth(4); ip.drawRect(10, 10, imp.getWidth() - 20, imp.getHeight() - 20); IJ.save(imp, "D:/created.jpg"); /* extension: * ".tif", ".jpg", ".gif", ".zip", ".raw", ".avi", ".bmp", * ".fits", ".pgm", ".png", ".lut", ".roi", ".txt" */ //imp.show(); } |
1 2 3 4 5 6 7 8 9 10 11 12 | public static void createJpegImage2(int width, int height) { ImagePlus imp = IJ.createImage("created.jpg", "jpg", width, height, 32); ImageProcessor ip = imp.getProcessor(); ip.setColor(Color.BLUE); ip.setLineWidth(4); ip.drawRect(10, 10, imp.getWidth() - 20, imp.getHeight() - 20); IJ.saveAs(imp, "jpeg","D:/created.jpg"); /* type: * "tiff", "jpeg", "gif", "zip", "raw", "avi", "bmp", "fits", "pgm", * "png", "text image", "lut", "text" */ } |
Output: D:\created.jpg
2. Image Load and Convert To Gray Scale
1 2 3 4 5 6 7 | public static void convertToGray() { ImagePlus ip = IJ.openImage("D:/lena.png"); ImageConverter ic = new ImageConverter(ip); FileSaver fs = new FileSaver(ip); ic.convertToGray8(); fs.saveAsJpeg("D:/lena_gray.jpg"); } |
Input: lena.png
Output: lena_gray.jpg
3. Image Convert to Jpeg
1 2 3 4 5 6 7 | public static void convertToJpeg(int quality) { ImagePlus ip = IJ.openImage("D:/lena.png"); FileSaver fs = new FileSaver(ip); FileSaver.setJpegQuality(quality); FileSaver.getJpegQuality(); fs.saveAsJpeg("D:/lena.jpg"); } |
4. Image Resize
4.1 Image Resize by Width (with the aspect ratio maintained)
1 2 3 4 5 6 7 8 | public static void resizeImage() { ImagePlus imp = IJ.openImage("D:/lena.png"); ImageProcessor ip = imp.getProcessor(); ip = ip.resize(imp.getWidth()/2); imp.setProcessor(ip); FileSaver fs = new FileSaver(imp); fs.saveAsPng("D:/lena_half.png"); } |
4.2 Image Resize by Width and Height
1 2 3 4 5 6 7 | public static void resizeImage2() { ImagePlus imp = IJ.openImage("D:/lena.png"); ImageProcessor ip = imp.getProcessor(); ip = ip.resize(imp.getWidth(),imp.getHeight()/2); imp.setProcessor(ip); IJ.save(imp,"D:/lena_resized.png"); } |
Output: lena_resized.png
4.3 Image Rescale
1 2 3 4 5 6 | public static void rescaleImage() { ImagePlus imp = IJ.openImage("D:/lena.png"); ImageProcessor ip = imp.getProcessor(); ip.scale(1.0,0.5); //xScale, yScale IJ.save(imp,"D:/lena_rescaled.png"); } |
Output: lena_rescaled.png (image size is not changed)
5. Image Crop
1 2 3 4 5 6 7 8 9 | public static void cropImage() { ImagePlus imp = IJ.openImage("D:/lena.png"); ImageProcessor ip = imp.getProcessor(); ip.setRoi(0, 0, imp.getWidth()/2, imp.getHeight()/2); //x, y, w, h ip = ip.crop(); imp.setProcessor(ip); FileSaver fs = new FileSaver(imp); fs.saveAsPng("D:/lena_crop.png"); } |
Output: lena_crop.jpg
6. Image Smooth
1 2 3 4 5 6 7 | public static void smoothImage() { ImagePlus imp = IJ.openImage("D:/lena.png"); for( int i=0; i < 10; i++ ) imp.getProcessor().smooth(); FileSaver fs = new FileSaver(imp); fs.saveAsPng("D:/lena_smooth.png"); } |
Input: lena.png
7. Image Rotate
1 2 3 4 5 6 7 8 9 10 11 12 13 | public static void rotateImage() { ImagePlus imp = IJ.openImage("D:/lena.png"); ImageProcessor ip = imp.getProcessor(); ImageProcessor ip_left = ip.rotateLeft(); ImageProcessor ip_right = ip.rotateRight(); imp.setProcessor(ip_left); IJ.save(imp,"D:/lena_left.png"); imp.setProcessor(ip_right); IJ.save(imp,"D:/lena_right.png"); ip.rotate(45); imp.setProcessor(ip); IJ.save(imp,"D:/lena_rotated.png"); } |
Input: lena.png
Output: lena_right.png
Output: lena_rotated.png
8. Image Overlay
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | public static void overlayImage() { ImagePlus imp = new ImagePlus("D:/lena.png"); BufferedImage bi = imp.getBufferedImage(); ImagePlus imp2 = imp.duplicate(); imp2.setProcessor(imp2.getProcessor().resize(imp2.getWidth()/2)); new ImageConverter(imp2).convertToGray8(); BufferedImage bi2 = imp2.getBufferedImage(); Graphics2D g = bi.createGraphics(); g.drawImage(bi2, 0, 0, null); ImagePlus imp3 = new ImagePlus("combined", bi); FileSaver fs = new FileSaver(imp3); fs.saveAsJpeg("D:/lena_overlay.jpg"); } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | public static void overlayImage2() { ImagePlus imp = new ImagePlus("D:/lena.png"); BufferedImage bi = imp.getBufferedImage(); ImagePlus imp2 = imp.duplicate(); imp2.setProcessor(imp2.getProcessor().resize(imp2.getWidth()/2)); new ImageConverter(imp2).convertToGray8(); BufferedImage bi2 = imp2.getBufferedImage(); BufferedImage combined = new BufferedImage(imp.getWidth(), imp.getHeight(), BufferedImage.TYPE_INT_ARGB); Graphics2D g = combined.createGraphics(); g.drawImage(bi, 0, 0, null); g.drawImage(bi2, 0, 0, null); ImagePlus imp3 = new ImagePlus("combined", combined); FileSaver fs = new FileSaver(imp3); fs.saveAsJpeg("D:/lena_overlay2.jpg"); } |
Input: lena.png
output: lena_overlay.png
9. Image Partial Edit
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | public static void grayPartialImage() { ImagePlus imp = new ImagePlus("D:/lena.png"); ImageProcessor ip = imp.getProcessor(); BufferedImage bufferedImage = imp.getBufferedImage(); for(int y=0; y < bufferedImage.getHeight()/2; y++) { for(int x=0; x < bufferedImage.getWidth()/2; 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); FileSaver fs = new FileSaver(grayImg); fs.saveAsJpeg("D:/lena_partial.jpg"); } |
Output:: lena_partial.jpg
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | public static void clearPartialImage() { ImagePlus imp = new ImagePlus("D:/lena.png"); ImageProcessor ip = imp.getProcessor(); BufferedImage bufferedImage = imp.getBufferedImage(); for(int y=0; y < bufferedImage.getHeight()/2; y++) { for(int x=0; x < bufferedImage.getWidth()/2; x++) { int rgb = 0x00ffffff; bufferedImage.setRGB(x, y, rgb); } } ImagePlus grayImg = new ImagePlus("gray", bufferedImage); FileSaver fs = new FileSaver(grayImg); fs.saveAsJpeg("D:/lena_white.jpg"); } |
Output: lena_white.jpg
댓글 없음:
댓글 쓰기