如何使用 opencv copyTo() 函数? [英] How to use opencv copyTo() function?

查看:28
本文介绍了如何使用 opencv copyTo() 函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已阅读

请注意,在掩码数组中填充了一个额外的维度,以便可以广播.

I have read through the documentation for copyTo() but am still confused on how this function would be applied to the following code. This anwer states that we can use the copyTo function instead of 255-x. How would this function be applied in this case? I would appreciate a code snippet.

#   Compute the gradient map of the image
def doLap(image):

    # YOU SHOULD TUNE THESE VALUES TO SUIT YOUR NEEDS
    kernel_size = 5         # Size of the laplacian window
    blur_size = 5           # How big of a kernal to use for the gaussian blur
                            # Generally, keeping these two values the same or very close works well
                            # Also, odd numbers, please...

    blurred = cv2.GaussianBlur(image, (blur_size,blur_size), 0)
    return cv2.Laplacian(blurred, cv2.CV_64F, ksize=kernel_size)

#
#   This routine finds the points of best focus in all images and produces a merged result...
#
def focus_stack(unimages):
    images = align_images(unimages)

    print "Computing the laplacian of the blurred images"
    laps = []
    for i in range(len(images)):
        print "Lap {}".format(i)
        laps.append(doLap(cv2.cvtColor(images[i],cv2.COLOR_BGR2GRAY)))

    laps = np.asarray(laps)
    print "Shape of array of laplacians = {}".format(laps.shape)

    output = np.zeros(shape=images[0].shape, dtype=images[0].dtype)

    abs_laps = np.absolute(laps)
    maxima = abs_laps.max(axis=0)
    bool_mask = abs_laps == maxima
    mask = bool_mask.astype(np.uint8)
    for i in range(0,len(images)):
        output = cv2.bitwise_not(images[i],output, mask=mask[i])

    return 255-output

解决方案

Sorry that I kind of misled you there. Although it works nicely in C++, I cannot find the binding in Python. You can, however, use numpy.copyto function.

Here is a small demo that shows that both method (bitwise_not and copyto) produce identical result.

import cv2
import numpy as np

# Create two images
im1 = np.zeros((100, 100, 3), np.uint8)
im1[:] = (255, 0, 0)
im2 = np.zeros((100, 100, 3), np.uint8)
im2[:] = (0, 255, 0)

# Generate a random mask
ran = np.random.randint(0, 2, (100, 100), np.uint8)

# List of images and masks
images = [im1, im2]
mask = [ran, 1-ran]

not_output = np.zeros((100, 100, 3), np.uint8)
copy_output = np.zeros((100, 100, 3), np.uint8)

for i in range(0, len(images)):
    # Using the 'NOT' way
    not_output = cv2.bitwise_not(images[i], not_output, mask=mask[i])
    # Using the copyto way
    np.copyto(copy_output, images[i], where=mask[i][:, :, None].astype(bool))

cv2.imwrite('not.png', 255 - not_output)
cv2.imwrite('copy.png', copy_output)

Note that an extra dimension was padded to the mask array so that it can be broadcasted.

这篇关于如何使用 opencv copyTo() 函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