如何在Python中使用VIPS执行逻辑操作和逻辑索引?

时间:2015-10-18 06:36:03

标签: python opencv numpy image-processing vips

我有以下使用Python和OpenCV的代码。简而言之,我有一堆不同焦深的图像。代码在所有焦深(z)中的每个(x,y)位置处挑选出具有最大拉普拉斯响应拉普拉斯响应的像素,从而创建聚焦堆叠图像。函数get_fmap创建一个二维数组,其中每个像素将包含具有最大日志响应的焦平面的编号。在以下代码中,注释掉的行是我当前的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

2 个答案:

答案 0 :(得分:1)

fmapfmap在问题代码中初始化为3D数组,而问题文本则指出输出log_response是2D数组。所以,我假设将fmaplog_response = np.zeros_like(img[:,:,0], dtype='single') fmap = np.zeros_like(img[:,:,0], dtype='uint8') 初始化为2D数组,其形状与每个图像相同。因此,编辑将是 -

.argmax(2)

现在,回到问题的主题,您将逐个对每个图像执行2D过滤,并获得所有堆叠图像的过滤输出的最大索引。如果你不知道cv2.filter2D的文档,它也可以在多维数组上使用,给我们一个多维数组作为输出。然后,在所有图像中获取最大索引就像fmap = cv2.filter2D(img,-1,log_kernel).argmax(2) 一样简单。因此,实施必须非常有效,而且很简单 -

    select
    sum(a.dubizzle) as dubizzle,
    sum(b.JustRentals) as JustRentals,
    sum(c.JustProperty) as JustProperty,
    sum(d.propertyfinder) as propertyfinder 
from
(
    (
    select count(id) as dubizzle
    from crm_rentals
    where portals_name like '%dubizzle%'
    UNION
    select count(id) as dubizzle
    from crm_sales
    where portals_name like '%dubizzle%'
    ) as a ,

    (
    select count(id) as JustRentals
    from crm_rentals
    where portals_name like '%JustRentals%'
    UNION
    select count(id) as JustRentals
    from crm_sales
    where portals_name like  '%JustRentals%'
    ) as b,

    (
    select count(id)  as JustProperty
    from crm_rentals
    where portals_name like '%JustProperty%'
    UNION
    select count(id)  as JustProperty
    from crm_sales
    where portals_name like '%JustProperty%'
    ) as c ,

    (
    select count(id) as propertyfinder
    from crm_rentals
    where portals_name like '%propertyfinder%'
    UNION
    select count(id) as propertyfinder
    from crm_rentals
    where portals_name like '%propertyfinder%'
    ) as d
)

答案 1 :(得分:1)

在咨询Python VIPS manual和一些反复试验之后,我已经提出了自己的答案。我的numpy和OpenCV实现可以转换成这样的VIPS:

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方法实现逻辑索引:

result_img = (test_condition).ifthenelse(value_if_true, value_if_false)

语法相当灵活。测试条件可以是相同大小的两个图像之间或图像与值之间的比较,例如, img1 > img2img > 5。同样,value_if_true可以是单个值或Vips图像。