Showing posts with label Rectangle. Show all posts
Showing posts with label Rectangle. Show all posts

Friday, 25 March 2016

OpenCV C++ Code for Drawing a Chessboard Pattern

This OpenCV Tutorial is about drawing a Chess Board Pattern.
Refer the Code Below:



//Opencv Example of Drawing a Chess Board Pattern
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
using namespace cv;
  
int main( )
{ 
 int a=400/8;
  // Create black empty images
  Mat image = Mat::zeros( 400, 400, CV_8UC3 );
    
  // Draw a rectangle ( 5th argument is not -ve)  
  for(int y=0;y<=3;y++)
  for(int x=0;x<=3;x++)
  {
  rectangle( image, Point( x*a*2, y*a*2), Point( a*(2*x+1), a*(2*y+1)), Scalar( 255, 255, 255 ), -1, 4 );
  imshow("Image1",image);
  waitKey( 250 );
  }
  for(int y=0;y<=3;y++)
  for(int x=0;x<=3;x++){
  rectangle( image, Point( a*(2*x+1), a*(2*y+1)), Point( (x+1)*a*2, (y+1)*a*2), Scalar( 255, 255,255 ), -1, 4 );
  imshow("Image1",image);
  waitKey( 250 );
  }
  waitKey( 0 );
  return(0);
}



Output:

Sunday, 20 March 2016

Opencv C++ Code for drawing Rectangle

Draws a simple, thick, or filled up-right rectangle.

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

Parameters:
img – Image.
pt1 – Vertex of the rectangle.
pt2 – Vertex of the rectangle opposite to pt1.
rec – Alternative specification of the drawn rectangle.
color – Rectangle color or brightness (grayscale image).
thickness – Thickness of lines that make up the rectangle. Negative values, like CV_FILLED , mean that the function has to draw a filled rectangle.
lineType – Type of the line. See the line() description.
shift – Number of fractional bits in the point coordinates.



#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 rectangle 
  rectangle(image , Point (100,100), Point (300,300), Scalar( 0, 0, 255 ), 1, 8);
  imshow("Image",image);
  
  waitKey( 0 );
  return(0);
}

Tuesday, 1 March 2016

OpenCV C++ Code for Drawing a Pentagon

This Opencv C++ Tutorial is about drawing a Pentagon.

In the previous tutorials we learn about drawing a Rectangle and a Line.
To draw the Square we obtained the Co-ordinates of the vertices of the square and then joined those vertices with a Line.

Similarly in order to draw the Pentagon we first need to obtain the Co-ordinates of its Vertices.



Refer the Figure Below:



ϴ=72º (∵ ϴ = 360º/5)
Now, In ∆OBC,
Seg OB=Seg OC;
Thus, m∠OBC=m∠OCB=x;
x+x+72º=180º ( Since Sum of All angles of a Triangle is 180º )
2x=180º - 72º ;
x=54º
i.e. m∠OBC=m∠OCB=54º;
Also,
m∠ABC=108º;
Thus, m∠ABQ=72º; & Seg BC=a;
Seg QB=a*Cos(72º);
∴ QR=QB + BC + CR;
& QB=CR;
Thus, 2*a*Cos(72º) + a =QR
where QR is the Length of the Side of the window.
Here QR=500
Thus a=500/(1+2*Cos(72º));



//Opencv C++ Example for drawing a Pentagon 
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>
#include <math.h>
using namespace cv;
using namespace std;
  
int main( )
{    double pi=3.14;
 
  //Length of a Side of Regular Pentagon
  int a=500/(1+2*cos(pi*72/180));
  
  // Create black empty images
  Mat image = Mat::zeros( 500, 500, CV_8UC3 );
   
  line( image, Point((a*cos(pi*72/180)), 500), Point(500-(a*cos(pi*72/180)),500), Scalar( 255, 255, 0 ), 2, 8 );
  imshow("Image",image); 
  waitKey( 500 );
  line( image, Point(500-(a*cos(pi*72/180)),500), Point( 500, 500-(a*sin(pi*72/180)) ), Scalar( 255, 255, 0 ), 2, 8 );
  imshow("Image",image); 
  waitKey( 500 );
  line( image, Point(500, 500-(a*sin(pi*72/180))), Point(250, 0), Scalar( 255, 255, 0 ), 2, 8 );
  imshow("Image",image); 
  waitKey( 500 );
  line( image, Point(250, 0), Point(0, 500-(a*sin(pi*72/180))), Scalar( 255, 255, 0 ), 2, 8 );
  imshow("Image",image); 
  waitKey( 500 ); 
  line( image, Point(0, 500-(a*sin(pi*72/180))), Point((a*cos(pi*72/180)), 500), Scalar( 255, 255, 0 ), 2, 8 );
  imshow("Image",image); 
  waitKey( 0 );
  return(0);
}


Output:-