- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我已经通读了 documentation for copyTo()但我仍然对如何将此功能应用于以下代码感到困惑。 This anwer说明我们可以使用 copyTo 函数代替 255-x。在这种情况下将如何应用此功能?我将不胜感激代码片段。
# 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
最佳答案
对不起,我在那里误导了你。尽管它在 C++ 中运行良好,但我在 Python 中找不到绑定(bind)。但是,您可以使用 numpy.copyto功能。
这是一个小演示,显示两种方法(bitwise_not
和 copyto
)产生相同的结果。
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)
请注意,一个额外的维度被填充到掩码数组中,以便它可以被广播。
关于python - 如何使用 opencv copyTo() 函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51448049/
我正在尝试将一个范围从一张纸复制到另一张纸(同时保留公式)。我使用 copyTo 编写了一个简单的脚本: function copyRangeAcrossSheets(source_sheet,sou
我想将文件 c:\a1\b2\c3\foo.txt 复制到 d:\a1\b2\c3\foo.txt。 D 驱动器上不存在子目录,如果我尝试直接执行 CopyTo(),则会出现 IO 异常。我一直没能找
我有以下方法,但出于某种原因,第一次调用 Copy to 似乎什么也没做?有人知道为什么吗?该方法的输入被压缩,如果需要,base64 可以提供该方法。 private byte[] GetFileC
我想知道在 C# 或 VB.NET 中如何使用 ArrayList 的 CopyTo 方法> 对象将其内容复制到其他 ArrayList(不是简单的数组)。 注意:我不是在寻找像 ArrayList
谁能解释为什么这段代码的输出只是“hello”以及这段代码的含义? ( 0, characterArray, 0, characterArray.Length ); 输出显示: The charact
我有一个 WCF 服务,它允许我以 block 的形式上传文件。我想知道的是,这段代码是否会导致上传的流在任何情况下都只能部分附加到目标流? 我的日志显示所有发送的流都是 512000 字节(这是我在
我如何使用 Stream CopyTo不覆盖现有输出文件的方法? public void MergeAndDeleteFiles(string outputFile, IEnumerable inpu
考虑以下代码: var results = searcher.FindAll(); SearchResult[] srList = new SearchResult[results.Count]; r
我快要失去理智了。在过去的一个小时里,我一直试图让 GzipStream 压缩一个字符串,但出于某种原因,它拒绝将整个字节数组写入内存流。起初我认为它与 using 语句有关,但即使在删除它们之后似乎
我正在使用 Java CIFS Client Library但面临的问题是 copyTo 函数不起作用。我有一个包含文件的文件夹。我想将这些文件读取到其他网络路径。 String path1 = "
我有下表: writing_agent_number|agent_name |individual_sub_name -------------------- --------------
我正在尝试将较小的图像复制到较大图像的中心。这是代码: src.copyTo(dest(Rect(50,50,src.cols,src.rows))); src 和 dest 都是 Mats,dest
我尝试过在这个论坛上搜索、阅读文档和谷歌搜索。我找不到问题的答案。 下面简要介绍一下我正在尝试做的事情: 从网络摄像头读取Mat对象frame(在父类OpenCVVideoWorker中声明为私有(p
我正在尝试通过 C++ 在 OpenCV 2.4 中使用蒙版,将较小的图像粘贴到较大的图像中。 在没有 mask 的情况下,我使用以下代码将小图像复制到大图像: smallImage.copyTo(l
我想使用 openCv 的 copyTo() 函数对图像应用蒙版,但蒙版不起作用。我仍然获得相同的图像。 当我在网上搜索时,我看到了一些建议,例如: -源图像和目标图像必须不同, -源图像必须具有 C
TiffBitmapDecoder decoder = new TiffBitmapDecoder(imageStreamSource, BitmapCreateOptions.PreservePix
我正在比较 OpenCL UMat 和 OpenCL Mat,我运行了 cvtColor() 函数 500 次。 int OpenCL_UMat() { Mat img; UMat u
我想合并 2 个大文件但是 atm 我的代码只在复制 1 个文件后更新进度有没有更好的方法来报告进度这是我的复制代码 atm max = files.Count; MessageBox.Show(
我在将数据从 MemoryStream 复制到 ZipArchive 内的 Stream 时遇到问题。以下内容不起作用 - 它仅返回 114 个字节: GetDataAsByteArray(IData
我看到CopyToAsync有 CancellationToken论据。但我不需要CopyToAsync因为我的代码已经在后台线程中执行了。我只想拥有CopyTo重载需要 CancellationTo
我是一名优秀的程序员,十分优秀!