Create Zooming Effect Tutorial

Advertisements


Zooming simply means enlarging a picture in a sense that the details in the image became more visible and clear.

We are going to use OpenCV function resize to apply zooming to images. It can be found under Imgproc package. Its syntax is given below:

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

In the resize function , you have to pass source image, destination image and its size, zooming factor and the interpolation method to use.

The interpolation methods available along with their description are listed below:

Sr.NoInterpolation method & description
1INTER_NEAREST
A nearest-neighbor interpolation.
2INTER_LINEAR
A bilinear interpolation (used by default).
3INTER_AREA
A resampling using pixel area relation. It may be a preferred method for image decimation, as it gives more-free results.
4INTER_CUBIC
A bicubic interpolation over 4x4 pixel neighborhood.
5INTER_LANCZOS4
A Lanczos interpolation over 8x8 pixel neighborhood.

Apart from the resize method, there are other methods provide by the Imgproc class. They are listed below:

Sr.NoMethod & Description
1cvtColor(Mat src, Mat dst, int code, int dstCn)
Converts an image from one colour space to another.
2dilate(Mat src, Mat dst, Mat kernel)
Dilates an image by using a specific structuring element.
3equalizeHist(Mat src, Mat dst)
Equalizes the histogram of a grayscale image.
4filter2D(Mat src, Mat dst, int ddepth, Mat kernel, Point anchor, double delta)
Convolves an image with the kernel.
5GaussianBlur(Mat src, Mat dst, Size ksize, double sigmaX)
Blurs an image using a Gaussian filter.
6integral(Mat src, Mat sum)
Calculates the integral of an image.

Example

The following example demonstrates the use of Imgproc class to apply zooming to an image.

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

Output

Original Image

Create Zooming Effect Tutorial

Zoomed Image(Zooming factor: 2)

Create Zooming Effect Tutorial

Advertisements
Advertisements