当前位置:网站首页>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
- "Chris Richardson microservices series" uses API gateway to build microservices
- 初级软件测试必问面试题
- Image editor for their AutoLayout environment
- Storage optimization of performance tuning methodology
- EBS Oracle 11g cloning steps (single node)
- The solution to the problem that Oracle hugepages are not used, causing the server to be too laggy
- 阿龙的感悟
- 笔记本电脑蓝牙怎么用来连接耳机
- MySQL连接断开报错MySQLdb._exceptions.OperationalError 4031, The client was disconnected by the server
猜你喜欢

The statistics of leetcode simple question is the public string that has appeared once

Recovery technology with checkpoints

The American Championship is about to start. Are you ready?

EBS Oracle 11g cloning steps (single node)

Common interview questions of redis factory

Sentinel production environment practice (I)

华为游戏多媒体服务调用屏蔽指定玩家语音方法,返回错误码3010

How can Huawei online match improve the success rate of player matching

Shell script, awk uses if, for process control

Livelocks and deadlocks of concurrency control
随机推荐
Unique occurrence times of leetcode simple questions
Getting started with microservices (resttemplate, Eureka, Nacos, feign, gateway)
Defect detection - Halcon surface scratch detection
微服务链路风险分析
Blocking protocol for concurrency control
Index optimization of performance tuning methodology
AD637使用筆記
Implementation technology of recovery
科技云报道:算力网络,还需跨越几道坎?
Oracle advanced query
Summary of El and JSTL precautions
科技云报道荣膺全球云计算大会“云鼎奖”2013-2022十周年特别贡献奖
1.3 years of work experience, double non naked resignation agency face-to-face experience [already employed]
An exception occurred in Huawei game multimedia calling the room switching method internal system error Reason:90000017
The American Championship is about to start. Are you ready?
Net small and medium-sized enterprise project development framework series (one)
Huawei fast game failed to call the login interface, and returned error code -1
Code bug correction, char is converted to int high-order symbol extension, resulting in changes in positivity and negativity and values. Int num = (int) (unsigned int) a, which will occur in older com
boundary IoU 的计算方式
Blocking of concurrency control