Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to install opencv-python Image processing

2025-02-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains the "opencv-python image processing how to install", the article explains the content is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in depth, together to study and learn "opencv-python image processing how to install" it!

First, install opencv

With regard to the installation of opencv, if you use pycharm under the windows system, you can directly use the pip command on the terminal or click set-python interpreter to enter opencv-python.

If you are using conda, you can also use the pip command or conda install installation

It's easier to say under linux. And there will be much less bug for no reason, so it is recommended to use linux system. You can enter pip or pip3 install opencv-python from the terminal under linux, provided that python is installed and the path variable is set.

II. Use of opencv

To use opencv, you must import the opencv package at the beginning of the code, which is different from the installation name, using the command:

Import cv2

Import the package. And matplotlib and numpy need to be installed.

1. Reading of pictures

Img = cv2.imread ("car_green.jpg")

In parentheses is the name of the image file, which can be done if the code is in the same directory as the picture, but if it is not in the same directory, you need to include the full path to the picture file. Using this statement to read the picture will return a matrix of type ndarray (number of rows, columns, channels)

Show the picture you just read:

Note: the image format read by opencv is in BGR format.

Cv2.imshow ("name", img) # wait time, millisecond, 0 means any key terminates cv2.waitKey (0)

2. Get the video stream of the camera

Def video_get (): capture = cv.VideoCapture (0) # Save every frame of the video stream while True: ret, frame = capture.read () # Flip the image, the original image is frame = cv.flip (frame, 1) cv.imshow ("video", frame) c = cv.waitKey (50) if c = 27: break

3. Save the acquired image

Cv.imwrite (file path you want to save, image file)

4. Gray map conversion

Read directly as a grayscale image when reading:

Img = cv2.imread (path, cv.IMREAD_GRAYSCALE)

Read in color and convert to grayscale image

Img = cv.imread ("car_red.jpg") img = cv.cvtColor (img, cv.COLOR_BGR2GRAY) cv.imshow ("ing", img) cv.waitKey (0) cv.destroyAllWindows ()

5. Get some areas of the image

Because the picture you read is of type ndarray, you can use the slicing operation to get a portion of the image:

Roi = img [start: end, start: end]

6. Split and merge the channels of the color picture

Because the color picture is three-channel, assuming the picture size is 128x128, then it contains three 128-128 color matrices.

Img = cv.imread ("car_red.jpg") cv.imshow ("img", img) # Note format: BGRb, g, r = cv.split (img) # Channel merging, pay attention to the order of the matrix, so the synthesized picture is BGR format img = cv.merge ((b, g, r)) cv.imshow ("img1", img) cv.waitKey (0) cv.destroyAllWindows ()

After the three channels are extracted, each channel is actually a separate grayscale image if displayed, because the number of channels in the picture becomes 1.

If you want to display a color in RGB, you can use a slice to change the other color channel to 0

7. Boundary filling

Fill the boundary with the picture:

Cv.copyMakeBorder (img, 50, 50, 50, 50, borderType=cv.BORDER_REFLECT)

The parameters are the picture source, the filling distance from the top to the bottom (50, 50, 50 and 50), and the borderType parameter indicates the filling method.

BORDER_REPLICATE: copy method, copy the most edge pixels of the picture

BORDER_REFLECT: reflection method in which the pixels of the image of interest are copied on both sides

BRDER_REFLECT_101: reflection method, with the most edge pixel as the axis

BORDER_WRAP: outer packing method

BORDER_CONSTANT: constant method, constant value filling

Remember to define a variable before the function to accept the return value of the function.

8. Image size change

Use the resize function to modify the picture size

Red = cv.imread ("car_red.jpg") # the tuples in parentheses represent the length and width of the picture, that is, red = cv.resize for the columns and rows of the matrix (red, (475,245))

We use length and width to represent the size of the picture, the size of the matrix uses rows and columns, and the length of the corresponding picture corresponds to the column of the matrix, so we should pay attention to whether the size of the input is the size of the picture or the size of the matrix. there is no difference for the square matrix, but it has a great impact on other matrices.

Another way is not to specify its size, but to change the image by specifying the expansion multiple of the x and y axes.

Red = cv.resize (red, (0,0), fx=3, fy=4)

9. Image fusion

If you want the matrix to be able to be added, it must be of the same size, and after the two images are processed into pictures of the same size, they can be fused.

Green = cv.imread ("car_green.jpg") print (green.shape) red = cv.imread ("car_red.jpg") red = cv.resize (red, (475,245) print (red.shape) res = cv.addWeighted (red, 0.4, green, 0.6,0) cv.imshow ("res", res) cv.waitKey (0) cv.destroyAllWindows ()

The essence of image fusion is matrix addition, and the above function can be regarded as a formula:

F = ared + bgreen + c

Where a = 0.4 b = 0.6, c = 0, these coefficients can be set by ourselves.

The processed picture is as follows:

Thank you for your reading, the above is the content of "opencv-python image processing how to install", after the study of this article, I believe you have a deeper understanding of how to install opencv-python image processing, the specific use of the situation also needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report