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

 #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. 【CCF】 Markdown 模拟

    #include<iostream> #include<cstdio> #include<string> #include<cstring> #incl ...

  2. ALICTF2014 EvilAPK4脱壳分析

    相关文件可以在下面链接中下载: http://pan.baidu.com/s/1sjpvFy9 1 简述 该apk使用libmobisec.so函数实现对dex的解密还原.真正的dex为assets目 ...

  3. windows系统部署springboot项目及绑定域名

    http://note.youdao.com/noteshare?id=c3ccea255affd2c5d79231d67fa29103&sub=187AEEEA5CF34531A2C2315 ...

  4. [暑假集训--数论]poj2909 Goldbach's Conjecture

    For any even number n greater than or equal to 4, there exists at least one pair of prime numbers p1 ...

  5. 【04】react 之 复合组件

    1.1.  什么是组件? 前端开发中组件也称为UI组件,组件即将一段或几段完成各自功能的代码段封装为一个或几个独立的部分.UI组件包含了这样一个或几个具有各自功能的代码段,最终完成了用户界面的表示.R ...

  6. MySQL常用查询方法

    SELECT TIME(NOW()); -- 15:23:07 SELECT CURTIME(NOW());-- 15:23:07 SELECT ABS(-4); -- 4 SELECT 5 MOD ...

  7. 页面get post等查看

    原文发布时间为:2010-03-08 -- 来源于本人的百度文章 [由搬家工具导入] http://www.fiddler2.com/Fiddler2/firstrun.asp

  8. [LeetCode] Balanced Binary Tree 深度搜索

    Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...

  9. cmd窗口情况下:windows下cmd默认的编码是GBK

    想在windows下查看sqlite的utf-8中文需要先 执行chcp 65001把当前页换为utf-8编码 chcp 命令: chcp 65001  就是换成UTF-8代码页,在命令行标题栏上点击 ...

  10. 《Linux命令行与shell脚本编程大全 第3版》Linux命令行---33

    以下为阅读<Linux命令行与shell脚本编程大全 第3版>的读书笔记,为了方便记录,特地与书的内容保持同步,特意做成一节一次随笔,特记录如下: