Sunday 27 December 2015

Opencv code for Drawing Prime Spiral-Amazing effects

This Opencv Tutorial is about drawing a Square Spiral(Prime Spiral):
In the previous tutorial we learn about drawing an Line.
http://opencv-hub.blogspot.in/2015/12/opencv-code-for-drawing-line-cplusplus.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:
Opencv Tutorial to draw Square Spiral

2 comments: