Note that there are some explanatory texts on larger screens.

plurals
  1. POPerformance comparison of OpenCV-Python interfaces, cv and cv2
    primarykey
    data
    text
    <p>A few days back, I started using new OpenCV-Python interface, <code>cv2</code>.</p> <p>My question is regarding the comparison of <code>cv</code> and <code>cv2</code> interface.</p> <p>Regarding the ease of use, new <code>cv2</code> interface has improved far greater, and it is really easy and fun to work with <code>cv2</code>.</p> <p>But what about speed?</p> <p>I made two small code snipplets, one in <code>cv</code> and another in <code>cv2</code>, to check the performances. Both does the same function, access pixels of an image, test it, make some modifications, etc.</p> <p>Below is the code:</p> <hr> <p><em><code>cv2 interface</code></em>:</p> <pre><code>import time import numpy as np import cv2 gray = cv2.imread('sir.jpg',0) width = gray.shape[0] height = gray.shape[1] h = np.empty([width,height,3]) t = time.time() for i in xrange(width): for j in xrange(height): if gray[i,j]==127: h[i,j]=[255,255,255] elif gray[i,j]&gt;127: h[i,j]=[0,0,255-gray[i,j]] else: h[i,j]=[gray[i,j],0,0] t2 = time.time()-t print "time taken = ",t2 </code></pre> <p>=====================================================</p> <p>And result is:</p> <p>time taken = 14.4029130936</p> <p>======================================================</p> <p><em><strong>cv interface:</em></strong></p> <pre><code>import cv,time gray = cv.LoadImage('sir.jpg',0) h = cv.CreateImage(cv.GetSize(gray),8,3) t=time.time() for i in xrange(gray.width): for j in xrange(gray.height): k = cv.Get2D(gray,j,i)[0] if k==127: cv.Set2D(h,j,i,(255,255,255)) elif k&gt;127: cv.Set2D(h,j,i,(0,0,255-k)) else: cv.Set2D(h,j,i,(k,0,0)) t2 = time.time()-t print "time taken = ",t2 cv.ShowImage('img',h) cv.WaitKey(0) </code></pre> <p>======================================================</p> <p>The result is:</p> <p>time taken = 1.16368889809</p> <p>=======================================================</p> <p>See, here old <code>cv</code> is about <code>12 times faster</code> than <code>cv2</code>. And resulting images are same. (input image is of size 720x540)</p> <p>Why does this happen?</p> <p>Is cv2 slower compared to cv?</p> <p>Or am I making any mistake here? Is there a faster method in cv2 for the above code?</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.
 

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