There are many times when we actually need to zoom-in an image or zoom-out an image.
The same can be done very easily in opencv.
The below code is for zooming the image by two
Refer the code below:
Many times we just need not zoom the image twice,but instead mutliple times.
The code given below takes input from the user in terms of factors which we need to zoom the image.
Refer the code below:
The same thing of zooming the image can be done by adding Trackbar to it.Thus by dragging the cursor on the Trackbar we can dynamically change the zooming factor of an image.
Refer the code below:
Output:
The same can be done very easily in opencv.
The below code is for zooming the image by two
Refer the code below:
// OpenCV Image Zooming Tutorial #include <opencv2/core/core.hpp> #include <opencv2/highgui/highgui.hpp> #include <iostream> using namespace cv; using namespace std; int main() { Mat src1,src2; src1 = imread("C:\\Users\\arjun\\Desktop\\arj.jpg",CV_LOAD_IMAGE_COLOR); if( !src1.data ) { printf("Error loading src1 \n"); return -1;} cout<<"src1.rows="<<src1.rows<<endl; cout<<"src1.cols="<<src1.cols<<endl; src2 = Mat::zeros(src1.rows*2,src1.cols*2, CV_8UC3); cout<<"Magnification factor is 2"<<endl; for (int i=0; i<src1.cols ; i++) { for (int j=0 ; j<src1.rows ; j++) { Vec3b color1 = src1.at<Vec3b>(Point(i,j)); for(int p=0;p<2;p++) { for(int q=0;q<2;q++) { Vec3b color2 = src2.at<Vec3b>(Point(i,j)); color2.val[0] = color1.val[0]; color2.val[1] = color1.val[1]; color2.val[2] = color1.val[2]; src2.at<Vec3b>(Point(i*2+p,j*2+q)) = color2; } } } } //imwrite("C:\\Users\\arjun\\Desktop\\zoomed.jpg",src2); namedWindow("Original Image",CV_WINDOW_AUTOSIZE); imshow("Original Image", src1); namedWindow("Scaled Image",CV_WINDOW_AUTOSIZE); imshow("Scaled Image", src2); waitKey(0); return 0; }
Many times we just need not zoom the image twice,but instead mutliple times.
The code given below takes input from the user in terms of factors which we need to zoom the image.
Refer the code below:
// OpenCV Image Zooming Tutorial #include <opencv2/core/core.hpp> #include <opencv2/highgui/highgui.hpp> #include <iostream> using namespace cv; using namespace std; int main() { Mat src1,src2; src1 = imread("C:\\Users\\arjun\\Desktop\\arj.jpg",CV_LOAD_IMAGE_COLOR); if( !src1.data ) { printf("Error loading src1 \n"); return -1;} int a; do //while(a<=0) { cout<<"Enter the Zooming factor greater than 0 "<<endl; cin>>a; cout<<"Magnification factor is: "<<a<<endl; if( a<=0 ) { cout<<"Invalid values... Please re-enter the correct values \n"; } }while(a<=0); src2 = Mat::zeros(src1.rows*a,src1.cols*a, CV_8UC3); for (int i=0; i<src1.cols ; i++) { for (int j=0 ; j<src1.rows ; j++) { Vec3b color1 = src1.at<Vec3b>(Point(i,j)); for(int p=0;p<a;p++) { for(int q=0;q<a;q++) { Vec3b color2 = src2.at<Vec3b>(Point(i,j)); color2.val[0] = color1.val[0]; color2.val[1] = color1.val[1]; color2.val[2] = color1.val[2]; src2.at<Vec3b>(Point(i*a+p,j*a+q)) = color2; } } } } //imwrite("C:\\Users\\arjun\\Desktop\\zoomed.jpg",src2); namedWindow("Original Image",CV_WINDOW_AUTOSIZE); imshow("Original Image", src1); namedWindow("Scaled Image",CV_WINDOW_AUTOSIZE); imshow("Scaled Image", src2); waitKey(0); return 0; }
The same thing of zooming the image can be done by adding Trackbar to it.Thus by dragging the cursor on the Trackbar we can dynamically change the zooming factor of an image.
Refer the code below:
// OpenCV Image Zooming Tutorial with Trackbar #include <opencv2/core/core.hpp> #include <opencv2/highgui/highgui.hpp> #include <iostream> using namespace cv; using namespace std; Mat src2,src1; int a=1; const int zoom_slider_max = 50; void zoom_trackbar( int , void* ) { src2 = Mat::zeros(src1.rows*a,src1.cols*a, CV_8UC3); for (int i=0; i<src1.cols ; i++) { for (int j=0 ; j<src1.rows ; j++) { Vec3b color1 = src1.at<Vec3b>(Point(i,j)); for(int p=0;p<a;p++) { for(int q=0;q<a;q++) { Vec3b color2 = src2.at<Vec3b>(Point(i,j)); color2.val[0] = color1.val[0]; color2.val[1] = color1.val[1]; color2.val[2] = color1.val[2]; src2.at<Vec3b>(Point(i*a+p,j*a+q)) = color2; } } } } imshow("Scaled Image", src2); cout<<"Image Got Processed for Zoom factor="<<a<<endl; } int main() { src1 = imread("C:\\Users\\arjun\\Desktop\\arj.jpg",CV_LOAD_IMAGE_COLOR); if( !src1.data ) { printf("Error loading src1 \n"); } namedWindow("Scaled Image",CV_WINDOW_AUTOSIZE); createTrackbar( "Magnify", "Scaled Image", &a, zoom_slider_max, zoom_trackbar ); zoom_trackbar( a, 0 ); //imwrite("C:\\Users\\arjun\\Desktop\\zoomed.jpg",src2); namedWindow("Original Image",CV_WINDOW_AUTOSIZE); imshow("Original Image", src1); waitKey(0); return 0; }
Output:
No comments:
Post a Comment