Thursday, 11 June 2015

Drawing a circle by dynamically taking the value of its centre and radius from the user

This opencv tutorial is about drawing a circle by dynamically taking radius and centre values from the user.

Drawing a Circle(Opencv):
Syntax:
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).

Here is the opencv code below:
#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 Concentric  Circles(opencv):
Here is the opencv code below 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);
}
Drawing circle by taking the value of radius from the user:
Here is the code below for drawing concentric circles by dynamically 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);
}

No comments:

Post a Comment