2011-07-13 04:43:02 +02:00
|
|
|
/**
|
|
|
|
* @function pointPolygonTest_demo.cpp
|
|
|
|
* @brief Demo code to use the pointPolygonTest function...fairly easy
|
|
|
|
* @author OpenCV team
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "opencv2/highgui/highgui.hpp"
|
|
|
|
#include "opencv2/imgproc/imgproc.hpp"
|
|
|
|
#include <iostream>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
using namespace cv;
|
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @function main
|
|
|
|
*/
|
2012-11-07 15:21:20 +01:00
|
|
|
int main( void )
|
2011-07-13 04:43:02 +02:00
|
|
|
{
|
|
|
|
/// Create an image
|
|
|
|
const int r = 100;
|
|
|
|
Mat src = Mat::zeros( Size( 4*r, 4*r ), CV_8UC1 );
|
|
|
|
|
|
|
|
/// Create a sequence of points to make a contour:
|
|
|
|
vector<Point2f> vert(6);
|
|
|
|
|
2012-11-07 15:21:20 +01:00
|
|
|
vert[0] = Point( 3*r/2, static_cast<int>(1.34*r) );
|
2011-07-13 04:43:02 +02:00
|
|
|
vert[1] = Point( 1*r, 2*r );
|
2012-11-07 15:21:20 +01:00
|
|
|
vert[2] = Point( 3*r/2, static_cast<int>(2.866*r) );
|
|
|
|
vert[3] = Point( 5*r/2, static_cast<int>(2.866*r) );
|
2011-07-13 04:43:02 +02:00
|
|
|
vert[4] = Point( 3*r, 2*r );
|
2012-11-07 15:21:20 +01:00
|
|
|
vert[5] = Point( 5*r/2, static_cast<int>(1.34*r) );
|
2011-07-13 04:43:02 +02:00
|
|
|
|
|
|
|
/// Draw it in src
|
|
|
|
for( int j = 0; j < 6; j++ )
|
2012-10-17 01:18:30 +02:00
|
|
|
{ line( src, vert[j], vert[(j+1)%6], Scalar( 255 ), 3, 8 ); }
|
2011-07-13 04:43:02 +02:00
|
|
|
|
|
|
|
/// Get the contours
|
|
|
|
vector<vector<Point> > contours; vector<Vec4i> hierarchy;
|
|
|
|
Mat src_copy = src.clone();
|
|
|
|
|
2012-10-17 01:18:30 +02:00
|
|
|
findContours( src_copy, contours, hierarchy, RETR_TREE, CHAIN_APPROX_SIMPLE);
|
2011-07-13 04:43:02 +02:00
|
|
|
|
|
|
|
/// Calculate the distances to the contour
|
|
|
|
Mat raw_dist( src.size(), CV_32FC1 );
|
|
|
|
|
|
|
|
for( int j = 0; j < src.rows; j++ )
|
|
|
|
{ for( int i = 0; i < src.cols; i++ )
|
2012-11-07 15:21:20 +01:00
|
|
|
{ raw_dist.at<float>(j,i) = (float)pointPolygonTest( contours[0], Point2f((float)i,(float)j), true ); }
|
2011-07-13 04:43:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
double minVal; double maxVal;
|
|
|
|
minMaxLoc( raw_dist, &minVal, &maxVal, 0, 0, Mat() );
|
|
|
|
minVal = abs(minVal); maxVal = abs(maxVal);
|
2012-10-17 01:18:30 +02:00
|
|
|
|
2011-07-13 04:43:02 +02:00
|
|
|
/// Depicting the distances graphically
|
|
|
|
Mat drawing = Mat::zeros( src.size(), CV_8UC3 );
|
|
|
|
|
|
|
|
for( int j = 0; j < src.rows; j++ )
|
|
|
|
{ for( int i = 0; i < src.cols; i++ )
|
2012-10-17 01:18:30 +02:00
|
|
|
{
|
2011-07-13 04:43:02 +02:00
|
|
|
if( raw_dist.at<float>(j,i) < 0 )
|
2012-11-07 15:21:20 +01:00
|
|
|
{ drawing.at<Vec3b>(j,i)[0] = (uchar)(255 - abs(raw_dist.at<float>(j,i))*255/minVal); }
|
2011-07-13 04:43:02 +02:00
|
|
|
else if( raw_dist.at<float>(j,i) > 0 )
|
2012-11-07 15:21:20 +01:00
|
|
|
{ drawing.at<Vec3b>(j,i)[2] = (uchar)(255 - raw_dist.at<float>(j,i)*255/maxVal); }
|
2011-07-13 04:43:02 +02:00
|
|
|
else
|
2012-10-17 01:18:30 +02:00
|
|
|
{ drawing.at<Vec3b>(j,i)[0] = 255; drawing.at<Vec3b>(j,i)[1] = 255; drawing.at<Vec3b>(j,i)[2] = 255; }
|
2011-07-13 04:43:02 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Create Window and show your results
|
2012-11-07 15:21:20 +01:00
|
|
|
const char* source_window = "Source";
|
2011-07-13 04:43:02 +02:00
|
|
|
namedWindow( source_window, CV_WINDOW_AUTOSIZE );
|
|
|
|
imshow( source_window, src );
|
|
|
|
namedWindow( "Distance", CV_WINDOW_AUTOSIZE );
|
|
|
|
imshow( "Distance", drawing );
|
|
|
|
|
|
|
|
waitKey(0);
|
|
|
|
return(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|