In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces the pyramid outline and template matching of opencv image in python. The content is very detailed. Interested friends can use it for reference. I hope it can help you.
1. Image pyramid ① Gauss pyramid
Sampling down, there will be less and less data, which can be reduced by deleting even rows and columns.
Sampling up, there will be more and more data, doubling the image in each direction, and filling the new rows and columns with zeros. The same kernel is used to convolution the enlarged image to obtain an approximate value.
After upsampling, the picture becomes larger.
After downsampling, the picture becomes smaller.
② Laplace Pyramid
For the original image, downsampling is performed first, and then upsampling is performed, and the sampling result is subtracted from the original image.
The above calculation method is the first layer, and the second layer is calculated, and the result obtained from the first layer is calculated as the original image.
two。 Image outline
Cv.findContours (img,mode,method)
The most commonly used mode:
RETR_TREE, detect all contours, and reconstruct the entire hierarchy of nested contours
The most commonly used method:
CHAIN_APPROX_NONE: output contours as Freeman chain codes, and all other methods output polygons (sequences of vertices)
CHAIN_APPROX_SIMPLE: compress horizontal, vertical, and oblique parts, that is, functions retain only their end parts.
① looks for contours
The first parameter contours returned by the function is the profile information we need. It is the form of a list. Each element is an array. There may be multiple contours in an image, and there may be outer and inner contours, all of which can be called through the returned contours.
② contour feature
The area and perimeter of the contour can be obtained by calling different functions.
③ contour drawing
The first parameter is the picture to be drawn, the second is the outline information, the third is the index of the outline, the fourth is the color channel used, and the fifth is the width of the line.
You can also draw a boundary rectangle or circumscribed circle of the outline:
3. Template matching
The template matching template slides on the original image from the origin to calculate the difference between the template and the place where the image is covered by the template. There are six differences in opencv, and then the results of each calculation are put in a matrix and output as the result. If the original image is AB and the template is ab size, the output result is (A-a+1) * (B-b+1)
TM_SQDIFF: square difference, the smaller the more relevant
TM_CCORR: correlation, the bigger the correlation, the more relevant
TM_CCOEFF: the larger the correlation coefficient, the more relevant it is.
TM_SQDIFF_NORMED: the normalized square is different, the closer to 0, the more relevant
TM_CCORR_NORMED: normalized correlation, the closer to 1, the more relevant
TM_CCOEFF_NORMED: normalized correlation coefficient, the closer to 1, the more relevant
Suggestion: try to use the normalization method.
① template matching
Read the picture and the template that needs to be matched, and gray it:
The following is the length and width of the template.
Match through the matchTemplate method:
② matching frame line drawing
For the returned values obtained by matching, the minimum and maximum values and their corresponding coordinates are obtained by minMaxLoc. The required target value varies depending on the method you choose. For example, TM_SQDIFF_NORMED: the normalized square is different, that is, the closer to 0, the more relevant, that is, the smaller the better.
When you get the best values and coordinates, you can draw a rectangular box.
In the rectangle method, the parameters are in turn: the picture to be drawn, the coordinates of the upper left corner of the box, the coordinates of the lower right corner of the box, the brightness of the box, and the line thickness of the box. The results are as follows:
③ multi-object matching
Template matching can also match multiple objects. If there are multiple content matching with the template in the picture, there will be multiple results with high scores in the content returned by cv.matchTemplate. After setting the threshold, the coordinates of the results with high scores will be processed in the same way, and all the boxes can be drawn.
4. Histogram statistics ① histogram drawing
After the image matrix is flattened, the frequency histogram of different pixels can be drawn directly by plt.hist method.
② histogram statistics
Cv2.calcHist (images,channels,mask,histSize,ranges)
Parameters:
Imges: incoming ima
Channels: color channel, if it is grayscale image, it is [0], if it is color, it can be [0], [1], [2]. Correspond to BGR respectively.
Mask: mask. You can pass this parameter if you need to mask the image.
HistSize: it is the number of bin. You can enter it directly.
Ranges: range of pixel values, usually: [0256]
Note: when passing parameters, you need to enclose them in square brackets.
The calcHist return value is the statistical value of the corresponding color. When I is 0, it is BBJ 1 that corresponds to GPY 2 and R
Mask operation of ③ histogram
Create a mask, where the length and width of the picture is used as the length and width of the mask, and set the place you need to display to 1, and the rest to 0.
Mask operation can be realized through bitwise_and, that is, "and" operation.
Apply Mask to the picture and draw a histogram:
④ histogram equalization
The histogram can be equalized by cv.equalizeHist method.
The histogram is compared as follows:
The corresponding image changes as follows:
The above approach is to equalize the overall situation directly. You can see that the face of the statue in the picture has become blurred. This problem can be solved by adaptive histogram equalization, which divides the picture into different pieces and equalizes each piece separately.
5. Fourier transform
Low-pass filtering is to retain the low frequency, the image will become blurred. High-pass filtering is to retain the high frequency, the details of the image will be enhanced.
In opencv, there are mainly cv2.dft () and cv2.idft (). In the results obtained by converting the input image to np.float32 format first, the part with a frequency of 0 will be in the upper left corner, usually converted to the central position, and the result obtained by cv2.dft () can be realized by shift transform. The result is dual-channel (real part and imaginary part), and usually needs to be converted to image format before it can be displayed (0255).
The image above is the form in which the picture is converted into a grayscale image and displayed after it is converted to the frequency domain.
In low-pass filtering, when converting to the frequency domain and putting the low-frequency ones in the middle, the image can be removed by high-frequency filtering only by setting a mask. Then through the inverse conversion, the image can be restored. The specific code is as follows:
In the high-pass filter, you only need to convert the mask in the above code, and change 0 to 1. 1 to 0 to achieve high-pass filtering. The final picture is shown as follows:
It can be seen that the picture only retains the high frequency, that is, the part that changes dramatically, that is, the edge of the image.
On the python opencv image pyramid outline and template matching is how to share here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.