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

 #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. java递归处理文件夹和文件

    import java.io.File; /** * 文件综合使用示例 */ public class FileDelete { public static void main(String[] ar ...

  2. pat 甲级 L3-002. 堆栈

    L3-002. 堆栈 时间限制 200 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 大家都知道“堆栈”是一种“先进后出”的线性结构,基本操作有 ...

  3. Javascript的SEO优化技巧

    原文发布时间为:2010-10-22 -- 来源于本人的百度文章 [由搬家工具导入] 1.外部崁入javascript在撰写一些比较复杂的网页特效,如下拉式选单等,会产生大量的javascript码, ...

  4. div模拟dropdownlist控件 div下拉菜单

    原文发布时间为:2009-10-16 -- 来源于本人的百度文章 [由搬家工具导入] 控件发布:div2dropdownlist(div模拟dropdownlist控件) div3dropdownli ...

  5. Http协议和Tomcat服务器安装与eclipse集成(重要)

    一.Http协议 1.什么是Http协议 HTTP,超文本传输协议(HyperText Transfer Protocol)是互联网上应用最为广泛的     一种网络协议.所有的WWW文件都必须遵守这 ...

  6. asp.net内置对象 Response对象使用介绍

    Response对象是HttpRespone类的一个实例.该类主要是封装来自ASP.NET操作的HTTP相应信息.Response对象将数据作为请求的结果从服务器发送到客户浏览器中,并提供有关响应的消 ...

  7. 解决 Mac OS X 下 IntelliJ IDEA、jEdit 等 Java 程序中文标点输入无效的方法

    Mac OS X 下基于 Java 的程序(如 IntelliJ IDEA.jEdit 等)会出现中文标点输入无效的问题,在中文输入法状态,可以输入中文字,但输入中文标点最后上去的是英文标点.查阅了相 ...

  8. interview fb2

    2014.7.8fb #include <iostream> using namespace std; struct TreeNode{ int val; TreeNode *left; ...

  9. LeetCode OJ-- Combination Sum II **

    https://oj.leetcode.com/problems/combination-sum-ii/ 一列数,每个数只能用一次或者不用,给出和为target的组合. 递归写的深搜,使用了编程技巧, ...

  10. 站点部署,IIS配置优化指南

    目录 一. 二. 三. 四. 五. 六. 七.       安全性 八.       多服务器IIS集中化管理web 通常把站点发布到IIS上运行正常后,很少会去考虑IIS提供的各种参数,如何配置才是 ...