Skip to main content
 首页 » 编程设计

python之从图像和标签中高效地提取补丁

2025年01月19日10mengfanrong

我有一个分割项目。我有图像和标签,它们包含分割的基本事实。图像很大,并且包含很多“空白”区域。 我想从图像和标签中剪切补丁,以便补丁中​​包含非零标签。

我需要它尽可能高效。

我写了下面的代码,但是很慢。任何改进将不胜感激。

import numpy as np 
import matplotlib.pyplot as plt 
让我们创建虚拟数据
img = np.random.rand(300,200,3) 
img[240:250,120:200]=0 
 
mask = np.zeros((300,200)) 
mask[220:260,120:300]=0.7 
mask[250:270,140:170]=0.3 
 
f, axarr = plt.subplots(1,2, figsize = (10, 5)) 
axarr[0].imshow(img) 
axarr[1].imshow(mask)[![enter image description here][1]][1] 
plt.show() 

我低效的代码:

IM_SIZE = 60     # Patch size 
 
x_min, y_min = 0,0 
x_max = img.shape[0] - IM_SIZE 
y_max = img.shape[1] - IM_SIZE 
xd, yd, x, y = 0,0,0,0 
 
if (mask.max() > 0): 
    xd, yd = np.where(mask>0) 
 
    x_min = xd.min() 
    y_min = yd.min() 
    x_max = min(xd.max()- IM_SIZE-1, img.shape[0] - IM_SIZE-1) 
    y_max = min(yd.max()- IM_SIZE-1, img.shape[1] - IM_SIZE-1) 
 
    if (y_min >= y_max): 
 
        y = y_max 
        if (y + IM_SIZE >= img.shape[1] ):  
            print('Error') 
 
    else: 
        y = np.random.randint(y_min,y_max) 
 
    if (x_min>=x_max): 
 
        x = x_max 
        if (x+IM_SIZE >= img.shape[0] ): 
            print('Error') 
 
    else: 
        x = np.random.randint(x_min,x_max ) 
print(x,y)     
img = img[x:x+IM_SIZE, y:y+IM_SIZE,:] 
mask = mask[x:x+IM_SIZE, y:y+IM_SIZE] 
 
f, axarr = plt.subplots(1,2, figsize = (10, 5)) 
axarr[0].imshow(img) 
axarr[1].imshow(mask) 
plt.show() 

请您参考如下方法:

line profiler给出的结果截图如下:

大部分时间由 mask.max()(可以更改为 np.max(mask) 以获得一些加速)和 np.where(mask>0) 使用。

如果您每次都需要在不同的掩码上使用 where 函数,请查看 numexpr .或者你可以使用 joblib通过并行运行许多此类情况来存储给定掩码的 x/y_min/max 结果。

使用 numba.jit 重新排列函数给我更好的结果:

@jit 
def temp(mask): 
    xd, yd = np.where(mask>0) 
 
    x_min = np.min(xd) 
    y_min = np.min(yd) 
    x_max = min(np.max(xd)- IM_SIZE-1, img.shape[0] - IM_SIZE-1) 
    y_max = min(np.max(yd)- IM_SIZE-1, img.shape[1] - IM_SIZE-1) 
    return x_min,x_max,y_min,y_max 
 
def solver_new(img): 
    IM_SIZE = 60     # Patch size 
 
    x_min, y_min = 0,0 
    x_max = img.shape[0] - IM_SIZE 
    y_max = img.shape[1] - IM_SIZE 
    xd, yd, x, y = 0,0,0,0 
 
    if (np.max(mask) > 0): 
        x_min,x_max,y_min,y_max = temp(mask) 
        if (y_min >= y_max): 
 
            y = y_max 
            if (y + IM_SIZE >= img.shape[1] ):  
                print('Error') 
 
        else: 
            y = np.random.randint(y_min,y_max) 
 
        if (x_min>=x_max): 
 
            x = x_max 
            if (x+IM_SIZE >= img.shape[0] ): 
                print('Error') 
 
        else: 
            x = np.random.randint(x_min,x_max ) 
    return x,y 

由于图像和补丁的大小很小,结果的意义不大,因为缓存对时间有很大影响。问题中发布的实现大约需要 200us,此处发布的实现大约需要 90us。