当前位置:网站首页>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
边栏推荐
- Web3为互联网带来了哪些改变?
- How to view Apache log4j 2 remote code execution vulnerability?
- PIP install beatifulsoup4 installation failed
- MATLAB | App Designer·我用MATLAB制作了一款LATEX公式实时编辑器
- Performance monitoring of database tuning solutions
- Leetcode simple question: the minimum cost of buying candy at a discount
- How to use tensorflow2 for cat and dog classification and recognition
- poj 3237 Tree(樹鏈拆分)
- MySQL actual combat 45 lecture learning (I)
- Oracle checkpoint queue - Analysis of the principle of instance crash recovery
猜你喜欢
A number of ventilator giants' products have been recalled recently, and the ventilator market is still in incremental competition
Database recovery strategy
The simple problem of leetcode is to split a string into several groups of length K
U盘的文件无法删除文件怎么办?Win11无法删除U盘文件解决教程
Server optimization of performance tuning methodology
[Yugong series] go teaching course 003-ide installation and basic use in July 2022
Installation of VMware Workstation
华为游戏多媒体调用切换房间方法出现异常Internal system error. Reason:90000017
【愚公系列】2022年7月 Go教学课程 003-IDE的安装和基本使用
Getting started with microservices (resttemplate, Eureka, Nacos, feign, gateway)
随机推荐
Talking about MySQL index
Type of fault
K210 learning notes (IV) k210 runs multiple models at the same time
CA certificate trampled pit
Performance monitoring of database tuning solutions
HYSBZ 2243 染色 (树链拆分)
Countdown to 92 days, the strategy for the provincial preparation of the Blue Bridge Cup is coming~
MySQL disconnection reports an error MySQL ldb_ exceptions. OperationalError 4031, The client was disconnected by the server
Meituan dynamic thread pool practice ideas, open source
Leetcode simple question check whether all characters appear the same number of times
Interview questions for famous enterprises: Coins represent a given value
如何开发引入小程序插件
How to organize an actual attack and defense drill
1.3 years of work experience, double non naked resignation agency face-to-face experience [already employed]
华为快游戏调用登录接口失败,返回错误码 -1
笔记本电脑蓝牙怎么用来连接耳机
Blocking of concurrency control
微服务链路风险分析
Bitbucket installation configuration
Defect detection - Halcon surface scratch detection