Ask Your Question

C++ to python, numpy-OpenCv

asked 2018-10-23 03:22:54 -0600

Caseguy gravatar image

How can i convert this line into python code

Mat out = Mat::ones(src.rows+maxYOffset, src.cols+maxXOffset, src.type());

i have tried using np.zeros() and np.ones.

edit retag flag offensive close merge delete


and the problem is ?

berak gravatar imageberak ( 2018-10-23 03:25:12 -0600 )edit

also, please do NOT delete your previous questions, and ask a new one.

it does not solve the "duplicate question" problem, as you're deleteing previous comments / answers, too.

berak gravatar imageberak ( 2018-10-23 03:26:45 -0600 )edit

I saw [Deleted], so i del it.

IndexError: only integers, slices (`:`), ellipsis (`...`), numpy.newaxis (`None`) and integer or boolean arrays are valid indices

The error i get

Caseguy gravatar imageCaseguy ( 2018-10-23 03:28:27 -0600 )edit

leave anything as is now, but take the advice for future posts, please.

berak gravatar imageberak ( 2018-10-23 03:33:35 -0600 )edit

Will do that.

Caseguy gravatar imageCaseguy ( 2018-10-23 03:35:44 -0600 )edit

and how do you get to that error ?

berak gravatar imageberak ( 2018-10-23 03:35:55 -0600 )edit
Caseguy gravatar imageCaseguy ( 2018-10-23 03:54:28 -0600 )edit

1 answer

Sort by ยป oldest newest most voted

answered 2018-10-23 03:42:20 -0600

berak gravatar image

step 1: use the builtin help:

>>> help(np.ones)

step 2: it needs a shape and a type here, so something like:

a = np.ones((h,w,c),np.uint8)

caveat: with multi-channel (c>1) Mat's opencv will only fill the 1st channel with 1, the others are left at 0 (to be compliant with complex data), while numpy will fill all of them with 1

edit flag offensive delete link more


Adding on to this, the type of a numpy array can be obtained with the ndarray.dtype attribute, i.e.:

out = np.ones((h,w,c), dtype=src.dtype)
nrsyed gravatar imagenrsyed ( 2018-10-29 20:02:17 -0600 )edit

Question Tools

1 follower


Asked: 2018-10-23 03:22:54 -0600

Seen: 880 times

Last updated: Oct 23 '18