Image Pyramid – Expanding Image

Image pyramid is widely used in computer vision which allow us to convert an image / frame to a different size ( smaller or larger ) than its original size. There are two possible method available in openCV which allow us to up size and down size an image.

  • A collection of images, all of them are transformed from an original image / frame ( either down sampled or up sampled )
  • Provides two type of image pyramids
    • Gaussian pyramid ( used to down sample image / frame)
    • Laplacian pyramid ( use to up sample image / frame)
image pyramid

Image Pyramid Example. source of the image

Down Size Image

A set of reduced images, added on top of each other will construct a pyramid (higher the layer, smaller the size ).  To produce the reduced layer of images the two steps are involved in Gaussian pyramid (Convolve with  a Gaussian kernel and remove every even number row and column) . The resulting image will be exactly one quarter of its predecessor. Iterating on original image will produce different layers of pyramid. OpenCV provides pyrDown function ( using Gaussian pyramid ) to down size the image.

pyrDown(frameSrc,frameDest,Size( frameTmp.cols/2, frameTmp.rows/2 ));

Here is the complete code to down sample the image or video frame.

void ExpandFrame(){
	VideoCapture cap("seq_hotel.avi");

	cv::Mat frameSrc;
	if(!cap.isOpened())
	{
		cout<<"Capture Not Opened"<<endl; } while(1) { cap>>frameSrc;

		if(frameSrc.empty())
		{
			cout<<"Capture Finished"<<endl; 
			break;
		} 
		Mat frameTmp = frameSrc;
		Mat frameDst;
		pyrDown( frameSrc, frameDst, Size( frameTmp.cols/2, frameTmp.rows/2 ) );
		cv::imshow("Up / Down Sampled Frame", frameDst);
		waitKey(1);
	}
}

Up Size Image

To up size the image twice of its original size in each dimension, add even rows and columns filled with zeros and then approximate the values of missing image pixels by performing convolution with the similar kernal discussed above. OpenCV provides pyrUp function ( using Gaussian pyramid ) to up size the image.

pyrUp(frameSrc,frameDest,Size( frameTmp.cols*2, frameTmp.rows*2 ));

Following is a sample program that can be used to up sample the video frames.

void ExpandFrame(){
	VideoCapture cap("seq_hotel.avi");

	cv::Mat frameSrc;
	if(!cap.isOpened())
	{
		cout<<"Capture Not Opened"<<endl; } while(1) { cap>>frameSrc;

		if(frameSrc.empty())
		{
			cout<<"Capture Finished"<<endl; 
			break;
		} 
		Mat frameTmp = frameSrc;
		Mat frameDst;
		pyrUp( frameSrc, frameDst, Size( frameTmp.cols*2, frameTmp.rows*2 ) );
		cv::imshow("Up / Down Sampled Frame", frameDst);
		waitKey(1);
	}
}

Output of the above program can shown in the following video tutorial.

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.