/******************实现功能:判断平面任一点是否在指定多边形内********************/

 #include <string>
#include <vector>
#include <math.h>
#include <iostream>
#include <fstream>
#include <sstream> using namespace std; typedef struct { int x, y; } Point; //算法说明 file:///D:/ddb/相机标定/Point_In_Polygon.pdf
// a Point is defined by its coordinates {int x, y;}
//=================================================================== // isLeft(): tests if a point is Left|On|Right of an infinite line.
// Input: three points P0, P1, and P2
// Return: >0 for P2 left of the line through P0 and P1
// =0 for P2 on the line
// <0 for P2 right of the line
// See: Algorithm 1 "Area of Triangles and Polygons" inline int
isLeft(Point P0, Point P1, Point P2)
{
return ((P1.x - P0.x) * (P2.y - P0.y)
- (P2.x - P0.x) * (P1.y - P0.y));
} //==================================================================
// cn_PnPoly(): crossing number test for a point in a polygon
// Input: P = a point,
// V[] = vertex points of a polygon V[n+1] with V[n]=V[0]
// Return: 0 = outside, 1 = inside
// This code is patterned after [Franklin, 2000]
int
cn_PnPoly(Point P, Point* V, int n)
{
int cn = ; // the crossing number counter // loop through all edges of the polygon
for (int i = ; i < n; i++)
{
// edge from V[i] to V[i+1]
if (((V[i].y <= P.y) && (V[i + ].y > P.y))||((V[i].y > P.y) && (V[i + ].y <= P.y))) // an upward crossing
{
// a downward crossing
// compute the actual edge-ray intersect x-coordinate
float vt = (float)(P.y - V[i].y) / (V[i + ].y - V[i].y);
if (P.x < V[i].x + vt * (V[i + ].x - V[i].x)) // P.x < intersect
++cn; // a valid crossing of y=P.y right of P.x
}
}
return (cn & ); // 0 if even (out), and 1 if odd (in)
} // wn_PnPoly(): winding number test for a point in a polygon
// Input: P = a point,
// V[] = vertex points of a polygon V[n+1] with V[n]=V[0]
// Return: wn = the winding number (=0 only when P is outside)
int
wn_PnPoly(Point P, Point* V, int n)
{
int wn = ; // the winding number counter
// loop through all edges of the polygon
for (int i = ; i<n; i++)
{
// edge from V[i] to V[i+1]
if (V[i].y <= P.y)
{
// start y <= P.y
if (V[i + ].y > P.y) // an upward crossing
if (isLeft(V[i], V[i + ], P) > ) // P left of edge
++wn; // have a valid up intersect
}
else
{ // start y > P.y (no test needed)
if (V[i + ].y <= P.y) // a downward crossing
if (isLeft(V[i], V[i + ], P) < ) // P right of edge
--wn; // have a valid down intersect
}
}
return wn;
} int
wn_PnPoly_ddb(Point& P, const std::vector<Point>& V) // std::vector<Point>& V = vertex points of a polygon V[n + 1] with V[n] = V[0] , 注意多一个点首尾相同
{
int wn = ; // the winding number counter
int n = V.size() - ; // loop through all edges of the polygon
for (int i = ; i<n; i++)
{
// edge from V[i] to V[i+1]
if (V[i].y <= P.y)
{
// start y <= P.y
if (V[i + ].y > P.y) // an upward crossing
if (isLeft(V[i], V[i + ], P) > ) // P left of edge
++wn; // have a valid up intersect
}
else
{ // start y > P.y (no test needed)
if (V[i + ].y <= P.y) // a downward crossing
if (isLeft(V[i], V[i + ], P) < ) // P right of edge
--wn; // have a valid down intersect
}
}
return wn;
} void
readImageROIpolygen(const std::string& path, std::vector<Point>& pointVec, int i)
{
char name[];
string filename;
Point p2d; int length = sprintf(name, "%d%s", i + , "_ROI.txt");
filename = path + "/" + name;
ifstream infile(filename); string Fline;
if (infile) // 有该文件
{
while (getline(infile, Fline)) // Fline中不包括每行的换行符
{
//cout << Fline << endl;
string buf;
stringstream ss(Fline); // 字符流ss
vector<string> tokens; // vector // 按照逗号分隔
while (getline(ss, buf, ','))
{
tokens.push_back(buf);
} ////stringstream 按空格分开字符输入
//while (ss >> buf)
//{
// tokens.push_back(buf);
//} if (tokens.size()<)
{
continue;
}
else if (tokens.size() == )
{
int j = ;
p2d.x = atof(tokens[j].c_str()); //根据字段顺序做修改 对应图像 u v
j++;
p2d.y = atof(tokens[j].c_str());
pointVec.push_back(p2d);
}
}
pointVec.push_back(pointVec[]);
//std::cout << i << endl;
}
else // 没有该文件
{
cout << "error:: no such file" << endl;
}
infile.close(); } /**************** Test ****************/
void
main()
{
Point detectPoint;
detectPoint.x = ; //1363, 1260 //2896,2276 //2653,390
detectPoint.y = ;
std::string path="矫正后图像"; std::vector<Point> pointVec;
readImageROIpolygen(path, pointVec,);
if (pointVec.size()==)
{
cout << "no ROI!!!" << endl;
return;
} if (wn_PnPoly_ddb(detectPoint, pointVec) == )
{
cout << "detectPoint outside ImageROIpolygen!!!!" << endl;
}
else
{
cout << "detectPoint in ImageROIpolygen!!!!" << endl;
} system("pause");
}

