What Is Different Between All These Opencv Python Interfaces

What is different between all these OpenCV Python interfaces?

Officially, OpenCV releases two types of Python interfaces, cv and cv2.

cv:

I started working on cv. In this, all OpenCV data types are preserved as such. For example, when loaded, images are of format cvMat, same as in C++.

For array operations, there are several functions like cvSet2D, cvGet2D, etc. And some discussions say, they are slower.

For imageROI, you need special functions like cvSetImageROI.

If you find contours, cvSeq structures are returned which is not so good to work with compared to Python lists or NumPy arrays.

(And I think, soon its development will be stopped. Earlier, there was only cv. Later, OpenCV came with both cv and cv2. Now, there in the latest releases, there is only the cv2 module, and cv is a subclass inside cv2. You need to call import cv2.cv as cv to access it.)

cv2:

And the latest one is cv2. In this, everything is returned as NumPy objects like ndarray and native Python objects like lists,tuples,dictionary, etc. So due to this NumPy support, you can do any numpy operation here. NumPy is a highly stable and fast array processing library.

For example, if you load an image, an ndarray is returned.

array[i,j] gives you the pixel value at (i,j) position.

Also, for imageROI, array slicing can be used like ROI=array[c1:c2,r1:r2]. No need of separate functions.

To add two images, there isn't a need to call any function, just do res = img1+img2. (But NumPy addition is a modulo operation for uint8 arrays like images. See the article Difference between Matrix Arithmetic in OpenCV and Numpy to know more.

Contours returned are lists of Numpy arrays. You can find a detailed discussion about Contours in Contours - 1 : Getting Started.

In short, with cv2 everything is simplified and pretty fast.

A simple discussion on how NumPy speed up cv2 is in Stack Overflow question Performance comparison of OpenCV-Python interfaces, cv and cv2.

pyopencv:

I don't know much about this since I haven't used it. But it seems to have stopped further development.

I think it would be better to stick on to official libraries.

In short, I would recommend you to use cv2!

EDIT: You can see installation procedure for the cv2 module in Install OpenCV in Windows for Python.

Performance comparison of OpenCV-Python interfaces, cv and cv2

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

The difference between simpleCV and openCV

OpenCV is a library that can be used with tons of different languages (C, C++, Java, Python, etc.). It provides standard things such as image capture, image manipulation, etc.

SimpleCV on the other hand is a framework including several libraries (as far as I know not only OpenCV) and uses Python for scripting. Due to the nature of Python, you can either run scripts or use an interactive shell to do computer vision stuff and related tasks.

Which one to choose? This really depends on your usage scenario. For quick prototyping I'd guess SimpleCV is far superior, but for actual implementation/usage, OpenCV offers a lot more possibilities (although at a higher complexity; e.g. being able to be included in native applications as well as embedded systems).

Does performance differ between Python or C++ coding of OpenCV?

As mentioned in earlier answers, Python is slower compared to C++ or C. Python is built for its simplicity, portability and moreover, creativity where users need to worry only about their algorithm, not programming troubles.

But here in OpenCV, there is something different. Python-OpenCV is just a wrapper around the original C/C++ code. It is normally used for combining best features of both the languages, Performance of C/C++ & Simplicity of Python.

So when you call a function in OpenCV from Python, what actually run is underlying C/C++ source. So there won't be much difference in performance.( I remember I read somewhere that performance penalty is <1%, don't remember where. A rough estimate with some basic functions in OpenCV shows a worst-case penalty of <4%. ie penalty = [maximum time taken in Python - minimum time taken in C++]/minimum time taken in C++ ).

The problem arises when your code has a lot of native python codes.For eg, if you are making your own functions that are not available in OpenCV, things get worse. Such codes are ran natively in Python, which reduces the performance considerably.

But new OpenCV-Python interface has full support to Numpy. Numpy is a package for scientific computing in Python. It is also a wrapper around native C code. It is a highly optimized library which supports a wide variety of matrix operations, highly suitable for image processing. So if you can combine both OpenCV functions and Numpy functions correctly, you will get a very high speed code.

Thing to remember is, always try to avoid loops and iterations in Python. Instead, use array manipulation facilities available in Numpy (and OpenCV). Simply adding two numpy arrays using C = A+B is a lot times faster than using double loops.

