Saturday, June 25, 2016

Image Zoom Out and Zoom In using OpenCV

Posted by Md. Hanif Ali Sohag on Saturday, June 25, 2016 in | No comments
Image Zoom Out and Zoom In
In this tutorial you will learn how to zoom out or zoom in an image using OpenCV 

Code:

#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"

#include <math.h>
#include <stdlib.h>
#include <stdio.h>
#include <iostream>


using namespace cv;
using namespace std;

/// Global variables
Mat src, dst, tmp;
char* window_name = "Workshop : Pyramids Demo";

int main(int argc, char** argv)
{
	/// General instructions
	cout << "Zoom In-Out demo"<<endl;	
	cout << " ------------------"<<endl;
	cout<<" * [u] -> Zoom in"<<endl;
	cout<<" * [d] -> Zoom out"<<endl;
	cout << " * [q] -> Close program" << endl;

	/// Test image - Make sure it s divisible by 2^{n}

	src = imread("c://test//image1.jpg");

	if (!src.data)
	{
		printf(" No data! -- Exiting the program \n");
		return -1;
	}

	tmp = src;
	dst = tmp;
	/// Create window
	namedWindow(window_name, CV_WINDOW_AUTOSIZE);
	imshow(window_name, dst);
	/// Loop

	while (char(waitKey(1) != 'q'))
	{
		int c;

		c = waitKey(1);
		
		if ((char)c == 'u')
		{
			pyrUp(tmp, dst, Size(tmp.cols * 2, tmp.rows * 2));
			cout << " ** Zoom In: Image x 2"<<endl;
		}
		else if ((char)c == 'd')
		{
			pyrDown(tmp, dst, Size(tmp.cols / 2, tmp.rows / 2));
			cout<<" ** Zoom Out: Image / 2"<<endl;
		}
		imshow(window_name, dst);
		tmp = dst;
	}
	return 0;
}

Output:
Screenshot of the original, zoom out and zoom in images

0 comments:

Post a Comment