22

After cropping an image how can I resize it?

Mat croppedimage = cropImage( image, rect );
Mat resizeimage = croppedimage.resize( any dimension ); //need to change this line
1
  • Header says "how to resize image with opencv" but answer describes to resize a "cropped image" not a Java File or Java Buffered Image or something else. So my question is "how to get Mat object out of BufferedImage or at least File?" Sep 15, 2016 at 12:55

3 Answers 3

59

I think, you want this.

e.g.

Mat croppedimage = cropImage(image,rect);
Mat resizeimage = new Mat();
Size sz = new Size(100,100);
Imgproc.resize( croppedimage, resizeimage, sz );
1
  • 1
    What should I put for the size if i want the application to calculate the size. Eg. fx = 0.5 and fy = 0.5. Tried putting 0 and Size() for dsize but doesnt work. Nov 7, 2018 at 16:18
6

If you want to scale an image using OpenCV java then do the following:

  import static org.opencv.imgproc.Imgproc.*;
  import static org.opencv.imgcodecs.Imgcodecs.imread;

Main code:

   Mat src  =  imread("imageName.jpg");
   Mat resizeimage = new Mat();
   Size scaleSize = new Size(300,200);
   resize(src, resizeimage, scaleSize , 0, 0, INTER_AREA);

For downscaling it is recommended to use: INTER_AREA and for upscaling use INTER_CUBIC

For more details: OpenCV Ref for Resize

4
  1. Please read manual for c++ method Resize it's the same to java.

  2. You can choose a type of interpolation. In some cases it's important for a best result.

    Mat croppedImage = cropImage(image,rect);
    Mat resizeImage = new Mat(anyHeight, anyWidth, croppedImage.type());   
    int interpolation = Imgproc.INTER_CUBIC;   
    Imgproc.resize(croppedImage, resizeImage, resizeImage.size(), 0, 0, interpolation );
    

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.