#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
using namespace cv;
using namespace std;
int main( int argc, char** argv )
{
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);
vert[0] = Point( 1.5*r, 1.34*r );
vert[1] = Point( 1*r, 2*r );
vert[2] = Point( 1.5*r, 2.866*r );
vert[3] = Point( 2.5*r, 2.866*r );
vert[4] = Point( 3*r, 2*r );
vert[5] = Point( 2.5*r, 1.34*r );
//Draw it in src
for( int j = 0; j < 6; j++ )
{ line( src, vert[j], vert[(j+1)%6], Scalar( 255 ), 3, 8 ); }
// Get the contours
vector<vector<Point> > contours; vector<Vec4i> hierarchy;
Mat src_copy = src.clone();
findContours( src_copy, contours, hierarchy, RETR_TREE, CHAIN_APPROX_SIMPLE);
// 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++ )
{ raw_dist.at<float>(j,i) = pointPolygonTest( contours[0], Point2f(i,j), true ); }
}
double minVal; double maxVal;
minMaxLoc( raw_dist, &minVal, &maxVal, 0, 0, Mat() );
minVal = abs(minVal); maxVal = abs(maxVal);
//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++ )
{
if( raw_dist.at<float>(j,i) < 0 )
{ drawing.at<Vec3b>(j,i)[0] = 255 - (int) abs(raw_dist.at<float>(j,i))*255/minVal; }
else if( raw_dist.at<float>(j,i) > 0 )
{ drawing.at<Vec3b>(j,i)[2] = 255 - (int) raw_dist.at<float>(j,i)*255/maxVal; }
else
{ drawing.at<Vec3b>(j,i)[0] = 255; drawing.at<Vec3b>(j,i)[1] = 255; drawing.at<Vec3b>(j,i)[2] = 255; }
}
}
//Create Window and show your results
char* source_window = "Source";
namedWindow( source_window, CV_WINDOW_AUTOSIZE );
imshow( source_window, src );
namedWindow( "Distance", CV_WINDOW_AUTOSIZE );
imshow( "Distance", drawing );
waitKey(0);
return(0);
}
opencv-Point Polygon Test
原创
©著作权归作者所有:来自51CTO博客作者Digital2Slave的原创作品,请联系作者获取转载授权,否则将追究法律责任
上一篇:CIF,4CIF,QCIF,D1
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
Python OpenCV #2 - OpenCV中的GUI功能
本文介绍了OpenCV的基本方发,包括图像读取、显示和写入。
python ide OpenCV -
OpenCV 点表示:Point 类
Point 类数据结构表示了二维
OpenCV 二维 2d 数据结构 -
java 去list 集合中的下一个
一些大公司面试总会问一些Java集合相关的知识,如ArrayList实现原理、HashMap实现原理等。所以作者准备做一个针对Java集合进行剖析的专题(后续几篇文章会全部围绕Java集合展开)。本篇文章没有对Java集合中的具体某个集合做详细剖析,只是让大家对Java集合有个宏观把控,为后面的学习做铺垫。Java集合架构图 Java集合框架图 如上图所示,框架图主要分为两大类,第一类是
java 去list 集合中的下一个 java list 某个重复列 java list有序还是无序 java map keyset java 有序map