vtomic85
vtomic85

Reputation: 611

Resize and crop an image using ImageJ

I'm trying to resize and crop an image using ImageJ. Here's the code:

ImagePlus ip1 = IJ.openImage("_Pic.jpg");
ImagePlus ip2 = IJ.openImage("_Pic.jpg");

ImageProcessor imgP1 = ip1.getProcessor();
ImageProcessor imgP2 = ip2.getProcessor();

FileSaver fs1 = new FileSaver(ip1);
FileSaver fs2 = new FileSaver(ip2);

/* Trying to resize */
imgP1.resize(100); // also tried with width and height
fs1.saveAsJpeg("Resized.jpg");

/* Trying to crop */
imgP2.setRoi(100, 100, 200, 200);
imgP2.crop();
fs2.saveAsJpeg("Cropped.jpg");

Unfortunately, the newly created files are identical to the original one.

So far I've found out how to blur, smooth, invert, translate, rotate, ..., but these two are giving me hard time. Anybody has an idea?

Upvotes: 2

Views: 8040

Answers (2)

Jan Eglinger
Jan Eglinger

Reputation: 4090

Your cross-posted question to the ImageJ forum was answered there by Stefan Helfrich:

If you take a look at the Javadocs for ImageProcessor you'll see that resize() as well as crop() return new ImageProcessor instances and do not operate on this. That's why you'll have to use the ImagePlus.setProcessor(ImageProcessor) method to add the returned ImageProcessors to ip1 and ip2.


When cross-posting like this, please always include links to the other posts, so people finding this question later will have a chance to follow the discussion.

Upvotes: 8

Mininova Web
Mininova Web

Reputation: 75

circle crop: https://youtu.be/OyiOFh1pD3k

resize: https://youtu.be/N_jddMMhzqc

combine both code.

Upvotes: -1

Related Questions