Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Its an interesting question. I am seeing the same problem. I posted a bug and got a reply. <a href="http://code.opencv.org/issues/1748" rel="noreferrer">http://code.opencv.org/issues/1748</a></p> <p>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.</p> <p>So the only mechanism that works is to premake dst before addition. I.e.</p> <pre><code>dst = np.zeros(...) dst = cv2.add(a, a, dst=dst, mask=mask) </code></pre> <p>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.</p> <p>my code looks like:</p> <pre><code>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 </code></pre> <p>FYI: timings (10k repeats):</p> <pre><code>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 </code></pre> <p>FYI: Submit bugs following instructions here: <a href="http://code.opencv.org/projects/OpenCV/wiki/WikiStart" rel="noreferrer">http://code.opencv.org/projects/OpenCV/wiki/WikiStart</a></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload