Tablet number detection method and system

By performing adaptive threshold calculation, binary processing and a variety of image processing technologies on the images at the opening of the medicine box, accurate detection of the number of pills is achieved, solving the problems of inefficiency and accuracy in the prior art, and improving the accuracy and efficiency of detection.

CN119693331APending Publication Date: 2025-03-25KUNMING UNIV OF SCI & TECH
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202411766687.X
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2024-12-04
Publication Date
2025-03-25

AI Technical Summary

Technical Problem

The prior art has problems of inefficiency and accuracy in detecting tablet counts. Especially in complex environments such as overlapping tablets, irregular shapes or changes in light, detection accuracy is difficult to ensure.

Method used

A tablet number detection method is adopted to obtain the image at the opening of the medicine box, perform adaptive threshold calculation and binarization processing, and combine refined processing, global area filtration, vertical slice processing and connectivity domain analysis to achieve accurate detection of the tablet number.

Benefits of technology

It improves the accuracy and efficiency of tablet detection, can accurately detect the number of tablets in complex environments, reduce the rate of misjudgment, and ensure the safety and efficiency of drug packaging.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN119693331A_ABST
    Figure CN119693331A_ABST
Patent Text Reader

Abstract

The invention discloses a tablet number detection method and system, and belongs to the technical field of image data processing. The method comprises the following steps: acquiring an image of a medicine box opening; calculating a self-adaptive threshold value of the image at the opening of the medicine box, and performing binarization processing on the image at the opening of the medicine box according to the self-adaptive threshold value to obtain a binarized image; carrying out fine processing on the binarized image to obtain a refined image; performing global area filtering on the refined image to obtain a filtered image; performing vertical slicing processing on the filtered image to obtain a sliced image; and performing connected domain analysis on the sliced image to obtain the number of the tablets. Through fine processing, global area filtering, vertical slicing processing and connected domain analysis, the independence of the connected domain in the graph can be ensured, a noise region is effectively filtered out, the accuracy of the number of tablets is improved, and the accuracy of tablet detection is remarkably improved.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the technical field of image data processing, and in particular to a method and system for detecting the number of pills. Background Art

[0002] In the modern pharmaceutical industry, with the continuous development of pharmaceutical production technology and the improvement of automation, strict control of drug quality and improvement of production efficiency have become one of the important factors for the competitiveness of pharmaceutical companies. The accuracy of the number of tablets is directly related to the correctness of the drug dosage and the safety of the patient's medication. Therefore, in the drug packaging process, it is very important to ensure the accuracy of the number of tablets in each medicine box.

[0003] Traditional methods of detecting the number of pills rely heavily on the precision of mechanical devices and the carefulness of manual inspection. However, as the speed of pharmaceutical production lines continues to increase, pharmaceutical factories need to detect the number of pills in a more efficient way. Although mechanical inspection equipment has improved efficiency to a certain extent, its accuracy is often affected when facing complex environments, such as overlapping tablets, irregular shapes, or light changes. Moreover, there are relatively few machines or research on this type of detection quantity, and it can even be said that there is none. In addition, manual inspection is not only inefficient because it relies on human operation, but also easily affected by operator fatigue or subjective judgment errors, which can easily cause errors in the number of pills. Therefore, finding a more intelligent, accurate, and efficient detection method has become an urgent problem that the pharmaceutical industry needs to solve.

[0004] Most existing research focuses on tablet defect detection, component analysis or quality classification, and relatively few studies focus on tablet quantity detection. Especially in complex production environments, problems such as tablet overlap and connection of different parts of multiple tablets are still difficult problems that need to be solved by existing technologies. Summary of the invention

[0005] The purpose of the present invention is to overcome the deficiencies in the prior art and provide a method and system for detecting the number of pills, which can effectively deal with the problem of detecting the number of pills in complex environments and ensure the accuracy and detection efficiency of the number of pills in the production process.

[0006] To achieve the above object, the present invention is implemented by adopting the following technical solutions:

[0007] In one aspect, the present invention provides a method for detecting the number of pills, comprising:

