5

I have this piece of code that works fine on Python. I want to do the same thing in C/C++ but i do not understand how works kernel in C++:

kernel = np.ones((3,3),np.uint8)
opening = cv2.morphologyEx(thresh,cv2.MORPH_OPEN,kernel, iterations = 2)

kernel and opening are Mat objects.

I have also

mat1=np.uint8(mat1)

I do not understand what is this np prefix.

1
  • The np prefix is the numpy library. OpenCV uses that library for all numeric operations, because Python arrays are very inefficient for numeric computations. Commented Dec 1, 2015 at 8:28

1 Answer 1

4

This line:

kernal = np.ones((3,3)), np.uint8)

is the same as doing this in C++:

Mat m = Mat(3, 3, CV_8UC1, cv::Scalar(1));

As MaruisSiuram said the np prefix is for the numpy library, this is not used in C++ you can just use the OpenCV Matrix container.

This line:

mat1=np.uint8(mat1)

is casting mat1 to the type uint8 which can be done like so:

mat1.convertTo(mat1, CV_8UC1);
Sign up to request clarification or add additional context in comments.

4 Comments

Two problems that I see: 1. kernel should be 8UC1, 2. 1 might be interpreted as data address. So Mat m = Mat(3, 3, CV_8UC1, cv::Scalar(1)); or Mat m = Mat::ones(3, 3, CV_8UC1);
I know it defaults to "1" so ive updated with the more obvious scalar solution. Thanks!
Thanks. Another question: In python we can do this: mat1[mat2==255] = 0. What is the equivalent en C++ ?
You may want also to mention getStructuringElement(). @testpresta mat1.setTo(0, mat2 == 255);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.