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

Example Analysis of basic Operation of python opencv Image processing

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article is to share with you about the basic operation of python opencv image processing example analysis, the editor thinks it is very practical, so share it with you to learn, I hope you can get something after reading this article, say no more, follow the editor to have a look.

① reads images

② display image

In this function, name is the name of the display window, you can enter any string, and img is the read picture matrix.

WaitKey () means the length of time the picture stays. If set to 0, it will remain until the picture is closed.

③ video reading

Use the VideoCapture method to read the video, and if it can be read successfully, the first parameter returned by vc.read () is True, otherwise it is False. The second parameter returned, frame, is the read video image. Only one can be read at this time.

This function can display the read images in turn according to the set speed. The smaller the number in cv.waitKey, the faster the display.

Gray only sets the picture to a grayscale image, or it can be displayed directly with frame, which is the original image. The last 0xFF==27 is the ESC key, which exits when pressed.

④ image capture

Because the image is reading the matrix, it is OK to use the matrix interception method.

Extraction and restoration of ⑤ color channel

When CV reads the image, the color is BGR rather than RBG, which is extracted and restored using split and merge methods.

You can display an image that retains only one color channel by setting the other channels to 0.

⑥ boundary filling

After you specify the size, there are different filling methods, which are set through borderType.

⑦ numerical calculation

If it is directly added by matrix calculation, the remainder operation will be carried out when the result exceeds 255.

If you use the cv.add method, all those exceeding 255will be truncated to 255mm.

Modification of image size: cv.resize (image matrix, dimension length and width tuple)

⑧ image fusion

The image is fused by cv.addWeighted, and the parameters are: the first picture, the weight, the second picture, the weight, the degree of fine-tuning. The weight given is equivalent to the transparency of the image. The two pictures will overlap directly.

two。 Threshold and smoothing ① set threshold and image processing

Threshold processing is performed on the image through cv.threshold. In the above code, 127is the threshold and 255is the maximum value. The following is the method of processing, for example, BINARY is processed as 0 and 1, that is, the picture displayed is only black and white. Plus INV is the reverse, that is, white and black.

② image smoothing-mean filtering

The blur method, the first argument passed in is the image, and the second is the core size. That is, how much range to process the image, such as 3-3 or 5-5 and so on.

③ image smoothing-box filtering

④ image smoothing-Gaussian filtering

⑤ image smoothing-median filtering

Through the above filtering methods to filter the lena image, it is found that the median filter is the best, because the median filter will directly abandon the noise, while other filtering methods need to take into account the noise and calculate.

3. Morphological processing of image ① etching operation

Kernel is still the core and, as above, represents the size of the processing scope. The parameters of erode method are the image to be processed, the kernel and the number of iterations. Each iteration erodes part of the image. So if there is a fine burr will be directly eroded, thick lines will become thinner. The more iterations, the greater the erosion.

② expansion operation

This operation is equivalent to the reverse operation of the corrosion operation. Will expand the original content. If the original picture has a burr, the burr will become larger as it expands.

③ open operation and closed operation

Opening and closing operations are actually a combination of expansion and corrosion operations, with a sequence, as shown in the figure above. All are realized through the morphologyEx method, and the parameters can be changed.

4. Image gradient processing ① gradient operation

The gradient is the boundary obtained by expansion-corrosion. It is still implemented by the morphologyEx method.

② top hat and black hat

Top hat = original input-open operation result

Black hat = closed operation-original input

In fact, the above two operations are packaged into functions.

Gradient processing of ③ Image

Three operators: Sobel, Scharr, Laplacian

The input parameters of the Sobel operator and Scharr operator in the above code are: picture to be processed, image depth (usually specified as-1), x direction, y direction

Therefore, if only x is specified as 1, it is actually half calculated. It is necessary to change the value to an absolute value and calculate the other half in the case of yard1. The combination of the two will be a complete process.

Scharr operator: increase the left and right values, the result is more obvious.

Laplacian operator: more sensitive to changes and sensitive to noise points. The Laplace operator is not usually used alone. The principle is that the center point is compared with the surrounding point, and there is no need to enter x and y.

5. Edge detection ① Canny edge detection

Process:

1. Use a Gaussian filter to smooth the image and filter out noise

two。 Calculate the gradient intensity and direction of each pixel in the image

3. Non-maximum suppression is applied to eliminate the spurious effect caused by edge detection.

4. Apply double threshold detection to determine real and potential edges

5. The final edge detection is achieved by suppressing isolated weak edges.

The above is the comparison of the two kinds of edge detection, and the results are as follows

In this method, two thresholds are set, the places greater than the high threshold are retained, the places below the low threshold are discarded, and in the place between high and low, if there is a link with the reserved place, it is retained, otherwise it is discarded.

It can be seen that when the threshold is set high, there are fewer details. When it is low, there are more details.

The above is an example analysis of the basic operation of python opencv image processing. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.

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