Thursday, 19 November 2015

Flip an image in opencv by using flip function


Syntax:
C++: void flip(InputArray src, OutputArray dst, int flipCode)

Flips a 2D array around vertical, horizontal, or both axes.

Parameters:
src – input array.
dst – output array of the same size and type as src.
flipCode – a flag to specify how to flip the array.

 0 means flipping around the x-axis
 positive value (for example, 1) means flipping around y-axis
 Negative value (for example, -1) means flipping around both axes

Here is the code of flipping the image:

// OpenCV Image Flipping Tutorial 
#include <opencv2/core/core.hpp> 
#include <opencv2/highgui/highgui.hpp> 
#include <iostream>

using namespace cv;
using namespace std;

int main()
{
  double alpha, beta; 

 Mat src1,src2;
 // Read image (same size, same type )
 src1 = imread("C:\Users\arjun\Desktop\flipsrc.jpg");//Linux.jpg");//atom.jpg");//"Linux.jpg"); //f1.jpg
 
 if( !src1.data ) { printf("Error loadind src1 n"); return -1;}

 //Create Window
 namedWindow("Original Image", CV_WINDOW_AUTOSIZE );
 imshow( "Original Image", src1 );
 
 flip(src1,src2,0);
 namedWindow("Flip-x-axis", CV_WINDOW_AUTOSIZE );
 imshow( "Flip-x-axis", src2 );
 imwrite( "C:\Users\arjun\Desktop\flipXaxis.jpg",src2);


 flip(src1,src2,1);
 namedWindow("Flip-y-axis", CV_WINDOW_AUTOSIZE );
 imshow( "Flip-y-axis", src2 );
 imwrite( "C:\Users\arjun\Desktop\flipYaxis.jpg",src2);

 flip(src1,src2,-1);
 namedWindow("Flip-z-axis", CV_WINDOW_AUTOSIZE );
 imshow( "Flip-z-axis", src2 );
 imwrite( "C:\Users\arjun\Desktop\flipZaxis.jpg",src2);

 waitKey(0);
 return 0;
 
}

Input Image:
opencv image  without flip

Output:
Flipping around the X axis
opencv Flipping around the X axis

Flipping around the Y axis
opencv Flipping around the Y axis

Flipping around both the axis
opencv Flipping around both the axis
We can also put the flip function under for loop and obtain the three flipping of an image as shown below:
// OpenCV Image Flipping Tutorial 
#include <opencv2/core/core.hpp> 
#include <opencv2/highgui/highgui.hpp> 
#include <iostream>

using namespace cv;
using namespace std;

int main()
{
  double alpha, beta; 

 Mat src1,src2;
 // Read image (same size, same type )
 src1 = imread("C:\\Users\\arjun\\Desktop\\arj.jpg");
 
 if( !src1.data ) { printf("Error loading src1 n"); return -1;}
 
 //Create Window
 namedWindow("Original Image", CV_WINDOW_AUTOSIZE );
 imshow( "Original Image", src1 );
 for(int i=-1;i<2;i++)
 {
 flip(src1,src2,i);
 namedWindow("Flip", CV_WINDOW_AUTOSIZE );
 imshow( "Flip", src2 );
 waitKey(5000);
  }
 waitKey(0);
 return 0;
 }

Output:

No comments:

Post a Comment