如何使用 OpenCV 在 Java 中调整图像大小?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20902290/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 05:09:58  来源:igfitidea点击:

How to resize an image in Java with OpenCV?

javaopencvimage-resizing

提问by midhun0003

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

采纳答案by berak

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

回答by Alexander Mckey

  1. Please read manual for c++ method Resizeit'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 );
    
  1. 请阅读 c++ 方法的手册Resize它与 java 相同。

  2. 您可以选择一种插值类型。在某些情况下,获得最佳结果很重要。

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

回答by asmmahmud

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

如果要使用 OpenCV java缩放图像,请执行以下操作:

  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

对于缩小,建议使用:INTER_AREA,对于放大使用 INTER_CUBIC

For more details: OpenCV Ref for Resize

有关更多详细信息:用于调整大小的 OpenCV 参考