Thursday 25 February 2016

OpenCV C++ Code for drawing an 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)

Parameters:
img Image.
center Center of the ellipse.
axes Half of the size of the ellipse main axes.
angle Ellipse rotation angle in degrees.
startAngle Starting angle of the elliptic arc in degrees.
endAngle Ending angle of the elliptic arc in degrees.
box Alternative ellipse representation via RotatedRect or CvBox2D. This means that the function draws an ellipse inscribed in the rotated rectangle.
color Ellipse color.
thickness Thickness of the ellipse arc outline, if positive. Otherwise, this indicates that a filled ellipse sector is to be drawn.
lineType Type of the ellipse boundary. See the line() description.
shift Number of fractional bits in the coordinates of the center and values of axes.

Note: If you want to draw the whole ellipse and not an arc, choose startAngle 0 and endAngle 360.

Note: CIRCLE is a special case of ELLIPSE whose ECCENTRICITY is equal to ONE.
i.e whose both the axis are of equal length.



//Code for drawing an ellipse
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
using namespace cv;
int main( )
{
Mat image = Mat::zeros( 400, 400, CV_8UC3 );
//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);
ellipse( image, Point( 200, 200 ), Size( 100.0, 150.0 ), 45, 0, 360, Scalar( 255, 0, 0 ), 3, 8 );
ellipse( image, Point( 200, 200 ), Size( 100.0, 150.0 ), 90, 0, 360, Scalar( 0,255, 0 ), 3, 8 );
ellipse( image, Point( 200, 200 ), Size( 100.0, 150.0 ), 135, 0, 360, Scalar( 0, 0, 255 ), 3, 8 );
ellipse( image, Point( 200, 200 ), Size( 100.0, 150.0 ), 180, 0, 360, Scalar( 255,255, 0 ), 3, 8 );
imshow("Image",image);
waitKey( 0 );
return(0);
}

Saturday 20 February 2016

OpenCV C++ Code for drawing Circles

Syntax:
C++ :void circle(Mat& img, Point center, int radius, const Scalar& color, int thickness=1, int lineType=8, int shift=0)

Parameters:
img Image where the circle is drawn.
center Center of the circle.
radius Radius of the circle.
color Circle color.
thickness Thickness of the circle outline, if positive. Negative thickness means that a filled circle is to be drawn.
lineType Type of the circle boundary. See the line() description.
shift Number of fractional bits in the coordinates of the center and in the radius value.
The function circle draws a simple or filled circle with a given center and radius.

Note: To draw a filled circle in Opencv ,take the value of line type as Negative (e.g -2).


#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
using namespace cv;
  
int main( )
{    
  // Create black empty images
  Mat image = Mat::zeros( 400, 400, CV_8UC3 );
    
  // Draw a circle 
  circle( image, Point( 200, 200 ), 32.0, Scalar( 0, 0, 255 ), 1, 8 );
  imshow("Image",image);
  
  waitKey( 0 );
  return(0);
}



Drawing a Circle by taking values from the user.
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>
using namespace cv;
using namespace std;
 
int main( )
{    
  // Create black empty images
  /*It basically defines the size of the window where circle would be drawn*/
  Mat image = Mat::zeros( 400, 400, CV_8UC3 );
  int i,x,y; 
  // Draw a circle 
  while(true)
  {
  cout<<"Enter the co-ordinates of the centre"<<endl;
  //Get value of centre from the user it should be 0<x<400 and 0<y<400
  //Bcoz size of window is (400,400)
  cin>>x>>y;
  //Display the co-ordinate of the centre in the form (x,y)
  cout<<"Co-ordinates of the centre is ("<<x<<","<<y<<")"<<endl;
  cout <<"Enter the value of radius"<<endl;
   //Take the value of centre from the user
  cin >>i;
  //Function for drawing the circle
  circle( image, Point( x, y ), i, Scalar( 0, 0, 255 ), 1, 8 );
  imshow("Image",image);
  waitKey( 1000 );
  }
  return(0);
}



