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);
}

No comments:

Post a Comment