An automatic processing method for core photos based on template matching and Hough transform
By applying template matching and Hough transform methods in core photo processing, the core box image is automatically identified, rotated and straightened, which solves the problem of low efficiency of traditional manual processing and achieves efficient and accurate automatic processing of core photos.
Patent Information
- Application Number
- CN202410577369.2
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2024-05-10
- Publication Date
- 2025-01-17
- Estimated Expiration
- 2044-05-10
AI Technical Summary
In geological exploration, during the shooting and processing of core photos, due to the inconsistent placement angles and dense arrangement of core boxes, the adjacent core boxes in front and back will inevitably be involved during the shooting process. The traditional processing method requires geological staff to manually handle it. Rotational alignment and cropping are inefficient and the accuracy needs to be improved.
An automated core photo processing method based on template matching and Hough transform is adopted. By setting matching marks on the core box, template matching is used to identify the mark position, and combined with Canny edge detection and Hough transform, the core photos are automatically rotated, straightened, and cropped.
It realizes the automatic rotation, straightening and cropping of core photos, improves processing efficiency, reduces errors caused by manual operations, and can realize automatic background cropping of core photos in computer language.
Smart Images

Figure CN118537346B_ABST
Abstract
Description
Technical Field
[0001] The invention relates to the technical field of image processing, and in particular to an automatic processing method for core photos based on template matching and Hough transform. Background Art
[0002] In the field of geological exploration, according to the provisions of the "Regulations for Original Geological Cataloging of Solid Mineral Exploration DZT0078-2015", all core boxes that have been inspected and sorted must be digitally photographed and archived box by box, and only one set of horizontal core boxes can appear in each image. However, the core boxes in the core library are usually densely arranged in rows, so it is inevitable that the adjacent core boxes will be involved in the shooting process, and the placement angles of each core box are different. The traditional practice is that after the geological staff takes photos of the core boxes in the core library, they manually use image processing software (such as Windows system image viewer or Photoshop and other software) on the computer to rotate, straighten and crop all the core photos to ensure that each photo involves only one set of horizontal core boxes. However, in geological exploration work involving a large number of deep holes, this mechanical manual processing process is very time-consuming and labor-intensive.
[0003] For a large number of core photos to be processed, the traditional approach of geologists is to use image software to manually rotate, straighten, and crop each core photo one by one before saving them. This processing method is not only inefficient, but also needs to be improved in accuracy. Summary of the invention
[0004] In order to solve the technical problems existing in the above-mentioned background technology, the present invention provides an automatic processing method of core photos based on template matching and Hough transform.
[0005] The present invention provides a method for automatically processing core photos based on template matching and Hough transform, comprising the following steps:
[0006] Step S1: Determine the color and style of the matching mark and generate a matching template;
[0007] Step S2: setting matching marks at the upper left corner and the lower right corner of the core box respectively, and obtaining an image of the core box with the matching marks;
[0008] Step S3: using the matching template to identify the matching mark in the image, setting the pixel threshold, preliminarily cropping the image to obtain the target core box image in the middle part of the image, converting the image color space from RGB to GRAY color space through color space transformation, and then processing the core box image using the Canny edge detection algorithm to generate the contour straight line of the target core box convex groove in the image;
[0009] Step S4: using Hough transform, detecting the straight line that meets the requirements of the target core box in the image, and the polar coordinate analytical expression of the straight line; converting the polar coordinate analytical expression of the straight line into a rectangular coordinate analytical expression, and calculating the slope of the straight line to obtain the deflection angle of the straight line; sequentially obtaining the deflection angles of all convex groove straight lines of the target core box in the preliminary cropped image, calculating the average value of the deflection angles, obtaining the deflection angle of the core box in the image, and rotating and straightening the image to obtain the straightened image of the core box to be cropped;
[0010] Step S5: using the matching template, identifying the matching marks at the upper left corner and the lower right corner of the rectified core box image, and recording the pixel coordinates of the matching mark positions;
[0011] Step S6: cropping the rectified core box image according to the recorded pixel coordinates to obtain a processed core box image.
[0012] In some embodiments, the matching mark at the upper left corner and the matching mark at the lower right corner of the core box are set in different colors, and the matching marks are drawn with waterproof material.
[0013] In some embodiments, the match mark at the upper left corner of the core box is drawn as a red arrow, and the match mark at the lower right corner is drawn as a blue arrow using a waterproof marker.
[0014] In some embodiments, in step S1, the specific steps of generating a matching template include:
[0015] Setting matching marks on the core box and obtaining an image of the core box containing the template;
[0016] Use the np.array function to create HSV range arrays corresponding to red and blue respectively, and then use the cv2.inRange function to create red HSV mask and blue HSV mask respectively;
[0017] The cv2.threshold function is used to perform a color mask-based binarization process on the template core box image, and the arrow part in the binary image is generated as a matching template.
[0018] In some embodiments, in step S3, the core box image is processed using the Canny edge detection algorithm, specifically including:
[0019] Grayscale the image to obtain a grayscale image;
[0020] Perform Gaussian filtering on the grayscale image to obtain a filtered image;
[0021] The Sobel operator is used to calculate the gradient magnitude and direction of the filtered image, and the non-maximum value of the gradient magnitude is suppressed to compress the edge bandwidth;
[0022] The compressed edge bandwidth is binarized using a hysteresis threshold to obtain a binarized image;
[0023] A connection operation is performed on the binary image to connect the disconnected edges to obtain a final edge image, wherein the edge image contains the straight line contour information of each row of convex grooves in the core box.
[0024] In some embodiments, step S3 specifically includes:
[0025] Use a Gaussian filter to smooth the image, where the Gaussian function H(x,y) is:
[0026]
[0027] G(x,y)=f(x,y)*H(x,y)
[0028] Where f(x,y) is the image function, G(x,y) is the Gaussian function, and σ is the standard deviation;
[0029] The magnitude and direction of the gradient are calculated using the finite difference of the first-order partial derivative, and the first-order difference convolution module is used
[0030]
[0031]
[0032]
[0033] Amplitude:
[0034]
[0035] direction:
[0036]
[0037] Use non-maximum suppression technology to process the gradient amplitude. First, set the gradient value of the central pixel of each point area to M, and then compare M with the gradient values of the two pixels on the gradient line of the point. If these two gradient values are smaller than M, set M = 0.
[0038] The edge is detected and connected by the dual threshold algorithm, and the image is processed using two thresholds of different sizes, T1 and T2, to obtain two threshold edge images N1[i, j] and N2[i, j], where T2>T1;
[0039] The double threshold method is used to connect the edges into contours in the N2[i, j] image. When the endpoint of the contour is reached, the edge connected to the contour is searched at the neighboring point position of N1[i, j].
[0040] This operation is repeated, and the dual threshold algorithm continuously collects the edges of the image in the image N2[i, j] until the image N2[i, j] is connected.
[0041] In some embodiments, step S5 specifically includes:
[0042] The red mask is used to binarize the rectified core box image to obtain a binary image 1. The binary image 1 is matched with the matching template. The image of the matching template is T, the width is M, the height is N, the binary image 1 is S, and the similarity between the matching template and the binary image 1 is D. The formula for calculating the similarity using the square difference is as follows:
[0043]
[0044] The higher the similarity between the binary image 1 and the matching template, the smaller the D value. If D = 0, it means that the two are completely matched. The area with the smallest D in the binary image 1 is selected as the template matching result, and the position of the red arrow is identified;
[0045] The rectified core box image is binarized using the blue mask to obtain a binary image 2; the binary image 2 is template matched with the matching template to identify the position of the blue arrow.
[0046] The beneficial effects of the present invention are:
[0047] The present invention integrates various image processing algorithms to automatically rotate, straighten and crop core photos, uses color space transformation to convert the image color space from RGB to HSV color space, improves the accuracy and efficiency of Canny edge detection, uses Canny edge detection algorithm to improve the Hough transform straight line recognition effect, uses Hough transform to realize the rotation and straightening of core photos, and uses template matching technology to realize the recognition and positioning of the target core box matching mark, thereby realizing automatic rotation, straightening and cropping of core photos. Compared with the previous geologists manually rotating and cropping 150 core photos in an hour on average, this function can realize the background automatic cropping of core photos through computer language, reduce the errors caused by manual operation, and improve the efficiency of cropping core photos. BRIEF DESCRIPTION OF THE DRAWINGS
[0048] Figure 1 The upper left corner matching mark and the binarized image in the embodiment of the present invention;
[0049] Figure 2 The lower right corner matching mark and the binarized image in the embodiment of the present invention;
[0050] Figure 3 is the reverse matching mark;
[0051] Figure 4 The schematic diagram of core boxes being densely arranged in rows in the core library;
[0052] Figure 5 Schematic diagram of the target core box, the upper core box, and the lower core box;
[0053] Figure 6 To detect the straight line contour results of the core box convex groove using the Canny algorithm;
[0054] Figure 7 To use the red HSV mask to binarize the core image;
[0055] Figure 8 To identify the matching mark in the upper left corner of the target core box;
[0056] Fig. 9 To identify the matching mark in the upper left corner of the lower core box;
[0057] Fig.10 It is the secondary template matching recognition result after clipping with the ordinate of the matching mark in the upper left corner of the lower core box as the boundary;
[0058] Fig.11 To identify the matching mark in the lower right corner of the upper core box, use the blue HSV mask to binarize the core image;
[0059] Fig.12 The secondary template matching recognition result after clipping with the ordinate of the matching mark at the lower right corner of the upper core box as the boundary;
[0060] Fig.13 To identify the matching mark in the lower right corner of the target core box;
[0061] Fig.14 A user interface created using the Tkinter graphical interface library;
[0062] Fig.15 The figure is a flow chart of the method of the present invention. DETAILED DESCRIPTION
[0063] The following is a description of exemplary embodiments of the present disclosure in conjunction with the accompanying drawings, including various details of the embodiments of the present disclosure to facilitate understanding, which should be considered as merely exemplary. Therefore, it should be appreciated by those of ordinary skill in the art that various changes and modifications may be made to the embodiments described herein without departing from the scope and spirit of the present disclosure.
[0064] Based on the problem of low efficiency of manual processing of a large number of core photos by geological staff, the present invention proposes a method for automatic rotation, straightening and cropping of core photos based on template matching and Hough transform, which can realize automatic processing of core photos. This embodiment adopts Python language and is mainly implemented by using OpenCV computer vision library, such as Fig.15 As shown, the specific steps include:
[0065] Step S1: Determine the color and style of the matching mark and generate a corresponding matching template.
[0066] Since it is necessary to identify the pixel area where the target core box is located in the image by identifying the matching mark, different colors or even shapes are used as matching marks to improve the template matching effect;
[0067] like Figure 1-2 As shown, in this embodiment, a red arrow is used at the upper left corner of the core box and a blue arrow is used at the lower right corner for marking;
[0068] Using different color HSV masks to binarize the upper left and lower right matching marks can effectively reduce noise interference outside the target color. The matching marks only need to ensure that they use different colors, that is, each recognition is only aimed at identifying the matching mark in the upper left or lower right corner of the target core box; in this step, the shapes of the two matching marks only need to be based on the principle of convenient drawing, and there are no strict requirements for the shapes of the matching marks.
[0069] To obtain a matching template, you need to first draw a matching mark on the core box and obtain the core box image containing the template;
[0070] First, use the np.array function to create HSV range arrays corresponding to red and blue respectively;
[0071] Secondly, use cv2.inRange function to create red HSV mask and blue HSV mask respectively;
[0072] Finally, the cv2.threshold function is used to perform a color mask-based binarization process on the template core box image, and the arrow part in the binarization result is generated as the matching template, such as Figure 1 shown.
[0073] To ensure the subsequent template matching recognition effect, the shape of the matching template should have a certain degree of distinction.
[0074] Usually, there is a raised arrow in the upper left corner of the core box with the same color as the core box. To improve the template matching effect, just use a waterproof marker to fill in the raised arrow. Figure 1 shown.
[0075] The arrow in the lower right corner of the target core box reduces the complexity of the marking as much as possible, such as Figure 2 As shown, the arrow can be marked with only two strokes.
[0076] Step S2: Use a waterproof marker pen to make matching marks on the upper left corner and the lower right corner of the core box, use a digital device to take pictures of the core box, and collect high-definition core box images.
[0077] To avoid the core being blocked by the sun's shadow when taking photos of some core boxes, geologists usually choose to take photos of the target core box in the upper core box or the lower core box according to the actual situation. To avoid the subsequent manual rotation of some reverse-shot images by 180°, you can set the reverse matching mark ( Figure 3 ) to determine whether the image needs to be rotated 180°, thereby avoiding geologists from finding inverted core box images from numerous core box images and manually rotating them to the same direction.
[0078] If a matching mark in the opposite direction is identified in the image, the image is rotated 180° and then step S3 is executed. Otherwise, step S3 is directly executed.
[0079] The core boxes are densely arranged in rows in the core library. Figure 4 As shown, when using digital equipment to photograph core boxes, it is inevitable that the adjacent core boxes will be involved. Therefore, after collecting the core box images, geologists usually need to use computer image processing software to rotate, straighten and crop the core box images one by one.
[0080] According to the provisions of the "Original Geological Logging Regulations for Solid Mineral Exploration DZT0078-2015", the core should be cleaned and sprayed with water to keep it moist before taking pictures. In order to avoid blurring the matching marks on the core box when spraying water on the core, it is necessary to use a waterproof marker to make matching marks. The position of the matching marks in the core box is as follows: Figure 5 shown.
[0081] like Figure 5 As shown in the figure, since the target core box is placed in the middle of the shooting range when taking pictures, the red arrow of the upper core box and the blue arrow of the lower core box are not photographed. The core box photos taken only include the red arrow and blue arrow of the target core box, the blue arrow of the upper core box and the red arrow of the lower core box.
[0082] Since template matching may identify the mark of a non-target core box, for example, when identifying the red arrow of the target core box, the red arrow of the lower core box may be identified. In order to improve the robustness of automatic recognition, this embodiment performs preliminary processing on the pixel position of the template matching recognition result, and determines whether the recognition result is the matching mark of the target core box by judging the ratio of the pixel position of the recognition result in the core box image.
[0083] In geological exploration projects, core boxes are usually transported to the core warehouse by the drilling construction team regularly after completing a certain footage. Usually, the daily footage of a single drilling rig is less than 60 meters (about 12 core boxes), which means that the drilling construction personnel only need to mark the core box with a waterproof marker pen once every 2 hours on average. This workload is negligible for the drilling construction team. Therefore, this step can be entrusted to the construction drillers after consultation with the drilling construction team. When the drilling construction personnel mark the footage, sampling rate and other information, they draw the corresponding marks on the core box according to the matching marks determined in step S1.
[0084] Step S3: using template matching to identify matching marks in the image, setting a pixel threshold, and preliminarily cropping the image to obtain a partial image of the target core box. The image color space is converted from RGB to GRAY color space through color space transformation, and then the core box image is processed using the Canny edge detection algorithm to generate the contour straight line of the target core box convex groove in the image;
[0085] Use template matching to identify the matching mark in the upper left corner of the target core box. If the red arrow below the target core box is identified, use the vertical coordinate of the red arrow identified this time to crop out the lower core box, and then perform template matching again.
[0086] After completing the template matching, in order to improve the accuracy of the rotation angle and avoid the influence of the deflection of the upper core box or the lower core box on the actual rotation angle of the target core box, by setting a reasonable pixel threshold, the transforms.CenterCrop function is used to preliminarily crop the center part of the target core box image to extract the straight line part of the convex groove of the core box.
[0087] Use color space transformation to convert the image color space from RGB to GRAY color space.
[0088] The core box images collected by digital cameras are in color and are easily affected by environmental factors such as lighting. It is necessary to use the cv2.cvtColor function to convert the core box images taken by digital devices into grayscale, so as to remove noise interference and enhance the contrast details of the core box images. The first parameter of the cv2.cvtColor function is the source image, and the second parameter is cv2.COLOR_BGR2GRAY.
[0089] like Figure 6 As shown in the figure, before performing Hough transform, you need to use cv2.Canny function to perform edge detection. The main purpose of edge detection is to find points in the image where the brightness changes significantly, and remove interfering points during the detection process to reduce the amount of detection data and retain the structural properties of the image to a certain extent.
[0090] In general, by reasonably setting the cv2.Canny function parameters, the straight line contour information of each row of convex grooves in the core box will be formed in the generated edge detection result image, so the straight line contour of the convex groove of the core box can be detected by edge detection. In this embodiment, the first parameter of the cv2.Canny function is the image to be detected converted into the GRAY color space, the second parameter is the threshold parameter for edge connection, which is 350 in this embodiment, the third parameter is the threshold parameter for edge detection, which is 400 in this embodiment, and the fourth parameter is the output Canny edge detection result image.
[0091] The Canny edge detector first grays the input image; then performs Gaussian filtering on the grayscale image to smooth the image and remove noise; uses the Sobel operator to calculate the gradient amplitude and direction of the filtered image; performs non-maximum suppression on the gradient amplitude to compress the edge bandwidth; uses a hysteresis threshold to binarize the compressed edge bandwidth to obtain a binary image; and performs a connection operation on the binary image to connect the disconnected edges to obtain the final edge image.
[0092] This embodiment uses the Canny operator to perform edge detection. The Canny operator first uses a Gaussian filter to smooth the image, and finally uses the non-extreme value suppression technology to obtain its edge. The steps are as follows:
[0093] Use a Gaussian filter to smooth the image, where the Gaussian function H(x,y) is:
[0094]
[0095] G(x,y)=f(x,y)*H(x,y)
[0096] Where f(x,y) is the image function, G(x,y) is the Gaussian function, and σ is the standard deviation.
[0097] Use the finite difference of the first-order partial derivative to calculate the magnitude and direction of the gradient, using the first-order difference convolution template:
[0098]
[0099]
[0100]
[0101] Amplitude:
[0102]
[0103] direction:
[0104]
[0105] Use non-maximum suppression technology to process the gradient amplitude. Obtaining the global gradient value of the image does not mean that the edge of the image has been obtained. To obtain the edge of the image, the maximum point of the non-local gradient amplitude must be set to zero, and then its local maximum point must be retained. Only in this way can the refined edge be finally obtained. The specific method is: first set the gradient value of the central pixel of each point area to M, and then compare M with the gradient values of the two pixels on the gradient line of the point. If these two gradient values are smaller than M, set M = 0.
[0106] The edge is detected and connected by the double threshold algorithm. The image is processed by two thresholds of different sizes, T1 and T2, to obtain two threshold edge images N1[i, j] and N2[i, j], where T2>T1. The edge is connected into a contour in the N2[i, j] image by the double threshold method. When the endpoint of the contour is reached, the edge connected to the contour is searched at the neighboring point position of N1[i, j].
[0107] This operation is repeated, and the dual threshold algorithm continuously collects the edges of the image in the image N2[i, j] until the image N2[i, j] is connected.
[0108] Step S4: using Hough transform, detecting the straight lines that meet the requirements in the target core box image and the polar coordinate analytical expression of the straight line; converting the polar coordinate analytical expression of the straight line into a rectangular coordinate analytical expression, and calculating the slope of the straight line to obtain the deflection angle of the straight line; sequentially obtaining the deflection angles of all convex groove straight lines in the target core box image after preliminary cropping, calculating the average value of the deflection angles, obtaining the deflection angle of the target core box in the image, and rotating and straightening the image to obtain the straightened core box image to be cropped;
[0109] In the embodiment, the cv2.HoughLines function is used to detect straight lines that meet the requirements and generate the corresponding polar coordinate analytical expression. The first parameter is the image returned by the Canny edge detection, the second parameter is the accuracy of the distance r in pixels, which is 1 in this embodiment, and the third parameter is the accuracy of the angle θ, which is π / 180 in this embodiment, indicating that all possible angles are searched. The fourth parameter is that the line segment is detected only when it exceeds the set threshold, and the threshold parameter is 0 in this embodiment.
[0110] The Hough transform is a method for finding lines, circles, and other simple shapes in an image.
[0111] A straight line in Cartesian space determines a point in Hough space, and conversely, a point in Hough space determines a straight line in Cartesian space.
[0112] The principle of Hough transform is to transform the points on a specific figure into a set of parameter spaces, and find a solution corresponding to the maximum value based on the cumulative results of the points in the parameter space. This solution corresponds to the parameters of the geometric shape to be found. For example, in this method, when identifying a straight line, the polar coordinate analytical expression of the straight line will be obtained.
[0113] The Hough transform is able to detect any object described by its model by understanding its shape (such as line, circle, ellipse, etc.) and aiming to find the position and direction in the image. The problem of finding an object (described by a model) in an image is solved by finding the position of the model in the image. The problem of finding the position of the model is converted into the problem of finding the transformation parameters that map the model to the image. By giving the value of the transformation parameters, the position of the model in the image can be determined. The original image operated by the Hough transform is a binary image. Therefore, the original image should be binarized or Canny edge detection should be performed before using the Hough transform.
[0114] Use Hough transform to detect straight lines. In the image xy coordinate space, the line passing through the point (x i ,y i ) is represented by a straight line:
[0115] y i =ax i +b
[0116] In the formula, parameter a is the slope and b is the intercept.
[0117] In the coordinate space, through the point (x i ,y i There are countless straight lines with different parameters a and b. Now let the parameters a and b be variables, and let x i and i is considered as a constant, the expression is:
[0118] b=-x i a+y i
[0119] It is thus converted into parameter plane ab, and this transformation is called Hough transform.
[0120] From the above, we can know that the point (x i ,y i ) can be transformed into the unique equation of the point in the parameter space. Similarly, for the point (x i ,y i ) is another point (x j ,y j ) This point also has a unique equation corresponding to it in the parameter space, and its expression is:
[0121] b=-x j a+y j
[0122] Obviously, the above two straight lines intersect at a point (a0, b0) in the parameter space.
[0123] In summary, we can know that: for a straight line in the image coordinate space, after transforming the straight line from the coordinate plane to the parameter plane, each point on the straight line has a corresponding straight line on the parameter plane ab, and these straight lines will intersect at the point (a0, b0) on the parameter plane ab. Conversely, all straight lines that intersect at the same point in the parameter space can find a collinear point corresponding to them in the coordinate space. Therefore, as long as we find some edge points of the desired shape, we can determine the equation of the straight line connecting these points using the Hough transform.
[0124] First, assuming that the linear polar coordinate expression returned by the Hough transform is (ρ,θ), first replace ρ in the polar coordinate expression 2 , ρcosθ and ρsinθ are respectively expressed by x 2 +y 2 , x and y are substituted to obtain the rectangular coordinate expression of the straight line; secondly, in this embodiment, any two x1 and y2 are selected, and the corresponding y1 and y2 are calculated; finally, the rotation angle is obtained to be arctan((y2-y1) / (x2-x1)).
[0125] Use the calculated angle to use the ndimage.rotate function to automatically rotate and straighten the image;
[0126] The first parameter of the ndimage.rotate function is the image to be rotated, and the second parameter is the rotation angle arctan((y2-y1) / (x2-x1)).
[0127] Step S5: using template matching technology, identifying the matching marks at the upper left corner and the lower right corner of the rectified core box image, and recording the pixel coordinates of the matching mark positions;
[0128] When using Canny edge detection to identify the straight part of the convex groove of the target core box, it is necessary to identify the matching mark in the upper left corner of the core box for preliminarily cropping the pixel range of the target core box. At the same time, in the specific implementation process of the embodiment, in order to improve the robustness of the method, it is necessary to determine whether template matching is needed again based on the template matching recognition result, that is, this step should have a certain degree of reusability.
[0129] In this embodiment, the template matching operation required to be performed in this step is implemented using a function in the Python language. When template matching is required, the function only needs to be called and the corresponding parameters passed in to output the template matching result, thereby achieving the reusability of the function and the readability of the program.
[0130] The effect of thresholding in HSV channel is better than that in RGB channel, so HSV channel is used for thresholding. HSV is a color space created by AR Smith in 1978 based on the intuitive characteristics of color, also known as the hexagonal pyramid model.
[0131] The HSV color model refers to a subset of visible light in the H, S, V three-dimensional color space. It contains all colors in a certain color domain. In this model, each color is represented by hue (H), saturation (S), and brightness (V).
[0132] By querying the HSV color system in OpenCV, it can be known that in the HSV channel, the hue (H) corresponding to red has two ranges. In addition, it can be found in the implementation process of the embodiment that the intensity of sunlight will affect the HSV range where the red part of the captured core image is located. Therefore, using a single red color mask for thresholding cannot completely and successfully separate the red in the image.
[0133] The red arrow in the embodiment is identified using HSV color space analysis. Since the red HSV under different sunlight intensities is distributed in two ranges, in order to improve the robustness of the program, two HSV threshold ranges are used to create a red mask and the image is binarized after a logical AND operation, thereby correctly identifying the red arrow in the image under different intensities of sunlight.
[0134] In this embodiment, the HSV ranges of red are (0-10, 43-255, 46-255) and (156-180, 43-255, 46-255). After defining the two red HSV ranges, the cv2.inRange function is used to create corresponding red masks and then a logical AND operation is performed.
[0135] like Figure 7 As shown, the image is binarized using a red mask;
[0136] The cv2.threshold function is used to binarize the image using a red mask. Since the red mask is used for binarization, the image after binarization only contains the red arrow part for template matching, and the image does not contain the blue arrow outline part in the lower right corner of the core box, thereby avoiding misidentification of matching marks and improving the accuracy of template matching.
[0137] The template matching method finds the area with the closest grayscale to the matching template image in the image to be matched as the target area for image matching. The template matching process can be divided into three main steps: selecting a template, calculating similarity, and determining the target position. The template matching method calculates the matching degree between the template image and the overlapping sub-image to be matched from left to right and from top to bottom in the image to be detected. The greater the matching degree, the greater the possibility that the two are similar. This method is simple and easy to identify, does not require a large amount of sample training, nor does it require complex algorithms and a large amount of computing resources. It can quickly and accurately detect targets in the image and provide visual results.
[0138] Template matching is a key step in realizing automatic cutting of core boxes. The matching template is generated and stored before matching. The principle is to compare the matching mark to be identified with the created matching template by feature pairing and compare their similarity. If the similarity between the two binarized shapes is very high, it can be determined that the match is successful in theory.
[0139] Template matching is a method used to search and find the location of a template image in a larger image. This method slides from left to right and from top to bottom on the core box image to be detected. The template starts from the upper left corner of the source image and moves from left to right and from top to bottom in units of the upper left corner pixel of the template. Every time a pixel is reached, an image of the same size as the template is cut out from the source image with this pixel as the upper left corner vertex to perform pixel comparison operations with the template.
[0140] The principle of the template matching algorithm is as follows: let the image of the matching template be T, the width be M, the height be N, the image of the core box to be identified be S, and the similarity between them be D. For example, the formula for calculating the similarity using the square difference is as follows:
[0141]
[0142] The higher the similarity between the matching mark in the core box image to be matched and the matching template, the smaller the D value. If D = 0, it means that the two are completely matched. However, the matching marks on each core box are usually drawn manually by construction drillers and are not completely consistent with the matching template. Therefore, in this embodiment, it is only necessary to find the area with the smallest D in the image to be matched as the template matching result.
[0143] Use cv2.matchTemplate function to match the binarized core box image with the matching template. The first parameter of this function is the image binarized with red mask, the second parameter is the matching template image, and the third parameter is cv2.TM_CCOEFF_NORMED. This parameter is the template matching method using normalized correlation coefficient. The normalized correlation coefficient between the template and the image block is calculated. The larger the normalized correlation coefficient (the smallest corresponding D), the higher the matching degree.
[0144] After template matching, the cv2.minMaxLoc function is used to find the pixel position of the optimal matching result to complete image recognition.
[0145] Finally, you need to determine the position of the red arrow.
[0146] In order to reduce the area occupied by the core boxes, the core boxes are usually placed in a row in the core library. Therefore, when taking pictures of the target core box, it is inevitable to take pictures of the matching marks of the upper core box and the lower core box of the target core box. Therefore, in order to improve the robustness of the method, it is necessary to fully consider the recognition results of various template matching.
[0147] Since the template matching method is used, this method will calculate the matching degree between the template image and the overlapping sub-image to be matched, and return the result with the highest matching degree. The matching marks used in the core box are manual marks. It is very likely that the red arrow in the lower core box is more similar to the matching template. Therefore, the approximate range of the pixel position of the recognition result in space should be preliminarily judged.
[0148] In this embodiment, since the target core box is placed in the middle area of the shooting in the shooting core box image, Figure 5 It can be seen that the target core box red arrow is usually located at the upper part of the image, while the lower core box red arrow is located at the lower part of the image. Therefore, half of the vertical pixels of the image can be used to determine whether the returned template matching result is correct.
[0149] If the red arrow of the target core box is correctly identified, Figure 8 As shown, execute the next step at this time.
[0150] If the red arrow of the lower core box is identified, Fig. 9 As shown in the figure, the lower red arrow vertical coordinate pixel is used as the boundary to crop the lower core box part in the image. At this time, only the upper core box and the target core box are retained, as shown in the figure. Fig.10 As shown, in order to avoid the red arrow of the lower core box being preferentially identified again during the secondary identification, the template matching is performed again.
[0151] Since the red arrow of the target core box has been identified, in order to locate the position of the target core box in the image, it is only necessary to identify the blue arrow.
[0152] The method for identifying the blue arrow of the core box is basically the same as the method for identifying the red arrow and the logic is basically the same, so it will not be repeated. It is only explained whether the template matching needs to be performed again according to the recognition result. It should be noted that the blue HSV range in the embodiment is (100-124, 43-255, 46-255)
[0153] from Figure 5 It can be seen that the upper core box blue arrow is usually located above the target core box red arrow, while the lower core box blue arrow is located below the target core box red arrow. Therefore, by comparing the vertical coordinate pixels of the identified blue arrow with the vertical coordinate pixels of the identified target core box red arrow, it can be determined whether the identified blue arrow is the target core box blue arrow.
[0154] If the blue arrow of the upper core box is identified, Fig.11 As shown, at this time, the upper core box is cropped with the upper core box blue arrow vertical coordinate pixel as the boundary to avoid secondary recognition and prioritize matching the upper core box blue arrow again, and perform template matching again.
[0155] The blue arrow of the upper core box is used as the boundary, and the secondary recognition results after cropping are as follows Fig.12 shown.
[0156] If the blue arrow of the lower core box is identified, Fig.13 As shown, the blue arrow indicates that the target core box was correctly identified.
[0157] Step S6: cropping the rectified core box image according to the recorded pixel coordinates, and finally obtaining a cropped horizontal core box image.
[0158] According to the blue arrow pixel coordinates of the target core box, combined with the red arrow pixel coordinates, the target core box can be cropped;
[0159] When batch automatic processing of core box images is performed, the pixel aspect ratio of the cropped core box images will usually stabilize within a small range. At this time, it is only necessary to output the pixel aspect ratio of each image and arrange them in order. Images with significantly larger or smaller aspect ratios indicate that the image may be interfered by various external factors and thus cannot identify matching marks well.
[0160] In order to improve the efficiency of the verification of the core box automatic processing results by this method, after cropping the core photos, the cropped image pixel aspect ratio and the corresponding picture number are saved synchronously, and the image pixel aspect ratios are arranged in order. At this time, geologists only need to focus on the high and low values of the pixel aspect ratios of each image and the corresponding picture numbers arranged in descending or ascending order.
[0161] In order to facilitate the use of geologists, the Tkinter library is used to create a graphical user interface (GUI), such as Fig.14 As shown (the actual encapsulated exe executable file user interface also includes other implemented functions, and the user buttons shown in the middle column are the operations corresponding to this embodiment).
[0162] Tkinter is Python's standard graphical user interface library, which provides the function of creating windows, buttons, text boxes and other GUI elements. When defining components, the Tkinter library uses the command attribute to bind the corresponding event function, which is simple to use. The front end mainly uses three components, selecting the path of the core image to be processed, setting the save path of the image after automated processing, and running automatic cropping. Among them, running the automatic cropping component encapsulates the above steps S3 to S6; after clicking the button to select the path of the core image to be processed and setting the save path of the image after automated processing, the file dialog box will be called for path selection, and the path display of the front end will be dynamically changed by changing the variable of type StringVar.
[0163] When there are many third-party libraries imported into the code, using the pyinstaller command in the command prompt (CMD) to package Python files into exe files usually results in a mismatch between the Pyinstaller version and the Python version or the third-party library version, making it difficult to package the Python code into an exe file. Using Anaconda for packaging can solve the compatibility matching problems of various function libraries, and the exe files generated in the Anaconda virtual environment are relatively
[0164] The above specific implementations do not constitute a limitation on the protection scope of the present disclosure. It should be understood by those skilled in the art that various modifications, combinations, sub-combinations and substitutions can be made according to design requirements and other factors. Any modification, equivalent substitution and improvement made within the spirit and principle of the present disclosure shall be included in the protection scope of the present disclosure.
Claims
1. A method for automatically processing core photos based on template matching and Hough transform, characterized in that: The following steps are involved: Step S1: Determine the color and style of the matching mark and generate a matching template; Step S2: setting matching marks at the upper left corner and the lower right corner of the core box respectively, and obtaining an image of the core box with the matching marks; Step S3: using the matching template to identify the matching mark in the image, setting the pixel threshold, preliminarily cropping the image to obtain the target core box image in the middle part of the image, converting the image color space from RGB to GRAY color space through color space transformation, and then processing the core box image using the Canny edge detection algorithm to generate the contour straight line of the target core box convex groove in the image; Step S4: using Hough transform, detecting the straight line that meets the requirements of the target core box in the image, and the polar coordinate analytical expression of the straight line; converting the polar coordinate analytical expression of the straight line into a rectangular coordinate analytical expression, and calculating the slope of the straight line to obtain the deflection angle of the straight line; sequentially obtaining the deflection angles of all convex groove straight lines of the target core box in the preliminary cropped image, calculating the average value of the deflection angles, obtaining the deflection angle of the core box in the image, and rotating and straightening the image to obtain the straightened image of the core box to be cropped; Step S5: using the matching template, identifying the matching marks at the upper left corner and the lower right corner of the rectified core box image, and recording the pixel coordinates of the matching mark positions; Step S6: cropping the rectified core box image according to the recorded pixel coordinates to obtain a processed core box image; The matching mark at the upper left corner and the matching mark at the lower right corner of the core box are set in different colors, and the matching marks are drawn with waterproof material; In step S3, the core box image is processed using the Canny edge detection algorithm, specifically including: Grayscale the image to obtain a grayscale image; Perform Gaussian filtering on the grayscale image to obtain a filtered image; The Sobel operator is used to calculate the gradient magnitude and direction of the filtered image, and the non-maximum value of the gradient magnitude is suppressed to compress the edge bandwidth; The compressed edge bandwidth is binarized using a hysteresis threshold to obtain a binarized image; Performing a connection operation on the binary image to connect the disconnected edges to obtain a final edge image, wherein the edge image contains straight line contour information of each row of convex grooves in the core box; In step S3, it specifically includes: Use a Gaussian filter to smooth the image, where the Gaussian function H(x,y) is: G(x,y)=f(x,y)*H(x,y) Where f(x,y) is the image function, G(x,y) is the Gaussian function, and σ is the standard deviation; Use the finite difference of the first-order partial derivative to calculate the magnitude and direction of the gradient, and use the first-order difference convolution template: Amplitude: direction: Use non-maximum suppression technology to process the gradient amplitude. First, set the gradient value of the central pixel of each point area to M, and then compare M with the gradient values of the two pixels on the gradient line of the point. If these two gradient values are smaller than M, set M = 0. The edge is detected and connected by the dual threshold algorithm, and the image is processed using two thresholds of different sizes, T1 and T2, to obtain two threshold edge images N1[i, j] and N2[i, j], where T2>T1; The double threshold method is used to connect the edges into contours in the N2[i, j] image. When the endpoint of the contour is reached, the edge connected to the contour is searched at the neighboring point position of N1[i, j]. This operation is repeated, and the dual threshold algorithm continuously collects the edges of the image in the image N2[i, j] until the image N2[i, j] is connected.
2. The method for automatically processing core photos based on template matching and Hough transform according to claim 1, characterized in that: With a waterproof marker pen, the match mark in the upper left corner of the core box was drawn as a red arrow, and the match mark in the lower right corner was drawn as a blue arrow.
3. The method for automatically processing core photos based on template matching and Hough transform according to claim 2, characterized in that: In step S1, the specific steps of generating a matching template include: Setting matching marks on the core box and obtaining an image of the core box containing the template; Use the np.array function to create HSV range arrays corresponding to red and blue respectively, and then use the cv2.inRange function to create red HSV mask and blue HSV mask respectively; The cv2.threshold function is used to perform a color mask-based binarization process on the template core box image, and the arrow part in the binary image is generated as a matching template.
4. The method for automatically processing core photos based on template matching and Hough transform according to claim 3, characterized in that: In step S5, it specifically includes: The red mask is used to binarize the rectified core box image to obtain a binary image 1. The binary image 1 is matched with the matching template. The image of the matching template is T, the width is M, the height is N, the binary image 1 is S, and the similarity between the matching template and the binary image 1 is D. The formula for calculating the similarity using the square difference is as follows: The higher the similarity between the binary image 1 and the matching template, the smaller the D value. If D = 0, it means that the two are completely matched. The area with the smallest D in the binary image 1 is selected as the template matching result, and the position of the red arrow is identified; The rectified core box image is binarized using the blue mask to obtain a binary image 2; the binary image 2 is template matched with the matching template to identify the position of the blue arrow.
Citation Information
Patent Citations
Two-dimensional code image correction method and correction device
CN110309687A
Intelligent detection method based on computer vision
CN116309516A
Distribution network line information extraction and automatic mapping method and system
CN117390802A