当前位置:网站首页>Polygon point test
Polygon point test
2022-07-29 02:28:00 【Ask for it, get it, give it up, lose it】
1. Learning content
In this section we will learn about OpenCV Of pointPolygonTest() function :
C++:
double cv::pointPolygonTest (InputArray contour, Point2f pt, bool measureDist)
Python:
cv.pointPolygonTest(contour, pt, measureDist) ->retval
Parameters :
contour: Enter the profile .
pt: Points to test .
measureDist: If it is true , This function estimates the symbolic distance from the point to the nearest contour edge . otherwise , The function only checks whether the point is within the contour .
This function determines that the point is within the contour 、 Outside , Or on the edge ( Or coincident with vertex ). It will return positive accordingly ( Inside )、 negative ( external ) Or zero ( edge ) value . When measureDist=false when , The return values are **+1**,-1 and 0. otherwise , The return value is the signed distance between the point and the nearest contour edge .
2. Code case
2.1 Python Code
from __future__ import print_function
from __future__ import division
import cv2 as cv
import numpy as np
# Create an image
r = 100
src = np.zeros((4*r, 4*r), dtype=np.uint8)
# Create a hexagon 6 vertices
vert = [None]*6
vert[0] = (3*r//2, int(1.34*r))
vert[1] = (1*r, 2*r)
vert[2] = (3*r//2, int(2.866*r))
vert[3] = (5*r//2, int(2.866*r))
vert[4] = (3*r, 2*r)
vert[5] = (5*r//2, int(1.34*r))
# Draw according to the six vertices 6 Edge shape
for i in range(6):
cv.line(src, vert[i], vert[(i+1)%6], ( 255 ), 3)
# Get the outline of the hexagon
contours, _ = cv.findContours(src, cv.RETR_TREE, cv.CHAIN_APPROX_SIMPLE)
# Calculate the distance from the point on the graph to the hexagon ( Signed )
raw_dist = np.empty(src.shape, dtype=np.float32)
for i in range(src.shape[0]):
for j in range(src.shape[1]):
raw_dist[i,j] = cv.pointPolygonTest(contours[0], (j,i), True)
# Find the signed maximum and minimum value
minVal, maxVal, _, maxDistPt = cv.minMaxLoc(raw_dist)
minVal = abs(minVal)
maxVal = abs(maxVal)
# Graphically represent distance
drawing = np.zeros((src.shape[0], src.shape[1], 3), dtype=np.uint8)
for i in range(src.shape[0]):
for j in range(src.shape[1]):
if raw_dist[i,j] < 0:
drawing[i,j,0] = 255 - abs(raw_dist[i,j]) * 255 / minVal
elif raw_dist[i,j] > 0:
drawing[i,j,2] = 255 - raw_dist[i,j] * 255 / maxVal
else:
drawing[i,j,0] = 255
drawing[i,j,1] = 255
drawing[i,j,2] = 255
cv.circle(drawing,maxDistPt, int(maxVal),(255,255,255), 1, cv.LINE_8, 0)
cv.imshow('Source', src)
cv.imshow('Distance and inscribed circle', drawing)
cv.waitKey()
2.2 C++ Code
#include <opencv2/highgui.hpp>
#include <opencv2/imgproc.hpp>
#include <iostream>
using namespace cv;
using namespace std;
int main( void )
{
const int r = 100;
Mat src = Mat::zeros( Size( 4*r, 4*r ), CV_8U );
vector<Point2f> vert(6);
vert[0] = Point( 3*r/2, static_cast<int>(1.34*r) );
vert[1] = Point( 1*r, 2*r );
vert[2] = Point( 3*r/2, static_cast<int>(2.866*r) );
vert[3] = Point( 5*r/2, static_cast<int>(2.866*r) );
vert[4] = Point( 3*r, 2*r );
vert[5] = Point( 5*r/2, static_cast<int>(1.34*r) );
for( int i = 0; i < 6; i++ )
{
line( src, vert[i], vert[(i+1)%6], Scalar( 255 ), 3 );
}
vector<vector<Point> > contours;
findContours( src, contours, RETR_TREE, CHAIN_APPROX_SIMPLE);
Mat raw_dist( src.size(), CV_32F );
for( int i = 0; i < src.rows; i++ )
{
for( int j = 0; j < src.cols; j++ )
{
raw_dist.at<float>(i,j) = (float)pointPolygonTest( contours[0], Point2f((float)j, (float)i), true );
}
}
double minVal, maxVal;
Point maxDistPt; // inscribed circle center
minMaxLoc(raw_dist, &minVal, &maxVal, NULL, &maxDistPt);
minVal = abs(minVal);
maxVal = abs(maxVal);
Mat drawing = Mat::zeros( src.size(), CV_8UC3 );
for( int i = 0; i < src.rows; i++ )
{
for( int j = 0; j < src.cols; j++ )
{
if( raw_dist.at<float>(i,j) < 0 )
{
drawing.at<Vec3b>(i,j)[0] = (uchar)(255 - abs(raw_dist.at<float>(i,j)) * 255 / minVal);
}
else if( raw_dist.at<float>(i,j) > 0 )
{
drawing.at<Vec3b>(i,j)[2] = (uchar)(255 - raw_dist.at<float>(i,j) * 255 / maxVal);
}
else
{
drawing.at<Vec3b>(i,j)[0] = 255;
drawing.at<Vec3b>(i,j)[1] = 255;
drawing.at<Vec3b>(i,j)[2] = 255;
}
}
}
circle(drawing, maxDistPt, (int)maxVal, Scalar(255,255,255));
imshow( "Source", src );
imshow( "Distance and inscribed circle", drawing );
waitKey();
return 0;
}

Refer to the directory
https://docs.opencv.org/4.x/dc/d48/tutorial_point_polygon_test.html
边栏推荐
- 记一次 ERROR scheduler.AsyncEventQueue: Dropping event from queue shared导致OOM
- PS + PL heterogeneous multicore case development manual for Ti C6000 tms320c6678 DSP + zynq-7045 (2)
- Excel uses countif statistics
- Explain asynchronous tasks in detail: task status and lifecycle management
- 网络安全漏洞管理的探索与实践
- 一文理解分布式开发中的服务治理
- 当Synchronized遇到这玩意儿,有个大坑,要注意
- Kubesphere multi node installation
- 指针——黄金阶段
- Prometheus + alertmanager message alert
猜你喜欢

指针——黄金阶段

2022/07/28 学习笔记 (day18) 常用API

Interprocess communication - detailed explanation of the pipeline (explanation of graphic cases)

如何把thinkphp5的项目迁移到阿里云函数计算来应对流量洪峰?

Responsive dream weaving template home decoration building materials website
![[cloud native and 5g] micro services support 5g core network](/img/c9/4ccacd1e70285c2ceb50c324e5018c.png)
[cloud native and 5g] micro services support 5g core network

响应式织梦模板酒店客房类网站

ES6事件绑定(v-on用法)

WebView attack

Prometheus + AlertManager 消息预警
随机推荐
7/28 高斯消元解线性方程组+高斯消元解异或线性方程组 +求组合数ii
The financing demand of 129 million yuan was released, and the roadshow of the Dake city project continued to irrigate the "good seedlings" of scientific innovation
Jetpack -- understand the use of ViewModel and livedata
字符流综合练习解题过程
MySQL之数据查询(多表查询)
Meeting notice of meeting OA
向量相似度评估方法
记一次 ERROR scheduler.AsyncEventQueue: Dropping event from queue shared导致OOM
JVM memory overflow online analysis dump file and online analysis open.Hprof file to get JVM operation report how jvisualvm online analysis
ES6详解 快速上手!
裂开了,一次连接池参数导致的雪崩问题
特殊流&Properties属性集实例遇到的问题及解决方法
6年测试经验,教大家测试~如何把控项目
防止勒索软件攻击数据的十种方法
指针——黄金阶段
Excel 用countif 统计
The first of the five tips for using browsers efficiently is the most practical
Servlet三种实现方式
Data security and privacy computing summit - development and application of security intersection in privacy Computing: Learning
Even PostgreSQL problem: expected authentication request from server, but received V