当前位置:网站首页>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
边栏推荐
- Interprocess communication in the "Chris Richardson microservice series" microservice architecture
- Poj3414 extensive search
- Stored procedures and stored functions
- 华为游戏多媒体调用切换房间方法出现异常Internal system error. Reason:90000017
- 【愚公系列】2022年7月 Go教学课程 004-Go代码注释
- Database tuning solution
- Overview of database recovery
- Matlab | app designer · I used Matlab to make a real-time editor of latex formula
- Official clarification statement of Jihu company
- 如何组织一场实战攻防演练
猜你喜欢

Efficiency difference between row first and column first traversal of mat data types in opencv

A number of ventilator giants' products have been recalled recently, and the ventilator market is still in incremental competition

数博会精彩回顾 | 彰显科研实力,中创算力荣获数字化影响力企业奖

How to view Apache log4j 2 remote code execution vulnerability?

多家呼吸机巨头产品近期被一级召回 呼吸机市场仍在增量竞争

极狐公司官方澄清声明

Oracle checkpoint queue - Analysis of the principle of instance crash recovery

CA certificate trampled pit

Ad637 notes d'utilisation

Concurrency control of performance tuning methodology
随机推荐
如何开发引入小程序插件
Advantages and disadvantages of the "Chris Richardson microservice series" microservice architecture
Server optimization of performance tuning methodology
Interview questions for basic software testing
数据泄露怎么办?'华生·K'7招消灭安全威胁
EL与JSTL注意事项汇总
【愚公系列】2022年7月 Go教学课程 004-Go代码注释
如何组织一场实战攻防演练
HDU 4391 Paint The Wall 段树(水
Poj3414 extensive search
CA certificate trampled pit
How to add new fields to mongodb with code (all)
Summary of El and JSTL precautions
Experienced inductance manufacturers tell you what makes the inductance noisy. Inductance noise is a common inductance fault. If the used inductance makes noise, you don't have to worry. You just need
Poj3414广泛搜索
Pl/sql basic syntax
boundary IoU 的计算方式
AD637使用筆記
Form artifact
微服務鏈路風險分析