OpenCV - Image Pyramids



Pyramid is an operation on an image where,

  • An input image is initially smoothed using a particular smoothing filter (ex: Gaussian, Laplacian) and then the smoothed image is subsampled.

  • This process is repeated multiple times.

During the pyramid operation, the smoothness of the image is increased and the resolution (size) is decreased.

Pyramid Up

In Pyramid Up, the image is initially up-sampled and then blurred. You can perform Pyramid Up operation on an image using the pyrUP() method of the imgproc class. Following is the syntax of this method −

pyrUp(src, dst, dstsize, borderType)

This method accepts the following parameters −

  • src − An object of the class Mat representing the source (input) image.

  • mat − An object of the class Mat representing the destination (output) image.

  • size − An object of the class Size representing the size to which the image is to be increased or decreased.

  • borderType − A variable of integer type representing the type of border to be used.

Example

The following program demonstrates how to perform the Pyramid Up operation on an image.

import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.core.Size;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;

public class PyramidUp {
   public static void main( String[] args ) {
      // Loading the OpenCV core library
      System.loadLibrary( Core.NATIVE_LIBRARY_NAME );

      // Reading the Image from the file and storing it in to a Matrix object
      String file ="E:/OpenCV/chap13/pyramid_input.jpg";
      Mat src = Imgcodecs.imread(file);

      // Creating an empty matrix to store the result
      Mat dst = new Mat();

      // Applying pyrUp on the Image
      Imgproc.pyrUp(src, dst, new Size(src.cols()*2,  src.rows()*2), Core.BORDER_DEFAULT);

      // Writing the image
      Imgcodecs.imwrite("E:/OpenCV/chap13/pyrUp_output.jpg", dst);

      System.out.println("Image Processed");
   }
}

Assume that following is the input image pyramid_input.jpg specified in the above program.

Pyramid Input

Output

On executing the program, you will get the following output −

Image Processed

If you open the specified path, you can observe the output image as follows −

Pyramid Up Output

Pyramid Down

In Pyramid Down, the image is initially blurred and then down-sampled. You can perform Pyramid Down operation on an image using the pyrDown() method of the imgproc class. Following is the syntax of this method −

pyrDown(src, dst, dstsize, borderType)

This method accepts the following parameters −

  • src − An object of the class Mat representing the source (input) image.

  • mat − An object of the class Mat representing the destination (output) image.

  • size − An object of the class Size representing the size to which the image is to be increased or decreased.

  • borderType − A variable of integer type representing the type of border to be used.

Example

The following program demonstrates how to perform the Pyramid Down operation on an image.

import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.core.Size;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;

public class PyramidDown {
   public static void main( String[] args ) {
      // Loading the OpenCV core library
      System.loadLibrary( Core.NATIVE_LIBRARY_NAME );

      // Reading the Image from the file and storing it in to a Matrix object
      String file ="E:/OpenCV/chap13/pyramid_input.jpg";
      Mat src = Imgcodecs.imread(file);

      // Creating an empty matrix to store the result
      Mat dst = new Mat();

      // Applying pyrDown on the Image
      Imgproc.pyrDown(src, dst, new Size(src.cols()/2,  src.rows()/2),
         Core.BORDER_DEFAULT);

      // Writing the image
      Imgcodecs.imwrite("E:/OpenCV/chap13/pyrDown_output.jpg", dst);

      System.out.println("Image Processed");
   } 
}

Assume that following is the input image pyramid_input.jpg specified in the above program.

Pyramid Input

Output

On executing the program, you will get the following output −

Image Processed

If you open the specified path, you can observe the output image as follows −

Pyramid Down Output

Mean Shift Filtering

In Mean Shifting pyramid operation, an initial step of mean shift segmentation of an image is carried out.

You can perform pyramid Mean Shift Filtering operation on an image using the pyrDown() method of the imgproc class. Following is the syntax of this method.

pyrMeanShiftFiltering(src, dst, sp, sr)

This method accepts the following parameters −

  • src − An object of the class Mat representing the source (input) image.

  • mat − An object of the class Mat representing the destination (output) image.

  • sp − A variable of the type double representing the spatial window radius.

  • sr − A variable of the type double representing the color window radius.

Example

The following program demonstrates how to perform a Mean Shift Filtering operation on a given image.

import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;

public class PyramidMeanShift {
   public static void main( String[] args ) {
      // Loading the OpenCV core library
      System.loadLibrary( Core.NATIVE_LIBRARY_NAME );

      // Reading the Image from the file and storing it in to a Matrix object
      String file ="E:/OpenCV/chap13/pyramid_input.jpg";
      Mat src = Imgcodecs.imread(file);

      // Creating an empty matrix to store the result
      Mat dst = new Mat();

      // Applying meanShifting on the Image
      Imgproc.pyrMeanShiftFiltering(src, dst, 200, 300);

      // Writing the image
      Imgcodecs.imwrite("E:/OpenCV/chap13/meanShift_output.jpg", dst);
      
      System.out.println("Image Processed");
   } 
}

Assume that following is the input image pyramid_input.jpg specified in the above program.

Pyramid Input

Output

On executing the program, you will get the following output −

Image Processed

If you open the specified path, you can observe the output image as follows −

Mean Shift Filtering Output
Advertisements