OpenCV求两个区域的交集

一、文章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

猜你喜欢

转载自blog.csdn.net/libaineu2004/article/details/125921783