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

22

裁剪图片后如何调整大小?

Mat croppedimage = cropImage( image, rect );
Mat resizeimage = croppedimage.resize( any dimension ); //need to change this line

标题说的是“如何使用OpenCV调整图像大小”,但答案讲述的是如何调整“裁剪过的图像”,而不是Java文件、Java缓冲图像或其他东西。因此,我的问题是“如何从BufferedImage或至少File中获取Mat对象?” - Bahadir Tasdemir
3个回答

59

我觉得,你想要这个

例如:

Mat croppedimage = cropImage(image,rect);
Mat resizeimage = new Mat();
Size sz = new Size(100,100);
Imgproc.resize( croppedimage, resizeimage, sz );

1
如果我想让应用程序计算大小,那么我应该为大小放置什么?例如,fx = 0.5和fy = 0.5。尝试将dsize设置为0和Size(),但不起作用。 - Syaiful Nizam Yahya

6
如果您想要使用OpenCV Java缩放图像,请按照以下步骤操作:

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

主要代码:

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

对于缩小图像,建议使用INTER_AREA,对于放大图像,建议使用INTER_CUBIC。

更多详情请参见: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 );
    

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接