我想为具有两个约束的非二进制用例实现扩展操作:1)结果应该是Opencv的cv2.dilate方法的副本,2)计算时间应该比cv2快10倍。扩张法。下面是我的相同代码段:
import cv2
from numba import jit
from scipy.ndimage.morphology import (generate_binary_structure,
iterate_structure)
import numpy as np
import time
struct = generate_binary_structure(2, 1) #default structuring element
neighborhood = iterate_structure(struct, 20) #create a structuring element of shape(41,41)
neighborhood = neighborhood.astype(np.uint8) #convert the dtype from bool to uint8
n_h,n_w = neighborhood.shape #get the shape of the structuring kernel
test_arr = np.random.random((320,240)) #create a test data to perform dilation
app_len = (n_h - 3)//2 + 1 #compute the requirements for zero padding
@jit(nopython=True)
def compute_dilation(test_arr,app_len,neighborhood,n_h):
#create an array to hold the values of the dilation operation
dil_arr = np.zeros((test_arr.shape))
#get the height and width of the test_arr
test_arr_h,test_arr_w = test_arr.shape
#create an array for zero padding the test array
#zero padding will help to avoid multiple if else checks while performing the dilation operation on the test_arr
zero_padded_array = np.zeros((test_arr_h+2*app_len,test_arr_w+2*app_len))
#append the test array to the zero padded array
zero_padded_array[app_len:app_len+test_arr_h,app_len:app_len+test_arr_w] = test_arr
#iterate over each of the pixel values and populate them with the result of dilation operation
for i in range(test_arr_h):
for j in range(test_arr_w):
#perform an element wise multiplication followed by extracting the maximum value
dil_arr[i,j] = np.max(np.multiply(zero_padded_array[i:i+n_h,j:j+n_h],neighborhood))
#return the output
return dil_arr
st_numba = time.time() #log the start time
numba_out = compute_dilation(test_arr,app_len,neighborhood,n_h)
print(time.time()-st_numba) #compute the elapsed difference
st_cv2 = time.time() #log the start time
cv2_out = cv2.dilate(test_arr,neighborhood)
print(time.time()-st_cv2) #compute the elapsed time
#validate the results of the dilation outputs from opencv vs the custom implementation
print(np.array_equal(numba_out,cv2_out))
尽管两种方法的输出都匹配,但是cv2.dilate方法所花费的时间(40毫秒)比我的自定义实现(900毫秒)好得多。我应该做些什么改变才能达到预期的效果?