Thursday, 19 November 2015

Flip an Image along X,Y and both X,Y axis without using opencv flip function

Code to flip an Image along X axis:
// OpenCV Image Flipping along X axis 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);
 src2=src1.clone();

 if( !src1.data ) { printf("Error loading src1 \n"); return -1;}
 if( !src2.data ) { printf("Error loading src2 \n"); return -1;}

 cout<<"src1.rows="<<src1.rows<<endl;
 cout<<"src1.cols="<<src1.cols<<endl;

for (int i=0 ; i<src1.cols ; i++){
for (int j=0 ; j<src1.rows ; j++)
 {
Vec3b color2 = src1.at<Vec3b>(Point(i,j));
Vec3b color1 = src2.at<Vec3b>(Point((src1.cols-1)-i,j));
      color2.val[0] = color1.val[0];
      color2.val[1] = color1.val[1];
      color2.val[2] = color1.val[2];
   
      src1.at<Vec3b>(Point(i,j)) = color1;
 }
 }
 
namedWindow("Display Flipped Image",CV_WINDOW_AUTOSIZE); 
imshow("Display Flipped Image", src1); 
//imwrite( "C:\\Users\\arjun\\Desktop\\X-axis_flip.jpg",src1);

namedWindow("Original Image",CV_WINDOW_AUTOSIZE); 
imshow("Original Image", src2);

 waitKey(0);
 return 0;
}
Input:
opencv input image

Output:
opencv Flipping around X axis

Code to flip an Image along Y axis:
// OpenCV Image Flipping along Y axis 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);
 src2=src1.clone();

 if( !src1.data ) { printf("Error loading src1 \n"); return -1;}
 if( !src2.data ) { printf("Error loading src2 \n"); return -1;}

 cout<<"src1.rows="<<src1.rows<<endl;
 cout<<"src1.cols="<<src1.cols<<endl;

for (int i=0 ; i<src1.cols ; i++){
for (int j=0 ; j<src1.rows ; j++)
 {
Vec3b color2 = src1.at<Vec3b>(Point(i,j));
Vec3b color1 = src2.at<Vec3b>(Point(i,(src1.rows-1)-j));
      color2.val[0] = color1.val[0];
      color2.val[1] = color1.val[1];
      color2.val[2] = color1.val[2];
   
      src1.at<Vec3b>(Point(i,j)) = color1;
 }
 }
 
namedWindow("Display Flipped Image",CV_WINDOW_AUTOSIZE); 
imshow("Display Flipped Image", src1); 
imwrite( "C:\\Users\\arjun\\Desktop\\Y-axis_flip.jpg",src1);

namedWindow("Original Image",CV_WINDOW_AUTOSIZE); 
imshow("Original Image", src2);

 waitKey(0);
 return 0;
}

Input:
opencv flip

Output:
opencv Flipping around Y axis

Code to flip an Image along both the  axis:
// OpenCV Image Flipping along both the axis 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);
 src2=src1.clone();

 if( !src1.data ) { printf("Error loading src1 \n"); return -1;}
 if( !src2.data ) { printf("Error loading src2 \n"); return -1;}

 cout<<"src1.rows="<<src1.rows<<endl;
 cout<<"src1.cols="<<src1.cols<<endl;

for (int i=0 ; i<src1.cols ; i++){
for (int j=0 ; j<src1.rows ; j++)
 {
Vec3b color2 = src1.at<Vec3b>(Point(i,j));
Vec3b color1 = src2.at<Vec3b>(Point((src1.cols-1)-i,(src1.rows-1)-j));
      color2.val[0] = color1.val[0];
      color2.val[1] = color1.val[1];
      color2.val[2] = color1.val[2];
   
      src1.at<Vec3b>(Point(i,j)) = color1;
 }
 }
 
namedWindow("Display Flipped Image",CV_WINDOW_AUTOSIZE); 
imshow("Display Flipped Image", src1); 
imwrite( "C:\\Users\\arjun\\Desktop\\Both-axis_flip.jpg",src1);

namedWindow("Original Image",CV_WINDOW_AUTOSIZE); 
imshow("Original Image", src2);

 waitKey(0);
 return 0;
}

Input:
opencv image flipping

Output:
opencv Flipping around both axis

No comments:

Post a Comment