Determine which version of OpenCV

后端 未结 4 1666
一个人的身影
一个人的身影 2021-01-17 08:16

I want to write to short code snippet in python, to determine which version of OpenCV has been installed in my System. How do i do it ? Thank you.

4条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-17 08:37

    Convenient functions to check OpenCV version in run-time

    def cv2():
        return opencv_version("2")
    
    def cv3():
        return opencv_version("3")
    
    def cv4():
        return opencv_version("4")
    
    def opencv_version(version):
        import cv2
        return cv2.__version__.startswith(version)
    

    Useful when performing cv2.findContours() since return signature varies by version

    # Using OpenCV 2.X or OpenCV 4
    if cv2() or cv4():
        cnts, _ = cv2.findContours(image, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
    
    # Using OpenCV 3
    elif cv3():
        _, cnts, _ = cv2.findContours(image, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
    

提交回复
热议问题