Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7522409
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T02:30:50+00:00 2026-05-30T02:30:50+00:00

A few days back, I started using new OpenCV-Python interface, cv2 . My question

  • 0

A few days back, I started using new OpenCV-Python interface, cv2.

My question is regarding the comparison of cv and cv2 interface.

Regarding the ease of use, new cv2 interface has improved far greater, and it is really easy and fun to work with cv2.

But what about speed?

I made two small code snipplets, one in cv and another in cv2, to check the performances. Both does the same function, access pixels of an image, test it, make some modifications, etc.

Below is the code:


cv2 interface:

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]>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

=====================================================

And result is:

time taken = 14.4029130936

======================================================

cv interface:

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>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)

======================================================

The result is:

time taken = 1.16368889809

=======================================================

See, here old cv is about 12 times faster than cv2. And resulting images are same. (input image is of size 720×540)

Why does this happen?

Is cv2 slower compared to cv?

Or am I making any mistake here? Is there a faster method in cv2 for the above code?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T02:30:51+00:00Added an answer on May 30, 2026 at 2:30 am

    The image returned by cv2.imread() is an array object of NumPy. So you can use NumPy’s functions to speedup calculation.

    The following program shows how to speedup your origin for loop version by using item(), itemset() method of ndarray object.

    import time
    import numpy as np
    import cv2
    
    gray = cv2.imread('lena_full.jpg',0)
    height, width = gray.shape
    h = np.empty((height,width,3), np.uint8)
    
    t = time.time()
    for i in xrange(height):
        for j in xrange(width):
            k = gray.item(i, j)
            if k == 127:
                h.itemset(i, j, 0, 255)
                h.itemset(i, j, 1, 255)
                h.itemset(i, j, 2, 255)
            elif k > 127:
                h.itemset(i, j, 0, 0)
                h.itemset(i, j, 1, 0)
                h.itemset(i, j, 2, 255-k)
            else:
                h.itemset(i, j, 0, k)
                h.itemset(i, j, 1, 0)
                h.itemset(i, j, 2, 0)
    print time.time()-t
    

    And the following program show how to create the palette first, and use NumPy’s array index to get the result:

    t = time.time()
    palette = []
    for i in xrange(256):
        if i == 127:
            palette.append((255, 255, 255))
        elif i > 127:
            palette.append((0,0,255-i))
        else:
            palette.append((i, 0, 0))
    palette = np.array(palette, np.uint8)
    
    h2 = palette[gray]
    
    print time.time() - t
    
    print np.all(h==h2)
    

    The output is:

    0.453000068665
    0.0309998989105
    True
    

    The cv version output is :

    0.468999862671
    

    Note: the length of axis 0 is the height of the image, the length of axis 1 is the width of the image

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

A few days ago I posted this question and everyone suggested me to use
I started reading about implementing various data structures a few days back, and got
A few days back I felt this question to be dumb and dint post
So I started using Git a few days ago. ( Very late to the
I've started looking into Appcelerator 1.8.0 few days and back and i'm a little
Few days back, I posted a question about getting student marks results from database
A few days back, while writing an answer for this question here on overflow
We are using hudson version 2.0.0. Few days back after restarting hudson, i found
I am a newbie. I started php coding few days back. I want to
I started to learn OO a few days back, I'm quite OK with procedural

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.