[0008] Acquire an image of the opening of the medicine box;

[0009] Calculating an adaptive threshold of the image at the opening of the medicine box, and binarizing the image at the opening of the medicine box according to the adaptive threshold to obtain a binarized image;

[0010] Performing refinement processing on the binarized image to obtain a refined image;

[0011] Performing global area filtering on the refined image to obtain a filtered image;

[0012] vertically slicing the filtered image to obtain a sliced ​​image;

[0013] Connected domain analysis is performed on the sliced ​​image to obtain the number of pills.

[0014] Optionally, the image at the opening of the medicine box is a single-channel image.

[0015] Optionally, calculating an adaptive threshold of the image at the opening of the medicine box, and binarizing the image at the opening of the medicine box according to the adaptive threshold to obtain a binarized image, including:

[0016] Smoothing the image at the opening of the medicine box to obtain a smoothed image;

[0017] Using a weighted difference method, the features of the smoothed image are extracted to obtain a differenced image;

[0018] Converting the image after the difference processing into an 8-bit unsigned format to obtain an image after format conversion;

[0019] Using the Otsu algorithm, an adaptive threshold of the image after the format conversion is calculated to obtain a binarized local image;

[0020] Repeat the process until the binarization of the entire image is completed to obtain a binarized image.

[0021] Optionally, the binarized image is represented as:

[0022] ;

[0023] ;

[0024] ;

[0025] ;

[0026] In the formula, Represents the image after binarization; Represents the grayscale value of the image at (x, y); represents the adaptive threshold; represents the argmax function; represents the variance between pixel classes; Indicates the proportion of pixels whose grayscale value is less than the adaptive threshold; Indicates the proportion of pixels whose grayscale value is greater than the adaptive threshold; The average gray value of the ratio of pixels whose gray value is less than the adaptive threshold; The average gray value of the proportion of pixels whose gray value is greater than the adaptive threshold; Represents the weighted average gray value.

[0027] Optionally, the binarized image is refined using a morphological operator to obtain a refined image, which is expressed as:

[0028] ;

[0029] ;

[0030] ;

[0031] In the formula, represents the refined image; Represents the image after binarization; A structural element representing a selection; represents the domain; Indicates that the structural element is at position The translation of .

[0032] Optionally, the filtered image is represented as:

[0033] ;

[0034] In the formula, represents the filtered image; Indicates The area of ​​the connected domain; Indicates the minimum filtering threshold; Indicates the maximum filtering threshold.

[0035] Optionally, vertically slicing the filtered image to obtain a sliced ​​image includes:

[0036] According to the number of columns of the filtered image, the filtered image is divided into ten equal slices to obtain sliced ​​images;

[0037] The sliced ​​image is stored in a slice container, where the type of the slice container is Mat type.

[0038] Optionally, performing a connected domain analysis on the sliced ​​image to obtain the number of pills in the medicine box includes:

[0039] The sliced ​​image is scanned using a connected domain of eight connectivity and a label in CV_16U format to obtain a scanned image. For an image of non-background pixels, the labels of its adjacent image pixels are checked. If the adjacent image pixels have the same label, the scanned image pixel is assigned the label. If the adjacent image pixels do not have the same label, a new label is created for the scanned image pixel.

[0040] Using the union-find algorithm, image pixels in the same connected domain are merged into the same label;

[0041] The process is repeated until all sliced ​​images are traversed, and the label with the largest connected domain in the final label of the image is deducted from the length of the upper side of the medicine box and the length of the lower side of the medicine box to obtain the number of pills.

[0042] Optionally, the connected domain of the eight-connectivity is expressed as:

[0043] ;

[0044] ;

[0045] In the formula, Represents the connected domain of the image at (x, y) with eight connectivity; Represents the connected domain of the image at (x, y) with four connectivity.

[0046] In another aspect, the present invention provides a system for detecting the number of pills, comprising:

[0047] An image acquisition module is used to: acquire an image of the opening of the medicine box;

