📜  Java DIP-创建缩放效果

📅  最后修改于: 2020-12-14 05:44:00             🧑  作者: Mango


缩放是放大图像以使图像中的细节变得更清晰可见的过程。

我们使用OpenCV函数调整大小将缩放应用于图像。可以在Imgproc软件包下找到。其语法如下-

Imgproc.resize(source,destination, destination.size(),zoomFactor,zoomFactor,Interpolation);

在resize函数,我们传递源图像,目标图像及其大小,缩放系数以及要使用的插值方法。

可用的插值方法如下所述-

Sr.No. Interpolation method & Description
1

INTER_NEAREST

It is nearest-neighbour interpolation.

2

INTER_LINEAR

It is bilinear interpolation (used by default).

3

INTER_AREA

It is resampling using pixel area relation. It may be a preferred method for image decimation, as it gives more-free results.

4

INTER_CUBIC

It is a bi-cubic interpolation over 4×4 pixel neighbourhood.

5

INTER_LANCZOS4

It is a Lanczos interpolation over 8×8 pixel neighbourhood.

除了调整大小方法外,Imgproc类还提供其他方法。他们简要描述-

Sr.No. Method & Description
1

cvtColor(Mat src, Mat dst, int code, int dstCn)

It converts an image from one color space to another.

2

dilate(Mat src, Mat dst, Mat kernel)

It dilates an image by using a specific structuring element.

3

equalizeHist(Mat src, Mat dst)

It equalizes the histogram of a grayscale image.

4

filter2D(Mat src, Mat dst, int ddepth, Mat kernel, Point anchor, double delta)

It convolves an image with the kernel.

5

GaussianBlur(Mat src, Mat dst, Size ksize, double sigmaX)

It blurs an image using a Gaussian filter.

6

integral(Mat src, Mat sum)

It calculates the integral of an image.

以下示例演示了如何使用Imgproc类将缩放应用于图像。

import org.opencv.core.Core;
import org.opencv.core.Mat;

import org.opencv.highgui.Highgui;
import org.opencv.imgproc.Imgproc;

public class Main {
   public static void main( String[] args ) {
   
      try {
         int zoomingFactor = 2;
         System.loadLibrary( Core.NATIVE_LIBRARY_NAME );
         
         Mat source = Highgui.imread("image.jpg", Highgui.CV_LOAD_IMAGE_GRAYSCALE);
         Mat destination = new Mat(source.rows() * zoomingFactor, source.cols()*  zoomingFactor,source.type());  
         
         Imgproc.resize(source, destination, destination.size(),  zoomingFactor,zoomingFactor,Imgproc.INTER_NEAREST);
         Highgui.imwrite("zoomed.jpg", destination);
         
      } catch (Exception e) {
         System.out.println("Error: "+e.getMessage());
      }
   }
}

输出

当您执行给定的代码时,将看到以下输出-

原始图片

创建缩放效果教程

图像放大(缩放系数− 2)

创建缩放效果教程