我有以下使用Python和OpenCV的代碼。簡而言之,我有一堆在不同震源深度拍攝的圖像。這些代碼在所有焦深(z)中具有最大高斯拉普拉斯響應拉普拉斯位置的每個(x,y)位置挑選出像素,從而創建焦點疊加圖像。函數get_fmap
創建一個2d陣列,其中每個像素將包含具有最大日誌響應的焦平面的編號。在以下代碼中,註釋掉的行是我當前的VIPS實現。它們在函數定義中看起來不兼容,因爲它只是部分解決方案。如何在Python中使用VIPS執行邏輯操作和邏輯索引?
# from gi.repository import Vips
def get_log_kernel(siz, std):
x = y = np.linspace(-siz, siz, 2*siz+1)
x, y = np.meshgrid(x, y)
arg = -(x**2 + y**2)/(2*std**2)
h = np.exp(arg)
h[h < sys.float_info.epsilon * h.max()] = 0
h = h/h.sum() if h.sum() != 0 else h
h1 = h*(x**2 + y**2 - 2*std**2)/(std**4)
return h1 - h1.mean()
def get_fmap(img): # img is a 3-d numpy array.
log_response = np.zeros_like(img[:, :, 0], dtype='single')
fmap = np.zeros_like(img[:, :, 0], dtype='uint8')
log_kernel = get_log_kernel(11, 2)
# kernel = get_log_kernel(11, 2)
# kernel = [list(row) for row in kernel]
# kernel = Vips.Image.new_from_array(kernel)
# img = Vips.new_from_file("testimg.tif")
for ii in range(img.shape[2]):
# img_filtered = img.conv(kernel)
img_filtered = cv2.filter2D(img[:, :, ii].astype('single'), -1, log_kernel)
index = img_filtered > log_response
log_response[index] = img_filtered[index]
fmap[index] = ii
return fmap
然後fmap
將被用來挑選出來自不同焦平面的像素來創建一個焦點堆棧圖像
這是一個非常大的圖像上完成的,而且我覺得VIPS可能做得更好比OpenCV就此。但是,官方文檔僅提供有關Python綁定的信息。根據我在互聯網上可以找到的信息,我只能夠進行圖像卷積工作(在我的情況下,它比OpenCV快一個數量級)。我想知道如何在VIPS中實現這一點,尤其是這些行?
log_response = np.zeros_like(img[:, :, 0], dtype = 'single')
index = img_filtered > log_response
log_response[index] = im_filtered[index]
fmap[index] = ii
我是vips維護者。你的代碼看起來不錯。你看到vips'logmat'函數了嗎?它可能適合你。它創建一個日誌掩碼http://www.vips.ecs.soton.ac.uk/supported/current/doc/html/libvips/libvips-create.html#vips-logmat從Python使用(例如)'Vips .Image.logmat(2,0.1)'。它會生成近似整數掩碼和可分離掩碼,這可以提供有用的加速。如果你願意,你可以使用字符串'「float」'而不是'Vips.Precision.FLOAT'。 – user894763
(第2部分)你需要'索引'圖像嗎?我認爲你可以將其重寫爲'fmap =(img_filtered> log_response).ifthenelse(ii + 1,0)',我可能錯過了一些東西。如果你有任何可以共享的基準,我會很好奇速度和內存使用與opencv相比。 – user894763
嘗試:'x = Vips.Image.logmat(2,0.1); x.matrixprint()'查看vips製作的日誌文件。添加'precision =「float」'獲得一個浮點版本。 – user894763