Java DIP - Image Pyramids

Image pyramid is nothing but a method to display a multi-resolution image. The lowermost layer is a highest-resolution version of image and the topmost layer is a lowest-resolution version of the image. Image pyramids are used to handle image at different scales.

In this chapter we perform some down sampling and up sampling on images.

We use OpenCV functions pyrUp and pyrDown. They can be found under Imgproc package. Its syntax is given below −

Imgproc.pyrUp(source, destination, destinationSize);
Imgproc.pyrDown(source, destination,destinationSize);

The parameters are described below −

Sr.No. Parameter & Description


It is the source image.



It is the destination image.



It is the size of the output image. By default, it is computed as Size((src.cols*2), (src.rows*2)).

Apart from the pyrUp and pyrDown methods, there are other methods provided by the Imgproc class. They are described briefly −

Sr.No. Method & Description

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

It converts an image from one color space to another.


dilate(Mat src, Mat dst, Mat kernel)

It dilates an image by using a specific structuring element.


equalizeHist(Mat src, Mat dst)

It equalizes the histogram of a grayscale image.


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

It convolves an image with the kernel.


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

It blurs an image using a Gaussian filter.


integral(Mat src, Mat sum)

It calculates the integral of an image.


The following example demonstrates the use of Imgproc class to perform up sampling and down sampling on an image.

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

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

public class main {
   public static void main( String[] args ) {
         System.loadLibrary( Core.NATIVE_LIBRARY_NAME );
         Mat source = Highgui.imread("digital_image_processing.jpg",
         Mat destination1 = new Mat(source.rows()*2, source.cols()*2,source.type());
         destination1 = source;
         Imgproc.pyrUp(source, destination1, new  Size(source.cols()*2   source.rows()*2));
         Highgui.imwrite("pyrUp.jpg", destination1);
         source = Highgui.imread("digital_image_processing.jpg", 
         Mat destination = new Mat(source.rows()/2,source.cols()/2, source.type());
         destination = source;
         Imgproc.pyrDown(source, destination, new Size(source.cols()/2,  source.rows()/2));
         Highgui.imwrite("pyrDown.jpg", destination);
      } catch (Exception e) { 
         System.out.println("error: " + e.getMessage());


When you execute the given code, the following output is seen −

Original Image

Image Pyramids Tutorial

On the original image, pyrUp(UP Sampling) and pyrDown(Down Sampling) are performed. The output after sampling is as shown below −

PyrUP Image

Image Pyramids Tutorial

pyrDown Image

Image Pyramids Tutorial