A method for detecting pearlite lamellar spacing using Python
Patent Information
- Application Number
- CN202411366647.6
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2024-09-29
- Publication Date
- 2025-02-07
- Estimated Expiration
- 2044-09-29
AI Technical Summary
[0005]有鉴于此,本发明的目的在于提出一种利用python检测珠光体片层间距的方法,以解决现有方法操作复杂,效率较低、测量结果受主观因素影响大的问题
[0029]本发明提供了一种快速计算珠光体片层间距及其数量的方式,相较于传统的人工统计,更加省时省力且更准确;
Smart Images

Figure CN118887273B_ABST
Abstract
Description
Technical Field
[0001] The invention relates to the technical field of image processing, and in particular to a method for detecting pearlite lamellar spacing by using Python. Background Art
[0002] Pearlite is an important microstructure in steel materials, which is composed of wide strips of ferrite and narrow strips of cementite arranged in parallel. Its interlamellar spacing consists of a layer of ferrite and a layer of cementite, and the interlamellar spacing has an important influence on the deep drawing performance of the material. In the study of the troostitization of related wires, it is necessary to identify its interlamellar spacing in order to analyze the manufacturing process. At present, this identification process depends on the inspection personnel, and the measurement method adopts the average value after vertically drawing a certain number of interlamellar spacings. There are problems such as the offset of the starting and end points in the drawing, and the offset of the drawing line and the orientation of ferrite or pearlite. The operation is relatively complicated, the efficiency is low, and the measurement results are greatly affected by subjective factors.
[0003] Deep learning has always been one of the most popular research topics in computer technology, especially in recent years, the development of deep learning has been very rapid. Studies have shown that deep learning has surpassed humans in certain specific image recognition capabilities, because deep learning models can learn advanced features from ultra-high dimensional data, have strong noise resistance, and generalization capabilities. Deep learning is not only used in image recognition, but also in many fields such as image processing, image generation, machine translation, object detection, image segmentation, and robotics.
[0004] Although deep learning has achieved great success, due to the high complexity of subclasses, insufficient feature differences, and the difficulty in obtaining data sources, there are very few studies combining deep learning with the steel micro-industry, and only a few references can be found. Therefore, developing an automated and efficient pearlite lamellar detection method has important application value. Summary of the invention
[0005] In view of this, the purpose of the present invention is to propose a method for detecting pearlite lamellar spacing using python to solve the problems of complex operation, low efficiency and measurement results being greatly affected by subjective factors in the existing methods.
[0006] Based on the above purpose, the present invention provides a method for detecting pearlite lamellar spacing using Python, comprising:
[0007] S1, preprocessing the collected pearlite image;
[0008] S2, searching for the contour of the pearlite object in the preprocessed pearlite image to obtain contour boundary point information;
[0009] S3. According to the information of the contour boundary points, the orientation of the pearlite is determined, and the pearlite lamella spacing is calculated based on the orientation.
[0010] Preferably, the pretreatment comprises:
[0011] S101, identifying the length of the ruler;
[0012] S102, converting the collected image into a grayscale image;
[0013] S103 , binarizing the grayscale image according to a set threshold value to obtain a binarized image.
[0014] Preferably, searching for the contour comprises:
[0015] Find connected areas in the binary image, extract the edges of these connected areas, and draw the extracted edges to complete the visualization of the recognition results.
[0016] Preferably, calculating the spacing of the contours comprises:
[0017] S301, using the minimum circumscribed rectangle method, find a minimum rotatable circumscribed matrix that can surround the entire contour, and the side formed by the angle formed by the short side of the circumscribed rectangle and the x-axis is perpendicular to the pearlite;
[0018] S302, using the method of finding the largest inscribed circle, find a largest inscribed circle of the contour, and twice the radius of the inscribed circle is the corresponding pearlite lamellar spacing.
[0019] Preferably, the method further comprises:
[0020] S4. Use the PIL tool to display the measured pearlite lamellar spacing data as line segments on the binary image, and number the pearlite on the image.
[0021] Preferably, finding the contour of the pearlite object comprises:
[0022] S201, scanning the image line by line, determining the boundary in the image according to the change of pixel values, and connecting the boundary points into a closed state by detecting the connectivity of the region in the image, thereby obtaining the outline of half of the pearlite;
[0023] S202, invert the color of the image, and re-execute the steps in S201 to obtain the outline of the other half of the pearlite;
[0024] S203, using a contour merging method to reorder the merged contour points to obtain a complete pearlite contour.
[0025] Preferably, the merged profile comprises:
[0026] Using the polar coordinate system, the center of all points of the contour is obtained as the origin of the polar coordinate system, and the polar coordinates of each contour point are compared in turn, sorted in a clockwise direction, and the contours of the two halves are traversed;
[0027] If the distance between two points is less than 1, the two adjacent points are deleted from the contour.
[0028] Beneficial effects of the present invention:
[0029] The present invention provides a method for quickly calculating the pearlite lamellar spacing and its number, which is more time-saving, labor-saving and more accurate than traditional manual statistics.
[0030] The present invention uses the method of finding the radius of the inscribed circle to find the width of the irregular geometric body;
[0031] The method of automatically calculating the number of pearlite lamellae and their spacing using a computer of the present invention can improve work efficiency and increase the universality of detection because the computer has fast calculation speed, high accuracy, no subjective assumptions, and low cost. BRIEF DESCRIPTION OF THE DRAWINGS
[0032] In order to more clearly illustrate the technical solutions in the present invention or the prior art, the drawings required for use in the embodiments or the description of the prior art will be briefly introduced below. Obviously, the drawings in the following description are only for the present invention. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying creative work.
[0033] Figure 1 This is a flow chart of a method for detecting pearlite lamellar spacing using Python according to an embodiment of the present invention;
[0034] Figure 2 Schematic diagram of an original image and a binarized image according to an embodiment of the present invention;
[0035] Figure 3 The binarized picture and the picture with outlines depicted in the embodiment of the present invention;
[0036] Figure 4 A picture depicting numbering and line segment lengths for an embodiment of the present invention;
[0037] Figure 5 is the original image of Example 1 of the present invention;
[0038] Figure 6 is the binarized image of Embodiment 1 of the present invention;
[0039] Figure 7 The manually selected picture in Embodiment 1 of the present invention;
[0040] Figure 8 This is a picture of the output result of Example 1 of the present invention;
[0041] Fig. 9 It is the original image of Example 2 of the present invention;
[0042] Fig.10 is a binarized image according to Embodiment 2 of the present invention;
[0043] Fig.11 The manually selected picture of Embodiment 2 of the present invention;
[0044] Fig.12 This is a picture of the output result of Example 2 of the present invention. DETAILED DESCRIPTION
[0045] In order to make the objectives, technical solutions and advantages of the present invention more clearly understood, the present invention is further described in detail below in conjunction with specific embodiments.
[0046] It should be noted that, unless otherwise defined, the technical terms or scientific terms used in the present invention should be understood by people with ordinary skills in the field to which the present invention belongs. The "first", "second" and similar words used in the present invention do not indicate any order, quantity or importance, but are only used to distinguish different components. "Include" or "comprise" and similar words mean that the elements or objects appearing before the word include the elements or objects listed after the word and their equivalents, without excluding other elements or objects. "Connect" or "connected" and similar words are not limited to physical or mechanical connections, but may include electrical connections, whether direct or indirect. "Up", "down", "left", "right" and the like are only used to indicate relative positional relationships. When the absolute position of the described object changes, the relative positional relationship may also change accordingly.
[0047] like Figure 1 As shown, the embodiment of this specification provides a method for detecting pearlite interlamellar spacing using Python, comprising the following steps:
[0048] S1. First, we need to collect appropriate pearlite images and preprocess them, including identifying the scale length, graying the image, binarization, and removing noise. Binarization can separate pearlite from the background, making subsequent analysis more accurate.
[0049] Identifying the length of the ruler is the process of reading the line segment representing the length in the image and the length number it represents into the machine for storage. There is a line segment and a number representing the length of the line segment at a fixed position in the lower left corner of each image. The program will identify the number of pixels and the length number of the line segment at the beginning to determine the actual length represented by each pixel in the image, in preparation for the subsequent measurement. Grayscale is the process of converting a color image into a grayscale image. In a grayscale image, the value of each pixel represents the brightness or grayscale level of the pixel, and no longer contains color information. The purpose of binarization is to convert the image into a form with only two values, usually 0 and 255 (or 0 and 1). After binarization, the pixels in the image are either marked as white (usually represented by the maximum value, such as 255) or marked as black (usually represented by the minimum value, such as 0), depending on whether the grayscale value of the pixel exceeds the set threshold. We need to set the threshold according to the specific situation of the image (such as whether the boundary between pearlite and the background is clear, and whether the color difference is obvious) to obtain a suitable binary image. After obtaining the binary image, we need to deal with the noise. The purpose of noise removal is to eliminate unnecessary, random, independent and meaningless pixels in the image. The original image and the image after binarization are as follows: Figure 2 shown.
[0050] S2. After obtaining the binary image, search for the contour of the pearlite object in the binary image, use different contours to distinguish the position and size information of different pearlites, and save the contour boundary point information. The method of finding the contour is to find the connected areas in the binary image and extract the edges of these areas. Use cv2.drawContours to draw the detected edges on the image to complete the visualization of the recognition results. The contour search can be implemented using the findContours interface of opencv. The binary image and the image with the contour are as follows: Figure 3 shown.
[0051] S3. After finding the contour, its spacing needs to be calculated, and the approximate orientation of the pearlite needs to be determined. The method used is the minimum circumscribed rectangle method, that is, finding a minimum rotatable circumscribed rectangle that can surround the entire contour. Since pearlite is generally longer than it is wide, the angle formed by the short side of the circumscribed rectangle and the x-axis is perpendicular to the pearlite.
[0052] The pearlite interlamellar spacing is calculated based on the orientation. The method used is to find the largest inscribed circle, that is, to find the largest inscribed circle of the contour. The twice of the radius of the inscribed circle is the pearlite interlamellar spacing. Here you can also call the opencv interface, cv2.pointPolygonTest function.
[0053] S4. In order to visually see the measured area and quantity, you need to use the PIL tool to display the measured data on the graph. Use the draw.line function to draw line segments on the image. At the same time, in order to know the position of pearlite on the image, you can use draw.txt to number it on the image. At the same time, use the print function to print out the length in the console. The picture depicting the number and line segment length is as follows Figure 4 shown.
[0054] As an implementation method, the specific implementation method of identifying the ruler length in the above method is as follows:
[0055] The template matching algorithm is used to identify numbers. Since the position, size and shape of the numbers in the output image are consistent, and the image size and size are also consistent, it is only necessary to take a screenshot of the possible output numbers, convert them into a grayscale image, convert them into base64 encoding and embed them in the program code, and then perform template matching with the numbers in the corresponding positions in the input image. The program will compare the ruler numbers in the input image with the numbers stored in the program in base64 encoding, and output a credibility value for each number. The higher the credibility, the more similar the two numbers are. Find the number with the highest credibility and return it to know the number representing the length of the ruler segment.
[0056] The identification ruler is based on the pixel method. It counts from left to right when it encounters the first black pixel, and stops counting when it encounters a white pixel and then a black pixel, to identify how many pixels there are between line segments.
[0057] As an implementation method, in step S2, the specific implementation method of searching the pearlite contour is as follows:
[0058] Using the findContours interface in opencv, this method uses a contour search algorithm based on image scan lines and connectivity.
[0059] The main idea of this algorithm is to scan the image line by line, determine the boundaries in the image according to the changes in pixel values, and connect the boundary points into closed contours by detecting the connectivity of the regions in the image.
[0060] It will return the coordinate information of the contour points. After obtaining the coordinate information of the contour, it will remove extremely small contours caused by noise or other reasons.
[0061] In this way, half of the pearlite contour is found. Then, the color of the image is reversed and the other half of the contour is found again. Then, the contour merging method is used to reorder the merged contour points according to the front and back of the centroid, and the complete pearlite contour is obtained.
[0062] The idea of contour merging is to traverse the points in two contours. If the Euclidean distance between two points is less than 1, the two adjacent points are deleted from the contour. The idea of contour traversal is to use the polar coordinate system, first find the center of all points as the origin of the polar coordinate system, and then compare the polar coordinates of each contour point in turn and sort them in a clockwise direction.
[0063] As an implementation manner, in step S2, the specific method of drawing the outline on the picture for visualization is:
[0064] Draw a contour with a width of 2 on the picture according to the contour information;
[0065] The open source minAreaRect method in OpenCV returns the center coordinates of the minimum circumscribed rectangle, the length and width information, and the angle formed by the counterclockwise rotation of the x-axis and the first side of the rectangle. According to the length and width information of the rectangle returned by minAreaRect, if the first parameter is greater than the second parameter, it means that the angle is the angle formed with the long side, and it needs to be increased by 90 degrees. The result is the angle between the short side of the rectangle and the x-axis.
[0066] Then use the pointPolygonTest algorithm to calculate, output the value to the console and number each pearlite in the picture.
[0067] This specification provides two specific examples to verify the above method, wherein Example 1 is as follows Figure 5 As shown in the figure, it is a metal microstructure diagram, which is first converted into Figure 6 The binarized image shown in the figure is then manually judged to identify the area that needs to be measured, and other cropping tools are used to crop the area that needs to be measured, such as Figure 7 As shown, after running S2-S4 of the above method, the recognized image and slice spacing can be output, such as Figure 8 shown.
[0068] The original picture of Example 2 is as follows Fig. 9 As shown in FIG. 1 , it is a metal microstructure diagram, which is converted into a binary image by S1 in the above method, such as Fig.10 As shown in the figure, the area to be measured is manually judged, and other cropping tools are used to crop the area to be measured, such as Fig.11 As shown, after running S2-S4 of the above method, the recognized image and slice spacing can be output, such as Fig.12 shown.
[0069] The data of Example 1 and Example 2 were statistically tested, and the measured results were compared with the manual measurement results as shown in Tables 1 and 2 below, respectively.
[0070] Table 1 Comparison of measurement results of Example 1
[0071]
[0072] Table 2 Comparison of measurement results of Example 1
[0073]
[0074] From the comparison results, the measurement results of this method are close to those of manual measurement, which shows that this method is accurate and effective.
[0075] Those skilled in the art should understand that the discussion of any of the above embodiments is only exemplary and is not intended to imply that the scope of the present invention is limited to these examples; under the concept of the present invention, the technical features in the above embodiments or different embodiments can also be combined, the steps can be implemented in any order, and there are many other changes in different aspects of the present invention as described above, which are not provided in detail for the sake of simplicity. Any omissions, modifications, equivalent substitutions, improvements, etc. made within the spirit and principles of the present invention should be included in the scope of protection of the present invention.
Claims
1. A method for detecting pearlite interlamellar spacing using Python, characterized in that: The method comprises: S1, preprocessing the collected pearlite image; S2, searching for the contour of the pearlite object in the preprocessed pearlite image to obtain contour boundary point information; S3, judging the orientation of pearlite according to the information of the contour boundary points, and calculating the pearlite lamellar spacing according to the orientation; The pearlite lamellar spacing obtained by calculation includes: S301, using the minimum circumscribed rectangle method, finding a minimum rotatable circumscribed matrix that can surround the entire contour, wherein the angle formed by the short side of the circumscribed rectangle and the x-axis is perpendicular to the pearlite; S302, using the method of finding the largest inscribed circle to find a largest inscribed circle of the contour, and twice the radius of the inscribed circle is the corresponding pearlite lamellar spacing.
2. The method for detecting pearlite interlamellar spacing using Python according to claim 1, characterized in that: The pre-processing comprises: S101, identifying the length of the ruler; S102, converting the collected image into a grayscale image; S103 , binarizing the grayscale image according to a set threshold value to obtain a binarized image.
3. The method for detecting pearlite interlamellar spacing using Python according to claim 1, characterized in that: The step of finding the contour of the pearlite object comprises: Find connected areas in the binary image, extract the edges of these connected areas, and draw the extracted edges to complete the visualization of the recognition results.
4. The method for detecting pearlite interlamellar spacing using Python according to claim 1, characterized in that: The method further comprises: S4. Use the PIL tool to display the measured pearlite lamellar spacing data as line segments on the binary image, and number the pearlite on the image.
5. The method for detecting pearlite interlamellar spacing using Python according to claim 1, characterized in that: The step of finding the contour of the pearlite object comprises: S201, scanning the image line by line, determining the boundary in the image according to the change of pixel values, and connecting the boundary points into a closed state by detecting the connectivity of the region in the image, thereby obtaining the outline of half of the pearlite; S202, invert the color of the image, and re-execute the steps in S201 to obtain the outline of the other half of the pearlite; S203, using a contour merging method to reorder the merged contour points to obtain a complete pearlite contour.
6. The method for detecting pearlite interlamellar spacing using Python according to claim 5, characterized in that: The contour merging method comprises: Using the polar coordinate system, the center of all points of the contour is obtained as the origin of the polar coordinate system, and the polar coordinates of each contour point are compared in turn, sorted in a clockwise direction, and the contours of the two halves are traversed; If the distance between two points is less than 1, the two adjacent points are deleted from the contour.
Citation Information
Patent Citations
Method for calculating pearlite lamellar spacing in steel based on AVIZO software
CN117314998A