[0048] A binarization processing module is used to: calculate an adaptive threshold of the image at the opening of the medicine box, and perform binarization processing on the image at the opening of the medicine box according to the adaptive threshold to obtain a binarized image;

[0049] A refinement processing module is used to: perform refinement processing on the binarized image to obtain a refined image;

[0050] A global area filtering module is used to perform global area filtering on the refined image to obtain a filtered image;

[0051] A vertical slicing processing module is used to: perform vertical slicing processing on the filtered image to obtain a sliced ​​image;

[0052] The connected domain analysis module is used to perform connected domain analysis on the sliced ​​image to obtain the number of pills.

[0053] Compared with the prior art, the present invention has the following beneficial effects:

[0054] The present invention can automatically calculate the optimal threshold value through binarization processing, ensure the separation effect of image foreground and background, reduce the interference of manual adjustment, and ensure the independence of connected domains in the graph, effectively filter out noise areas, increase the accuracy of the number of tablets, and significantly improve the accuracy of tablet detection through fine processing, global area filtering, vertical slicing processing and connected domain analysis. Compared with traditional tablet quantity detection methods, it can cope with higher-speed production lines, greatly improve the detection efficiency, reduce the misjudgment rate, and can accurately detect the number of tablets in complex environments, ensure the safety and efficiency of drug packaging, solve the problem of uneven number of tablets in each package, and realize efficient automatic quality assurance detection. BRIEF DESCRIPTION OF THE DRAWINGS

[0055] Figure 1 Shown is a schematic diagram of the process of the method for detecting the number of tablets in one embodiment of the present invention;

[0056] Figure 2 It is a schematic diagram showing the best shooting method of the image at the opening of the medicine box of the present invention in one embodiment;

[0057] Figure 3 It is a schematic diagram showing the superiority of overcoming the defects of industrial chips in the prior art;

[0058] Figure 4 Shown is a schematic diagram of a digital tablet management system in one embodiment of the present invention;

[0059] Figure 5 The figure is a schematic diagram of the accuracy of pill quantity detection in a complex environment in one embodiment of the present invention. DETAILED DESCRIPTION

[0060] The technical solution of the present invention is described in detail below through the accompanying drawings and specific embodiments. It should be understood that the embodiments of the present invention and the specific features in the embodiments are detailed descriptions of the technical solution of the present invention, rather than limitations on the technical solution of the present invention. The embodiments of the present invention and the technical features in the embodiments may be combined with each other unless there is a conflict.

[0061] The term "and / or" is only a description of the association relationship between related objects, indicating that there can be three relationships. For example, A and / or B can mean: A exists alone, A and B exist at the same time, and B exists alone. In addition, the character " / " generally indicates that the related objects are in an "or" relationship.

[0062] Example 1

[0063] like Figure 1 As shown, this embodiment introduces a method for detecting the number of pills, which specifically includes the following steps:

[0064] Step 1: Get the image of the opening of the medicine box. That is, under the appropriate fill light solution, the industrial camera is fixed beside the conveyor belt of the factory production line, and the position of the opening of the medicine box is fixed to obtain a single-channel image of the opening to meet the high-speed operation. The captured image is as follows Figure 2 shown.

[0065] Step 2: Calculate the adaptive threshold of the image at the opening of the medicine box, and perform binarization processing on the image at the opening of the medicine box according to the adaptive threshold to obtain a binarized image, specifically:

[0066] The acquired single-channel image is smoothed to obtain a smoothed image, so as to reduce noise and enhance image details;

[0067] Extracting the significant features of the smoothed image using a weighted difference method to obtain a differenced image;

[0068] Next, the image composed of pixel values ​​obtained after the difference processing is converted into an 8-bit unsigned format to obtain an image after format conversion;

[0069] Finally, the Otsu algorithm is used to calculate the optimal threshold under the window and complete the local binarization of the image;

[0070] The whole image is binarized repeatedly to obtain a binarized image for subsequent measurement. The essential idea of ​​the Otsu algorithm is to maximize the inter-class variance. The core algorithm expression is:

[0071] ;

[0072] Formula (1) gives the inter-class variance, which is used to measure the difference between two types of pixels. The larger the inter-class variance, the greater the difference in the grayscale distribution of the two types of pixels, and the better the segmentation effect. This formula calculates the square of the difference between the weighted grayscale means of the two categories of pixels and combines the proportion of each type of pixels to reflect the effect of threshold segmentation, which can better distinguish the foreground and background. Theoretically, formula (1) cannot be obtained directly. The specific derivation process is as follows:

[0073] use The intra-class variance formula Written as: , and then extract the common factors: , using hidden conditions , can be transformed into , and finally use the associative law and hidden conditions , we can get the formula of between-class variance .and and The difference is is the within-class variance, is the between-class variance.

[0074] (2);

[0075] Formula (2) is a traversal process, which traverses all possible thresholds to find the optimal threshold that maximizes the inter-class variance. The threshold corresponding to the maximum inter-class variance can best distinguish the two types of pixels.

[0076] In the formula, Represents the image after binarization; Represents the grayscale value of the image at (x, y); Indicates the horizontal coordinate of the element; Indicates the vertical coordinate of the element; represents the adaptive threshold; represents the argmax function; Indicates the proportion of pixels whose grayscale value is less than the adaptive threshold; Indicates the proportion of pixels whose grayscale value is greater than the adaptive threshold; The average gray value of the ratio of pixels whose gray value is less than the adaptive threshold; The average gray value of the proportion of pixels whose gray value is greater than the adaptive threshold; Represents the weighted average gray value.

[0077] After finding the maximum inter-class threshold, the obtained image is converted into a binary image according to the maximum threshold of each window to prepare for subsequent recognition.

[0078] Step 3: Performing refinement processing on the binarized image to obtain a refined image, specifically:

[0079] First, use OpenCV to obtain the structural element of the morphological operation. After experiments, it is determined that it is a 5×5 rectangle. Use this structural element to dilate the image first and then erode it to avoid disconnection at some fine points of the connected domain in the image, ensure the independence of the connected domain, and facilitate subsequent connected domain analysis. Dilation processing first:

[0080] ;

[0081] Then take the corrosion treatment:

[0082] ;

[0083] The refined morphological processing of preprocessing can be completed according to the formula:

[0084] ;

[0085] Get the refined image.

[0086] In the formula, represents the refined image; Represents the image after binarization; A structural element representing a selection; represents the domain; Indicates that the structural element is at position The translation at and The two symbols represent expansion and erosion respectively.

[0087] Step 4: Perform global area filtering on the refined image to obtain a filtered image, specifically:

[0088] Global area filtering is used to filter out too small or too large connected domains by setting the area filtering threshold. Too small connected domains are usually noise, and too large connected domains may be caused by light strips caused by the working environment or overexposure. The filtered image is expressed as:

[0089] ;

[0090] In the formula, represents the filtered image; Indicates The area of ​​a connected domain; Indicates the minimum filtering threshold; Indicates the maximum filtering threshold.

[0091] Step 5: vertically slice the filtered image to obtain a sliced ​​image, specifically:

[0092] First, define a slice container of Mat type to store the cut image slices;

[0093] According to the number of columns of the filtered image, the filtered image is sliced ​​into ten equal parts to obtain sliced ​​images, and the sliced ​​images are stored in a slice container.

[0094] Step 6: Perform connected domain analysis on the sliced ​​image to obtain the number of pills, specifically:

[0095] First, the connected domain parameter is set to 8-connectivity, and the CV_16U label output format is used to ensure the correct identification of the connected domain and improve the detection accuracy of the number of pills. The meaning of 8-connectivity is to find the labels of the eight directions of the pixel point. The connected domain of the eight-connectivity is expressed as:

[0096] ;

[0097] ;

[0098] In the formula, A connected domain representing eight connectivity; A connected domain representing four-connectivity;