Opencv Code for drawing Concentric Circles.
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
using namespace cv;
  
int main( )
{    
  // Create black empty images
  Mat image = Mat::zeros( 400, 400, CV_8UC3 );
    
  // Draw a circle 
  for(int i=0; i<200;i=i+30)
  {
  circle( image, Point( 200, 200 ), i, Scalar( 0, 0, 255 ), 1, 8 );
  imshow("Image",image);
  
  waitKey( 1000 );
  }
  return(0);
}

Monday 15 February 2016

OpenCV C++ Code for drawing a Line

Syntax:
C++:void line(Mat& img, Point pt1, Point pt2, const Scalar& color, int thickness=1, int lineType=8, int shift=0)

Parameters:
img Image.
pt1 First point of the line segment.
pt2 Second point of the line segment.
color Line color.
thickness Line thickness.
lineType Type of the line:
                 8 (or omitted) - 8-connected line.
                 4 - 4-connected line.
                 CV_AA - antialiased line.
shift Number of fractional bits in the point coordinates.



//Opencv Code for drawing a Line
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
using namespace cv;
  
int main( )
{    
  // Create black empty images
  Mat image = Mat::zeros( 400, 400, CV_8UC3 );
    
  // Draw a line 
  line( image, Point( 15, 20 ), Point( 70, 50), Scalar( 110, 220, 0 ),  2, 8 );
  imshow("Image",image);
  
  waitKey( 0 );
  return(0);
}

Wednesday 10 February 2016

Zeros,Ones and Eyes in OpenCV


Matlab style Initializers
Mat::zeroes
Each element of the matrix is zero of the specified size.
Mat A;
A = Mat::zeros(3, 3, CV_32F);


Mat::ones
Each element of the matrix is one of the specified size
Mat A;
A = Mat::ones(3, 3, CV_32F);


Mat::eyes
It returns an identity matrix of the specified size.
Mat A;
A = Mat::eyes(3, 3, CV_32F);


Note:
We can also mention the scale factor of the matrix.
e.g:
A = Mat::ones(3, 3, CV_32F)* 5;
Here each element of the matrix is 5, because each element of the uniy matrix is multiplied by 5.


#include <opencv2/core/core.hpp>
#include <iostream>
#include <opencv2/highgui/highgui.hpp> 
 
using namespace cv;
using namespace std;
 
int main()
{
    Mat imgA = Mat::eye(5, 5, CV_8UC1);
cout << "imgA = \n " << imgA << "\n\n";
 
Mat imgB = Mat::ones(4, 4, CV_8UC1);
cout << "imgB = \n " << imgB << "\n\n";
 
Mat imgC = Mat::zeros(3,3, CV_8UC1);
cout << "imgC = \n " << imgC << "\n\n";
 
return 0;
}

Output:



Note:
Here we have selected the single channel matrix.(CV_8UC1)
For 3 channel matrices:


Code:
#include <opencv2/core/core.hpp>
#include <iostream>
#include <opencv2/highgui/highgui.hpp> 
 
using namespace cv;
using namespace std;
 
int main()
{
    Mat imgA = Mat::eye(5, 5, CV_8UC3);
cout << "imgA = \n " << imgA << "\n\n";
 
Mat imgB = Mat::ones(4, 4, CV_8UC3);
cout << "imgB = \n " << imgB << "\n\n";
 
Mat imgC = Mat::zeros(3,3, CV_8UC3);
cout << "imgC = \n " << imgC << "\n\n";
 
return 0;
}





See the difference in the output.Here the zeros,ones and eyes operator is applied only to 1 channel of the matrix.Rest of the other channel elements are taken 0.Thus two columns of 0 can be seen in between.

Also we doesn't mention the no. of channels by default it takes 1.
ie. CV_8U is equivalent to CV_8UC1.