5

I checked the documentation but its incomplete: there is no mention of what rtype parameter actually is.

I think it's a reduce type but I can't find any of variables like cv2.CV_REDUCE_SUM etc... I found this problem with many function that use different variable names. What's the best way to find proper names in cv2 API?

mac
  • 42,153
  • 26
  • 121
  • 131
pzo
  • 2,087
  • 3
  • 24
  • 42

3 Answers3

7

I found out that the appropriate variable can be found in the following package

cv2.cv

If you use CV_REDUCE_SUM operator on uint8 image you have to explicitly provide dtype parameter of bigger range to avoid overflowing (e.g.

slice = cv2.reduce(image, 1, cv2.cv.CV_REDUCE_SUM, dtype=numpy.int32)

If you use CV_REDUCE_AVG operation, result can't overflow that's why setting dtype is optional.

pzo
  • 2,087
  • 3
  • 24
  • 42
  • 3
    The parameter of dtype will cause error: `TypeError: an integer is required` Use `cv2.CV_32S` instead. – Nianliang Dec 02 '14 at 09:44
4

There are some omisions in the current new cv2 lib. Typically these are constants that did not get migrated to cv2 yet and are still in cv only. Here is some code to help you find them:

import cv2
import cv2.cv as cv
nms  = [(n.lower(), n) for n in dir(cv)] # list of everything in the cv module
nms2 = [(n.lower(), n) for n in dir(cv2)] # list of everything in the cv2 module

search = 'window'

print "in cv2\n ",[m[1] for m in nms2 if m[0].find(search.lower())>-1]
print "in cv\n ",[m[1] for m in nms if m[0].find(search.lower())>-1]
Neon22
  • 590
  • 5
  • 17
  • And in OpenCV 3, cv2.cv doesn't exist anymore + some constant name have changed, see : https://stackoverflow.com/a/33199152/5075502 – snoob dogg Apr 30 '18 at 23:48
2

If you're finding this while using Open CV 3.x or later, these constants have been renamed to cv2.REDUCE_SUM, cv2.REDUCE_AVG, cv2.REDUCE_MAX, and cv2.REDUCE_MIN.

An example of the working reduce function:

reducedArray = cv2.reduce(im, 0, cv2.REDUCE_MAX)

GitHub issue for documentation

Jeru Luke
  • 20,118
  • 13
  • 80
  • 87
french13
  • 75
  • 1
  • 7