A medicine bottle counting method for a full-automatic medicine bottle counting device of a medicine dispensing center
By taking vertical images of medicine bottles with a camera and using ellipse fitting technology to separate the bottle head and shoulder, the problem of low accuracy in medicine bottle counting under complex environments is solved, and highly accurate medicine bottle quantity statistics are achieved.
Patent Information
- Application Number
- CN202510492604.0
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2025-04-18
- Publication Date
- 2025-11-04
- Estimated Expiration
- 2045-04-18
AI Technical Summary
Existing methods for counting medicine bottles have low accuracy in complex environments, especially when the images of medicine bottles at the edges are unclear or obstructed, leading to inaccurate counting. Furthermore, reflection interference affects the counting results.
The system captures images of medicine bottles vertically downwards using a camera, obtains the bottle outline through edge detection, separates the bottle head and shoulder using ellipse fitting technology, categorizes the bottles by ellipse width, and counts the number of bottles by matching the coordinates of the ellipse center.
It improves the accuracy and anti-interference ability of medicine bottle counting, reduces the false detection and false detection rates, and is suitable for detection scenarios with complex backgrounds and high reflective interference.
Smart Images

Figure CN120411014B_ABST
Abstract
Description
TECHNICAL FIELD
[0001] The present application relates to the technical field of image vision, in particular to a medicine bottle counting method for a full-automatic medicine bottle counting device of a static dispensing center. BACKGROUND
[0002] The medicine bottles often need to be unpacked in the static dispensing center, and the different types of medicine bottles need to be counted and counted after unpacking. At present, the static dispensing center mainly adopts the manual counting method to count the number of medicine bottles. This method increases the workload in the case of a large number of medicine bottles and a complex environment. Especially in a long time of work, the attention of people will decrease, which leads to an increase in error rate and a decrease in counting speed. In addition, the manual counting method is prone to missing and repeated counting problems due to the negligence of personnel.
[0003] The image vision technology is also used for machine counting in the prior art. The machine counting method has a high requirement for the definition of the photographed medicine image. When there are interfering objects or other objects causing image distortion in the image, the accuracy of machine counting will decrease.
[0004] At present, the medicine bottle counting method in the image vision technology is generally contour extraction counting or counting according to a circular contour. When the image vision technology is used for machine counting, due to the centricity of camera imaging, the camera does not photograph the medicine bottle at the edge position directly, but has a small angle tilt. There is a little occlusion in the edge area, so the imaging of the medicine bottle in the edge area of the photographed image has an intermittent edge line, which cannot extract a complete contour, leading to a low counting accuracy. In addition, when the circular contour is used for counting, the contour imaging of the medicine bottle at the edge area is not a circle. It is difficult to extract a perfect circle contour due to reasons such as reflection or shooting angle tilt, leading to inaccurate detection quantity. SUMMARY
[0005] The purpose of the present application is to provide a medicine bottle counting method for a full-automatic medicine bottle counting device of a static dispensing center, to solve the problems of the existing recognition method that cannot photograph a complete medicine bottle contour and weak anti-interference ability, and to improve the accuracy of medicine bottle counting.
[0006] The technical solution of the present application is to provide a medicine bottle counting method for a full-automatic medicine bottle counting device of a static dispensing center, which comprises:
[0007] Step 1, a camera is used to vertically downwardly photograph a target medicine bottle image, edge detection is performed on the medicine bottle image to obtain a medicine bottle contour graph, wherein the medicine bottle contour at least includes a medicine bottle head and a shoulder position;
[0008] Step 2, the edge pixel points in the bottle contour image are fitted according to a circular or elliptical contour to detect an elliptical shape at the position of the bottle head and the shoulder of the bottle;
[0009] Step 3, the fitted ellipse is divided into two categories according to the length of the major axis, the first category of ellipse corresponds to the shoulder of the bottle, and the second category of ellipse corresponds to the head of the bottle, and the length of the major axis of the first category of ellipse is greater than that of the second category of ellipse;
[0010] Step 4, each first category of ellipse is sequentially traversed as a matching reference, for the currently selected first category of ellipse, a second category of ellipse is searched in a preset matching range around the first category of ellipse, it is judged whether the preset matching range contains a unique second category of ellipse, if yes, the two ellipses are paired, if not, the selected first category of ellipse is deleted, if there are multiple second category of ellipses, a connecting band is searched between the currently selected first category of ellipse and each second category of ellipse, for a single group of two ellipses, if there is a connecting band between the two ellipses, the two ellipses are paired, if there is no connecting band between the two ellipses, the next second category of ellipse is traversed until a paired second category of ellipse is found;
[0011] After each single fitted ellipse is completed, the two ellipses that are successfully paired are removed from the ellipses to be paired;
[0012] Step 5, the two ellipses that are successfully paired are marked as the same target bottle contour, the number of all target bottle contours in the image is counted, and finally the number of target bottles is obtained.
[0013] Further, step 2 specifically includes:
[0014] Step 21, a single pixel point is sequentially selected from the edge point set, and a plurality of interval points in the neighborhood range of the pixel point are taken as sample points and brought into an elliptical equation, the curve parameters corresponding to the current sample points are calculated, and the neighborhood range is greater than the pixel area covered by a single bottle in the image;
[0015] Step 22, it is verified whether the curve parameters corresponding to the current sample points satisfy the constraint condition of the ellipse, if yes, the ellipse corresponding to the current sample points is taken as an initial ellipse, step 23 is executed, if not, the next point in the edge point set is sequentially traversed;
[0016] Step 23, the pixel points in the coverage range of the ellipse are brought into the initial ellipse, iterative fitting optimization is performed, the final fitted ellipse is obtained, the contour points corresponding to the fitted ellipse are marked as boundary points of the fitted ellipse, and then the next point in the edge point set P is sequentially traversed until the traversal of all points is completed.
[0017] Further, step 21 specifically includes:
[0018] In the row coordinate priority mode, points are selected from the edge point set P in sequence, after each point is selected, n interval points in the neighborhood of the point are taken as sample points, n>=6, the coordinate values of the n sample points are taken into the ellipse fitting linear equation set:
[0019]
[0020] In the formula, A, B, C, D, E, F are parameters of the ellipse respectively, the ellipse fitting linear equation set is recorded as Mp=0, M is a coefficient matrix, p is an ellipse parameter matrix, T is a matrix transpose, when n=6, the matrix p is calculated by elimination method, when n>6, the least square method is used to fit the ellipse parameter matrix p.
[0021] Further, in step 23, the expression of the center coordinate (x0, y0) of the ellipse is:
[0022]
[0023] In the formula, x0 is the column coordinate of the center of the ellipse, and y0 is the row coordinate of the center of the ellipse.
[0024] Further, step 4 specifically includes:
[0025] Step 41, a single first type of ellipse is selected in the contour graph in the row coordinate priority and coordinate from small to large order, whether there is a second type of ellipse in the preset matching range of the current ellipse is searched, if there is a unique second type of ellipse, the two ellipses are paired, if the second type of ellipse is not found, the currently selected first type of ellipse is removed, and the next first type of ellipse is sequentially traversed;
[0026] Step 42, if there are multiple second type of ellipses in the preset matching range of the currently selected first type of ellipse, a bounding box is set for the area where the currently selected first type of ellipse and each second type of ellipse are located in the contour graph;
[0027] Step 43, a matching group is respectively constructed with the currently selected first type of ellipse and each second type of ellipse, for each matching group, the straight line where the centers of the two types of ellipses are located is taken as a boundary line, other pixel points in the current bounding box except the ellipse contour points are divided to obtain two straight line fitting areas, and the pixel points in the straight line fitting areas are respectively fitted.
[0028] Step 44: For each matching group, determine whether there is a fitted line connecting two ellipses in the current bounding box. If there is, determine that there is a connecting band between the two ellipses, pair the two ellipses in the current matching group and remove them from the fitted ellipses to be matched. If there is no matching group, determine the next matching group. If none of the matching groups corresponding to the currently selected first type of ellipse can be paired, return to step 41, search for the next first type of ellipse again and repeat the pairing process.
[0029] Further, in step 42, a bounding box is set for the regions containing the two ellipses of the current matching group in the contour map, specifically including:
[0030] Read the maximum column coordinate, minimum column coordinate, maximum row coordinate, and minimum row coordinate from the region containing any second-type ellipse within the currently selected first-type ellipse and the preset matching range. Use the rectangle formed by connecting the columns containing the maximum and minimum column coordinates and the rows containing the maximum and minimum row coordinates as the bounding boxes of the two current ellipses.
[0031] Furthermore, step 43 specifically includes:
[0032] For a single region of line fitting, calculate the mean column coordinates of all pixels. mean of row coordinates The covariance Cov(x,y) and variance Var(x) are expressed as:
[0033]
[0034] In the formula, (x j ,y j Let be the coordinates of the j-th pixel in the region to which the line is fitted. The fitted line is represented as:
[0035] y = kx + b
[0036]
[0037] In the formula, k is the slope and b is the intercept.
[0038] Furthermore, the connecting band is the region within the current bounding box where the fitted line meets the pairing condition. The pairing condition is that the angle between the fitted line and the line containing the centers of the two ellipses is within a preset angle range.
[0039] Furthermore, step 44 also includes:
[0040] Find the line containing the centers of the two ellipses, and use it as the first line. Then, use each fitted line as the second line. Compare the second lines with the first lines sequentially, and calculate the angle between the two sets of lines based on their slopes. The angle between the first and second lines is expressed as:
[0041]
[0042] In the formula, θ is the included angle of the first straight line and the second straight line, k1 is the slope of the first straight line, k2 is the slope of the second straight line, if k1 does not exist, then k=k2, if k2 does not exist, then k=k1.
[0043] The inventor of the present application finds that during the image shooting of the medicine bottles, the bottle top and the bottle shoulder of the medicine bottles are not always a circular profile, but have certain elliptical properties, and there is often a connecting profile between the bottle top and the bottle shoulder, therefore, the present application proposes to use an elliptical model and a connecting profile between the elliptical models for medicine bottle detection, which is better than the existing schemes of counting by profile extraction or counting according to a circular profile; the inventor of the present application finds that due to the existence of the camera optical axis, when shooting vertically downward, the medicine bottles in the area directly below can be shot to have complete profiles, while the medicine bottles at the edge of the shooting area will block each other and cannot be shot to have complete profiles, the counting by profile extraction can only cover part of the medicine bottles, and there is a missing situation, and the accuracy of counting is low; the counting according to a circular profile also faces the same problem, and is also affected by the camera optical axis, and cannot extract the circular profile of the medicine bottles at the edge, thereby leading to the missing counting, in addition, the counting according to a circular profile is also easily affected by the reflection, since the neck part of the medicine bottle is relatively thin, the liquid hanging on the wall is easy to occur, and the reflection of the liquid is relatively strong, when the profile is extracted, the profile of the liquid in the neck will be extracted, the profile of the liquid in the neck is similar in size to the profile of the head of the medicine bottle, and misjudgment is easy to occur, so that the accuracy of counting is low. The scheme of using an elliptical model and a connecting profile between the elliptical models for medicine bottle detection in the present application can solve the problems in the existing schemes, the present application divides the fitted elliptical profiles into two categories according to the width, then traverses each first-class elliptical according to the order of the center coordinates of the elliptical, finds a second-class elliptical in a preset matching range for matching, and finally determines the number of matching groups, such a way can not only avoid the problem of not being able to extract the complete profile of the medicine bottle, but also reduce the interference when counting according to a single circular profile, and more accurately detect the number of medicine bottles.
[0044] The beneficial effects of the present application are:
[0045] The technical solution in the application extracts the contours of the local regions with more distinctive geometric features in the medicine bottles through shape fitting, then matches the different contours extracted, and accurately counts the number of medicine bottles according to the number of matched contours; compared with the prior art method, the technical solution in the application improves the accuracy of medicine bottle counting and the anti-interference capability. The target recognition and counting method in the prior art relies on the recognition of the overall contour map of the object or the recognition of a single circular contour, and in the case of a complex background with medicine bottles shielding each other and light reflection interference, it is difficult to effectively distinguish the medicine bottles, especially for transparent or semi-transparent medicine bottles, and misrecognition or missed recognition is likely to occur, while the method in the application can more clearly capture the key features of the local positions of the medicine bottles, and through the matching of different contours, it can also filter out the interference of other noise or non-target contours, greatly reducing the false detection rate and the missed detection rate. In comparison, the technical solution in the application is more suitable for detection scenarios with complex background and large light reflection interference, and can accurately count the number of medicine bottles. BRIEF DESCRIPTION OF DRAWINGS
[0046] The above and / or additional aspects of the present application will become apparent and more readily appreciated from the following description of the embodiments, taken in conjunction with the accompanying drawings, in which:
[0047] Figure 1 is a schematic flow chart of a medicine bottle counting method for a full-automatic medicine bottle counting device in a pharmacy center according to an embodiment of the present application;
[0048] Figure 2 is a schematic diagram of the included angle between the straight line where the centers of two types of ellipses are located and the fitting straight line according to an embodiment of the present application;
[0049] Figure 3 is an image of a target medicine bottle captured by a camera vertically downward according to an embodiment of the present application;
[0050] Figure 4 is a contour map of a target medicine bottle according to an embodiment of the present application. DETAILED DESCRIPTION
[0051] In order to more clearly understand the above-mentioned purposes, features and advantages of the present application, the present application will be further described in detail below in conjunction with the drawings and specific embodiments. It should be noted that the embodiments of the present application and the features in the embodiments can be combined with each other without conflict.
[0052] In the following description, many specific details are set forth in order to provide a thorough understanding of the present application, but the present application can also be implemented in other ways different from those described herein, and therefore, the scope of protection of the present application is not limited by the specific embodiments disclosed below.
[0053] The Canny edge detection used in this embodiment is an image edge detection algorithm that aims to detect edges in an image and extract contours, widely used in computer vision and image processing. The output of Canny edge detection is a binary image that can show the edges of objects and intuitively display the contours in the image.
[0054] As Figure 1 shown, the embodiment provides a vial counting method for a full-automatic vial counting device of a compounding center, comprising:
[0055] Step 1, use a camera to vertically downward shoot the target vial, obtain the vial image, and perform edge detection on the vial image to obtain a vial contour map, wherein the vial contour at least includes the vial head and the vial shoulder position.
[0056] Place the target vial in the shooting area, set the camera above the shooting area, adjust the camera to make the camera lens vertically downward shoot, and clearly capture the image of all target vials in the shooting area, use Canny edge detection algorithm to extract the contour shape in the image (set edge gradient threshold, retain pixels with gradient greater than threshold), obtain the contour map of each vial in the image, wherein the contour map of the target vial is a binary image, the white part represents the detected edge contour of the target vial, and the black part represents the non-edge background area.
[0057] Step 2, curve fitting is performed on the edge pixel points in the vial contour map according to the circular or elliptical contour to detect the elliptical shape at the vial head and shoulder position.
[0058] All edge pixel points in the vial contour map are taken as edge point set P(x i ,y i ), wherein (x i ,y i ) is the column coordinate and row coordinate of the i-th point in the edge point set P, the edge point set is traversed, each time a single ellipse is fitted, the found elliptical contour points (contour points are points covered by the fitted elliptical contour in the edge point set P) are marked as boundary points of the fitted ellipse, until the remaining untraversed points in the edge point set P are less than the required sample number for fitting or all points in the edge point set P are traversed, the traversal is stopped; the traversal of a single pixel point specifically includes the following steps:
[0059] Step 21: Select a single pixel from the edge point set P in sequence, and use the pixel and multiple interval points in its neighborhood as sample points to input into the ellipse equation. Calculate the curve parameters corresponding to the current sample point. The neighborhood range is set according to empirical values and is larger than the pixel area covered by a single medicine bottle in the image. The interval points are multiple points extracted from the neighborhood at a preset interval, which can be set to 1-5 pixels. More preferably, before fitting, the pixels are merged in a 2*2, 3*3, or 4*4 manner to reduce the number of calculations.
[0060] Following a row coordinate-first approach, points are selected from the edge point set P in ascending order (i.e., the top-left pixel has the smallest row and column coordinates for an image). After selecting each point, n interval points within its neighborhood are used as sample points, where n ≥ 6. The coordinates of these n sample points are then substituted into the ellipse equation to construct a system of linear equations for ellipse fitting. The ellipse equation is expressed as:
[0061]
[0062] In the formula, A, B, C, D, E, and F are the parameters of the ellipse; the system of linear equations fitted to the ellipse is expressed as:
[0063]
[0064] The system of linear equations fitted to the ellipse is denoted as Mp = 0, where M is an n×6 coefficient matrix and p is the ellipse parameter matrix, p = [A, B, C, D, E, F]. T T is the matrix transpose. When n = 6, matrix p is calculated directly by elimination; when n > 6, the least squares method is used to fit the elliptic parameter matrix p.
[0065] Since p has no unique solution when n is greater than 6, it is necessary to find the optimal solution for the elliptic parameter matrix. The least squares method is used to fit the elliptic parameter matrix p, such that ||Mp|| 2 Minimize, that is, find the optimal solution of p when Mp≈0, let M T MP = 0, for M T M is decomposed into eigenvalues, and the eigenvalues λ and eigenvectors s satisfy the equation (M T The equation M)s=λs can be rewritten as (M)s=λs, which is a function of M T M-λI)s=0, where I is the identity matrix, used to ensure that the subtraction of matrices still results in matrices of the same dimension. Since λ is M T The eigenvalues of M, therefore the matrix (M) T M-λI) is not a full-rank matrix, and the matrix (M) T The determinant of M-λI is equal to zero, thus we get det(M-λI). TM-λI) = 0, all eigenvalues λ are solved by using the determinant equation, and the minimum eigenvalue λ is taken from the multiple eigenvalues min , the equation (M T -λI) = 0 is solved again min , and the minimum eigenvalue λ is obtained min , and the corresponding eigenvector s is taken as the elliptic parameter matrix p.
[0066] Step 22: verifying whether the curve parameters corresponding to the current sample point satisfy the constraint condition of the ellipse, if yes, taking the ellipse corresponding to the current sample point as the initial ellipse, and performing step 23, if not, returning to step 21 to sequentially select the next point in the edge point set P;
[0067] After obtaining the elliptic parameter matrix p, verifying whether the elliptic parameters corresponding to the current sample point satisfy the constraint condition B 2 -4AC < 0, if yes, taking the ellipse corresponding to the current sample point as the initial ellipse, and performing step 23 to calculate the contour points of the ellipse corresponding to the current sample point, if not, returning to step 21 to sequentially select the next pixel point in the edge point set P, and calculating the corresponding curve parameters again until a sample point satisfying the ellipse constraint condition is found or a stop traversal condition is reached (i.e., the number of remaining untraversed points in the edge point set P is less than the required number of samples for fitting or all points in the edge point set P are traversed).
[0068] Step 23: taking the pixel points within the coverage range of the ellipse into the initial ellipse, iteratively fitting and optimizing to obtain the final fitting ellipse, marking the contour points corresponding to the fitting ellipse as the boundary points of the fitting ellipse, wherein the boundary points are no longer sequentially traversed but still participate in the contour point searching process (i.e., a point may be marked as the boundary point of multiple ellipses), then sequentially traversing the next point in the edge point set P until the stop traversal condition is reached, and finally obtaining multiple ellipses and their corresponding center coordinates.
[0069] According to the parameters A, B, C, D, E, and F of the final fitting ellipse, the center coordinates of the ellipse are calculated, and the expression of the center coordinates (x0, y0) of the ellipse is:
[0070]
[0071] In the formula, x0 is the column coordinate of the center of the ellipse, and y0 is the row coordinate of the center of the ellipse.
[0072] In this embodiment, after obtaining the parameters A, B, C, D, E, and F of the ellipse, the collective center (x0, y0) and the major axis a of the ellipse are calculated according to these parameters.
[0073] In this embodiment, the fitted ellipse includes a circle, when the parameter B is 0 and the parameters A and C are equal, the ellipse degenerates into a circle.
[0074] In this embodiment, there are multiple medicine bottles in the shooting area, and the medicine bottles in the edge area cannot be completely shot to the contour, and contour counting is prone to missed detection; if only the bottle body head is identified, it is easy to be affected by the wall hanging liquid contour in the bottle neck, resulting in inaccurate detection quantity; and if the head and shoulder joint detection is used, more bottle body information can be obtained, and more accurate detection can be obtained.
[0075] Step 3, according to the long axis width, the fitted ellipse is divided into two categories, the first category of ellipse corresponds to the shoulder of the medicine bottle, and the second category of ellipse corresponds to the head of the medicine bottle, and the long axis width of the first category of ellipse is greater than that of the second category of ellipse.
[0076] Based on the parameters of the ellipse, the long axis width of each fitted ellipse is calculated, the fitted ellipse with the long axis width in the first width threshold range (set according to the size of the shoulder) is regarded as the first category of ellipse corresponding to the shoulder of the medicine bottle, and the fitted ellipse with the long axis width in the second width threshold range (set according to the size of the bottle head) is regarded as the second category of ellipse corresponding to the head of the medicine bottle, wherein the long axis direction width 2a is represented as:
[0077]
[0078] Step 4, sequentially traversing each first category of ellipse as a matching reference, for the currently selected first category of ellipse, searching for a second category of ellipse in a preset matching range around it, judging whether the preset matching range contains a unique second category of ellipse, if yes, pairing the two ellipses, if not, deleting the selected first category of ellipse, if there are multiple second category of ellipses, searching for a connecting band between the currently selected first category of ellipse and each second category of ellipse, for a single group of two ellipses, if there is a connecting band between them, pairing the current two ellipses, if there is no connecting band between them, traversing the next second category of ellipse, until a paired second category of ellipse is found, wherein after completing the traversal of a single fitted ellipse, the paired two ellipses are removed from the ellipses to be paired. Preferably, when fitting the ellipse, the error tolerance is improved, the semi-ellipse and other approximate ellipses are regarded as ellipses, and then the connecting band between the ellipses is used for secondary confirmation, and the missed detection possibility is reduced.
[0079] Step 41, in the order of row coordinate priority and coordinate value from small to large (i.e. the order of coordinate value from small to large, in the figure, the order from top to bottom and from left to right), a single first type ellipse is selected in the contour graph (a first type ellipse or a second type ellipse can be selected, preferably an ellipse corresponding to the shoulder of the bottle, i.e. a first type ellipse), and it is checked whether there is a second type ellipse in the preset matching range of the current ellipse. If there is a unique second type ellipse, the two ellipses are paired, and if no second type ellipse is found, the currently selected first type ellipse is removed from the ellipse to be paired, and the next first type ellipse is sequentially traversed;
[0080] In the embodiment, before counting the medicine bottles, an image of the medicine bottle is collected for ellipse fitting, and then the two paired ellipses are manually labeled, the center distance of each pair of ellipses is calculated, and the maximum distance from the center of the head of the medicine bottle to the center of the shoulder of the medicine bottle in the image is taken as the radius of the preset matching range.
[0081] The preset matching range is a circular area with the center of the single first type ellipse as the center, and the radius of the preset matching range is greater than the maximum distance from the center of the head of the medicine bottle to the center of the shoulder of the medicine bottle in the image. For a single first type ellipse, the center distance between it and each second type fitting ellipse in the preset matching range is calculated, and the second type ellipse with a center distance less than the radius of the preset matching range is selected as the object to be matched. Wherein, the center distance d between the first type ellipse and the second type ellipse is represented as:
[0082]
[0083] In the formula, x 0,c1 is the column coordinate of the current first type ellipse, x 0,c2 is the column coordinate of the current second type ellipse, y 0,c1 is the row coordinate of the current first type ellipse, and y 0,c2 is the row coordinate of the current second type ellipse.
[0084] Step 42, if there are multiple second type ellipses in the preset matching range of the currently selected first type ellipse, a bounding box is set for the area where the currently selected first type ellipse and each second type ellipse are located in the contour graph.
[0085] The maximum column coordinate, the minimum column coordinate, the maximum row coordinate and the minimum row coordinate are read from the area where the currently selected first type ellipse and any second type ellipse in the preset matching range are located, and the rectangle formed by the column where the maximum column coordinate and the minimum column coordinate are located and the row where the maximum row coordinate and the minimum row coordinate are located is taken as the bounding box of the current two ellipses.
[0086] Step 43, for each matching group, the region division is performed on the pixel points in the current bounding box other than the ellipse contour points, with the straight line on which the centers of the two types of ellipses in the current matching group are located as the demarcation line, to obtain two straight line fitting regions, and the straight line fitting is performed on the pixel points in the straight line fitting regions respectively.
[0087] The straight line fitting is performed on all the points in a single straight line fitting region, specifically, the column coordinate mean value of all the pixel points in the straight line fitting region is calculated and the row coordinate mean value The covariance Cov(x, y) (used to describe the joint trend of x and y) and the variance Var(x) (used to describe the dispersion degree of x) of all the pixel points are calculated, and are expressed as:
[0088]
[0089]
[0090] In the formula, (x j ,y j ) is the coordinate of the jth pixel point in the straight line fitting region, and the fitting straight line is expressed as:
[0091] y=kx+b
[0092]
[0093] In the formula, k is the slope, and b is the intercept.
[0094] Step 44, for each matching group, it is judged whether there is a fitting straight line connecting the two ellipses and satisfying the pairing condition in the current bounding box, if there is, it is determined that the current two ellipses are matched, the two ellipses in the current matching group are paired and removed from the fitting ellipses to be matched, as a matching group with successful pairing, if not, the next matching group is judged, if the current selected first type of ellipse corresponding to each matching group cannot be paired, step 41 is returned, the next first type of ellipse is searched and the pairing process is repeated.
[0095] The fitting straight line connecting the two ellipses and satisfying the pairing condition is judged as follows:
[0096] The straight line on which the centers of the current two ellipses are located is obtained as the first straight line, each fitting straight line is taken as the second straight line, the second straight line is compared with the first straight line in turn, and the included angle of the two groups of straight lines is calculated based on the slope, the included angle of the first straight line and the second straight line is expressed as:
[0097]
[0098] In the formula, θ is the included angle of the first straight line and the second straight line, k1 is the slope of the first straight line, and k2 is the slope of the second straight line. In the above second formula, if k1 does not exist, then k=k2, and if k2 does not exist, then k=k1.
[0099] It is judged whether there is a fitting straight line satisfying the matching condition in the current bounding box. If there is, it is determined that there is a connecting band between the current two ellipses. The two ellipses of the current matching group are paired, and the paired two ellipses are deleted from the fitting ellipses to be matched. If there is not, the second type of ellipse of the current group is excluded, and the next matching group is judged. In the embodiment, the preset angle range can be set to 0° to 20°.
[0100] For a single first type of ellipse, if there are multiple second type of ellipses capable of forming a pair with it in the preset matching range around it, the two ellipses of one matching group in which the sought included angle is closest to the included angle of the bottle body itself are taken as the successfully paired two ellipses, and the remaining matching groups are discarded. In the embodiment, the included angle of the bottle body itself can be set to 8°.
[0101] As shown in Figure 2 , the left side is the original image of a single target medicine bottle. After contour extraction, the right side image can be obtained. The two fitting ellipses are obtained by performing ellipse fitting on the right side image. The green lines in the figure are the two types of ellipses obtained by fitting. Among them, the ellipse with a shorter major axis corresponds to the position of the head of the medicine bottle, and the ellipse with a longer major axis corresponds to the position of the shoulder of the medicine bottle. The blue line in the figure is the straight line on which the centers of the two types of ellipses are located. The red line is a fitting straight line, and the included angle between the two is θ. In the embodiment, a higher gradient threshold is set to better highlight the contours of the top and shoulder.
[0102] Step 5: The successfully paired two ellipses are marked as the contour of the same target medicine bottle. The number of all target medicine bottle contours in the image is counted, and finally the number of target medicine bottles is obtained.
[0103] Example:
[0104] A camera is used to vertically downwardly shoot a medicine bottle image as shown in Figure 3 . As can be seen from the image, the positions of the head of the medicine bottle and the shoulder of the medicine bottle can be clearly seen, while other positions are blocked. The edge of the medicine bottle image is detected to obtain a medicine bottle contour image as shown in Figure 4 . As can be seen from the medicine bottle contour image, the contours of the head of the medicine bottle, the shoulder of the medicine bottle, and the position of the neck are relatively clear, while the contours of other positions are unclear and cannot extract the complete contour of the medicine bottle. The medicine bottle contour image is fitted according to the circular or elliptical contour to obtain the elliptical shape of the head of the medicine bottle and the shoulder of the medicine bottle. The fitted ellipses are divided into first type of ellipses and second type of ellipses, wherein the green ellipse is the first type of ellipse, and the red ellipse is the second type of ellipse.
[0105] To illustrate the matching, let's take the outlines of two medicine bottles in the image as examples, such as... Figure 4 As shown, the two types of ellipses in the middle of the image are nested, meaning the center of the second type of ellipse is located within the region of the first type of ellipse. In this case, there is only one second type of ellipse within the preset matching range of the first type of ellipse, therefore these two ellipses belong to the same target medicine bottle outline. The two types of ellipses at the top of the image are not nested, meaning the center of the second type of ellipse is not within the region of the first type of ellipse. Figure 4 As can be seen, there are connecting bands between the second type of ellipse and the first type of ellipse labeled 1, 2, and 3. The red second type of ellipse and the first type of ellipse labeled 1 have connecting bands. The pixels in the yellow box in the figure can fit a straight line, and the fitted straight line satisfies the pairing condition, so the two ellipses can be paired. However, there are no connecting bands between the red second type of ellipse and the second type of ellipse labeled 2 and 3, so the two ellipses cannot be paired. Therefore, the second type of ellipse and the first type of ellipse labeled 1 belong to the same target medicine bottle outline.
[0106] The technical solution of this invention is applicable to transparent or plastic medicine bottles, and semi-transparent glass or plastic medicine bottles, such as common ampoules, especially glass medicine bottles. Glass medicine bottles contain liquid medicine, and violent collisions should be avoided as much as possible during transportation. Therefore, the existing counting method using a stepped structure (setting steps of different heights to allow objects to roll down for counting) cannot count glass bottles. Furthermore, because glass bottles are transparent and highly reflective, the complete outline cannot be identified when using machine recognition. At the same time, it is also affected by the liquid medicine adhering to the bottle neck. When counting based on a single shape outline fails, errors are easily made, leading to inaccurate statistics.
[0107] The steps in this application can be rearranged, combined, or deleted according to actual needs.
[0108] The units in the device of this application can be merged, divided, or deleted according to actual needs.
[0109] Although this application has been disclosed in detail with reference to the accompanying drawings, it should be understood that these descriptions are merely exemplary and not intended to limit the application of this application. The scope of protection of this application is defined by the appended claims and may include various variations, modifications, and equivalents of the invention without departing from the scope and spirit of this application.
Claims
1. A vial counting method for a fully automated vial counting device for a compounding center, characterized by, The method comprises: Step 1, taking an image of a target medicine bottle vertically downward by using a camera, performing edge detection on the medicine bottle image to obtain a medicine bottle contour, wherein the medicine bottle contour at least includes a medicine bottle head and a shoulder position; Step 2, performing curve fitting on edge pixel points in the medicine bottle contour according to a circular or elliptical contour to detect an elliptical shape at the medicine bottle head and the shoulder position; Step 3, dividing the fitted ellipse into two categories according to the major axis width, the first category of ellipse corresponding to the medicine bottle shoulder, and the second category of ellipse corresponding to the medicine bottle head, the major axis width of the first category of ellipse being greater than that of the second category of ellipse; Step 4, sequentially traversing each first category of ellipse as a pairing reference, for the currently selected first category of ellipse, searching for a second category of ellipse within a preset matching range around the first category of ellipse, judging whether the preset matching range contains a unique second category of ellipse, if yes, pairing the two ellipses, if not, deleting the selected first category of ellipse, if there are multiple second category of ellipses, searching for a connecting band between the currently selected first category of ellipse and each second category of ellipse, for a single group of two ellipses, if there is a connecting band between the two ellipses, pairing the two ellipses, if there is no connecting band between the two ellipses, traversing the next second category of ellipse until a paired second category of ellipse is found; wherein, after completing the traversal of each fitted ellipse, the two ellipses successfully paired are removed from the ellipses to be paired; Step 5, marking the two ellipses successfully paired as the same target medicine bottle contour, counting the number of all target medicine bottle contours in the image, and finally obtaining the number of target medicine bottles.
2. The vial counting method for a fully automated vial counting apparatus of a compounding center according to claim 1, wherein, The step 2 specifically comprises: Step 21, sequentially selecting a single pixel point from the edge point set, taking the pixel point and multiple interval points in its neighborhood range as sample points to bring into an elliptical equation, and calculating the curve parameters corresponding to the current sample points, wherein the neighborhood range is greater than the pixel area covered by a single medicine bottle in the image; Step 22, verifying whether the curve parameters corresponding to the current sample points satisfy the constraint condition of the ellipse, if yes, taking the ellipse corresponding to the current sample points as an initial ellipse, and performing step 23, if not, returning to step 21 to sequentially traverse the next point in the edge point set; Step 23, taking the pixel points in the coverage range of the ellipse into the initial ellipse, iteratively fitting and optimizing to obtain a final fitted ellipse, marking the contour points corresponding to the fitted ellipse as boundary points of the fitted ellipse, and then sequentially traversing the next point in the edge point set P until the traversal of all points is completed.
3. The vial counting method for a fully automated vial counting apparatus of a compounding center according to claim 2, wherein, The step 21 specifically comprises: In a row coordinate priority manner, sequentially selecting points from the edge point set P, after selecting each point, taking n interval points in the neighborhood range of the point as sample points, n≧6, taking the coordinate values of the n sample points into an elliptical equation to construct an elliptical fitting linear equation set: In the formula, A, B, C, D, E, and F are parameters of the ellipse, the elliptical fitting linear equation set is denoted as Mp=0, M is a coefficient matrix, p is an ellipse parameter matrix, T is a matrix transpose, when n=6, the matrix p is calculated by elimination method, and when n>6, the least square method is used to fit the ellipse parameter matrix p.
4. The vial counting method for a fully automated vial counting apparatus of a compounding center according to claim 3, wherein, In the step 23, the expression of the center coordinate (x0, y0) of the ellipse is: In the formula, x0 is the column coordinate of the center of the ellipse, and y0 is the row coordinate of the center of the ellipse.
5. The vial counting method for a fully automated vial counting apparatus of a compounding center according to claim 4, wherein, The step 4 specifically comprises: In step 41, a single first-type ellipse is selected in the contour map in the order of row coordinate priority and coordinate from small to large, and it is judged whether there is a second-type ellipse in the preset matching range of the current ellipse, if there is a unique second-type ellipse, the two ellipses are paired, and if no second-type ellipse is found, the currently selected first-type ellipse is removed, and the next first-type ellipse is sequentially traversed; In step 42, if there are multiple second-type ellipses in the preset matching range of the currently selected first-type ellipse, a bounding box is set for the region in the contour map where the currently selected first-type ellipse and each second-type ellipse is located; In step 43, a matching group is constructed respectively with the currently selected first-type ellipse and each second-type ellipse, for each matching group, a straight line passing through the centers of the two types of ellipses in the current matching group is taken as a boundary line, other pixel points in the current bounding box except the ellipse contour points are divided to obtain two straight line fitting regions, and the pixel points in the straight line fitting regions are fitted respectively; In step 44, for each matching group, it is judged whether there is a fitted straight line connecting the two ellipses in the current bounding box, if there is, it is determined that there is a connecting band between the current two ellipses, the two ellipses of the current matching group are paired and removed from the matching ellipses, if not, the next matching group is judged, if each matching group corresponding to the currently selected first-type ellipse cannot be paired, step 41 is returned, the next first-type ellipse is found and the pairing process is repeated.
6. The vial counting method for a fully automated vial counting apparatus of a compounding center according to claim 5, wherein, In step 42, the bounding box is set for the region in the contour map where the two ellipses of the current matching group are located, specifically comprising: The maximum column coordinate, the minimum column coordinate, the maximum row coordinate and the minimum row coordinate are read from the region where the currently selected first-type ellipse and any second-type ellipse in the preset matching range are located, and the rectangle formed by connecting the column where the maximum column coordinate and the minimum column coordinate are located and the row where the maximum row coordinate and the minimum row coordinate are located is taken as the bounding box of the current two ellipses.
7. The vial counting method for a fully automated vial counting apparatus of a compounding center according to claim 5, wherein, In step 43, specifically comprising: For a single straight line fitting region, the mean value x of the column coordinates of all pixel points, the mean value y of the row coordinates, the covariance Cov(x, y) and the variance Var(x) are calculated, and are expressed as: where (x j ,y j ) is the coordinate of the jth pixel point in the linear fitting region, and the fitted straight line is represented as: y=kx+b In the formula, k is the slope, and b is the intercept.
8. The vial counting method for a fully automated vial counting apparatus of a compounding center according to claim 5, wherein, The connecting band is the region where the fitted straight line satisfying the pairing condition in the current bounding box is located, and the pairing condition is that the included angle between the fitted straight line and the straight line where the centers of the current two ellipses are located is within the preset angle range.
9. The vial counting method for a fully automated vial counting apparatus of a compounding center according to claim 8, wherein, In step 44, further comprising: The straight line where the centers of the current two ellipses are located is taken as a first straight line, each fitted straight line is taken as a second straight line, the second straight line is compared with the first straight line in turn, and the included angle between the two groups of straight lines is calculated based on the slope, and the included angle between the first straight line and the second straight line is expressed as: In the formula, θ is the included angle between the first straight line and the second straight line, k1 is the slope of the first straight line, and k2 is the slope of the second straight line, if k1 does not exist, k=k2, and if k2 does not exist, k=k1.
Citation Information
Patent Citations
PE bottle detecting and sorting method based on machine vision
CN106824816A
On-line PE bottle identification and positioning method based on machine vision
CN106875441A