Friday 29 January 2016

Opencv C++ Code for Image translation with the help of Trackbar

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:
// 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;
}

Monday 18 January 2016

Opencv C++ Code for Rotating an Image with the help of a Trackbar

This OpenCV tutorial is about rotating an Image about its centre by varying the slider position on the Trackbar.
Refer the Opencv C++ Code below:

// OpenCV C++ Tutorial of Rotating an Image with the help of Trackbar
#include <opencv2/core/core.hpp> 
#include <opencv2/highgui/highgui.hpp> 
#include <iostream>

using namespace cv;
using namespace std;

//Global Variables
const int rotation_slider_max=360;
const float pi=3.14;
float x,y;
int slider_value=0;

/// Matrices to store images
Mat src1,src2;

void on_trackbar( int, void* )
{
 src2 = Mat::zeros(src1.rows,src1.cols, CV_8UC3);
 for (float i=0; i<src1.cols ; i++){
 for (float j=0 ; j<src1.rows ; j++)
 {
   x=((i-src1.cols/2)*cos(slider_value*pi/180)-(j-src1.rows/2)*sin(slider_value*pi/180)+src1.cols/2);
   y=((i-src1.cols/2)*sin(slider_value*pi/180)+(j-src1.rows/2)*cos(slider_value*pi/180)+src1.rows/2);

   if((x>=0 && x<src1.cols) && (y>=0 && y<src1.rows)){
        Vec3b color1 = src1.at<Vec3b>(Point(i,j));
        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(x,y)) = color2;
   }
    }
 }
 imshow("Rotated 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;}

 namedWindow("Rotated Image",CV_WINDOW_AUTOSIZE); 
    namedWindow("Original Image",CV_WINDOW_AUTOSIZE); 
    imshow("Original Image", src1);

 createTrackbar( "Rotation", "Rotated Image", &slider_value, rotation_slider_max, on_trackbar );
 on_trackbar( slider_value, 0 );
 waitKey(0);
 return 0;
}

Saturday 16 January 2016

Opencv C++ Code for Image Rotation

This Opencv C++ tutorial is about rotating an Image about its centre without using the warpAffine function.
Consider the Figure below:
Point A is rotated from an angle ϴ to an angle ϴ'.


Hence,
x=rCos(ϴ) & y=rSin(ϴ)
And
x'=rCos(ϴ+ϴ') & y'=rSin(ϴ+ϴ')
Thus,
x'=rCos(ϴ)Cos(ϴ')-rSin(ϴ)Sin(ϴ')
x'=xCos(ϴ')-ySin(ϴ')

y'=rSin(ϴ)Cos(ϴ')+rCos(ϴ)Sin(ϴ')
y'=yCos(ϴ')+xSin(ϴ')
Thus,
x'=xCos(ϴ')-ySin(ϴ')
y'=yCos(ϴ')+xSin(ϴ')


This is the matrix obtained when we want to rotate an Image about Origin. If we want to rotate an Image about any other point say(p,q), Then the equation would be,
x'=(x-p)Cos(ϴ')-(y-q)Sin(ϴ')+p
y'=(y-q)Cos(ϴ')+(x-p)Sin(ϴ')+q
Hence the Matrix would be,


Here is the Opencv C++ Example of image rotation about its centre:
// OpenCV Code of rotating an image about its Centre 
#include <opencv2/core/core.hpp> 
#include <opencv2/highgui/highgui.hpp> 
#include <iostream>

using namespace cv;
using namespace std;

int main()
{
 const float pi=3.14;
 int x,y;
 Mat src1,src2;

 //Take the Input Image in src1
 src1 = imread("C:\\Users\\arjun\\Desktop\\opencv-logo.png",CV_LOAD_IMAGE_COLOR);
 if( !src1.data ) { printf("Error loading src1 \n"); return -1;}

 src2 = Mat::zeros(src1.rows,src1.cols, CV_8UC3);

 //Angle by which to rotate an Image
 for (int a=0 ; a<360 ; a++)
 {
 //Logic of Image Rotation
 for (float i=0; i<src1.cols ; i++){
 for (float j=0 ; j<src1.rows ; j++)
 {
   //x'=(x-p)Cos(ϴ')-(y-q)Sin(ϴ')+p
   x=((i-src1.cols/2)*cos(a*pi/180)-(j-src1.rows/2)*sin(a*pi/180)+src1.cols/2);
   //y'=(y-q)Cos(ϴ')+(x-p)Sin(ϴ')+q
   y=((i-src1.cols/2)*sin(a*pi/180)+(j-src1.rows/2)*cos(a*pi/180)+src1.rows/2);

   if((x>=0 && x<src1.cols) && (y>=0 && y<src1.rows)){
  Vec3b color1 = src1.at<Vec3b>(Point(i,j));
  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(x,y)) = color2;
   }
    }
 }
waitKey(50);
namedWindow("Rotated Image",CV_WINDOW_AUTOSIZE); 
imshow("Rotated Image", src2); 
namedWindow("Original Image",CV_WINDOW_AUTOSIZE); 
imshow("Original Image", src1);

 }
 waitKey(0);
 return 0;
}

