OpenCV  3.0.0-rc1
Open Source Computer Vision
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
Point Polygon Test

Goal

In this tutorial you will learn how to:

Theory

Code

This tutorial code's is shown lines below. You can also download it from here

1 
9 #include <iostream>
10 #include <stdio.h>
11 #include <stdlib.h>
12 
13 using namespace cv;
14 using namespace std;
15 
19 int main( void )
20 {
22  const int r = 100;
23  Mat src = Mat::zeros( Size( 4*r, 4*r ), CV_8UC1 );
24 
26  vector<Point2f> vert(6);
27 
28  vert[0] = Point( 3*r/2, static_cast<int>(1.34*r) );
29  vert[1] = Point( 1*r, 2*r );
30  vert[2] = Point( 3*r/2, static_cast<int>(2.866*r) );
31  vert[3] = Point( 5*r/2, static_cast<int>(2.866*r) );
32  vert[4] = Point( 3*r, 2*r );
33  vert[5] = Point( 5*r/2, static_cast<int>(1.34*r) );
34 
36  for( int j = 0; j < 6; j++ )
37  { line( src, vert[j], vert[(j+1)%6], Scalar( 255 ), 3, 8 ); }
38 
40  vector<vector<Point> > contours; vector<Vec4i> hierarchy;
41  Mat src_copy = src.clone();
42 
43  findContours( src_copy, contours, hierarchy, RETR_TREE, CHAIN_APPROX_SIMPLE);
44 
46  Mat raw_dist( src.size(), CV_32FC1 );
47 
48  for( int j = 0; j < src.rows; j++ )
49  { for( int i = 0; i < src.cols; i++ )
50  { raw_dist.at<float>(j,i) = (float)pointPolygonTest( contours[0], Point2f((float)i,(float)j), true ); }
51  }
52 
53  double minVal; double maxVal;
54  minMaxLoc( raw_dist, &minVal, &maxVal, 0, 0, Mat() );
55  minVal = abs(minVal); maxVal = abs(maxVal);
56 
58  Mat drawing = Mat::zeros( src.size(), CV_8UC3 );
59 
60  for( int j = 0; j < src.rows; j++ )
61  { for( int i = 0; i < src.cols; i++ )
62  {
63  if( raw_dist.at<float>(j,i) < 0 )
64  { drawing.at<Vec3b>(j,i)[0] = (uchar)(255 - abs(raw_dist.at<float>(j,i))*255/minVal); }
65  else if( raw_dist.at<float>(j,i) > 0 )
66  { drawing.at<Vec3b>(j,i)[2] = (uchar)(255 - raw_dist.at<float>(j,i)*255/maxVal); }
67  else
68  { drawing.at<Vec3b>(j,i)[0] = 255; drawing.at<Vec3b>(j,i)[1] = 255; drawing.at<Vec3b>(j,i)[2] = 255; }
69  }
70  }
71 
73  const char* source_window = "Source";
74  namedWindow( source_window, WINDOW_AUTOSIZE );
75  imshow( source_window, src );
76  namedWindow( "Distance", WINDOW_AUTOSIZE );
77  imshow( "Distance", drawing );
78 
79  waitKey(0);
80  return(0);
81 }
Scalar_< double > Scalar
Definition: types.hpp:597
void minMaxLoc(InputArray src, double *minVal, double *maxVal=0, Point *minLoc=0, Point *maxLoc=0, InputArray mask=noArray())
Finds the global minimum and maximum in an array.
int rows
the number of rows and columns or (-1, -1) when the matrix has more than 2 dimensions ...
Definition: mat.hpp:1865
static MatExpr zeros(int rows, int cols, int type)
Returns a zero array of the specified size and type.
void imshow(const String &winname, InputArray mat)
Displays an image in the specified window.
#define CV_8UC1
Definition: cvdef.h:116
Definition: imgproc.hpp:409
Template class for short numerical vectors, a partial case of Matx.
Definition: matx.hpp:300
int cols
Definition: mat.hpp:1865
void line(InputOutputArray img, Point pt1, Point pt2, const Scalar &color, int thickness=1, int lineType=LINE_8, int shift=0)
Draws a line segment connecting two points.
Mat clone() const
Creates a full copy of the array and the underlying data.
MatSize size
Definition: mat.hpp:1882
unsigned char uchar
Definition: defs.h:284
void namedWindow(const String &winname, int flags=WINDOW_AUTOSIZE)
Creates a window.
Size2i Size
Definition: types.hpp:308
for i
Definition: modelConvert.m:63
#define CV_32FC1
Definition: cvdef.h:146
#define CV_8UC3
Definition: cvdef.h:118
Definition: imgproc.hpp:421
Definition: highgui.hpp:138
T abs(T x)
Definition: dist.h:58
double pointPolygonTest(InputArray contour, Point2f pt, bool measureDist)
Performs a point-in-contour test.
void findContours(InputOutputArray image, OutputArrayOfArrays contours, OutputArray hierarchy, int mode, int method, Point offset=Point())
Finds contours in a binary image.
int main(int argc, const char *argv[])
Definition: facerec_demo.cpp:67
n-dimensional dense array class
Definition: mat.hpp:726
_Tp & at(int i0=0)
Returns a reference to the specified array element.
Point2i Point
Definition: types.hpp:181
int waitKey(int delay=0)
Waits for a pressed key.

Explanation

Result

Here it is:

Point_Polygon_Test_Source_Image.png
Point_Polygon_Test_Result.jpg