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
midhun0003
  • 603
  • 1
  • 10
  • 26
  • 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?" – Bahadir Tasdemir Sep 15 '16 at 12:55

3 Answers3

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 );
berak
  • 39,159
  • 9
  • 91
  • 89
  • 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. – Syaiful Nizam Yahya Nov 07 '18 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

asmmahmud
  • 4,844
  • 2
  • 40
  • 47
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 );