Friday 15 January 2016

Opencv C++ Code for Drawing a Chessboard Pattern-Amazing Effects

This OpenCV Tutorial is about drawing a Chess Board Pattern.
Refer the Code Below:

//Opencv Example of Drawing a Chess Board Pattern
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
using namespace cv;
 
int main( )
{ 
 int a=400/8;
  // Create black empty images
  Mat image = Mat::zeros( 400, 400, CV_8UC3 );
   
  // Draw a rectangle ( 5th argument is not -ve)  
  for(int y=0;y<=3;y++)
  for(int x=0;x<=3;x++)
  {
  rectangle( image, Point( x*a*2, y*a*2), Point( a*(2*x+1), a*(2*y+1)), Scalar( 255, 255, 255 ), -1, 4 );
  imshow("Image1",image);
  waitKey( 250 );
  }
  for(int y=0;y<=3;y++)
  for(int x=0;x<=3;x++){
  rectangle( image, Point( a*(2*x+1), a*(2*y+1)), Point( (x+1)*a*2, (y+1)*a*2), Scalar( 255, 255,255 ), -1, 4 );
  imshow("Image1",image);
  waitKey( 250 );
  }
  waitKey( 0 );
  return(0);
}

Output:

Opencv-Example-Draw-ChessBoard-Pattern

Thursday 14 January 2016

OpenCV C++ Code for drawing a Pentagon

This Opencv C++ Tutorial is about drawing a Pentagon.
In the previous tutorials we learn about drawing a
Square and a Line.
To draw the Square we obtained the Co-ordinates of the vertices of the square and then joined those vertices with a Line.
Similarly in order to draw the Pentagon we first need to obtain the Co-ordinates of its Vertices.

Refer the Figure Below:

How-to-draw-pentagon-opencv-tutorial

      ϴ=72º (∵ ϴ = 360º/5)
Now,  In ∆OBC,
      Seg OB=Seg OC;
Thus, m∠OBC=m∠OCB=x;
      x+x+72º=180º       ( Since Sum of All angles of a Triangle is 180º )
      2x=180º - 72º ;
      x=54º
      i.e.  m∠OBC=m∠OCB=54º;

Also,
      m∠ABC=108º;
Thus, m∠ABQ=72º; & Seg BC=a;
      Seg QB=a*Cos(72º);
∴     QR=QB + BC + CR;
&     QB=CR;
Thus, 2*a*Cos(72º) + a =QR
where QR is the Length of the Side of the window.
Here  QR=500
Thus  a=500/(1+2*Cos(72º));


//Opencv C++ Example for drawing a Pentagon 
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>
#include <math.h>
using namespace cv;
using namespace std;
 
int main( )
{    double pi=3.14;

  //Length of a Side of Regular Pentagon
  int a=500/(1+2*cos(pi*72/180));
 
  // Create black empty images
  Mat image = Mat::zeros( 500, 500, CV_8UC3 );
  
  line( image, Point((a*cos(pi*72/180)), 500), Point(500-(a*cos(pi*72/180)),500), Scalar( 255, 255, 0 ), 2, 8 );
  imshow("Image",image); 
  waitKey( 500 );
  line( image, Point(500-(a*cos(pi*72/180)),500), Point( 500, 500-(a*sin(pi*72/180)) ), Scalar( 255, 255, 0 ), 2, 8 );
  imshow("Image",image); 
  waitKey( 500 );
  line( image, Point(500, 500-(a*sin(pi*72/180))), Point(250, 0), Scalar( 255, 255, 0 ), 2, 8 );
  imshow("Image",image); 
  waitKey( 500 );
  line( image, Point(250, 0), Point(0, 500-(a*sin(pi*72/180))), Scalar( 255, 255, 0 ), 2, 8 );
  imshow("Image",image); 
  waitKey( 500 ); 
  line( image, Point(0, 500-(a*sin(pi*72/180))), Point((a*cos(pi*72/180)), 500), Scalar( 255, 255, 0 ), 2, 8 );
  imshow("Image",image); 
  waitKey( 0 );
  return(0);
}