[0099] Take out the first slice, perform the first scan on it, for each non-background pixel, check the labels of its upper, lower, left and right adjacent image pixels, if there are the same labels among its adjacent image pixels, assign the label to the scanned image pixel, if there are no same labels among its adjacent image pixels, create a new label for the scanned image pixel;

[0100] The second pass scans the image and uses the union-find algorithm to process label equivalence, merging pixels belonging to the same connected domain into the same label;

[0101] After the processing is completed, a labeled image will be generated, in which the value of each pixel in the image corresponds to the label value of the connected area to which it belongs (the labels are 1 to n, corresponding to different connected areas), and the label of the background pixel is 0.

[0102] After obtaining the first slice after labeling, the process is repeated until all sliced ​​images are traversed. The label with the largest connected domain in the final label of the image is deducted from the length of the upper side of the medicine box and the length of the lower side of the medicine box to obtain the number of pills.

[0103] Step 7: After the connected domain analysis, the following statistical information is calculated to optimize subsequent detection and analysis for the convenience of human-computer interaction:

[0104] Area: Count the total number of pixels in each connected domain and calculate the area of ​​the connected region. This information is helpful for debugging and preparing for later area filtering. The formula is:

[0105] ;

[0106] In the formula, Represents the total number of pixels in the connected domain; Indicates that the pixel is The pixel value at ; Indicates The horizontal coordinate of the pixel point; Indicates The vertical coordinate of the pixel.

[0107] Bounding Box: Calculate the minimum bounding box of each connected domain, that is, its minimum coverage area in the image, to evaluate the shape and position of the pills and assist in detecting the number of pills.

[0108] Centroid: Calculate the centroid coordinates of each connected domain. By tracking the centroid position, the counting accuracy and defect detection accuracy of the tablets can be further improved. The formula is:

[0109] ;

[0110] ;

[0111] In the formula, represents the abscissa of the center of mass; The vertical coordinate of the center of mass.

[0112] After the core algorithm design is completed, the design of the chip management system enters the subsequent links such as UI interface design, related hardware driver writing and hardware platform construction. Figure 4 As shown, it mainly focuses on user convenience and data visualization. The overall interface is divided into four parts, which are described as follows:

[0113] Nameplate display area: This area is used to display the system name and basic information, providing clear system identification and helping users quickly understand the status of the current operating system.

[0114] Tablet count display area: This area displays the number of tablets calculated by the core algorithm. The data is updated in real time to ensure that users can view the exact number of tablets at any time.

[0115] Real-time display area of ​​test images: This area is used to display the test images acquired and processed by the industrial camera, providing clear image feedback for users to conduct human-computer interaction and image monitoring. At the same time, users can intuitively view the tablet test process and results to ensure the correct operation of the system.

[0116] Functional button area:

[0117] ① "Start" button: used to start the system's automatic tablet counting function. The system relies on sensors to achieve real-time detection and counting of tablets, and is suitable for fully automated operation of the production line.

[0118] ② “Count pieces” button: Provides manual counting function. Users can use this button to perform debugging and manual control, which is convenient for manual adjustment and verification in special circumstances.

[0119] The design of the entire UI is concise and clear, with real-time monitoring and operation functions. It can not only meet the automation requirements of the production line, but also has the convenience of human-computer interaction. It also solves the problem of most of the overlapping tablets that has troubled the factory for a long time in the prior art. The superiority of this embodiment in overcoming the defects of industrial tablet counting in the prior art is shown in detail in the schematic diagram Figure 3 shown.

[0120] Example 2

[0121] This embodiment introduces a pill quantity detection system, including:

[0122] An image acquisition module is used to: acquire an image of the opening of the medicine box;

[0123] A binarization processing module is used to: calculate an adaptive threshold of the image at the opening of the medicine box, and perform binarization processing on the image at the opening of the medicine box according to the adaptive threshold to obtain a binarized image;

[0124] A refinement processing module is used to: perform refinement processing on the binarized image to obtain a refined image;

[0125] A global area filtering module is used to perform global area filtering on the refined image to obtain a filtered image;

