How to resize an image in Java with OpenCV?

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 );

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


  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 );