Ask Your Question

Updating of documentation (regarding reduce)

asked 2019-01-11 00:19:11 -0500

Hi, I have just started playing with opencv for Python, trying to do some simple calculations. I am using opencv-python v. and Python v. 3.6.6 I found the documentation for the reduce function to be a bit lacking, and it cost me some time to get it working. So my question is if the documentation could be updated. Specifically that the documentation could state that the only output types that are supported by the function are; CV_32S, CV_32F, and CV_64F. Furthermore the rtype is no longer called CV_REDUCE_* but is now just REDUCE_* I would just like other people to not have to go through the same hassle I had to, to get it working. Many thanks for the awesome library!

Kind regards, Jan Vinther Christensen

edit retag flag offensive close merge delete


you're probably looking at outdated documentation . (there is also a version selector at the top of the page)

berak gravatar imageberak ( 2019-01-11 00:28:17 -0500 )edit

nonetheless, nice that you care !

berak gravatar imageberak ( 2019-01-11 00:31:10 -0500 )edit

Thanks for the answer and the link :D I just did a search and the first thing I found was the outdated documentation, my bad.. Still it doesn't seem to say what the limits of the output types are in the newer documentation. Maybe it is just me who is to much of a rookie to fully understand the description of dtype. I have had inputs with uint8, but it surely wont return with that type.

pokesmurf gravatar imagepokesmurf ( 2019-01-11 06:02:31 -0500 )edit

1 answer

Sort by ยป oldest newest most voted

answered 2019-01-11 06:40:57 -0500

berak gravatar image

ook, let's look at it:

>>> help(cv2.reduce)
Help on built-in function reduce:

    reduce(src, dim, rtype[, dst[, dtype]]) -> dst
    .   @brief Reduces a matrix to a vector.
    .   The function #reduce reduces the matrix to a vector by treating the matrix rows/columns as a set of
    .   1D vectors and performing the specified operation on the vectors until a single row/column is
    .   obtained. For example, the function can be used to compute horizontal and vertical projections of a
    .   raster image. In case of #REDUCE_MAX and #REDUCE_MIN , the output image should have the same type as the source one.
    .   In case of #REDUCE_SUM and #REDUCE_AVG , the output may have a larger element bit-depth to preserve accuracy.
    .   And multi-channel arrays are also supported in these two reduction modes.
    .   The following code demonstrates its usage for a single channel matrix.
    .   @snippet snippets/core_reduce.cpp example
    .   And the following code demonstrates its usage for a two-channel matrix.
    .   @snippet snippets/core_reduce.cpp example2
    .   @Param src input 2D matrix.
    .   a single row. 1 means that the matrix is reduced to a single column.
    .   @Param rtype reduction operation that could be one of #ReduceTypes
    .   @sa repeat

why isn't there something about the dtype, above ?

but see, some REDUCE_XXX ops do support the same input and output formats:

>>> a = np.ones((10,10),np.uint8)
>>> z = cv2.reduce(a,0,cv2.REDUCE_MAX)
>>> z.dtype

but (that's probably, what you meant):

>>> z = cv2.reduce(a,0,cv2.REDUCE_SUM)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
cv2.error: OpenCV(3.4.3) C:\projects\opencv-python\opencv\modules\core\src\matrix_operations.cpp:1111: error: (-210:Unsupported format or combination of formats) Unsupported combination of input and output array formats in function 'cv::reduce'

ok, fair point, i see, what you mean ;) also, next problem:

>>> z = cv2.reduce(a,0,cv2.REDUCE_SUM, dtype=np.int32)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: an integer is required (got type type)

ohh, it wants a CV_ type, not a numpy one, where is that documented ?

>>> z = cv2.reduce(a,0,cv2.REDUCE_SUM, dtype=cv2.CV_32S)

so, yea, as always -- docs can only get better ;)

edit flag offensive delete link more
Login/Signup to Answer

Question Tools

1 follower


Asked: 2019-01-11 00:19:11 -0500

Seen: 218 times

Last updated: Jan 11 '19