For eg, you can check these articles :

  1. Fast Array Manipulation in Python
  2. Performance comparison of OpenCV-Python interfaces, cv and cv2

Is there no more module cv for opencv python?

In OpenCV 3.x cv was deprecated.
Some old cv modules can be found in cv2, as cv2.stereoRectify, others are not in opencv installation anymore due to legal concerns.

You may need to pip install opencv-contrib-python --user

What does the python interface to opencv2.fillPoly want as input?

The AssertionError is telling you that OpenCV wants a signed, 32-bit integer. The array of polygon points should have that particular data type (e.g. points = numpy.array(A,dtype='int32') ). You could also just cast it for the function call (i.e. my_array.astype('int32') ) or as a friend put it once...

"
Changing

   cv2.fillConvexPoly(binary_image, np.array(rect['boundary']), 255)
to

   cv2.fillConvexPoly(binary_image, np.array(rect['boundary'], 'int32'), 255)
"

OpenCV 2.4.3 and Python

I think you are taking it in the reverse path.

Actually, with the new cv2 module, OpenCV has become far more simple compared to old cv interface. Not just simple, but very fast and highly productive, due to the Numpy support. Only thing is that, we should know how to use it appropriately.

Here, you should use the function as follows :

img = cv2.imread('pic.jpg')
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)

I would like you to visit one SOF which shows some comparison between both the modules : What is different between all these OpenCV Python interfaces?

Another one SOF is here, which is a simple demonstration on how you can speed up the code with Numpy support : Performance comparison of OpenCV-Python interfaces, cv and cv2

You need not learn C++ or C to use OpenCV, although C++ is the official language. Still, Python-OpenCV has good support. Once you get a grip on how to use OpenCV, you will be able to convert C++ codes into Python yourself. Then you can learn OpenCV from C++ tutorials also. For example, I started learning OpenCV from "Learning OpenCV" by Gary Bradsky which is completely in C++. At that time, there was only cv interface.

As you mentioned in your comments, opencvpython.blogspot.com has some introductory tutorials. I started it focussing newbies in OpenCV.

Also, check this SOF for more tutorials : Books for OpenCV and Python?

OpenCV Python single (rather than multiple) blob tracking?

You need to do it like this :

1) Get the thresholded image using inRange function, and you can apply some erosion and dilation to remove small noisy particles. It will help to improve the processing speed.

2) find Contours using 'findContours' function

3) find areas of contours using 'contourArea' function and select one with maximum area.

4) Now find its center as you did, and track it.

Below is a sample code for this in new cv2 module :

import cv2
import numpy as np

# create video capture
cap = cv2.VideoCapture(0)

while(1):

# read the frames
_,frame = cap.read()

# smooth it
frame = cv2.blur(frame,(3,3))

# convert to hsv and find range of colors
hsv = cv2.cvtColor(frame,cv2.COLOR_BGR2HSV)
thresh = cv2.inRange(hsv,np.array((0, 80, 80)), np.array((20, 255, 255)))
thresh2 = thresh.copy()

# find contours in the threshold image
contours,hierarchy = cv2.findContours(thresh,cv2.RETR_LIST,cv2.CHAIN_APPROX_SIMPLE)

# finding contour with maximum area and store it as best_cnt
max_area = 0
for cnt in contours:
area = cv2.contourArea(cnt)
if area > max_area:
max_area = area
best_cnt = cnt

# finding centroids of best_cnt and draw a circle there
M = cv2.moments(best_cnt)
cx,cy = int(M['m10']/M['m00']), int(M['m01']/M['m00'])
cv2.circle(frame,(cx,cy),5,255,-1)

# Show it, if key pressed is 'Esc', exit the loop
cv2.imshow('frame',frame)
cv2.imshow('thresh',thresh2)
if cv2.waitKey(33)== 27:
break

# Clean up everything before leaving
cv2.destroyAllWindows()
cap.release()

You can find some samples on tracking colored objects here : https://github.com/abidrahmank/OpenCV-Python/tree/master/Other_Examples

Also, try to use new cv2 interface. It is a lot simpler and faster than old cv. For more details, checkout this : What is different between all these OpenCV Python interfaces?



Related Topics



Leave a reply



Submit