Thursday 10 March 2016

OpenCV C++ Code for drawing a Square Spiral

In the previous tutorial we learn about drawing an Line.
http://opencv-code.blogspot.in/2016/12/how-to-draw-line-opencv-cplusplus-example.html
Thus this opencv tutorial will be an extension of that tutorial with some added mathematical logic for drawing a square spiral.



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

Output:-

No comments:

Post a Comment