Image Pyramids Tutorial

Advertisements


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

We are going to use OpenCV function 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 along with their description are listed below:

Sr.NoParameters & description
1source
The Source image.
2destination
The destination image.
3destinationSize
size of the output image; by default, it is computed as Size((src.cols*2), (src.rows*2)).

Apart from the pyrUp and pyrDown method, there are other methods provided 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 perform up sampling and down sampling to 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 )
   {
      try{
         System.loadLibrary( Core.NATIVE_LIBRARY_NAME );
         Mat source = Highgui.imread("digital_image_processing.jpg",
         Highgui.CV_LOAD_IMAGE_COLOR);
         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", 
         Highgui.CV_LOAD_IMAGE_COLOR);
         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());
         }
   }
}

Output

Original Image

Image Pyramids Tutorial

On the above original image, pyrUp(UP Sampling) and pyrDown(Down Sampling) have been performed which have been shown in the output below:

PyrUP

Image Pyramids Tutorial

pyrDown

Image Pyramids Tutorial

Advertisements
Advertisements