How to perform boolean operation and boolean indexing using VIPS in Python?

I had the following codes that use Python and OpenCV. In short, I have a stack of images made with different focal depths. Codes select pixels at each position (x, y), which has the largest Laplacian of the Guass reaction among all focal depths (z), thus creating an image with focal glass. The function get_fmapcreates a 2d array in which each pixel will contain the number of the focal plane having the largest log response. In the following codes, the lines that are commented out are my current implementation of VIPS. They do not look compatible in defining a function, since this is only a 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 ,

, , 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
+1
2

Python VIPS . 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 > img2 img > 5. , value_if_true Vips.

+1

log_response fmap 3D- , , fmap . , , log_response fmap 2D- , . , -

log_response = np.zeros_like(img[:,:,0], dtype='single')
fmap = np.zeros_like(img[:,:,0], dtype='uint8')

, , 2D- . , cv2.filter2D, , . , .argmax(2). , -

fmap = cv2.filter2D(img,-1,log_kernel).argmax(2)
+1

Source: https://habr.com/ru/post/1608944/


All Articles