1

I am new to the python and here I have a quick question.

say in a while loop, I have a variable called center, and center is a list, as shown above:

     M = cv2.moments(c)
    center = (int(M["m10"] / M["m00"]), int(M["m01"] / M["m00"]))

center will always be [x,y] and since M is dynamically changed all the time, so does the center.

My question is how can I know whether center is updated or not? If it is updated, how can I mathematically compare this the new center with the previous center?

Thanks.

2
  • 2
    Your best bet is to keep the previous value in a different variable. Commented May 3, 2018 at 3:32
  • Why not just have a list recording the history of center and append to it every time it is updated? Commented May 3, 2018 at 3:37

1 Answer 1

2

You need to keep track of center. To do this create a variable to hold the previous value. This can be initialised to None at the start as you don't have a previous value. For example:

previous_center = None

while True:    

    #
    # Your loop code
    #

    M = cv2.moments(c)
    center = (int(M["m10"] / M["m00"]), int(M["m01"] / M["m00"]))

    if center != previous_center:
        print("New center", center)
        previous_center = center
Sign up to request clarification or add additional context in comments.

Comments

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.