[0126] A vertical slicing processing module is used to: perform vertical slicing processing on the filtered image to obtain a sliced ​​image;

[0127] The connected domain analysis module is used to perform connected domain analysis on the sliced ​​image to obtain the number of pills.

[0128] Example 3

[0129] Based on Example 1, this example introduces a specific experimental design of a method for detecting the number of tablets, including the following steps:

[0130] Step 1: Build the hardware platform. When building the platform, try to avoid using reflective conveyor belts, otherwise various noise points or relatively large light bands will appear in the collected pictures, affecting the counting results. When filling the light, you only need to ensure that the industrial camera can take a clear picture of the side profile of the tablets. Remember not to overexpose or blur it. It is best to use an industrial camera as the collection device, so that it can capture the high-speed running medicine box and focus better. The collection angle of view is preferably fixed, such as Figure 2 shown.

[0131] Step 2: Carry out software adaptation experiments. Because the light intensity in factories in different places is different, the corresponding pre-processing of the software will find corresponding changes, such as the critical value of area filtering and the relevant parameters of median filtering. In the experiment, we debugged the relevant parameters according to our test environment, such as setting the critical value of area filtering to 200, changing the vertical slice to 20 times, etc. Finally, after a small sample test, the accuracy of the standardized platform was tested to be about 98%. For details of the test results, see Figure 5 shown.

[0132] Those skilled in the art will appreciate that the embodiments of the present application may be provided as methods, systems, or computer program products. Therefore, the present application may adopt the form of a complete hardware embodiment, a complete software embodiment, or an embodiment combining software and hardware. Moreover, the present application may adopt the form of a computer program product implemented on one or more computer-usable storage media (including but not limited to disk storage, CD-ROM, optical storage, etc.) containing computer-usable program codes.

[0133] The present application is described with reference to the flowcharts and / or block diagrams of the methods, devices (systems), and computer program products according to the embodiments of the present application. It should be understood that each process and / or box in the flowchart and / or block diagram, as well as the combination of the processes and / or boxes in the flowchart and / or block diagram, can be implemented by computer program instructions. These computer program instructions can be provided to a processor of a general-purpose computer, a special-purpose computer, an embedded processor, or other programmable data processing device to generate a machine, so that the instructions executed by the processor of the computer or other programmable data processing device generate instructions for implementing the processes in the flowchart and / or block diagram. Figure 1 A process or multiple processes and / or boxes Figure 1 A device that provides the functions specified in a block or multiple blocks.

[0134] These computer program instructions may also be stored in a computer-readable memory capable of directing a computer or other programmable data processing device to operate in a specific manner, so that the instructions stored in the computer-readable memory produce an article of manufacture comprising an instruction device, which implements the process Figure 1 A process or multiple processes and / or boxes Figure 1 A function specified in one or more boxes.

[0135] These computer program instructions can also be loaded onto a computer or other programmable data processing device so that a series of operating steps are executed on the computer or other programmable device to produce a computer-implemented process, thereby providing instructions for implementing the process in the computer or other programmable device. Figure 1 A process or multiple processes and / or boxes Figure 1 A step that specifies a function in one or more boxes.

[0136] The embodiments of the present invention are described above in conjunction with the accompanying drawings, but the present invention is not limited to the above-mentioned specific implementation methods. The above-mentioned specific implementation methods are merely illustrative and not restrictive. Under the enlightenment of the present invention, ordinary technicians in this field can also make many forms without departing from the scope of protection of the purpose of the present invention and the claims, which all fall within the protection of the present invention.

Claims

1. A method for detecting the number of pills, characterized in that: include: Acquire an image of the medicine box opening; Calculating an adaptive threshold of the image at the opening of the medicine box, and binarizing the image at the opening of the medicine box according to the adaptive threshold to obtain a binarized image; Performing refinement processing on the binarized image to obtain a refined image; Performing global area filtering on the refined image to obtain a filtered image; vertically slicing the filtered image to obtain a sliced ​​image; Connected domain analysis is performed on the sliced ​​image to obtain the number of pills.

