Ask Your Question

changing my python code to c++?

asked 2019-02-27 06:46:16 -0500

usuf gravatar image
def subalg_fun(self):

    cap = cv2.VideoCapture(1)

    _, first_frame =
    first_gray = cv2.cvtColor(first_frame, cv2.COLOR_BGR2GRAY)
    first_gray = cv2.GaussianBlur(first_gray, (5, 5), 0)
    while True:

        _, frame =
        gray_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

        gray_frame = cv2.GaussianBlur(gray_frame, (5, 5), 0)
        sub_image = cv2.subtract(first_gray, gray_frame)
        thresh = cv2.threshold(sub_image , 20, 255, cv2.THRESH_BINARY)[1]
        thresh = cv2.dilate(thresh, None, iterations=3)
        result = not np.any(thresh)
        cv2.imshow("Frame", frame)
        cv2.imshow("sub", sub_image)
        if result is True:



        # cv2.imshow("difference", difference)
        key = cv2.waitKey(30)
        if key == 27:

i converted the above code from python to c++ successfully but my problem is "result = not np.any(thresh)" here we use numpy to find the difference what is alternate solution for c++

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

answered 2019-02-27 07:29:39 -0500

HYPEREGO gravatar image

updated 2019-02-27 07:34:50 -0500

From NumPy official documentation, the behavior of the function is the following:

numpy.any(a, axis=None, out=None, keepdims=novalue)

Test whether any array element along a given axis evaluates to True.

Also, in that case, you give to thie function just the input array, so in Python the function is called with some default parameters: axis= None, out=None and keepdims=novalue. Just the first one is interesting for you, so:

The default (axis = None) is to perform a logical OR over all the dimensions of the input array

So I suppose that if just one element of the array is positive, this function return true: you have just to check each element of the array/Mat to find if at least one element is positive (greter than 0, because 0 is false), so it return true, otherwise false.

edit flag offensive delete link more
Login/Signup to Answer

Question Tools

1 follower


Asked: 2019-02-27 06:46:16 -0500

Seen: 74 times

Last updated: Feb 27 '19