A standard target counting method based on machine vision
Through a standard object counting method based on machine vision, the medicine box image is corrected and linearly detected, which solves the problem of difficult counting superimposed medicine boxes in the prior art, achieves high-accuracy counting results, and saves computing power.
Patent Information
- Application Number
- CN202111457580.3
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2021-12-02
- Publication Date
- 2025-03-14
- Estimated Expiration
- 2041-12-02
AI Technical Summary
In the prior art, when identifying a medicine box, it is difficult to accurately count multiple superimposed medicine boxes, and the size data of the medicine box is required to be stored in advance, so the medicine box that has not been pre-stored data cannot be processed.
The standard object counting method based on machine vision is adopted to calculate the number of standard objects by correcting the input image and linear detection. The traditional machine learning method is not used to save computing power.
Accurate counting of superimposed medicine boxes is achieved, the accuracy of counting results is improved, and the drug box size data is not required in advance, saving computing power.
Smart Images

Figure CN114092468B_ABST
Abstract
Description
Technical Field
[0001] The invention relates to a standard target counting method based on machine vision. Background Art
[0002] At present, when identifying medicine boxes, multiple medicine boxes may be stacked and pass through the identification device, and the number of stacked medicine boxes needs to be counted. The current traditional counting method generally requires the size data of the medicine boxes to be pre-stored in the system, and a machine learning method is needed to call the size of the corresponding medicine box for quantity calculation when multiple medicine boxes overlap. A large amount of data support is required, and counting cannot be completed without the pre-stored medicine box size. Summary of the invention
[0003] The purpose of the present invention is to overcome the defects of the prior art and provide a standard target counting method based on machine vision. Based on the standard target extraction method, the input image is first corrected, and then the number of standard targets is calculated through image basic processing using straight line detection means. The standard target counting method of the present invention does not use traditional machine learning methods, saves computing power, and the counting results are highly accurate.
[0004] The technical solution to achieve the above purpose is: a standard target counting method based on machine vision, comprising the following steps:
[0005] S1, image preprocessing step: the input medicine box image is preprocessed in four steps: first, the medicine box image is converted into a grayscale image; then, the grayscale image is Gaussian filtered to extract image features while filtering noise points; then, the standard target extraction method is used to correct the medicine box image after Gaussian filtering; finally, the size of the corrected standard target is adjusted;
[0006] S2, line detection step: use the Sobel operator to calculate the image gradients in the horizontal and vertical directions respectively and normalize them, and then perform Canny filtering to remove small noise points in the photo; then use the opening and closing operation with a large number of iterations to make the straight lines with high confidence in the image thicker and longer until they run through the entire medicine box image, while the straight lines with low confidence will not run through or will be directly discarded; finally, perform a bitwise OR operation on the result images obtained in the horizontal and vertical directions to synthesize one image;
[0007] S3, quantity calculation step: find the boundary where the pixels change from dark to light or from light to dark in the medicine box image after the straight line detection step, calculate the number of straight lines, and then obtain the number of standard target medicine boxes; the straight line number calculation is divided into horizontal and vertical straight line number calculation, and in each direction, the minimum value of the retained number is calculated multiple times at different widths or heights;
[0008] Select the threshold as i and define three lists to store three types of points: the first type of point has a gray value greater than i and a gray value less than i-1, which is understood as a point from black to white; the second type of point has a gray value less than i-1 and a gray value greater than i, which is understood as a point from white to black; the third type of point is used to calculate and store the midpoints of the first two types of points after finding them; set the counter count to record the number of straight lines detected, and the count value increases every time a point from black to white is scanned;
[0009] After scanning all pixels, the midpoint is calculated and stored according to the pixel position from black to white and from white to black; it is judged whether there is a missed line at this time. If the count is less than 2, it means that a missed line is detected. At this time, the number of straight lines is set to 2; if there is more than one midpoint, the distance between the two midpoints is calculated to calculate the measured total width of the standard target. The total width of the standard target is subtracted from the actual width. If the absolute value is greater than the threshold, it means that a missed line is detected. At this time, the number of straight lines is automatically increased to make up for the missed line.
[0010] In the above-mentioned standard target counting method based on machine vision, step S1 specifically includes the following process:
[0011] S11, converting the medicine box image from BGR to grayscale;
[0012] S12, setting the Gaussian standard deviation to 1, calculating the filter size according to the standard deviation, then generating a Gaussian filter kernel and normalizing it, and finally using the filter kernel to convolve along the x and y axes respectively, performing Gaussian filtering on the grayscale image, and extracting image features while filtering noise points through Gaussian filtering;
[0013] S13, correcting the medicine box image using a standard target extraction method, specifically including the following process:
[0014] S131, Canny edge detection step: gradient size and direction calculation, non-maximum suppression and double threshold screening are performed on the grayscale image processed by Gaussian filtering in turn to accurately depict all edges in the medicine box image;
[0015] S132, opening and closing operation step: using an opening and closing operation with a large number of iterations to process the edge of the medicine box image obtained in the Canny edge detection step, first using a closing operation with a large number of iterations to eliminate the texture inside the medicine box image, converting the grayscale image into a pure color binary image through corrosion and expansion, to avoid the problem of cutting the medicine box into two due to incorrect recognition of the texture inside the medicine box; then using an opening operation and a median filter in turn to remove noise and burrs generated during the closing operation;
[0016] S133, find the contour with the largest area and fit it with the minimum enclosing rectangle: call OpenCV's findContours interface to track all contours in the image, calculate the contour area, find the contour with the largest area and find its minimum enclosing rectangle. Return the four vertices of the minimum enclosing rectangle, rearrange them according to the coordinates, and connect the rectangles with red straight lines to find the length and width of the rectangle in preparation for the subsequent perspective transformation;
[0017] S134, proportional perspective transformation step: the aspect ratio of the standard target is calculated by the length and width of the minimum circumscribed rectangle, the side with the larger value is set to 512 pixels, and the other side is scaled proportionally, and the rectangular upper surface of the standard target is extracted without deformation, so as to obtain a corrected medicine box image;
[0018] S14, the minimum circumscribed rectangle is transformed into a square, and the corrected medicine box image is widened by 1 / 2 of the rectangle width in the upper and lower directions, and by 1 / 2 of the rectangle length in the left and right directions, so that the minimum circumscribed rectangle of the corrected part to be processed is located in the middle of the widened medicine box image, and the length and width of the minimum circumscribed rectangle are correspondingly half of the length and width of the widened medicine box image.
[0019] The above-mentioned standard target counting method based on machine vision, S131, Canny edge detection step, specifically includes the following process:
[0020] First, the Sobel operator is used to calculate the size and direction of the pixel gradient in the horizontal and numerical directions of the grayscale image after Gaussian filtering. In the two-dimensional image, the Sobel kernel in the x direction and y direction is used for filtering respectively, and then the square root of their square values is added to get the size of the pixel gradient; the larger the gradient value, the more drastic the grayscale change of the image here, and it is very likely that this is the edge to be found; then the direction of the pixel gradient is calculated;
[0021] Then, the local maximum value of the pixel is selected through non-maximum suppression. First, it is determined whether the gradient direction of the pixel is closer to the x direction or the y direction. Then, the weight is calculated by linear interpolation to replace the sub-pixel, and the local maximum value of the gradient is obtained. The grayscale value of the pixel that is not the local maximum value is set to 0.
[0022] Finally, double threshold screening is performed, setting high and low thresholds. If the gradient is higher than the high threshold, it is marked as a strong edge and retained directly; if the gradient is lower than the low threshold, it is considered a pseudo edge and discarded directly; if the gradient is between the two thresholds, the choice is made by judging whether it is connected to the strong edge. If it is connected, it is marked as a weak edge point and retained, otherwise it is discarded; thus accurately depicting all the edges in the medicine box image.
[0023] In the above-mentioned standard target counting method based on machine vision, in step S4, when calculating the number of vertical straight lines, it is assumed that the width and height of the preprocessed square image are w. According to the rules for adjusting the size of the corrected image, the position of the standard target is between 1 / 4w and 3 / 4w in width and 1 / 4w and 3 / 4w in height. A height is selected, and the pixels are traversed from 1 / 8w in width to 7 / 8w in width to find the boundary where the pixels change from dark to light or from light to dark, and the number of vertical straight lines at the height is calculated.
[0024] In the above-mentioned standard target counting method based on machine vision, in step S4, the number of straight lines is calculated at 1 / 4 height and 3 / 4 height respectively and the minimum value is taken.
[0025] The standard target counting method based on machine vision of the present invention is based on the standard target extraction method. The input image is first corrected, and then the number of standard targets is calculated through image basic processing using straight line detection means. The standard target counting method of the present invention does not use traditional machine learning methods, saves computing power, and the counting results are highly accurate. BRIEF DESCRIPTION OF THE DRAWINGS
[0026] Figure 1 It is a flow chart of the standard target counting method based on machine vision of the present invention;
[0027] Figure 2 is a flow chart of the straight line detection steps;
[0028] Figure 3 This is the result image of a sample after straight line detection;
[0029] Figure 4 Schematic diagram for the definition of three types of points;
[0030] Figure 5 A flowchart of the steps for calculating the quantity;
[0031] Figure 6 This is a schematic diagram of the structure of the main panel of the standard target quantity calculation section. DETAILED DESCRIPTION
[0032] In order to enable those skilled in the art to better understand the technical solution of the present invention, the specific implementation methods thereof are described in detail below in conjunction with the accompanying drawings:
[0033] See also Figures 1 to 5 , an embodiment of the present invention, a standard target counting method based on machine vision, comprising the following steps:
[0034] S1, image preprocessing step: the input medicine box image is preprocessed in four steps: first, the medicine box image is converted into a grayscale image; then, the grayscale image is Gaussian filtered to extract image features while filtering noise points; then, the standard target extraction method is used to correct the medicine box image after Gaussian filtering; finally, the size of the corrected standard target is adjusted;
[0035] S2, line detection step: use the Sobel operator to calculate the image gradients in the horizontal and vertical directions respectively and normalize them, and then perform Canny filtering to remove small noise points in the photo; then use the opening and closing operation with a large number of iterations to make the straight lines with high confidence in the image thicker and longer until they run through the entire medicine box image, while the straight lines with low confidence will not run through or will be directly discarded; finally, perform a bitwise OR operation on the result images obtained in the horizontal and vertical directions to synthesize one image;
[0036] S3, quantity calculation step: find the boundary where the pixels change from dark to light or from light to dark in the medicine box image after the straight line detection step, calculate the number of straight lines, and then obtain the number of standard target medicine boxes; the straight line number calculation is divided into horizontal and vertical straight line number calculation, and in each direction, the minimum value of the retained number is calculated multiple times at different widths or heights;
[0037] Select the threshold as i and define three lists to store three types of points: the first type of point has a gray value greater than i and a gray value less than i-1, which is understood as a point from black to white; the second type of point has a gray value less than i-1 and a gray value greater than i, which is understood as a point from white to black; the third type of point is used to calculate and store the midpoints of the first two types of points after finding them; set the counter count to record the number of straight lines detected, and the count value increases every time a point from black to white is scanned;
[0038] After scanning all pixels, the midpoint is calculated and stored according to the pixel position from black to white and from white to black; it is judged whether there is a missed line at this time. If the count is less than 2, it means that a missed line is detected. At this time, the number of straight lines is set to 2; if there is more than one midpoint, the distance between the two midpoints is calculated to calculate the measured total width of the standard target. The total width of the standard target is subtracted from the actual width. If the absolute value is greater than the threshold, it means that a missed line is detected. At this time, the number of straight lines is automatically increased to make up for the missed line.
[0039] S1 image preprocessing steps, specifically including the following processes:
[0040] S11, convert the medicine box image from BGR to grayscale, the conversion formula is;
[0041] Gray(i,j)=[R(i,j)+G(i,j)+B(i,j)] / 3
[0042] Gray(i,j)=0.299*R(i,j)+0.587*G(i,j)+0.114*B(i,j)
[0043] S12, the calculation formula for each element of a Gaussian filter of size (2k+1)*(2k+1) is as follows:
[0044] H[i,j]=(1 / (2*pi*sigma**2))*exp(-1 / 2*sigma**2((ik-1)**2+(jk-1)**2))
[0045] Set the Gaussian standard deviation to 1, calculate the filter size based on the standard deviation, then generate a Gaussian filter kernel and normalize it, and finally use the filter kernel to convolve along the x and y axes to perform Gaussian filtering on the grayscale image. Gaussian filtering can filter out noise points and extract image features at the same time;
[0046] S13, correcting the medicine box image using a standard target extraction method, specifically including the following process:
[0047] S131, Canny edge detection steps: Canny edge detection consists of four parts: Gaussian filtering, gradient size and direction calculation, non-maximum suppression and double threshold screening. The grayscale image processed by Gaussian filtering is sequentially subjected to gradient size and direction calculation, non-maximum suppression and double threshold screening to accurately depict all edges in the medicine box image; specifically, the following processes are included:
[0048] First, the Sobel operator is used to calculate the size and direction of the pixel gradient in the horizontal and numerical directions in the grayscale image processed by Gaussian filtering. In the two-dimensional image, Sobel kernels in the x and y directions are used for filtering respectively, and then their square values are added and the square root is taken to get the size of the pixel gradient. The larger the gradient value, the more drastic the grayscale change of the image here, and this is most likely the edge to be found. Then the direction of the pixel gradient is calculated. According to the size of the input image, four blank images of equal size are established, which are used to save the gradient value, gradient amplitude and gradient direction in the x and y directions respectively.
[0049] Then, the local maximum value of the pixel is selected through non-maximum suppression. First, it is determined whether the gradient direction of the pixel is closer to the x direction or the y direction. Then, the weight is calculated by linear interpolation to replace the sub-pixel, and the local maximum value of the gradient is obtained. The grayscale value of the pixel that is not the local maximum value is set to 0.
[0050] Finally, double threshold screening is performed, setting high and low thresholds. If the gradient is higher than the high threshold, it is marked as a strong edge and retained directly; if the gradient is lower than the low threshold, it is considered a pseudo edge and discarded directly; if the gradient is between the two thresholds, the choice is made by judging whether it is connected to the strong edge. If it is connected, it is marked as a weak edge point and retained, otherwise it is discarded; thus accurately depicting all the edges in the medicine box image.
[0051] S132, opening and closing operation step: use an opening and closing operation with a large number of iterations to process the edge of the medicine box image obtained in the Canny edge detection step. First, use a closing operation with a large number of iterations to eliminate the texture inside the medicine box image. For example, use a closing operation with 70 iterations to eliminate the texture inside the medicine box image. Convert the grayscale image into a pure color binary image through corrosion and expansion to avoid the problem of cutting the medicine box into two due to the wrong recognition of the internal texture of the medicine box; then use the opening operation and median filtering in turn to remove the noise and burrs generated in the closing operation process; the use of expansion and corrosion operations with a large number of iterations in the opening and closing operation to process the image has achieved better results. The closing operation can effectively eliminate the internal texture of the standard target and solve the problem of misidentifying the standard target as multiple rectangles due to the texture; the opening operation and median filtering remove the burrs and small noise generated in the closing operation process.
[0052] S133, find the contour with the largest area and fit it with the minimum enclosing rectangle: call OpenCV's findContours interface to track all contours in the image, calculate the contour area, find the contour with the largest area and find its minimum enclosing rectangle. Return the four vertices of the minimum enclosing rectangle, rearrange them according to the coordinates, and connect the rectangles with red straight lines to find the length and width of the rectangle in preparation for the subsequent perspective transformation;
[0053] S134, proportional perspective transformation step: the aspect ratio of the standard target is calculated by the length and width of the minimum circumscribed rectangle, the side with the larger value is set to 512 pixels, and the other side is scaled proportionally, and the rectangular upper surface of the standard target is extracted without deformation, so as to obtain a corrected medicine box image;
[0054] S14, the minimum circumscribed rectangle is transformed into a square, and the corrected medicine box image is widened by 1 / 2 of the rectangle width in the upper and lower directions, and by 1 / 2 of the rectangle length in the left and right directions, so that the minimum circumscribed rectangle of the corrected part to be processed is located in the middle of the widened medicine box image, and the length and width of the minimum circumscribed rectangle are correspondingly half of the length and width of the widened medicine box image.
[0055] The calculation of the number of standard targets in the standard target counting method based on machine vision of the present invention mainly relies on straight line detection, so whether the minimum bounding rectangle is deformed has no effect on the result. For the convenience of processing, after the minimum bounding rectangle is deformed into a square, the interception window is expanded to half the length of one side of the minimum bounding rectangle in the upper and lower parts and the left and right parts. After these four steps of preprocessing, the corrected result of the original image can be obtained, and the deformed minimum bounding rectangle is in the middle of the corrected image, that is, from one quarter of the width to three quarters of the width, and from one quarter of the height to three quarters of the height.
[0056] Straight line detection:
[0057] The Sobel filter is one of the more commonly used filters in the field of image processing and computer vision. Sobel is used to calculate the gradient of the image in two directions. Sobel is a discrete differential operator filter that can be used to calculate the approximate value of the gradient. It uses a smaller discrete value to convolve the image, and the calculation cost is low.
[0058] Sobel uses two kernels in the horizontal direction and the vertical direction to convolve the original image. The calculation formula is as follows:
[0059]
[0060] A represents the original image, and the 3*3 matrix is the convolution kernel in two directions. * represents the convolution operation. The obtained Gx is an image containing the approximate value of the horizontal derivative of the original image. Similarly, Gy contains the approximate value of the vertical derivative of the original image. The calculated gradients are positive and negative, and may even exceed the range of [0,255]. We need to normalize them to get the gradient map that can be displayed normally in OpenCV. Normalization also standardizes the calculation process, which is convenient for subsequent processing.
[0061] For high-frequency changes, the gradient approximation calculated by Sobel is relatively rough, so we use the Canny filter to further filter out the small noise to obtain a better quality gradient map. At this point, the straight lines of each standard target edge have been obtained. In order to eliminate the influence of the small texture and noise inside the standard target on the result, we still use the opening and closing operation with a large number of iterations. Finally, the result images obtained in the horizontal and vertical directions are synthesized into one image by performing a bitwise OR operation.
[0062] See also Figure 2In line detection, the Sobel operator is used to calculate the image gradients in the horizontal and vertical directions and normalize them, and then the Canny filter is used to remove the small noise in the photo. Finally, the corrosion and dilation operation with a large number of iterations is set to make the straight lines with high confidence in the image thicker and longer until they run through the entire image, while the straight lines with low confidence will not run through or will be directly discarded. Finally, the result images obtained in the horizontal and vertical directions are combined into one image by performing a bitwise OR operation. Line detection is completed
[0063] Quantity calculation:
[0064] See also Figure 3 In order to explain the idea of quantity calculation, a sample straight line detection result is selected as an example. When calculating the number of vertical straight lines, it is assumed that the width and height of the preprocessed square image are w. According to the rules of image resizing after correction, the position of the standard target is between 1 / 4w and 3 / 4w in width and 1 / 4w and 3 / 4w in height. A height is selected, and the pixels are traversed from 1 / 8w in width to 7 / 8w in width. The boundary where the pixels change from dark to light or from light to dark is found, and the number of vertical straight lines at the height is calculated.
[0065] See also Figure 4 , intercepted Figure 3 As part of , select the threshold i and define three lists to store three types of points: the first type of point has a gray value greater than i and the gray value of the pixel on its left is less than i-1, which can be roughly understood as a point from black to white; the second type of point has a gray value less than i-1 and the gray value of the pixel on its left is greater than i, which can be roughly understood as a point from white to black; the third type is used to calculate and store the midpoints of the first two types of points after they are found. Set the counter count to record the number of detected straight lines, and count increments each time the first type of point is traversed.
[0066] In the case of one standard target, two straight lines will be detected, and in the case of multiple standard targets, more than two straight lines will be detected. Therefore, when count is 0 or 1, it means that a straight line is missed. At this time, count is directly set to 2, that is, the number of standard targets is considered to be 1. During the experiment, it was found that the leftmost straight line is often missed. Then solve this problem: if there are more than two detected straight lines, calculate the distance step between the two adjacent lines (between the 0th and the 1st); based on the existing number of lines, if it is correct, calculate the width of all standard targets; then subtract this width from its width (occupying half the image width w / 2). If this difference is greater than a certain threshold, it can be said that the leftmost line should be missed, and then count is incremented to fill in this line.
[0067] After the above steps, the vertical straight line detection at a certain height is completed. Because there may be noise in some cases, the lines with low confidence do not run through the entire page. In order to ensure the confidence rate, we need to repeat the above steps at different heights. In this article, we take 1 / 4 height and 3 / 4 height respectively, calculate the number of lines and return the minimum value. Please refer to Figure 5 , showing the main process of the straight line number calculation algorithm. The above describes in detail the calculation of the number of vertical straight line detections, and the same is true for the calculation of the number of horizontal straight line detections.
[0068] The number calculation is divided into horizontal and vertical, and each direction calculates the minimum value of the retained number multiple times at different widths or heights. Therefore, in this section, only one height of the vertical line detection code is taken as an example, and other operations are similar.
[0069] Calculate the number of three types of points and store them in three arrays. Whenever a pixel from black to white is scanned, the count value is incremented. After scanning all pixels, calculate the midpoint based on the pixel position from black to white and the pixel position from white to black and store it. Determine whether there is a missed line at this time. If the count is less than two, it means that a missed line has been detected. At this time, set the number of lines to 2; if the number of midpoints exceeds one, calculate the distance between the two midpoints to calculate the total width of the measured standard target, and subtract it from the actual width. If the absolute value is greater than the threshold, it means that a missed line has been detected. At this time, the number of lines is incremented to make up for the missed line.
[0070] The standard target counting method based on machine vision of the present invention takes about 500 to 550 milliseconds to process one image, which meets the requirements. The data set used in this test contains 36 samples, of which 1 sample is miscalculated, and the accuracy rate reaches 97.22%.
[0071] The standard target counting method based on machine vision of the present invention calls Python's tkFileDialog library to realize algorithm visualization. The visualization system is divided into two sections: standard target extraction and standard target quantity calculation. The two sections have similar functions. The standard target quantity calculation is used as an example for demonstration. The standard target extraction function panel can be used in the same way. The main panel 1 of the standard target quantity calculation section is as follows: Figure 6As shown, there are three buttons, namely, the Select Image button 11, the Calculate button 12 and the Display button 13, which realize the functions of batch selecting pictures, calculating the standard target quantity, counting the average processing time and displaying the processing results. The Select Image button 11 can batch select the pictures that need to be processed, but it is necessary to ensure that the picture names do not contain '.' or '..'. The Calculate button 12 can call the main function to execute the standard target quantity calculation algorithm for the picture just selected, and can count the average processing time of the picture. The Display Result button 13 can display the processing result graph in the form of an OpenCV window. When we select multiple pictures for batch processing, we can control the playback of different picture processing results by pressing any key.
[0072] The standard target counting method based on machine vision of the present invention is divided into three major steps: image preprocessing, straight line detection and quantity calculation. The image preprocessing part uses the standard target extraction method to correct the image. Then, the width and length of the rectangle are widened by 1 / 2 upward, downward, left and right respectively to ensure that the minimum bounding rectangle of the part to be processed after correction is located in the middle of the widened image, and the length and width of the minimum bounding rectangle are half of the length and width of the widened image respectively. The purpose of doing this is to facilitate subsequent calculations and operations. The straight line detection part uses the Sobel operator to calculate the image gradient and then performs opening and closing operations, while the quantity calculation relies on more complex logical judgments.
[0073] To sum up, the standard target counting method based on machine vision of the present invention is based on the standard target extraction method. The input image is first corrected, and then the straight line detection method is used to calculate the number of standard targets through basic image processing. The standard target counting method of the present invention does not use traditional machine learning methods, saves computing power, and the counting results are highly accurate, which has certain economic value.
[0074] In the standard target counting method based on machine vision of the present invention, straight line detection is affected by many factors. The reason for the error is that the color mutation of the QR code area is mistakenly identified as an edge, and after opening and closing operations, it is regarded as a straight line. Two new ideas are proposed here. Both ideas require the hospital to provide standard length, width and height data of standard targets: the first one uses the standard target length and width ratio calculation; the second one needs to mark the standard target type and use a neural network for classification.
[0075] The standard target extraction algorithm can keep the length and width ratio of the standard target unchanged. By measuring the size using a reference object, the length, width and height of multiple standard targets in the image can be obtained. After comparing and correcting the length and width in the standard database, the corrected height in the image is divided by the standard height to obtain the number of standard targets. However, this method is greatly affected by the angle and needs to be processed in specific research. For the same reason, labeling the standard target data, using a convolutional neural network to classify it at the beginning of the algorithm, and then comparing it with the corresponding data in the standard database is also a highly robust algorithm. However, due to the use of neural networks, issues such as computing power cost need to be considered, and it must be comprehensively designed based on many factors such as the input data scale, required response time, and project budget.
[0076] Due to the small amount of data provided by the hospital, the robustness of the algorithm cannot be tested well. More data is needed to correct it and try other algorithm designs and experiments.
[0077] Those skilled in the art should recognize that the above embodiments are only used to illustrate the present invention, and are not intended to limit the present invention. As long as they are within the spirit of the present invention, any changes or modifications to the above embodiments will fall within the scope of the claims of the present invention.
Claims
1. A standard target counting method based on machine vision, characterized in that: The following steps are involved: S1, image preprocessing step: the input medicine box image is preprocessed in four steps: first, the medicine box image is converted into a grayscale image; then, the grayscale image is Gaussian filtered to extract image features while filtering noise points; then, the standard target extraction method is used to correct the medicine box image after Gaussian filtering; finally, the size of the corrected standard target is adjusted; S2, line detection step: use the Sobel operator to calculate the image gradients in the horizontal and vertical directions respectively and normalize them, and then perform Canny filtering to remove small noise points in the photo; then use the opening and closing operation with a large number of iterations to make the straight lines with high confidence in the image thicker and longer until they run through the entire medicine box image, while the straight lines with low confidence will not run through or will be directly discarded; finally, perform a bitwise OR operation on the result images obtained in the horizontal and vertical directions to synthesize one image; S3, quantity calculation step: find the boundary where the pixels change from dark to light or from light to dark in the medicine box image after the straight line detection step, calculate the number of straight lines, and then obtain the number of standard target medicine boxes; the straight line number calculation is divided into horizontal and vertical straight line number calculation, and in each direction, the minimum value of the retained number is calculated multiple times at different widths or heights; Select the threshold as i and define three lists to store three types of points: the first type of point has a gray value greater than i and a gray value less than i-1, which is understood as a point from black to white; the second type of point has a gray value less than i-1 and a gray value greater than i, which is understood as a point from white to black; the third type of point is used to calculate and store the midpoints of the first two types of points after finding them; set the counter count to record the number of straight lines detected, and the count value increases every time a point from black to white is scanned; After scanning all pixels, the midpoint is calculated and stored according to the pixel position from black to white and from white to black. It is judged whether there is a missed line at this time. If the count is less than 2, it means that a missed line has been detected. At this time, the number of straight lines is set to 2. If there is more than one midpoint, the distance between the two midpoints is calculated to calculate the total width of the measured standard target. The difference between the total width of the standard target and the actual width is calculated. If the absolute value is greater than the threshold, it means that a missed line has been detected. At this time, the number of straight lines is automatically increased to make up for the missed line. Step S1 specifically includes the following process: S11, converting the medicine box image from BGR to grayscale; S12, setting the Gaussian standard deviation to 1, calculating the filter size according to the standard deviation, then generating a Gaussian filter kernel and normalizing it, and finally using the filter kernel to convolve along the x and y axes respectively, performing Gaussian filtering on the grayscale image, and extracting image features while filtering noise points through Gaussian filtering; S13, correcting the medicine box image using a standard target extraction method, specifically including the following process: S131, Canny edge detection step: gradient size and direction calculation, non-maximum suppression and double threshold screening are performed on the grayscale image processed by Gaussian filtering in turn to accurately depict all edges in the medicine box image; S132, opening and closing operation step: using an opening and closing operation with a large number of iterations to process the edge of the medicine box image obtained in the Canny edge detection step, first using a closing operation with a large number of iterations to eliminate the texture inside the medicine box image, converting the grayscale image into a pure color binary image through corrosion and expansion, to avoid the problem of cutting the medicine box into two due to incorrect recognition of the texture inside the medicine box; then using an opening operation and a median filter in turn to remove noise and burrs generated during the closing operation; S133, find the contour with the largest area and fit it with the minimum circumscribed rectangle: call the findContours interface of OpenCV to track all contours in the figure, calculate the contour area, find the contour with the largest area and find its minimum circumscribed rectangle, return the four vertices of the minimum circumscribed rectangle, rearrange them according to the coordinates, connect the rectangles with red straight lines, and find the length and width of the rectangle to prepare for the subsequent perspective transformation; S134, proportional perspective transformation step: the aspect ratio of the standard target is calculated by the length and width of the minimum circumscribed rectangle, the side with the larger value is set to 512 pixels, and the other side is scaled proportionally, and the rectangular upper surface of the standard target is extracted without deformation, so as to obtain a corrected medicine box image; S14, the minimum circumscribed rectangle is transformed into a square, and the corrected medicine box image is widened by 1 / 2 of the rectangle width in the upper and lower directions, and by 1 / 2 of the rectangle length in the left and right directions, so that the minimum circumscribed rectangle of the corrected part to be processed is located in the middle of the widened medicine box image, and the length and width of the minimum circumscribed rectangle are correspondingly half of the length and width of the widened medicine box image.
2. A standard target counting method based on machine vision according to claim 1, characterized in that: In step S4, when calculating the number of vertical straight lines, it is assumed that the width and height of the preprocessed square image are w. According to the rules for adjusting the size of the corrected image, the position of the standard target is between 1 / 4w and 3 / 4w in width and 1 / 4w and 3 / 4w in height. A height is selected, and the process is traversed from 1 / 8w in width to 7 / 8w in width to find the boundary where the pixels change from dark to light or from light to dark, and the number of vertical straight lines at the height is calculated.
3. A standard target counting method based on machine vision according to claim 2, characterized in that: In step S4, the 1 / 4 height and 3 / 4 height are respectively taken to calculate the number of straight lines and take the minimum value.
Citation Information
Patent Citations
Marker-based non-cooperative target pose measuring method
CN108562274A
An efficient segmentation method for infrared images of solar panels with uneven colors
CN109146895A