2. The method for detecting the number of pills according to claim 1, characterized in that: The image at the opening of the medicine box is a single-channel image.

3. The method for detecting the number of pills according to claim 1, characterized in that: Calculating an adaptive threshold of the image at the opening of the medicine box, and binarizing the image at the opening of the medicine box according to the adaptive threshold to obtain a binarized image, including: Smoothing the image at the opening of the medicine box to obtain a smoothed image; Using a weighted difference method, the features of the smoothed image are extracted to obtain a differenced image; Converting the image after the difference processing into an 8-bit unsigned format to obtain an image after format conversion; Using the Otsu algorithm, an adaptive threshold of the image after the format conversion is calculated to obtain a binarized local image; Repeat the process until the binarization of the entire image is completed to obtain a binarized image.

4. The method for detecting the number of pills according to claim 3, characterized in that: The binarized image is represented as: ; ; ; ; In the formula, Represents the image after binarization; Represents the grayscale value of the image at (x, y); represents the adaptive threshold; represents the argmax function; represents the variance between pixel classes; Indicates the proportion of pixels whose grayscale value is less than the adaptive threshold; Indicates the proportion of pixels whose grayscale value is greater than the adaptive threshold; The average gray value of the ratio of pixels whose gray value is less than the adaptive threshold; The average gray value of the proportion of pixels whose gray value is greater than the adaptive threshold; Represents the weighted average gray value.

5. The method for detecting the number of pills according to claim 1, characterized in that: The binarized image is refined using a morphological operator to obtain a refined image, which is expressed as: ; ; ; In the formula, represents the refined image; Represents the image after binarization; A structural element representing a selection; represents the domain; Indicates that the structural element is at position The translation of .

6. The method for detecting the number of pills according to claim 1, characterized in that: The filtered image is represented as: ; In the formula, represents the filtered image; Indicates The area of ​​a connected domain; Indicates the minimum filtering threshold; Indicates the maximum filtering threshold.

7. The method for detecting the number of pills according to claim 1, characterized in that: The filtered image is vertically sliced ​​to obtain a sliced ​​image, including: According to the number of columns of the filtered image, the filtered image is divided into ten equal slices to obtain sliced ​​images; The sliced ​​image is stored in a slice container, where the type of the slice container is Mat type.

8. The method for detecting the number of pills according to claim 1, characterized in that: Performing connected domain analysis on the sliced ​​image to obtain the number of pills in the medicine box includes: The sliced ​​image is scanned using a connected domain of eight connectivity and a label in CV_16U format to obtain a scanned image. For an image of non-background pixels, the labels of its adjacent image pixels are checked. If the adjacent image pixels have the same label, the scanned image pixel is assigned the label. If the adjacent image pixels do not have the same label, a new label is created for the scanned image pixel. Using the union-find algorithm, image pixels in the same connected domain are merged into the same label; The process is repeated until all sliced ​​images are traversed, and the label with the largest connected domain in the final label of the image is deducted from the length of the upper side of the medicine box and the length of the lower side of the medicine box to obtain the number of pills.

9. The method for detecting the number of pills according to claim 8, characterized in that: The connected domain of the eight-connectivity is expressed as: ; ; In the formula, Represents the connected domain of the image at (x, y) with eight connectivity; Represents the connected domain of the image at (x, y) with four connectivity.

10. A pill quantity detection system, characterized in that: include: An image acquisition module is used to: acquire an image of the opening of the medicine box; A binarization processing module is used to: calculate an adaptive threshold of the image at the opening of the medicine box, and perform binarization processing on the image at the opening of the medicine box according to the adaptive threshold to obtain a binarized image; A refinement processing module is used to: perform refinement processing on the binarized image to obtain a refined image; A global area filtering module is used to perform global area filtering on the refined image to obtain a filtered image; A vertical slicing processing module is used to: perform vertical slicing processing on the filtered image to obtain a sliced ​​image; The connected domain analysis module is used to perform connected domain analysis on the sliced ​​image to obtain the number of pills.