Lane line detection based on opencv (1) opncv installation and basic function use


foreword

` Reason for choosing opencv:
OpenCV is an open source computer vision library that provides a wide range of image processing and computer vision functions. It supports multiple programming languages, such as C++, Python, etc., and can be used across platforms.
OpenCV provides many functions and tools for lane line detection, including grayscale, smoothing, edge detection, Hough transform, etc. Through the combination of these functions, an effective lane line detection algorithm can be realized.
OpenCV also provides some sample codes and tutorials to enable users to quickly get started and start building their own lane detection programs. Additionally, OpenCV has an active community where users can get support and feedback.
In summary, OpenCV is a very powerful computer vision library that can be used to implement lane detection and other image processing tasks. Proficiency in the basic functions and algorithms of OpenCV can help build an efficient and accurate lane detection program.

As a novice, here I start by installing the opencv library.
Precondition: Windows 10 system
      has installed python and Pycharm

1. Opencv installation

   First, win+R, enter cmd to open the terminal, (here I assume that you have installed the python environment), enter pip install opencv-python , and then the system will automatically install, this process is relatively long, about 5 minutes, and then the installation is successful. (I downloaded it directly here. If it fails because of slow download speed, it is recommended to search for domestic mirror sources to download.)

   Open pycharm, create a new project, and use the following code to verify whether the installation is successful:

import cv2
print(cv2.__version__)

The result shows:
insert image description here
It means that my version information is correct.
but! ! !
There is still one problem that has been bugging me for a day.
When I use cv2., the compiler does not automatically complete the subsequent code for me, and if I manually enter cv2.imread(), ctrl+left mouse button cannot jump to the description of the function.
Therefore, I refer to many solutions, such as:
1. In pycharm's file-settings-compiler interpretation-import the installed cv2 path
2. Put the cv2.pyd in the root directory into the upper-level file site-packages .
But they all failed. Finally, I found that this seems to be because the opencv version in April 2023 is too new , which may be incompatible with the pycharm version, so I found a solution:

Roll back the opencv version to version 3.4

First, win+R, enter cmd to open the terminal, (here I default that you have installed the python environment), enter pip install opencv-python== , you will see the previous version number:
insert image description here
then select version 3.4, I chose 3.4.10.35,
enter pip install opencv-python==3.4.10.35 :
insert image description here
Then open pycharm, you can see cv2. Subsequent successful completion of the code, and support jump view.

2. Reading, displaying and saving pictures

1.cv2.imread

cv2.imread is a function for reading images in the OpenCV library, which can read images from the local file system and load them into memory.

The syntax of cv2.imread is as follows:

cv2.imread(filename, flags)
where filename is the image file name (including the path) to be read, and flags is an optional parameter specifying the way to read the image.

The possible flags of flags include:
cv2.IMREAD_COLOR, which means to read the image in color mode.
cv2.IMREAD_GRAYSCALE: reads the image in grayscale mode.
cv2.IMREAD_UNCHANGED: read image in raw mode, including alpha channel.

For example, the following code demonstrates how to use cv2.imread to read a color image:

import cv2

img = cv2.imread('test.jpg', cv2.IMREAD_COLOR)

The code will read an image named 'test.jpg' from the local file system and load it as a NumPy array. Note that an empty array will be returned if the file does not exist or if the file is not in the correct format.

2.cv2.imshow

cv2.imshow is a function used to display images in the OpenCV library, which can display image data in a window.

The syntax of cv2.imshow is as follows:

cv2.imshow(winname, mat)
where winname is the window name and mat is the image data to be displayed. Note that this function can only display the image data in the NumPy array. If you want to display images in other formats, you need to use other functions for conversion.

import cv2
img = cv2.imread('66666.png',cv2.IMREAD_GRAYSCALE)#以灰度图格式读取车道线图片
print(type(img))#打印格式
print(img.shape)#打印图片尺寸
cv2.imshow('image_DX2003zhouyilin',img)#显示灰度图片

3. cv2. imwrite

cv2.imwrite() is a function in the OpenCV Python library that can be used to save an image as a file. It accepts two parameters: the filename and the image array object to save. The filename should include the file extension to indicate the image format to save in (for example, .png, .jpg, etc.). The image array data type should be compatible with the selected file format.

import cv2
img = cv2.imread('66666.png',cv2.IMREAD_GRAYSCALE)#以灰度图格式读取车道线图片
print(type(img))#打印格式
print(img.shape)#打印图片尺寸
cv2.imshow('image_DX2003zhouyilin',img)#显示灰度图片
k = cv2.waitKey(0)#为了防止程序结束导致图片瞬间出现并消失,增加阻塞函数
cv2.imwrite('grey_image.bmp',img)#第一个参数是文件名,后面必须要有图片拓展名

After running, a picture file with the same suffix format will be generated in the project folder.

Summarize

This article only briefly introduces the use of the three basic functions in opencv, and will continue to study opencv in depth.

Guess you like

Origin blog.csdn.net/qq_53092944/article/details/130237346