当前位置:网站首页>OpenCV求两个区域的交集
OpenCV求两个区域的交集
2022-07-23 14:13:00 【libaineu2004】
一、文章1
https://blog.csdn.net/lingyunxianhe/article/details/104948684/
两个多边形相交区域面积求解的算法网上找到的有些层次不齐,但算法都大致相同,就是计算多边形线段相交求取交点,然后找到交叠区域。在查找算法和代码中发现一些好的程序,一并整理在此。
1、https://github.com/abreheret/polygon-intersection
Simple algo to find convex polygon intersection and compute area of polygone with using OpenCV
先放一张运行效果图:

这个ui界面可以鼠标交互,拖动多边形,动态查看交叠面积。这个做的非常好。
2、https://github.com/LazyVinh/convex-polygon-intersection
A rather simple algorithm for intersecting to polygons in the 2D space
先放一张运行效果图:

这个工程也做的非常好,能随机产生不同的多边形。而且代码很简洁,非常适合算法原理梳理。
3、https://www.cnblogs.com/kannyi/p/10734255.html
这是一篇博客上的使用c程序实现多边形相交面积求解,能编译通过,也能运行,但结果计算结果不对。
在github上搜索polygon intersect 找到的能较好的工程就1和2这两,其他的不是这问题就是那问题。
下面我自己使用opencv实现了一下一张图像中两个多边形相交区域面积求解
由于opencv没有直接求取两个多边形相交的算法和程序,在此我利用求解问题的特殊性并使用里面其他函数组合来解决这一问题
import os
import json
import cv2
import numpy as np
#从标注文件中获取多边形
def GetPolygon(AnnotPath):
with open(AnnotPath,'r') as FId:
AnnotInfo=json.load(FId)
shapes=AnnotInfo['shapes']
Polygon1=np.array(shapes[0]['points']).astype(int)
Polygon2=np.array(shapes[1]['points']).astype(int)
ImH=AnnotInfo["imageHeight"]
ImW=AnnotInfo["imageWidth"]
return ImH,ImW,Polygon1,Polygon2
def DrawPolygon(ImShape,Polygon,Color):
Im = np.zeros(ImShape, np.uint8)
try:
cv2.fillPoly(Im, Polygon, Color) # 只使用这个函数可能会出错,不知道为啥
except:
try:
cv2.fillConvexPoly(Im, Polygon, Color)
except:
print('cant fill\n')
return Im
def Get2PolygonIntersectArea(ImShape,Polygon1,Polygon2):
Im1 =DrawPolygon(ImShape[:-1],Polygon1,122)#多边形1区域填充为122
Im2 =DrawPolygon(ImShape[:-1], Polygon2, 133)#多边形2区域填充为133
Im = Im1 + Im2
ret, OverlapIm = cv2.threshold(Im, 200, 255, cv2.THRESH_BINARY)#根据上面的填充值,因此新图像中的像素值为255就为重叠地方
IntersectArea=np.sum(np.greater(OverlapIm, 0))#求取两个多边形交叠区域面积
#下面使用opencv自带的函数求取一下,最为对比
contours, hierarchy = cv2.findContours(OverlapIm,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
contourArea=cv2.contourArea(contours[0])
print('contourArea={}\n'.format(contourArea))
perimeter = cv2.arcLength(contours[0], True)
print('contourPerimeter={}\n'.format(perimeter))
RealContourArea=contourArea+perimeter
print('RealContourArea={}\n'.format(RealContourArea))
return IntersectArea,OverlapIm
if __name__ == '__main__':
AnnotPath='./test.json'
ImH,ImW,Polygon1,Polygon2=GetPolygon(AnnotPath)
ImShape=(ImH,ImW,3)
Im1 = DrawPolygon(ImShape, Polygon1, (255, 0, 0))
Im2 = DrawPolygon(ImShape, Polygon2, (0, 255, 0))
cv2.imshow('ColorPolygons', Im1 + Im2)
IntersectArea,OverlapIm=Get2PolygonIntersectArea(ImShape, Polygon1, Polygon2)
print('IntersectArea={}\n'.format(IntersectArea))
cv2.imshow('OverlapIm', OverlapIm)
cv2.waitKey(0)

第一张彩色图为两个多边形区域,第二个为重叠区域
————————————————
版权声明:本文为CSDN博主「粼粼淇」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/lingyunxianhe/article/details/104948684/
二、文章2
https://sxj731533730.blog.csdn.net/article/details/106818499

#include <opencv2/opencv.hpp>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>
using namespace std;
using namespace cv;
int main() {
//Mat test = imread("/home/ubuntu/CLionProjects/test/1.jpg");
Mat img = Mat::zeros(Size(400, 400), CV_8UC1);
Rect rec(100, 100, 100, 100);
img(rec) = Scalar(255, 255, 255);
imshow("img1", img);
Mat img1 = Mat::zeros(Size(400, 400), CV_8UC1);
Rect rec1(170, 150, 130, 120);
img1(rec1) = Scalar(255, 255, 255);
imshow("img2", img1);
Mat dst = Mat::zeros(Size(400, 400), CV_8UC1);;
bitwise_and(img, img1, dst);
int iVal255 = countNonZero(dst);
cout << "相交比的面积为" << iVal255 << endl;
imshow("and", dst);
waitKey(0);
return 0;
}
三、文章3
https://www.cnblogs.com/dwdxdy/p/3232110.html
https://download.csdn.net/download/qq_24038299/10260393
边栏推荐
- 虚拟机网络连接方式
- First deep search and first wide search of graph (realized by three methods)
- Microcomputer principle and technical interface practice in class
- Detector: detect objects with recursive feature pyramid and switchable atolos convolution
- 死磕遞歸1:遞推公式
- 【Flutter -- 布局】弹性布局(Flex 和 Expanded)
- Eureka notes
- General paging implementation
- keil错误和解决办法(1):FCARM - Output Name not specified, please check ‘Options for Target - Utilities‘
- SSD: Single Shot MultiBox Detector
猜你喜欢

Notes on Microcomputer Principle and technical interface

Bag of tricks for image classification "with convolutional neural networks"

The first stage of basic knowledge of numpy data analysis (numpy Foundation)

IDEA中给项目添加依赖的jar包

移动、电信、联通:5G To B的花式解法

Squeeze and incentive networks

灰色预测(MATLAB)

Scale Match for Tiny Person Detection

Microcomputer principle and technical interface practice in class

无心剑英汉双语诗006.《致爱妻》
随机推荐
PIP reports an error could not find a version that satisfies the... No matching distribution
C语言基础篇 —— 2-5 指针与函数知识点
Ros2 self study notes: rqt visualization tool
Could not load dynamic library ‘cudnn64_8.dll‘; dlerror: cudnn64_8.dll not found
低代码搭建校园信息化管理系统案例分析
Tensorflow2.x actual combat series softmax function
Heartless sword English Chinese bilingual poem 006. to my wife
Vscode - code and file changes cannot be saved
docker 安装redis
Fundamentals of C language -- 2-6 pointers, arrays and sizeof operators
详解一次SQL优化
keil错误和解决办法(1):FCARM - Output Name not specified, please check ‘Options for Target - Utilities‘
搜索二叉树——寻找节点,插入节点,删除节点
Priyanka Sharma, general manager of CNCF Foundation: read CNCF operation mechanism
Microcomputer principle and technical interface practice in class
Wechat applet class binding, how to bind two variables
Eureka notes
ROS2自学笔记:Rviz可视化工具
Weisfeiler-Lehman图同构测试及其他
数组和特殊矩阵的压缩存储