当前位置:网站首页>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
边栏推荐
猜你喜欢

MySQL:不是MySQL问题的MySQL问题

IR drop, EM, noise and antenna

keras——accuracy_score公式

线程池,我是谁?我在哪儿?

When does MySQL use table locks and row locks?

Scene notes

小程序商城如何精细化运营?

Pinduoduo app product details interface to obtain activity_ ID value (pinduoduo activity_id interface)

项目中遇到的问题及解决

Function secondary development / plug-in development of JMeter (detailed version)
随机推荐
七月集训(第23天) —— 字典树
PIP reports an error could not find a version that satisfies the... No matching distribution
Récursion des bosses 1: formule récursive
软件质量体系之思
Agile testing practice in large-scale teams
Nodejs implements token login registration (koa2)
Could not load dynamic library ‘cudnn64_ 8.dll‘; dlerror: cudnn64_ 8.dll not found
Failure analysis and solution of vscode PIO creation project
Sprintf and cv:: puttext
排序-介绍,代码思路,使用建议,代码实现-1
keil错误和解决办法(1):FCARM - Output Name not specified, please check ‘Options for Target - Utilities‘
使用 Preparedstatement 选择和显示记录的 JDBC 程序
Detailed explanation of SQL bool blind note and time blind note
Shell | ssh失败原因及解决方法不完全总结
串的初步认识
考过PMP对实际工作帮助大吗?
二十四节气之大暑
Wechat applet class binding, how to bind two variables
[web vulnerability exploration] SQL injection vulnerability
How to set up the router correctly