当前位置:网站首页>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
边栏推荐
- Meeting notice of meeting OA
- 7/28 Gauss elimination to solve linear equations + Gauss elimination to solve XOR linear equations + find the combination number II
- Vector similarity evaluation method
- Keil5 open the engineering prompt not found device solution
- Data security and privacy computing summit - development and application of security intersection in privacy Computing: Learning
- ES6详解 快速上手!
- "Activity recommendation" rush rush! 2022 international open source Festival has new content
- DevOps 团队如何抵御 API 攻击?
- What should I do if excel opens a CSV file containing Chinese characters and there is garbled code?
- 会议OA之会议通知
猜你喜欢

On Multithreading

第3章业务功能开发(线索备注的删除和修改)

What should I do if excel opens a CSV file containing Chinese characters and there is garbled code?

Character flow comprehensive exercise problem solving process

I was stunned by this question that I browsed 746000 times

Read the recent trends of okaleido tiger and tap the value and potential behind it
![[one · data | chained binary tree]](/img/83/d62a47f1264673f1e898335303a7a6.png)
[one · data | chained binary tree]

Responsive dream weaving template home decoration building materials website

记一次 ERROR scheduler.AsyncEventQueue: Dropping event from queue shared导致OOM

如何在多御安全浏览器中自定义新标签页?
随机推荐
How awesome is the architecture of "12306"?
一文搞懂 Redis 架构演化之路
On Multithreading
Prometheus + AlertManager 消息预警
ResNet50+k折交叉验证+数据增强+画图(准确率、召回率、F值)
Waiting queue wait_ queue
Keil5 open the engineering prompt not found device solution
virsh console连接失败问题
Altium designer outputs Gerber and other production documents
高效使用浏览器的5个小技巧,第1个技巧最实用
开启TLS加密的Proftpd安全FTP服务器安装指南
Data query of MySQL (multi table query)
7/28 高斯消元解线性方程组+高斯消元解异或线性方程组 +求组合数ii
响应式织梦模板酒店客房类网站
聊聊 Feign 的实现原理
发布融资需求1.29亿元,大科城项目路演持续浇灌科创“好苗子”
【上传图片2-可裁剪】
Read the recent trends of okaleido tiger and tap the value and potential behind it
Time pit in MySQL driver
The first of the five tips for using browsers efficiently is the most practical