Install and configure the OpenCV-Python-PyCharm development environment under Windows

1. Python installation

①Download the installation file of Python.
When Hao Hongjun wrote this article, Python has been updated to 3.10.8.
You can go to Python's official website to check which version is currently updated. Python's official website address: https://www.python.org/

The official website download link of each version of Python: https://www.python.org/ftp/python/

Here we take Python 3.6.8 as an example to illustrate the installation process of Python under Windows.

Python 3.6.8-Windows-64-bit official website download link: https://www.python.org/ftp/python/3.6.8/python-3.6.8-amd64.exe

The download link of the official website used in China is relatively slow. Please go to the page https://www.hhai.cc/thread-63-1-1.html for the download address of Baidu Netdisk .

②After the download is complete, run the file python-3.6.8-amd64.exe to install it.
Please check the Add Python 3.6 to PATH option (as shown in the figure below) during installation. This option can add Python to the system environment variables, which will facilitate subsequent configuration and use of Python.
Check the Add Python 3.6 to PATH option
③ After the installation is complete, how to check whether the installation is successful?
You can test whether the installation was successful like this. Open the Windows command console (you can open the command line window by entering cmd in "Run"), and then enter python. If the version information of Python is displayed, it means that the installation is successful. As shown in the figure below:
Test whether python is installed successfully
④Installing related libraries
In the process of using the OpenCV-Python development environment, other Python libraries will be used, among which the NumPy library and Matplotlib are the most frequent, so we simply install these two Python libraries first .
First install the NumPy library. The method is to reopen a cmd window and enter the command "pip install numpy", which will automatically download and install the NumPy library. As shown in the following two pictures:
insert image description here
insert image description here
Be careful here , do not enter "pip install numpy" in the cmd window that just tested whether python is installed, otherwise you will report an error "SyntaxError: invalid syntax", because you have entered Python through the Python command just now interactive mode, but it is wrong to execute the pip command in the Python interactive mode, so you need to reopen a cmd window, or exit the Python interactive mode through the command "exit()", as shown in the figure below: Reinstall the Matplotlib
insert image description here
library, enter the command "pip install matplotlib==3.2.0", and the library can be downloaded and installed automatically. Note that the version number must be specified here, otherwise there will be an error when importing the matplotlib library later, because the latest version of matplotlib is not compatible with the Python version we installed, and some modules may not be found.

How to test whether these two libraries are installed successfully? Can be tested as follows.
Open a new cmd window (note: in order for cmd to obtain the environment variable information of the latest library, it is best to open a new cmd window, otherwise there may be an error that the corresponding module cannot be found), then enter python to enter the interactive mode, and enter import If numpy as np and import matplotlib as plt can be run successfully, it means that the two libraries are installed successfully, as shown in the figure below:
insert image description here

Second, the installation of Pycharm

We usually don't use the IDE that comes with Python to develop python programs and applications. Here we recommend PyCharm as the development IDE.
The download and installation of this is very simple, you can refer to the following blog post:
https://blog.csdn.net/c_shell_python/article/details/79647627
It is worth noting that: the above blog post is to install pycharm first, and then install it python, so you need to set the location of the python interpreter in pycharm. If you install python first, and check "Add Python 3.6 to PATH" when installing python, you don't need to set it after installing PyCharm.
The Baidu network disk download address of the Pycharm installation package can be found on the page: https://www.hhai.cc/thread-66-1-1.html

According to the instructions in the above blog post, after you install it, create a new Project, and then you can start writing your own Python programs.
Just one thing to pay special attention to, that is, pay special attention to the choice of interpreter when creating a new project. If there is no choice here, there will be many unexpected situations in the future. For details, see the page https://www.hhai.cc/thread- 68-1-1.html

3. Install OpenCV and its extension library

Here we install OpenCV and its extension library through Python's pip.
First install OpenCV.
It's very simple. Open the cmd window and enter the command "pip install opencv-python==4.1.2.30" to install OpenCV. If the version number is not added later, the latest version of OpenCV will be installed. As shown in the figure below:
insert image description here
Next, install the extension library of OpenCV.
Why install the extension library? Although the functions in the OpenCV main library can meet the needs of most image processing, functions such as image refinement and SURF feature algorithms are placed in the Contrib extension module. Enter the command "pip install opencv-contrib-python==4.1.2.30" to install the extension library. Similarly, if the later version number is not specified, the latest extension library will be installed by default. Here, in order to maintain a good relationship with the Python main library Compatibility, we better specify the version number. As shown in the figure below:
insert image description here
How to verify whether OpenCV is successfully installed? Please look at the picture below.
insert image description here
The relevant commands are as follows:

C:\Users\Administrator>python
>>> import cv2 as cv
>>> print(cv.__version__)

If the result of the above picture appears, it means that the Python-OpenCV-PyCharm development environment configuration is successful.

Guess you like

Origin blog.csdn.net/wenhao_ir/article/details/123014519#comments_27237153