OpenCV 2.4.3 and Python

后端 未结 2 726
别那么骄傲
别那么骄傲 2021-02-02 17:29

Few days ago I went into searching for a good way to make a simple computer vision system. OpenCV library is something I need but it proved hard to learn with Python especially

相关标签:
2条回答
  • 2021-02-02 17:43

    To take it from another angle and allow you to run older code with new OpenCV installation versions...

    First off the move from cv to cv2 has to do with the library using different data structures for a lot of functions. The easiest way to tell if a function has changed between cv2 and cv is that cv functions start with a capital. Reworked cv2 functions seem to always have the first letter in lowercase. So if you are using an old book or old examples, you can still use the legacy cv. cv is now simply embedded in cv2. Simply use the following at the top of your scripts

        import cv2
        import cv2.cv as cv #required for old code not to be changed
    

    This allows you to simply run older code without changing it. I will demonstrate with your function call here. You had...

        cv.cvtColor('proba.jpg', 'CV_RGB2GRAY')
    

    The first thing I notice is that your function may be called wrong. (Given first letter of function is lower case it should start with cv2 not cv). Second is the 'code' you are passing the function. 'Codes' are members (coding noob here, forgive me if some of my vocab is inaccurate) of cv2 and cv but not always the same. You have 'CV_RGB2GRAY'. First off, no quotes. This is a cv 'code' not cv2. Also you are missing the 'cv.' in front. To demonstrate here is how I believe your function should be called for old cv version:

        cv.CvtColor('proba.jpg', cv.CV_RGB2GRAY) #Assuming you used listed imports
        cv2.cv.CvtColor('proba.jpg', cv2.cv.CV_RGB2GRAY) #Assuming you skipped second import
    

    And now cv2...

        cv2.cvtColor('proba.jpg', cv2.COLOR_RGB2GRAY)
    

    There you go, I hope this helps. Remember that given python runs off of scripts you can type anything you are unsure of directly into command line. This does wonders for helping me build my understanding (I first used python 5 days ago). For example if you were wondering why it wanted an integer in your function, when you type

        cv.CV_RGB2GRAY
    

    directly into the python command line, it spits '7' (handy that it is an int) back at you. The cv2 version spits out '7L'. Just remember to use the WaitKey() function now and again in some form otherwise highgui may not have the required time to process some commands, in some situations. Well that wraps it up. Sorry if I covered some things that were already covered or referenced to. If I did feel free to delete it, admins.

    0 讨论(0)
  • 2021-02-02 17:52

    I think you are taking it in the reverse path.

    Actually, with the new cv2 module, OpenCV has become far more simple compared to old cv interface. Not just simple, but very fast and highly productive, due to the Numpy support. Only thing is that, we should know how to use it appropriately.

    Here, you should use the function as follows :

    img = cv2.imread('pic.jpg')
    gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
    

    I would like you to visit one SOF which shows some comparison between both the modules : What is different between all these OpenCV Python interfaces?

    Another one SOF is here, which is a simple demonstration on how you can speed up the code with Numpy support : Performance comparison of OpenCV-Python interfaces, cv and cv2

    You need not learn C++ or C to use OpenCV, although C++ is the official language. Still, Python-OpenCV has good support. Once you get a grip on how to use OpenCV, you will be able to convert C++ codes into Python yourself. Then you can learn OpenCV from C++ tutorials also. For example, I started learning OpenCV from "Learning OpenCV" by Gary Bradsky which is completely in C++. At that time, there was only cv interface.

    As you mentioned in your comments, opencvpython.blogspot.com has some introductory tutorials. I started it focussing newbies in OpenCV.

    Also, check this SOF for more tutorials : Books for OpenCV and Python?

    0 讨论(0)
提交回复
热议问题