Output:

Tuesday 12 January 2016

OpenCV C++ Code for Drawing a Square wave

This Opencv Tutorial is about drawing a Square wave.
In the previous Opencv tutorial we learnt about drawing a Square.
http://opencv-hub.blogspot.in/2016/01/opencv-c-code-for-drawing-square.html Thus this tutorial is just an extension of that tutorial.
Here is the OPENCV C++ code for drawing a Square Wave:

//Opencv Code for Drawing a Square-Wave
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>
using namespace cv;
using namespace std;
 
int main( )
{   
  int count=0,a=0,b=50,i=0,j=50;
  // Create black empty images
  Mat image = Mat::zeros( 500, 500, CV_8UC3 );
  
  for(a=0;a<500;)
  {
   count++;
   if(count%2==0)
   {  
    i=a+50;
    j=b;
    
   }
   else{
    i=a;
    if(j==50){
     j=100;}
    else{
     j=50;
    }
    
   }
  // Draw a line 
  line( image, Point( a, b ), Point( i, j), Scalar( 255, 255, 0 ), 2, 8 );
  
     imshow("Image",image);
     waitKey( 250 );
  a=i;
  b=j;
  }
 
   waitKey( 0 );
  return(0);
}

Output:
Opencv C++ Tutorial for Drawing a Square wave

Monday 11 January 2016

Opencv C++ Code For Drawing a Semi-Circle

This Opencv Tutorial is about drawing a Semi-Circle
You might have wondered that how to draw a Semi-Circle in Opencv when we have no direct syntax available for it.
Even in the Syntax of drawing a circle in Opencv, we dont have any such parameters which can be modified for drawing a semicircle.
C++: void circle(Mat& img, Point center, int radius, const Scalar& color, int thickness=1, int lineType=8, int shift=0)
But, we know that a circle is a special case of an ellipse whose eccentricity is 1. And in the Opencv Ellipse Syntax:
C++: void ellipse(Mat& img, Point center, Size axes, double angle, double startAngle, double endAngle, const Scalar& color, int thickness=1, int lineType=8, int shift=0)
We can find the parameters like "Start Angle" and "End Angle".
And if we want to draw an Circle From an Ellipse we just need to mention the size of both the axes as same.
Thus,Here is the Opencv Code for drawing a Semi-Circle:

//Opencv C++ Tutorial for drawing a Semi-Circle
#include 
#include 
using namespace cv;
int main( )
{
 // Create black empty images
 Mat image = Mat::zeros( 500, 500, CV_8UC3 );
 // Draw a ellipse
 for(int i=10;i<=250;i=i+10)
 {
 ellipse( image, Point( 250, 250 ), Size( i, i ), 0, 0, 180, Scalar( 255, 255, 0 ), 2, 8 );
 imshow("Image",image);
 waitKey( 250 );
 }
 waitKey( 0 );

 return(0);
}

Output:
Opencv Tutorial For drawing a Semi-Circle

Sunday 10 January 2016

Opencv C++ code for drawing a Circular Spiral-Amazing Effects

This Opencv Tutorial is about drawing a Spiral.
In the previous tutorial we learn about drawing an Ellipse.

http://opencv-hub.blogspot.in/2015/12/code-for-drawing-ellipse-c.html
Thus this opencv tutorial will be an extension of that tutorial with some added mathematical logic for drawing a spiral.
To begin with we first start by drawing a semi-circle.
Now we just need to switch the orientation alternately and increase the radius of the semicircles to achieve the desired effect.
Here is the Opencv Code for drawing a Spiral:

//Opencv Code for Spiral Effect
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
using namespace cv;
int main( )
{
 // Create black empty images
 Mat image = Mat::zeros( 500, 500, CV_8UC3 );
 //Factor by which u need to increase the radius
 int x=5;

 for(int i=x;i<=250;i=i+x)
 {
  if(i%(2*x)==0){
 // Draw a ellipse
 ellipse( image, Point( 250+x, 250 ), Size( i, i ), 180, 180, 0, Scalar( 0, 255,255 ), 2, 8 );
 imshow("Image",image);
 waitKey( 100 );
  }
  else
  {
 ellipse( image, Point( 250, 250 ), Size( i, i ), 0, 0, 180, Scalar( 0, 255,255 ), 2, 8 );
 imshow("Image",image);
 waitKey( 100 );

  }
 }
 waitKey( 0 );

 return(0);
}

Output:
Opencv Tutorial For Drawing Spiral