问题描述
我有以下使用Python和OpenCV的代码。简而言之,我有一堆不同焦深的图像。代码在所有焦深(z)中的每个(x,y)位置处挑选出具有最大拉普拉斯响应拉普拉斯响应的像素,从而创建聚焦堆叠图像。函数 get_fmap
创建一个二维数组,其中每个像素将包含具有最大日志响应的焦平面的编号。在以下代码中,注释掉的行是我当前的VIPS实现。它们在函数定义中看起来不兼容,因为它只是部分解决方案。
I've had following codes that use Python and OpenCV. Briefly, I have a stack of image taken at different focal depth. The codes pick out pixels at every (x,y) position that has the largest Laplacian of Guassian response among all focal depth(z), thus creating a focus-stacked image. Function get_fmap
creates a 2d array where each pixel will contains the number of the focal plane having the largest log response. In the following codes, lines that are commented out are my current VIPS implementation. They don't look compatible within the function definition because it's only partial solution.
# 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
将用于从不同焦平面中挑选像素以创建焦点堆叠图像
and then fmap
will be used to pick out pixels from different focal planes to create a focus-stacked image
这是在非常大的图像上完成的,我觉得VIPS可能比OpenCV做得更好。但是,官方文档提供了有关其Python绑定的相当少的信息。根据我在互联网上可以找到的信息,我只能进行图像卷积工作(在我的情况下,它比OpenCV快一个数量级)。我想知道如何在VIPS中实现这一点,尤其是这些行?
This is done on an extremely large image, and I feel VIPS might do a better job than OpenCV on this. However, the official documentation provides rather scant information on its Python binding. From the information I can find on the internet, I'm only able to make image convolution work ( which, in my case, is an order of magnitude faster than OpenCV.). I'm wondering how to implement this in VIPS, especially these lines?
log_response = np.zeros_like(img[:, :, 0], dtype = 'single')
index = img_filtered > log_response
log_response[index] = im_filtered[index]
fmap[index] = ii
推荐答案
咨询和一些反复试验,我想出了自己的答案。我的numpy和OpenCV实现可以被翻译成这样的VIPS:
After consulting the Python VIPS manual and some trial-and-error, I've come up with my own answer. My numpy and OpenCV implementation in question can be translated into VIPS like this:
import pyvips
img = []
for ii in range(num_z_levels):
img.append(pyvips.Image.new_from_file("testimg_z" + str(ii) + ".tif")
def get_fmap(img)
log_kernel = get_log_kernel(11,2) # get_log_kernel is my own function, which generates a 2-d numpy array.
log_kernel = [list(row) for row in log_kernel] # pyvips.Image.new_from_array takes 1-d list array.
log_kernel = pyvips.Image.new_from_array(log_kernel) # Turn the kernel into Vips array so it can be used by Vips.
log_response = img[0].conv(log_kernel)
for ii in range(len(img)):
img_filtered = img[ii+1].conv(log_kernel)
log_response = (img_filtered > log_response).ifthenelse(img_filtered, log_response)
fmap = (img_filtered > log_response).ifthenelse(ii+1, 0)
逻辑索引是通过 ifthenelse
方法实现的:
Logical indexing is achieved through ifthenelse
method :
result_img = (test_condition).ifthenelse(value_if_true, value_if_false)
语法相当灵活。测试条件可以是相同大小的两个图像之间或图像与值之间的比较,例如, img1> img2
或 img> 5
。同样明智的,value_if_true可以是单个值或Vips图像。
The syntax is rather flexible. The test condition can be a comparison between two images of the same size or between an image and a value, e.g. img1 > img2
or img > 5
. Like wise, value_if_true can be a single value or a Vips image.
这篇关于如何在Python中使用VIPS执行逻辑操作和逻辑索引?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!