简体   繁体   English

python opencv cv2中的面具不起作用?

[英]Masks in python opencv cv2 not working?

While in general the new python bindings for opencv (cv2) are a beauty, "masks" don't seem to be working properly - unless I really get something wrong: 通常,新的opencv(cv2)的python绑定很漂亮,但“掩码”似乎无法正常工作-除非我真的弄错了:

For example "cv2.add" still works properly without a mask: 例如,“ cv2.add”在没有掩码的情况下仍然可以正常工作:

import cv2
a = ones((2,2,3), dtype=uint8)
cv2.add(a,a)

correctly gives 正确给出

array([[[2, 2, 2],
        [2, 2, 2]],

       [[2, 2, 2],
        [2, 2, 2]]], dtype=uint8)

But when you add a mask (and an out array "b" - which is required by for some reason is not assigned either) you get a RANDOM result, ie the result changes when you run the command multiple times 但是,当您添加一个掩码(并且出于某种原因而未分配出数组“ b”)时,也会得到一个RANDOM结果,即,当您多次运行命令时,结果会发生变化

myMask = zeros(a.shape[0:2], dtype = uint8)
mask[1,1] = 255
b = zeros(a.shape)
cv2.add(a,a,b,myMask)
cv2.add(a,a,b,myMask)

gives on my machine (Win7, 32bit,Python 2.7, opencv 2.3.1) 在我的机器上给出(Win7,32bit,Python 2.7,opencv 2.3.1)

In [34]: cv2.add(a,a,b,myMask)
Out[34]: 
array([[[ 26,   0, 143],
        [  5, 216, 245]],

       [[156,   5, 104],
        [  2,   2,   2]]], dtype=uint8)

In [35]: cv2.add(a,a,b,myMask)
Out[35]: 
array([[[35,  0,  0],
        [ 0,  3,  0]],

       [[ 0,  0,  3],
        [ 2,  2,  2]]], dtype=uint8)

... and something new on the next trial. ...以及下次试用的新内容。 Now either I get something seriously wrong, or there is a serious problem with the cv2 bindings. 现在,要么我遇到严重问题,要么cv2绑定出现严重问题。

Any suggestions? 有什么建议么?

Its an interesting question. 这是一个有趣的问题。 I am seeing the same problem. 我看到了同样的问题。 I posted a bug and got a reply. 我发布了一个错误并得到了回复。 http://code.opencv.org/issues/1748 http://code.opencv.org/issues/1748

The solution is simple. 解决方案很简单。 The dst array is undefined on creation and the operation changes only those destination array pixels p, for which mask(p)!=0. dst数组在创建时未定义,并且该操作仅更改了mask(p)!= 0的那些目标数组像素p。

So the only mechanism that works is to premake dst before addition. 因此,唯一有效的机制是在添加之前预先制作dst。 Ie

dst = np.zeros(...)
dst = cv2.add(a, a, dst=dst, mask=mask)

The next release will clear newly created images in operations such as cv2.add, cv2.subtract, cv2.bitwise_and/or/xor - so it will work without problem. 下一版本将通过cv2.add,cv2.subtract,cv2.bitwise_and和/或/ xor等操作清除新创建的映像-因此它将正常工作。

my code looks like: 我的代码如下:

import cv2
import numpy as np
import time

a = np.ones((2,2,3), dtype=np.uint8)

print "simple add"
t = time.time()
for i in range(10000):
    b = cv2.add(a,a)
print "%5.4f seconds" % (time.time()-t)
print b

print "\nnumpy add"
t = time.time()
for i in range(10000):
    b = a+a
print "%5.4f seconds" % (time.time()-t)
print b

# make mask same dimensions but 1 byte deep(not three)
mask = np.zeros(a.shape[:-1], dtype=np.uint8)
mask[1,1] = 255

print "\nmask", mask.shape
print mask

print "\nmasked add - uninitialised"
t = time.time()
for i in range(10000):
    b = cv2.add(a,a,mask=mask)
print "%5.4f seconds" % (time.time()-t)
print b
print "uninitialised entries are unmodified - so random.\n Inconsistent when run more than once."
print "same calc a second time..."
b = cv2.add(a,a,mask=mask)
print b

print "\nmasked add - using preinitialised dst"
t = time.time()
b = a.copy()
for i in range(10000):
    b = cv2.add(a,a,b,mask=mask)
print "%5.4f seconds" % (time.time()-t)
print b
print "Consistent when run more than once."
print "same calc a second time..."
b = a.copy()
b = cv2.add(a,a,b,mask=mask)
print b

FYI: timings (10k repeats): 仅供参考:计时(重复1万次):

cv2.add - no mask            0.0120 seconds
cv2.add - with mask          0.0160 seconds
np.add                       0.0190 seconds
cv2.add - uninitialised mask 0.0220 seconds

FYI: Submit bugs following instructions here: http://code.opencv.org/projects/OpenCV/wiki/WikiStart 仅供参考:在此处按照以下说明提交错误: http//code.opencv.org/projects/OpenCV/wiki/WikiStart

why not use numpy (inplace) masked expression? 为什么不使用numpy(inplace)掩码表达式?

b = array(a, copy=True)
b[mask] += a

mask is a boolean array, analog in opencv (cv2.add) where a value is changed when mask != 0 mask是一个布尔数组,在opencv(cv2.add)中为模拟数组,其中mask = 0时会更改值

PS: however, your code works fine in my machine (Win7 64, Python 2.6(32bit), OpenCV 2.3.0) though PS:但是,虽然您的代码在我的机器上运行正常(Win7 64,Python 2.6(32位),OpenCV 2.3.0)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM