Refer this code for shrinking the image by a factor of two:
// OpenCV Image Shrinking 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);//Linux.jpg");//atom.jpg");//"Linux.jpg"); //f1.jpg if( !src1.data ) { printf("Error loading src1 \n"); return -1;} src2 = Mat::zeros((src1.rows/2)+1,(src1.cols/2)+1, CV_8UC3); cout<<"Shrinking factor is: 2"<<endl for (float i=0; i<src1.cols ; i++) { for (float j=0 ; j<src1.rows ; j++) { Vec3b color1 = src1.at<Vec3b>(Point(i,j)); Vec3b color2 = src2.at<Vec3b>(Point(i/2,j/2)); color2.val[0] = color1.val[0]; color2.val[1] = color1.val[1]; color2.val[2] = color1.val[2]; src2.at<Vec3b>(Point(i/2,j/2)) = color2; } } namedWindow("Original Image",CV_WINDOW_AUTOSIZE); imshow("Original Image", src1); //imwrite("C:\\Users\\arjun\\Desktop\\shrinked.jpg",src2); namedWindow("Scaled Image",CV_WINDOW_AUTOSIZE); imshow("Scaled Image", src2); waitKey(0); return 0; }Next we have done slight modifications in the code, where we take input from the user for shrinking an image by a particular factor:
// OpenCV Image Shrinking 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=-1; while(a<=0){ cout<<"Enter the Shrinking factor greater than 0 "<<endl; cin>>a; if( a<=0 ) { cout<<"Invalid values... Please re-enter the correct values \n \n"; } } src2 = Mat::zeros(src1.rows/a+1,src1.cols/a+1, CV_8UC3); cout<<"Shrinking factor is: "<<a<<endl; for (int i=0; i<src1.cols-1 ; i++) { for (int j=0 ; j<src1.rows-1; j++) { Vec3b color1 = src1.at<Vec3b>(Point(i,j)); { { Vec3b color2 = src2.at<Vec3b>(Point(i/a,j/a)); color2.val[0] = color1.val[0]; color2.val[1] = color1.val[1]; color2.val[2] = color1.val[2]; src2.at<Vec3b>(Point(i/a,j/a)) = color2; } } } } // imwrite("C:\\Users\\arjun\\Desktop\\shrinked.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; }OpenCV image shrinking tutorial with the help of a trackbar:
// OpenCV Image Shrinking Tutorial with trackbar #include <opencv2/core/core.hpp> #include <opencv2/highgui/highgui.hpp> #include <iostream> using namespace cv; using namespace std; Mat src1,src2; int a=1; const int shrink_slider_max=50; void shrink_trackbar( int , void* ) { src2 = Mat::zeros(src1.rows/a+1,src1.cols/a+1, CV_8UC3); for (int i=0; i<src1.cols-1 ; i++) { for (int j=0 ; j<src1.rows-1; j++) { Vec3b color1 = src1.at<Vec3b>(Point(i,j)); { { Vec3b color2 = src2.at<Vec3b>(Point(i/a,j/a)); color2.val[0] = color1.val[0]; color2.val[1] = color1.val[1]; color2.val[2] = color1.val[2]; src2.at<Vec3b>(Point(i/a,j/a)) = color2; } } } } imshow("Scaled Image", src2); cout<<"Image Got Processed for Shrink 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"); return -1;} namedWindow("Scaled Image",CV_WINDOW_AUTOSIZE); createTrackbar( "Shrinking", "Scaled Image", &a,shrink_slider_max, shrink_trackbar ); shrink_trackbar( a, 0 ); // imwrite("C:\\Users\\arjun\\Desktop\\shrinked.jpg",src2); namedWindow("Original Image",CV_WINDOW_AUTOSIZE); imshow("Original Image", src1); waitKey(0); return 0; }
NIce Blog For Linux Learn Python the Hard Way
ReplyDelete