Opencv python basic and advanced operations provide corresponding source code and API

1. The 0 in waitKey(0) means waiting and press any key to terminate;
2. The order of Img.shape is h, w, c and the order of c is BGR;
3. The read image img = img+10 is equivalent Add 10 to each pixel;
4. The size of the two images must be equal during image fusion, otherwise an error will be reported: Solution: zoom a picture or crop a picture, the former may change the shape of the picture;
5. Boundary padding:
Insert picture description here
Insert picture description here
6
.Image threshold: THRESH_OTSU will automatically find a suitable threshold, suitable for double peaks, and the threshold parameter needs to be set to 0

thresh = cv2.threshold(gradX, 0, 255,cv2.THRESH_BINARY | cv2.THRESH_OTSU)[1]

Insert picture description here
7. Mean filter: the middle element is equal to the surrounding corresponding position multiplied by 9, generally choose an odd convolution kernel
Gaussian filter: determine the threshold in the convolution kernel by the degree of distance
Insert picture description here
Insert picture description here
8. A little trick: display all pictures side by side;
Insert picture description here
9. Corrosion Operation (morphological operations are for the white part): shrink the image inside, which is an operation performed after binarization;
Insert picture description here
Insert picture description here
10. Expansion operation: restore the damage caused by the corrosion operation to the image. Holes are filled;
Insert picture description here
Insert picture description here
11. Open operation and closed operation:
Close operation: Connect the features together to look more like a block, which is helpful for detecting the outer contour. Insert picture description here
12. Gradient operation: get boundary information.
Insert picture description here
13. Sobel operator: mixing is not recommended Calculating together, the effect is not good;
Insert picture description here
Insert picture description here
at least the image should be grayed before edge detection, because the gradient calculation does not reflect the difference of color changes, so it is better to convert to a gray image with only one color channel Perform edge detection. After having an in-depth understanding of image binarization and edge detection, I think that you can either directly use grayscale images for edge detection, or you can perform edge detection after binarization. The purpose of binarization is to further simplify the grayscale image and make the image The information in is more pure, and the edge brightness changes more obviously. If the threshold is selected well, unwanted weak edges can be filtered out, so that the outline of the image after edge processing is clearer. Another method is to perform edge detection first, and then binarization. This situation is generally applicable: If you want to obtain a binarized image, but because the original image has uneven lighting, the foreground and background grayscale differences are small, we cannot To get the complete target directly, then we can use the insensitivity of edge detection to light changes. First, we can do edge detection on the image to detect the contour of the target that we want to study further, and then calculate the original image based on the edge-only image. The average value of pixels of all edge points of the image, using this value as a threshold to perform binary processing on the original image, the target area can be obtained very well, and the connectivity of the target area is also very good;
14. Canny edge detection steps:
Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here
Among them: 80 and 150 respectively represent dual thresholds.
15. Image pyramid: Insert picture description here
down-sampling: down sampling is called down sampling along the pyramid, which is a reduction operation, the length and width become half of the original, and the area becomes 1/4 of the original.
Insert picture description here
Up-sampling:
Insert picture description here
Opencv code (the second one is continuous up/down sampling): Note: The color and clarity of the picture obtained by first up and then down or first down and up are not as good as before, regardless of upsampling or down sampling, the original information will be lost
Insert picture description here
Insert picture description here
16. Image contour, what is contour: scattered edges are not contours, but the contours are integral.
How to perform contour detection? Generally use binary image, processing into binary image can only be operated, the parameter usually uses RETR_TREE;
Insert picture description here
this function returns several values, namely: binary image, contour information, hierarchical structure, parameter: what to draw/return contour information on /Draw a few outlines -1 default all /drawing color/line width
Insert picture description here
Insert picture description here
17. Outline feature: take out each outline for calculation
Insert picture description here
18. Outline approximation: use a threshold to judge, one line segment can be used, one line segment can be used, otherwise it will be divided into two gradually
Insert picture description here
Insert picture description here
Approximate function: parameter: which contour/specified comparison value, generally used as a percentage of the perimeter
Source code: Insert picture description here
19. Insert picture description here
Circumscribed rectangle: circumscribed circle: Insert picture description here
20. Template matching:
Insert picture description here
Normalized, normalized results are more reliable : The
Insert picture description here
current method is as small as possible. The minimum value returned is the point in the upper left corner:
Insert picture description here
Insert picture description here
Draw a rectangle:
Insert picture description here
Result: Insert picture description here
Insert picture description here
21. Histogram operation: generally convert to grayscale for input image, if there is no grayscale conversion , Parameter two can specify the image, parameter three is a mask, you can use this if you want to count the histogram of a fixed area, parameter four is to specify the size of the abscissa of the histogram, such as 1, 2, 3, or 1 10 , 1020. Parameter five is the pixel range, usually parameter four and parameter five are fixed. The returned (256, 1) represents a two-dimensional, each abscissa corresponds to an ordinate
Insert picture description here
Insert picture description here
Insert picture description here
22. Mask operation:
Insert picture description here
Insert picture description here
23. Histogram equalization
Principle: How to do the basic mapping Insert picture description here
Source code:
Insert picture description here
Note:
Insert picture description here
24. Fu Li Leaf transformation: idft is an inverse transformation, which must be changed back when displayed. The input image in opencv must be converted to float32 format.
Insert picture description here
Insert picture description here
25.argparse module:
Insert picture description here
Insert picture description here

Guess you like

Origin blog.csdn.net/qq_42308217/article/details/109507994