banner



How To Install Opencv In Mac

Solarian Developer

My programming ramblings

Install OpenCV 4 with Python three on macOS Catalina

Posted on October 21, 2022 by Paul

Updated eleven February 2022

In this article, I will show you how to install OpenCV 4 with Python 3 on macOS Catalina.

At that place is also a video version of this tutorial:

MacOS comes past default with Python 2.7 which, at this point, receives only bug fixes and will be EOL by 2022. Python 3.ten is the future and it is supported by all major Python libraries. In this tutorial, nosotros'll employ the Python 3.8.

Start by installing the Command Line Tools for macOS. Please note, that you will demand the Command Line Tools even if you've already installed Xcode. Open a Concluding and write:

                                                    1                                    xcode-select --install                              

Once the Command Line Tools are installed, we tin can install Python.

The official installer of Python is a pkg file that volition start a GUI installer which will guide you through the installation. You tin also bank check the video version of this tutorial if you lot want to see how I did information technology.

As a side note, you can take multiple Python three versions installed on your macOS machine. If this is the instance, you lot tin select which version you want to apply past specifying the version number, due east.g.:

                                                    one                                    python3.seven                              

or:

                                                    1                                    python3.8                              

After the above, you can invoke Python 3.viii using the python3.8 command. python3 volition besides invoke the latest installer version of Python iii. This is what I see if I run python3.eight on my machine:

                                                    1                                    %                  python3.8                  2                                    Python 3.eight.1 (v3.8.1:1b293b6006, December xviii 2022, 14:08:53)                  three                                    [Clang six.0 (clang-600.0.57)] on darwin                  four                                    Type "help", "copyright", "credits" or "license" for more data.                  5                                    >>> quit()                  6                                    %                              

Next, let'southward follow best practices and create a new Python environment in which nosotros can install NumPy and OpenCV:

                                                    1                                    python3.8 -grand venv work                  2                                    source piece of work/bin/activate                              

At this indicate, your prompt should indicate that y'all are using the piece of work environment. Y'all can read more about Python environments in the documentation.

In one case an environment is activated, all the install commands volition apply only to the current environs. By default, if you close your Last, the surroundings is deactivated. If you lot want to be able to apply it, utilise the source work/bin/activate command.

We can install NumPy and OpenCV with:

                                                    1                                    pip install numpy                  2                                    pip install opencv-python==4.1.2.xxx                              

Please note that the above volition install the slightly older OpenCV 4.1, latest binary version doesn't seem to piece of work on macOS Catalina!

Every bit a side notation, when you are in a new environment you can employ python to invoke the Python interpreter, no demand to use the version number.

At this point, you should have OpenCV 4 and Python installed on your Mac. We tin can write a modest test program that will print the OpenCV version, load an prototype from the disk, catechumen the image to gray and bear witness the result. Starting time past downloading the next prototype:

Over the Clouds test image

Save it as clouds.jpg. In the same folder where you've saved the above image, create a new file demo.py and write the next code:

                                                                        1                                    import                  cv2                                      2                                                        3                                    print                  (                  "OpenCV version:"                  )                                      iv                                    print                  (                  cv2                  .                  __version__                  )                                      5                                                        6                                    img                  =                  cv2                  .                  imread                  (                  "clouds.jpg"                  )                                      7                                    greyness                  =                  cv2                  .                  cvtColor                  (                  img                  ,                  cv2                  .                  COLOR_BGR2GRAY                  )                                      eight                                                        nine                                    cv2                  .                  imshow                  (                  "Over the Clouds"                  ,                  img                  )                  10                                    cv2                  .                  imshow                  (                  "Over the Clouds - grayness"                  ,                  grayness                  )                  11                                    12                                    cv2                  .                  waitKey                  (                  0                  )                  13                                    cv2                  .                  destroyAllWindows                  ()                              

Run the code with:

                                                    i                                    python demo.py                              

(You lot can shut the ii windows by pressing ESC!)

You should see something similar in the side by side effigy. By default, the last image (the grayness one) will be over the first one. You need to move the window in order to see both images:

Python 4 OpenCV 4 test - convert image to gray

If you want to larn more about OpenCV and Python I would recommend reading OpenCV with Python Blueprints by One thousand. Beyeler:

or, OpenCV with Python By Case by Thou. Garrido and P. Joshi:



Source: https://solarianprogrammer.com/2019/10/21/install-opencv-python-macos/

Posted by: mcminnforperfatim.blogspot.com

0 Response to "How To Install Opencv In Mac"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel