当前位置:网站首页>Calculation method of boundary IOU
Calculation method of boundary IOU
2022-07-05 22:05:00 【Master Fuwen】
Boundary IoU
To put it bluntly, it's calculation Predict the boundaries of the picture and GT The boundaries of the picture Of IoU
It is the calculation of the two boundaries of maozi IoU Just fine
The problem is how to calculate the above figure The border Well ? , use Canny Edge detection and so on ? Calculate a gradient ? But to The border ah , There may also be edge ah , This is not The border Well
We can use Boundary IoU The calculation method of the original
This is the original :
We can shrink the original picture by one circle , Just like this.
then Original picture subtract A reduced version , Got it. The border
How can we get the original image of the reduced version ? This is the protagonist : corrosion
You can refer to here :
OpenCV Expansion and corrosion of image processing
Corrosion operation is opposite to expansion operation , That is to eliminate burrs , The judgment method is : Convolute the picture in the convolution kernel size .
Take the image (3 * 3) The minimum value in the area . Because we are binary images , That is to take 0( black ). summary : As long as the original picture 3 * 3 There are black ones in the range , The pixel is black .
Now let's look at the code directly , The code is shown Reference:
# GitHub repo: https://github.com/bowenc0221/boundary-iou-api
# Reference: https://gist.github.com/bowenc0221/71f7a02afee92646ca05efeeb14d687d
import cv2
import numpy as np
import matplotlib.pyplot as plt
# General util function to get the boundary of a binary mask.
# This function is used to get binary mask The boundary of the
def mask_to_boundary(mask, dilation_ratio=0.02):
""" Convert binary mask to boundary mask. :param mask (numpy array, uint8): binary mask :param dilation_ratio (float): ratio to calculate dilation = dilation_ratio * image_diagonal :return: boundary mask (numpy array) """
h, w = mask.shape
img_diag = np.sqrt(h ** 2 + w ** 2) # Calculate the diagonal length of the image
dilation = int(round(dilation_ratio * img_diag))
if dilation < 1:
dilation = 1
# Pad image so mask truncated by the image border is also considered as boundary.
new_mask = cv2.copyMakeBorder(mask, 1, 1, 1, 1, cv2.BORDER_CONSTANT, value=0)
kernel = np.ones((3, 3), dtype=np.uint8)
new_mask_erode = cv2.erode(new_mask, kernel, iterations=dilation)
# Because it was filled around 0, Therefore, it no longer needs four weeks here
mask_erode = new_mask_erode[1 : h + 1, 1 : w + 1]
# G_d intersects G in the paper.
return mask - mask_erode
def boundary_iou(gt, dt, dilation_ratio=0.02):
""" Compute boundary iou between two binary masks. :param gt (numpy array, uint8): binary mask :param dt (numpy array, uint8): binary mask :param dilation_ratio (float): ratio to calculate dilation = dilation_ratio * image_diagonal :return: boundary iou (float) """
gt_boundary = mask_to_boundary(gt, dilation_ratio)
dt_boundary = mask_to_boundary(dt, dilation_ratio)
intersection = ((gt_boundary * dt_boundary) > 0).sum()
union = ((gt_boundary + dt_boundary) > 0).sum()
boundary_iou = intersection / union
return boundary_iou
mask_to_boundary
Function is used to calculate the boundary mask, and boundary_iou
Used to calculate boundary_iou
,boundary_iou
Will call mask_to_boundary
.
This line is used to add 0, In this way, even the target pixels in the boundary area will be corroded
# Pad image so mask truncated by the image border is also considered as boundary.
new_mask = cv2.copyMakeBorder(mask, 1, 1, 1, 1, cv2.BORDER_CONSTANT, value=0)
Here are cv2.copyMakeBorder
Schematic diagram of operation , Actually, it's called Padding Just fine
These two lines are used to etch the image ,kernel size yes (3, 3)
kernel = np.ones((3, 3), dtype=np.uint8)
new_mask_erode = cv2.erode(new_mask, kernel, iterations=dilation) # iterations It refers to the number of corrosion
And then look at dilation The calculation of :
h, w = mask.shape
img_diag = np.sqrt(h ** 2 + w ** 2) # Calculate the diagonal length of the image
dilation = int(round(dilation_ratio * img_diag))
if dilation < 1:
dilation = 1
The number of corrosion is proportional to the length of the diagonal , If it is less than 1 Give directly to 1,dilation_ratio
It's a function parameter
Look at the last step :
# Because it was filled around 0, Therefore, it no longer needs four weeks here
mask_erode = new_mask_erode[1 : h + 1, 1 : w + 1]
Put the surrounding padding Remove pixels , Then subtract the two :
return mask - mask_erode
Finally, we get this picture :
boundary_iou
And general IoU The calculation is the same , One problem is , If intersection onion==0
when , There may be division 0 Wrong question , There is no
So it should be :
def boundary_iou(gt, dt, dilation_ratio=0.02):
""" Compute boundary iou between two binary masks. :param gt (numpy array, uint8): binary mask :param dt (numpy array, uint8): binary mask :param dilation_ratio (float): ratio to calculate dilation = dilation_ratio * image_diagonal :return: boundary iou (float) """
gt_boundary = mask_to_boundary(gt, dilation_ratio)
dt_boundary = mask_to_boundary(dt, dilation_ratio)
intersection = ((gt_boundary * dt_boundary) > 0).sum()
union = ((gt_boundary + dt_boundary) > 0).sum()
if union < 1:
return 0
boundary_iou = intersection / union
return boundary_iou
边栏推荐
- crm创建基于fetch自己的自定义报告
- 数据泄露怎么办?'华生·K'7招消灭安全威胁
- Database tuning solution
- Countdown to 92 days, the strategy for the provincial preparation of the Blue Bridge Cup is coming~
- Installation of VMware Workstation
- SecureCRT使用提示
- Oracle checkpoint queue - Analysis of the principle of instance crash recovery
- The real situation of programmers
- Summary of concurrency control
- Leetcode simple question: find the nearest point with the same X or Y coordinate
猜你喜欢
Deeply convinced plan X - network protocol basic DNS
A substring with a length of three and different characters in the leetcode simple question
Create a virtual machine on VMware (system not installed)
数博会精彩回顾 | 彰显科研实力,中创算力荣获数字化影响力企业奖
【愚公系列】2022年7月 Go教学课程 003-IDE的安装和基本使用
How to use tensorflow2 for cat and dog classification and recognition
PyGame practical project: write Snake games with 300 lines of code
Index optimization of performance tuning methodology
Huawei fast game failed to call the login interface, and returned error code -1
华为快游戏调用登录接口失败,返回错误码 -1
随机推荐
阿龙的感悟
K210 learning notes (IV) k210 runs multiple models at the same time
MATLAB | App Designer·我用MATLAB制作了一款LATEX公式实时编辑器
等到产业互联网时代真正发展成熟,我们将会看待一系列的新产业巨头的出现
Server optimization of performance tuning methodology
MySQL actual combat 45 lecture learning (I)
Pl/sql basic case
Did you brush the real title of the blue bridge cup over the years? Come here and teach you to counter attack!
Oracle triggers
Overview of concurrency control
Shell script, awk uses if, for process control
如何开发引入小程序插件
Lightweight dynamic monitorable thread pool based on configuration center - dynamictp
他们主动布局(autolayout)环境的图像编辑器
How can Huawei online match improve the success rate of player matching
Getting started with microservices (resttemplate, Eureka, Nacos, feign, gateway)
K210学习笔记(四) K210同时运行多个模型
Implementing Lmax disruptor queue from scratch (IV) principle analysis of multithreaded producer multiproducersequencer
The simple problem of leetcode is to split a string into several groups of length K
Index optimization of performance tuning methodology