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

 #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. Eclipse 无法查看第三方jar包文件源代码解决方法

    1.打开第三方依赖包,源文件的快捷键:ctrl + mouseClick 2.由于我们下载的第三方jar 包,如Spring等相关的依赖包时,并没有附加下载相应的源文件,所以经常出现如图的这种问题. ...

  2. mongoDB权威指南学习笔记

    //mongoDB第1-3章节添加,修改,修改器的笔记: //备注:和MySQL查询一样,时刻想着优化查询数据的时间和性能 //db.help() //数据库帮助信息 //db.blog.help() ...

  3. Linux中的小括号和大括号,${}/$()/()/{}/${var:-string}/${var:=string}/${var:+string}/${var:?string}/${var%pattern}/${var#pattern}/${var%%pattern}/${var##pattern}

    简单记录一下大小括号在Linux中的用处. 1.${var},这是Linux中变量的原形.所以$var,别忘记了你的本来面目. # a= # echo $a # echo ${a} # echo ${ ...

  4. js3:数据类型,数组,String各个属性,以及字符串表达式用eval计算

    原文发布时间为:2008-11-08 -- 来源于本人的百度文章 [由搬家工具导入] <html> <head> <title>js</title> & ...

  5. Google 最新的 Fuchsia OS【科技讯息摘要】

    转自:http://www.cnblogs.com/pied/p/5771782.html 就是看到篇报道,有点好奇,就去FQ挖了点东西回来. 我似乎已开始就抓到了重点,没错,就是 LK . LK 是 ...

  6. 记录: 一次解决整型溢出攻击(使用scala,隐式转换)

    最近项目遇到一次整型溢出攻击 有一个功能,玩家购买num个物品. 每个物品花费14货币. 客户端限制玩家只能购买 1-9999个该物品. 但是某玩家通过技术手段,获得了客户端的运行权限. 于是发送协议 ...

  7. 安卓edittext实现输入数字限制条件的效果

    我们知道edittext能指定输入字符类型,这次我们就来了解下在数字模式下的一些显示控制输入的效果 1.限制输入数字 android:inputType="number|numberDeci ...

  8. Codeforces 429D Tricky Function(平面最近点对)

    题目链接  Tricky Function $f(i, j) = (i - j)^{2} + (s[i] - s[j])^{2}$ 把$(i, s[i])$塞到平面直角坐标系里,于是转化成了平面最近点 ...

  9. 对CSDN的理性吐槽

    CSDN博客网站首页挂了....从使用CSDN博客以来,大大小小的故障出过十几次.........再这样的话我都要对这个网站失去信心了

  10. 洛谷——P1621 集合

    P1621 集合 题目描述 现在给你一些连续的整数,它们是从A到B的整数.一开始每个整数都属于各自的集合,然后你需要进行一下的操作: 每次选择两个属于不同集合的整数,如果这两个整数拥有大于等于P的公共 ...