detect——point_in_polygon的更多相关文章

  1. Microsoft Windows* SDK May 2010 或较新版本(兼容 2010 年 6 月 DirectX SDK)GPU Detect

    原文链接 下载代码样本 特性/描述 日期: 2016 年 5 月 5 日 GPU Detect 是一种简短的示例,演示了检测系统中主要显卡硬件(包括第六代智能英特尔® 酷睿™ 处理器产品家族)的方式. ...

  2. 【maven】pom.xml报错:Cannot detect Web Project version.

    新建的maven项目 报错如下: Cannot detect Web Project version. Please specify version of Web Project through &l ...

  3. JS: How to detect my browser version and operating system using JavaScript?

    Example: 1. for IE 11,  navigator.userAgent  returns "Mozilla/5.0 (Windows NT 6.1; WOW64; Tride ...

  4. python flask detect browser language

    python flask detect browser language   No problem. We won't show you that ad again. Why didn't you l ...

  5. javascript: detect mobile devices or browser

    http://detectmobilebrowsers.com/ http://hgoebl.github.io/mobile-detect.js/ http://www.hand-interacti ...

  6. ZOJ 3430 Detect the Virus

    传送门: Detect the Virus                                                                                ...

  7. Leetcode: Graph Valid Tree && Summary: Detect cycle in undirected graph

    Given n nodes labeled from 0 to n - 1 and a list of undirected edges (each edge is a pair of nodes), ...

  8. linux安装phpstorm出现 Startup Error: Unable to detect graphics environment

    在centos6.2下在安装phpstorm下遇到 Startup Error: Unable to detect graphics environment 其中jdk安装的版本是 1.7.0_79  ...

  9. GoldenGate: Extract Abend with Detect Inconsistency in Pdata (文档 ID 1355067.1)

    APPLIES TO: Oracle GoldenGate - Version 10.4.0.0 and laterInformation in this document applies to an ...

随机推荐

  1. iOS-读取txt文件中文乱码

    一.情景描述: 后台给一个txt文件,编码是utf-8,在Mac电脑Xcode开发环境下读取txt文件内容,汉字会出现乱码,英文没有乱码这种情况. 二.尝试解决方法: 修改编码格式,尝试了NSUTF1 ...

  2. Python之数据结构:列表

    列表:处理一组有序项目的数据结构 一.基本操作 1.列表运算符 list1=[2,3,4,5,6,7,8] print len(list1) print [1,2]+[3,4] print ['Hi' ...

  3. 【02】【转】Nodejs学习笔记(三)--- 事件模块

    目录 简介及资料 事件常用函数及使用 emitter.on(event, listener) emitter.emit(event, [arg1], [arg2], [...]) emitter.on ...

  4. input输入框与元素间有间隙

    <div class="container"> <button>1</button> <button>2</button> ...

  5. .NET返回上一页

    原文发布时间为:2010-05-25 -- 来源于本人的百度文章 [由搬家工具导入] if (Request.UrlReferrer != null)                {         ...

  6. struts 中继承ActionSupport类

    理论上Struts 2.0的Action无须实现任何接口或继承任何类型,但是,我们为了方便实现Action,大多数情况下都会继承 com.opensymphony.xwork2.ActionSuppo ...

  7. input上报流程分析【转】

    转自:http://blog.chinaunix.net/uid-28320320-id-3389196.html .参考文章 [Andorid]input系统的事件处理 .源码分析 linux )查 ...

  8. hdu 4520

    小Q系列故事——最佳裁判 Time Limit: 500/200 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Total ...

  9. LeetCode OJ--Multiply Strings **

    https://oj.leetcode.com/problems/multiply-strings/ 用字符串实现大数乘法,细节题,细节很多 class Solution { public: stri ...

  10. 10.1综合强化刷题 Day6

    T1 排序 题目描述 小Z 有一个数字序列a1; a2; .... ; an,长度为n,小Z 只有一个操作:选 定p(1<p<n),然后把ap 从序列中拿出,然后再插⼊到序列中任意位置. ...