This OpenCV Tutorial is about shifting an image in a window/performing image translation by varying the position on the slider with the help of Trackbars.
Refer the OpenCV C++ Example of Image Translation with the help of Trackbars below:
Refer the OpenCV C++ Example of Image Translation with the help of Trackbars below:
// OpenCV C++ Code for Image Translation Using TRackbars #include <opencv2/core/core.hpp> #include <opencv2/highgui/highgui.hpp> #include <iostream> using namespace cv; using namespace std; Mat src1,src2; int intensity_slider_max_x , intensity_slider_max_y , slider_valuex=0 , slider_valuey=0 ; void on_trackbar_x(int , void*){ src2 = Mat::zeros( src1.rows, src1.cols, CV_8UC3 ); for (int i=slider_valuex; i<src1.cols ; i++){ for (int j=slider_valuey ; j<src1.rows ; j++) { Vec3b color1 = src1.at<Vec3b>(Point(i-slider_valuex,j-slider_valuey)); 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,j)) = color2; } } imshow("Translated Image", src2); } void on_trackbar_y(int , void*){ src2 = Mat::zeros( src1.rows, src1.cols, CV_8UC3 ); for (int i=slider_valuex; i<src1.cols ; i++){ for (int j=slider_valuey ; j<src1.rows ; j++) { Vec3b color1 = src1.at<Vec3b>(Point(i-slider_valuex,j-slider_valuey)); 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,j)) = color2; } } imshow("Translated Image", src2); } int main() { src1 = imread("C:\\Users\\arjun\\Desktop\\opencv-logo.png",CV_LOAD_IMAGE_COLOR); if( !src1.data ) { printf("Error loading src1 \n"); return -1;} intensity_slider_max_x=src1.rows; intensity_slider_max_y=src1.cols; namedWindow("Translated Image",CV_WINDOW_AUTOSIZE); createTrackbar( "TranslationX", "Translated Image", &slider_valuex, intensity_slider_max_x, on_trackbar_x ); createTrackbar( "TranslationY", "Translated Image", &slider_valuey, intensity_slider_max_y, on_trackbar_y ); /// trackbar on_change function on_trackbar_x( slider_valuex,0 ); on_trackbar_y( slider_valuey,0 ); namedWindow("Original Image",CV_WINDOW_AUTOSIZE); imshow("Original Image", src1); waitKey(0); return 0; }
No comments:
Post a Comment