当前位置:网站首页>Opencv finding the intersection of two regions
Opencv finding the intersection of two regions
2022-07-23 17:20:00 【libaineu2004】
One 、 article 1
https://blog.csdn.net/lingyunxianhe/article/details/104948684/
Algorithm for solving the area of the intersection area of two polygons. Some levels found on the Internet are uneven , But the algorithms are roughly the same , It is to calculate the intersection of polygon segments and find the intersection , Then find the overlapping area . Find some good programs in the search algorithm and code , Put them together here .
1、https://github.com/abreheret/polygon-intersection
Simple algo to find convex polygon intersection and compute area of polygone with using OpenCV
First put a picture of the operation :

This ui The interface can interact with the mouse , Drag the polygon , Dynamically view the overlapping area . This is done very well .
2、https://github.com/LazyVinh/convex-polygon-intersection
A rather simple algorithm for intersecting to polygons in the 2D space
First put a picture of the operation :

This project is also done very well , It can generate different polygons randomly . And the code is simple , It is very suitable for combing algorithm principles .
3、https://www.cnblogs.com/kannyi/p/10734255.html
This is a blog use c Program to solve the intersection area of polygons , Can compile through , It can also run , But the result is wrong .
stay github On the search polygon intersect Find a better project 1 and 2 These two , The other is either this problem or that problem .
Now I use it myself opencv The area of the intersection area of two polygons in an image is solved
because opencv There is no algorithm or program to directly find the intersection of two polygons , Here I use the particularity of solving the problem and use other function combinations to solve this problem
import os
import json
import cv2
import numpy as np
# Get polygons from dimension files
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) # Using only this function may cause errors , I don't know why
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)# polygon 1 Area fill as 122
Im2 =DrawPolygon(ImShape[:-1], Polygon2, 133)# polygon 2 Area fill as 133
Im = Im1 + Im2
ret, OverlapIm = cv2.threshold(Im, 200, 255, cv2.THRESH_BINARY)# According to the filling value above , Therefore, the pixel value in the new image is 255 It's the overlap
IntersectArea=np.sum(np.greater(OverlapIm, 0))# Calculate the area of the overlapping area of two polygons
# Use opencv Find the function that comes with it , The best contrast
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)

The first color picture shows two polygon areas , The second is the overlapping area
————————————————
Copyright notice : This paper is about CSDN Blogger 「 Sparkling Qi 」 The original article of , follow CC 4.0 BY-SA Copyright agreement , For reprint, please attach the original source link and this statement .
Link to the original text :https://blog.csdn.net/lingyunxianhe/article/details/104948684/
Two 、 article 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 << " The area of the intersection ratio is " << iVal255 << endl;
imshow("and", dst);
waitKey(0);
return 0;
}
3、 ... and 、 article 3
https://www.cnblogs.com/dwdxdy/p/3232110.html
https://download.csdn.net/download/qq_24038299/10260393
边栏推荐
- OpenCV求两个区域的交集
- 考过PMP对实际工作帮助大吗?
- 简单了解首个 EVM 等效的 zkEVM Polygon 为何全力押注
- leetcode刷题记录
- 通用分页实现
- unity之制作二维码扫描
- Investment and finance report this week: Web3 game bear market attracts gold
- Keil errors and solutions (1): fcarm - output name not specified, please check 'options for target - Utilities‘
- PPPoE协议讲解以及拨号过程Wireshark抓包解析
- Nodejs implements token login registration (koa2)
猜你喜欢

Sorting - introduction, code ideas, usage suggestions, code implementation -1

Function secondary development / plug-in development of JMeter (detailed version)

零基础怎么自学软件测试?十年测试老鸟最强软件测试学习路线图

Detailed explanation of SQL bool blind note and time blind note

Explain SQL optimization in detail

leetcode刷题记录

Pymoo学习 (2):带约束的双目标优化问题

简单了解首个 EVM 等效的 zkEVM Polygon 为何全力押注

通过SSH方式访问内网RDS+mysql

食品安全|爱吃烟熏食品的注意了,这些知识你知道吗
随机推荐
Failure analysis and solution of vscode PIO creation project
深度学习学习记录-优化器的学习率的更新
日志瘦身骚操作:从5G优化到1G!
SQL報錯盲注詳解
php文件锁抽奖防止并发
腾讯撕开中国NFT的“遮羞布”
[MySQL]一、MySQL起步
Browser homology policy
【30. n-皇后问题】
蓝桥杯真题:卡片[通俗易懂]
Compose canvas pie chart effect drawing
OpenIM重大升级-群聊读扩散模型发布 群管理功能升级
Thoughts on software quality system
Récursion des bosses 1: formule récursive
阿里二面:什么是CAS?
PPPoE协议讲解以及拨号过程Wireshark抓包解析
软件质量体系之思
虚拟机网络连接方式
简单了解首个 EVM 等效的 zkEVM Polygon 为何全力押注
First deep search and first wide search of graph (realized by three methods)