A method and device for online detection of tobacco width based on YOLOv8
By using an online tobacco width detection method based on YOLOv8, the problems of low accuracy and large number of parameters in tobacco width detection are solved. A lightweight model is constructed to achieve high-precision and fast tobacco width detection.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- ZHENGZHOU TOBACCO RES INST OF CNTC
- Filing Date
- 2025-12-15
- Publication Date
- 2026-06-09
Smart Images

Figure CN122176028A_ABST
Abstract
Description
Technical Field
[0001] This invention relates to an online detection method and device for tobacco width based on YOLOv8, belonging to the field of cigarette manufacturing technology. Background Technology
[0002] The shredding process is a crucial step in cigarette manufacturing, primarily transforming sheet-like tobacco materials into shredded form to meet the processing requirements of cigarette production. It is a core technology within the cigarette manufacturing line. However, tobacco shred width is one of the main factors affecting cigarette product quality; different tobacco shred widths influence tar content, filling value, and other parameters. Therefore, accurate measurement of tobacco shred width is of great significance for controlling the physical properties of cigarettes, improving product quality, and reducing the consumption of raw and auxiliary materials.
[0003] Currently, researchers have proposed various measurement schemes for tobacco shred width: First, traditional methods, such as edge detection based on image processing, Hough transform, variable diameter circle method, coordinate point regression fitting method, and skeleton morphology analysis method. These methods are relatively mature and have low computational complexity, and can meet the measurement needs of tobacco shred width to a certain extent. However, when dealing with the complex and randomly distributed tobacco shred morphology commonly found in tobacco industrial production, they often suffer from poor feature robustness and insufficient algorithm generalization ability, resulting in limited measurement accuracy. Second, deep learning methods. With the rapid development of artificial intelligence technology, deep learning has gradually demonstrated its significant advantages in areas such as object detection and image recognition. These methods have strong automatic feature learning capabilities and high prediction accuracy, but they usually suffer from problems such as excessive model parameters and high computational resource consumption. Furthermore, in complex scenarios such as multiple overlapping and densely stacked targets in industrial settings, their detection capabilities and stability still need further optimization, such as the YOLOv3 method. Here, YOLOv3 is the third version of the YOLO series. YOLO (You Only Look Once) is a real-time object detection algorithm based on deep learning. Its core idea is to transform the object detection task into a single global inference problem, directly predicting the bounding box and category of objects on the image.
[0004] It is evident that the insufficient feature representation capability of existing detection methods is a key issue restricting measurement accuracy. Moreover, mainstream detection models generally suffer from excessively large parameter counts, which not only increases computational complexity but also leads to time delays in model training and inference, severely impacting detection efficiency. Summary of the Invention
[0005] The purpose of this invention is to provide an online tobacco width detection method and device based on YOLOv8, in order to solve the problems of low detection accuracy and large number of detection model parameters.
[0006] To achieve the above objectives, this invention proposes an online tobacco width detection method based on YOLOv8, comprising the following steps:
[0007] 1) Define the boundaries of several tobacco strands in the original image containing several tobacco strands;
[0008] 2) Call the pre-built YOLOv8-based tobacco detection model to extract several tobacco regions containing complete single tobacco shreds from the segmented image after dividing the tobacco shred boundaries;
[0009] 3) Calculate the pixel distance value in the width direction of each tobacco region in the corresponding image;
[0010] 4) Based on the pre-obtained spatial relationship model between tobacco width and image pixel units, determine the tobacco width corresponding to the pixel distance value in the width direction of all tobacco regions in the corresponding image.
[0011] Furthermore, a tobacco detection model based on YOLOv8 is constructed using the following method:
[0012] Based on the YOLOv8 model architecture, a network structure is built in the backbone network to perform convolution operations on the target channels of the input features, wherein the target channels are feature channels dynamically selected through a gating mechanism.
[0013] Before the SPPF module in the backbone network, a multi-scale convolutional attention module is added to capture local details and global contextual information in tobacco images.
[0014] The original loss function is replaced with a target loss function that balances sample weights through a dynamic adjustment focusing mechanism to generate the YOLOv8-based tobacco detection model.
[0015] Furthermore, by integrating the C2f module and the FasterBlock module, and embedding activation functions for dynamically filtering feature channels, the network structure for performing convolution operations on the target channels of the input features is generated.
[0016] Furthermore, the multi-scale convolutional attention module includes a channel attention block for weighting each channel of the feature map, a spatial attention block for weighting the spatial location of the feature map, and a multi-scale convolutional block for extracting multi-scale features.
[0017] Furthermore, the target loss function is expressed by the following formula:
[0018] ;
[0019] Specifically, , , ;
[0020] in, The target loss function; r is the non-monotonic focusing coefficient; β represents the outlier; δ is the gradient gain; α is the balance factor. The monotonic focusing coefficient, The normalization factor, i.e., the dynamic moving average, is indicated by the superscript. This indicates that a separation operation is being performed; The penalty items representing WIOU; This is due to IOU loss.
[0021] Furthermore, the marker watershed algorithm is used to divide the tobacco shred boundaries from the original image containing several tobacco shreds.
[0022] Furthermore, the variable diameter circle method is used to calculate the pixel distance value in the width direction of each tobacco region in the corresponding image.
[0023] Furthermore, based on the pre-obtained spatial relationship model between tobacco width and image pixel units, the tobacco width corresponding to the pixel distance value in the width direction of all tobacco regions in the corresponding image is determined by the following method:
[0024] Based on Zhang Zhengyou's calibration method, a spatial relationship model between tobacco width and image pixel units is established; the pixel distance values in the width direction of the image are converted into the actual physical distance of the tobacco width through a proportional transformation relationship.
[0025] Furthermore, the spatial relationship model is expressed by the following formula:
[0026] Where y is the actual physical size; x is the pixel distance value; and K is the conversion factor.
[0027] On the other hand, the present invention also proposes an online tobacco width detection device based on YOLOv8, including a processor, which is used to execute the above-mentioned online tobacco width detection method based on YOLOv8.
[0028] The beneficial effects of this invention are as follows: It divides the original image containing several tobacco shreds into several tobacco shred boundaries; it calls a pre-built tobacco shred detection model based on YOLOv8 to extract several tobacco shred regions containing complete single tobacco shreds from the segmented image after dividing the tobacco shred boundaries; it calculates the pixel distance value of each tobacco shred region in the corresponding image along the width direction; based on a pre-obtained spatial relationship model between tobacco shred width and image pixel units, it determines the tobacco shred width corresponding to the pixel distance value in the corresponding image along the width direction for all tobacco shred regions. This solves the problem of inaccurate detection of tobacco shreds in dense, overlapping, and disordered situations, leading to low accuracy in width measurement, and achieves rapid detection and high-precision measurement of tobacco shred width. Attached Figure Description
[0029] Figure 1 This is a schematic diagram of the original image in a practical application scenario of the online tobacco width detection method based on YOLOv8 proposed in this invention.
[0030] Figure 2 This is a schematic diagram of image segmentation in a practical application scenario for the online detection method of tobacco width based on YOLOv8 proposed in this invention;
[0031] Figure 3 This is a schematic diagram of the FasterBlock module used in constructing a YOLOv8-based tobacco detection model in a practical application scenario for the online tobacco width detection method based on YOLOv8 proposed in this invention.
[0032] Figure 4 This is a schematic diagram of the CGLU model used in constructing a YOLOv8-based tobacco detection model in a practical application scenario for the online tobacco width detection method based on YOLOv8 proposed in this invention.
[0033] Figure 5 This is a schematic diagram of the MSCA module used in constructing a YOLOv8-based tobacco detection model in a practical application scenario for the online tobacco width detection method based on YOLOv8 proposed in this invention.
[0034] Figure 6 This is a network structure diagram of the YOLOv8-based tobacco detection model CM-YOLOv8n, which is constructed in a practical application scenario for the online tobacco width detection method based on YOLOv8 proposed in this invention.
[0035] Figure 7 This is a schematic diagram of an online tobacco width detection method based on YOLOv8 proposed in this invention in an image acquisition scenario;
[0036] Figure 8This is a schematic diagram of the structure of the online tobacco width detection method based on YOLOv8 proposed in this invention for detecting tobacco in a practical application scenario using different models;
[0037] Figure 9 This is a flowchart illustrating the process of an online tobacco width detection method based on YOLOv8 proposed in this invention in a practical application scenario. Detailed Implementation
[0038] To make the objectives, technical solutions, and advantages of the present invention clearer, the present invention will be further described in detail below with reference to the accompanying drawings and embodiments.
[0039] Analysis of width detection methods reveals that insufficient feature representation capabilities are a key constraint on measurement accuracy. Current mainstream detection models generally suffer from excessively large parameter counts, increasing computational complexity and causing delays in model training and inference, severely impacting detection efficiency. Therefore, this invention proposes a phased "detect first, measure later" processing strategy, laying the foundation for subsequent geometric measurement through high-precision target detection. Currently, in terms of target detection model selection, YOLO technology has advanced to a level capable of meeting the needs of various complex applications and continues to be optimized. YOLOv8 stands out in the YOLO series due to its high accuracy and good stability, and is mainly divided into versions such as YOLOv8n, YOLOv8s, YOLOv8m, YOLOv8l, and YOLOv8x. Among them, YOLOv8n is the least complex model, maintaining high detection accuracy while possessing faster inference speed. Considering that tobacco detection requires both speed and high accuracy, this invention selects YOLOv8n as the benchmark model. However, it still faces two major challenges in the tobacco industry: ① the number of basic network parameters is too large; ② the traditional convolutional structure is not good at extracting features from densely stacked targets.
[0040] Therefore, the inventive concept of this invention is as follows: considering that tobacco detection needs to meet the requirements of speed and high accuracy, the YOLOv8n model with low complexity and high accuracy is selected as the benchmark model. The tobacco detection model is constructed by combining the C2f-fc module (C2f-Faster-CGLU, abbreviated as C2f-FC), the WIOU loss function and the MSCA (Multi-Scale Convolutional Attention) module. This accurately extracts the tobacco region in the original image, avoiding the problem of low detection efficiency due to large number of parameters in the existing tobacco width detection methods, and providing an accurate data source for subsequent tobacco width detection. At the same time, the invention cleverly utilizes the pixel distance value and the pre-obtained spatial relationship model between tobacco width and image pixel units to achieve high-precision measurement of tobacco width.
[0041] Detailed implementation method 1:
[0042] This invention proposes an online tobacco width detection method based on YOLOv8, comprising steps S11, S12, S13, and S14, specifically:
[0043] Step S11: Define the boundaries of several tobacco shreds in the original image containing several tobacco shreds. Here, the several tobacco shreds in the original image can be stacked or evenly arranged; there is no limitation. The stacked tobacco shreds refer to complex forms such as multi-target overlap or dense stacking, for example... Figure 1 The image shown is a schematic diagram of the original image in a practical application scenario of the online tobacco width detection method based on YOLOv8 proposed in this invention.
[0044] The process of defining the boundaries of several tobacco shreds refers to selecting and marking the outlines of the tobacco shreds from the original image, preparing for subsequent extraction of tobacco shred regions. When marking the tobacco shred boundaries, this invention preferably uses a watershed marking algorithm. The basic idea of this watershed algorithm is to view the image as a topographical feature in geodesy, where the gray value of each pixel in the image represents the altitude of that location, and each local minimum and its affected area is called a catchment basin, with the boundary of the catchment basin forming a watershed. Specifically:
[0045] First, morphological opening operations are performed on the input binary mask to remove noise, followed by closing operations to fill holes. These morphological opening and closing operations are two fundamental operations based on digital morphology. They process the binary mask using structuring elements to achieve specific image processing objectives. Opening involves erosion followed by dilation of the binary mask, eliminating small noise points, smoothing foreground boundaries, and separating adhered targets. Closing involves dilation followed by erosion of the binary mask, filling small holes, smoothing background boundaries, and connecting broken targets. Next, the processed mask is dilated to generate a defined background region, and the foreground probability is calculated based on distance transform. Foreground regions are then extracted using thresholding. After determining unknown regions by the difference between the background and foreground, connected component analysis is used to label the foreground region, incrementing all labels by 1, retaining 0 as background, and labeling unknown regions as -1. Then, connected component statistics are traversed to filter out small regions with an area less than 300 pixels and setting their corresponding labels to 0. Finally, the watershed algorithm is applied to delineate boundaries (see...). Figure 2 (The white lines in the image) complete the fine segmentation, filtering, and labeling of overlapping targets, and the final image result is as follows. Figure 2 As shown.
[0046] Step S12 involves calling a pre-built YOLOv8-based tobacco detection model to extract several tobacco regions containing complete single tobacco shreds from the segmented image after dividing the tobacco shred boundaries. Here, the segmented image is the image after dividing the original image into several tobacco shred boundaries. Regarding the selection of target detection models, YOLO technology has evolved to a level capable of meeting the needs of various complex applications and is still being optimized. YOLOv8 stands out in the YOLO series due to its high accuracy and good stability, and is mainly divided into versions such as YOLOv8n, YOLOv8s, YOLOv8m, YOLOv8l, and YOLOv8x. Among them, YOLOv8n is the model with the lowest complexity, maintaining high detection accuracy while having a faster inference speed. Considering that tobacco detection needs to meet the requirements of speed and high accuracy, this invention preferably uses YOLOv8n as the benchmark model.
[0047] It should be noted that the tobacco detection model based on YOLOv8 is constructed through the following steps S21, S22, and S23:
[0048] Step S21: Based on the YOLOv8 model architecture, a network structure is built in the backbone network to perform convolution operations on the target channels of the input features. The target channels are dynamically selected through a gating mechanism, significantly reducing the number of model parameters and computational complexity. Step S22: Before the SPPF (Spatial Pyramid Pooling-Fast) module in the backbone network, a multi-scale convolutional attention module is added to capture local details and global contextual information in the tobacco image, improving the model's ability to understand complex scenes in the original image. Step S23: The original loss function is replaced with a target loss function that balances sample weights through a dynamic focusing mechanism, effectively reducing the influence of set factors and improving the model's detection performance and generalization ability. Based on steps S21-S23, the YOLOv8-based tobacco detection model is generated. This model has the fewest detection parameters and significantly reduced floating-point operations, demonstrating higher detection efficiency and stronger generalization ability.
[0049] Step S13: Calculate the pixel distance value in the width direction of each tobacco region in the corresponding image; here, the variable diameter circle method is used to calculate the pixel distance value in the width direction of each tobacco region in its corresponding image. The variable diameter circle method uses a circle with an ever-increasing radius inside the tobacco outline until the circumference is tangent to the outer outline of the tobacco, and the diameter of the circle is used as the local tobacco width.
[0050] Step S14: Based on the pre-obtained spatial relationship model between tobacco width and image pixel units, determine the tobacco width corresponding to the pixel distance value in the width direction of all tobacco regions in the corresponding image; specifically, based on Zhang Zhengyou's calibration method, establish a spatial relationship model between tobacco width and image pixel units; convert the pixel distance value in the width direction of the image into the actual physical distance of the tobacco width through a proportional transformation relationship, wherein the spatial relationship model is expressed by the following formula:
[0051]
[0052] Where y represents the actual physical size; x represents the pixel distance value; and K is the conversion factor. It should be noted that the physical size reconstruction process infers or reconstructs the actual physical size of an object in the real world by using the pixel dimensions of the image. This process is based on the camera's intrinsic and extrinsic parameters. First, it determines the spatial relationship between the measured plane and the camera's imaging unit, and then calculates the correspondence between the real physical space and the pixel distance using a proportional relationship. The significance of the reconstruction lies in obtaining the true physical size of the actual space through the image space (i.e., the camera's imaging space). Calculations show that this invention preferably uses a conversion factor K = 0.033 mm / pixel as the obtained distribution of the actual physical size of abnormal tobacco shreds.
[0053] Through the above steps S11-S14 and S21-S23, a tobacco shred detection model based on the YOLOv8 model architecture, which has a small number of detection parameters and high accuracy, is used to extract the tobacco shred region, thereby improving the efficiency and accuracy of tobacco shred region extraction. Combined with the variable diameter circle method to calculate the pixel distance value of each tobacco shred region, the needs of different tobacco shred shapes / particles are met, providing an accurate data foundation for subsequent tobacco shred width conversion, thereby improving the accuracy of tobacco shred width detection.
[0054] Method Detailed Implementation 2:
[0055] Following the above embodiments of the present invention, in the online tobacco width detection method based on YOLOv8 proposed in the present invention, for step S21, it should be noted that by integrating the C2f module and the FasterBlock module, and embedding an activation function for dynamically filtering feature channels, the network structure for performing convolution operations on the target channels of the input features is generated.
[0056] Here, the C2f module enhances feature extraction capabilities through multi-scale feature fusion and residual connections. Its core function is to enhance feature extraction capabilities, optimize gradient flow, achieve feature fusion, and reduce the number of model parameters.
[0057] The FasterBlock module optimizes computational efficiency through efficient convolution and channel rearrangement. Its core value lies in performance optimization, improved computational efficiency, enhanced feature extraction capabilities, and lightweight model design. Figure 3 The diagram shows the structure of the FasterBlock module used in a practical application scenario when constructing a YOLOv8-based tobacco width online detection method proposed in this invention. FasterBlock is a high-efficiency neural network module consisting of a partial convolution (PConv) and two pointwise convolutions. It aims to accelerate feature extraction while reducing computation and memory access. In FasterBlock, PConv only performs convolution operations on 1 / 4 of the input feature channels, while the remaining 3 / 4 channels remain unchanged. This effectively enables feature fusion, thereby improving overall performance. The expressions for calculating the FLOPs and MAC of PConv and ordinary convolution are shown below:
[0058]
[0059]
[0060]
[0061] In the formula, h represents the image height; w represents the image width; k p Indicates the size of a 3×3 convolution kernel; c p `k1` represents the number of channels involved in the convolution; `k1` represents the size of the 1×1 convolution kernel; `c1` represents the number of input channels for a normal convolution. At that time, PConv's FLOPs are 1 / 16 of those of ordinary convolution, and PConv also has smaller memory accesses.
[0062] To better implement the gated channel attention mechanism, a 3×3 depthwise convolution operation is added before the activation function of the GLU gated branch. This convolution can dynamically adjust the receptive field based on input features and flexibly select the convolution mode, thereby improving feature extraction capabilities. Simultaneously, this improvement transforms the gated channel attention mechanism into a nearest-neighbor feature-based attention mechanism, enhancing the model's adaptability and flexibility to different inputs.
[0063] The activation function used for dynamically selecting feature channels is preferably a convolutional gated linear unit (CGLU) activation function, such as... Figure 4The diagram shows the structure of the CGLU model. Specifically, to better implement the gated channel attention mechanism, a 3×3 depthwise convolution operation is added before the activation function of the GLU gated branch. This convolution can dynamically adjust the receptive field according to the input features and flexibly select the convolution mode, thereby improving feature extraction capabilities. Simultaneously, this improvement transforms the gated channel attention mechanism into a nearest-neighbor feature-based attention mechanism, enhancing the model's adaptability and flexibility to different inputs.
[0064] Method Detailed Implementation 3:
[0065] Following the above embodiments of the present invention, in the online tobacco width detection method based on YOLOv8 proposed in the present invention, for the tobacco detection problem under dense and overlapping conditions, step S22 further includes a multi-scale convolutional attention module comprising a channel attention block for weighting each channel of the feature map, a spatial attention block for weighting the spatial position of the feature map, and a multi-scale convolutional block for extracting multi-scale features.
[0066] Here, an MSCA attention mechanism (i.e., multi-scale convolutional attention module) is designed before the SPPF module. Unlike other attention mechanisms, MSCA can capture local details and global contextual information in tobacco images at the same time by combining convolutional kernels of different scales, thereby improving the model's ability to understand complex scenes.
[0067] The MSCA module mainly consists of a Channel Attention Block (CAB), a Spatial Attention Block (SAB), and a Multi-scale Convolutional Block (MSCB), with the following structure: Figure 5 As shown in the diagram. In the MSCA module, the input feature map first extracts multi-scale features through the MSCB, and then uses deep convolutional kernels of different scales (such as 7×7, 11×11 and 21×21) to aggregate local feature information.
[0068] To reduce computational burden and flexibly extract tobacco features at various scales from images, MSCB uses one-dimensional depthwise convolutions (such as 7×1 and 1×7) to approximate large-scale convolution operations. Next, the feature map is further optimized through CAB and SAB. In CAB, channel attention weights are generated through global pooling and convolution operations to weight each channel of the feature map; while in SAB, spatial attention weights are generated through pooling and large-scale convolutions to weight the spatial location of the feature map. Subsequently, multi-scale features are fused with channel and spatial attention information, and the relationships between different channels are modeled through 1×1 convolutional layers to generate the optimized feature map. Finally, element-wise matrix multiplication (i.e., ...) is performed... Figure 5 middle" The attention map is weighted with the input feature map to obtain the final output feature.
[0069] Method Detailed Implementation 4:
[0070] Following the above embodiments of the present invention, in the online tobacco width detection method based on YOLOv8 proposed in this invention, regarding step S23, it should be noted that although the CIOU loss function is used in the original YOLOv8n network, CIOU uses a monotonic focusing mechanism and does not fully consider the balance between dense, overlapping, or small target tobacco shreds and easily detectable tobacco shreds. This leads to geometric factors such as aspect ratio and distance amplifying the negative gradient of low-quality samples, thereby affecting the detection performance and generalization ability of the model. Therefore, a Wise-IOU loss function with a dynamic non-monotonic focusing mechanism is introduced. This method balances sample weights by dynamically adjusting the focusing mechanism and uses outlier evaluation of anchor frame quality instead of traditional IOU calculation, effectively avoiding excessive penalty to the model by geometric factors (such as distance and aspect ratio), thereby improving the model's detection performance and generalization ability. The target loss function is expressed by the following formula:
[0071] ;
[0072] Specifically, , , ;
[0073] in, The target loss function; r is the non-monotonic focusing coefficient; β represents the outlier; δ is the gradient gain; α is the balance factor. The monotonic focusing coefficient, The normalization factor, i.e., the dynamic moving average, is indicated by the superscript. This indicates that a separation operation is being performed; The penalty items representing WIOU; This is due to IOU loss.
[0074] Where, when β=δ, i.e. r=1, according to the formula It is known that predicted boxes with higher outlier values will be assigned smaller gradient gains; that is, with a fixed outlier value, the predicted box can obtain the maximum gradient gain. Therefore, WIOU adopts the optimal dynamic gradient gain allocation strategy during training.
[0075] Method Detailed Implementation 5:
[0076] According to the specific implementation methods 2-5 above, in the online tobacco width detection method based on YOLOv8 proposed in this invention, the tobacco detection model based on YOLOv8 constructed in actual engineering practice is CM-YOLOv8n, as follows: Figure 6 The diagram shows the network structure of CM-YOLOv8n. In the actual construction of CM-YOLOv8n, firstly, a C2f-FC network is designed in the backbone network. This network performs partial convolution operations only on a subset of the input features and uses a gating mechanism to dynamically filter important feature channels, significantly reducing the number of model parameters and computational complexity. Secondly, an MSCA module is designed and placed before the SPPF module in the backbone network. This aims to enhance the model's ability to perceive important features through multi-scale feature aggregation and channel attention. Finally, the original CIOU loss function in the YOLOv8n network is replaced with a WIOU function. A dynamic focusing mechanism is used to balance sample weights, and outlier count is used instead of traditional IoU calculation, effectively reducing the influence of geometric factors and thus improving the model's detection performance and generalization ability.
[0077] Method Detailed Implementation 6:
[0078] The following section, combining the model training, evaluation, and application processes of the YOLOv8-based tobacco detection model, further explains the accuracy of the YOLOv8-based tobacco detection model, thereby achieving accurate detection of tobacco width:
[0079] 1. Regarding materials and instruments: such as Figure 7 The diagram illustrates an online tobacco width detection method based on YOLOv8 proposed in this invention, applied in an image acquisition scenario. The image acquisition device is an industrial camera, and its accompanying DahengGalaxy Viewer software allows for real-time control of the digital camera to automatically capture and store images via computer. When acquiring raw images, the camera is positioned perpendicular to the conveyor belt and maintained at a distance of 70cm from the tobacco, with an exposure time of 700µs, a frame rate of 5Hz, an image resolution of 1280 pixels × 1280 pixels, and is saved in JPG format.
[0080] 2. Data Processing Using a Labeled Watershed Algorithm: The dense overlap of tobacco shreds in the original image poses significant challenges to segmentation and extraction. To address this, a label-based watershed algorithm is employed. First, morphological opening operations are performed on the input binary mask to remove noise, followed by closing operations to fill holes. Then, the processed mask undergoes dilation to generate a defined background region, and foreground probabilities are calculated based on distance transformation. Thresholding is then used to extract and determine the foreground region. After identifying unknown regions through the difference between the background and foreground, connected component analysis is used to label the foreground region, incrementing all label values by 1, retaining 0 as the background, and labeling unknown regions as -1. Next, the connected component statistics are traversed, filtering out small regions with an area less than 300 pixels and setting their corresponding labels to 0. Finally, the watershed algorithm is applied to delineate boundaries, achieving refined segmentation of overlapping targets.
[0081] 3. Dataset Creation and Environment Configuration: After watershed segmentation, the tobacco shred targets were manually labeled and bounding boxes were drawn using the software LabelImg, generating a txt file. During model training, to enrich the dataset, algorithms such as image rotation, image flipping, image denoising, image noise addition, and brightness variation were used to enhance the tobacco shred image dataset. This increased sample diversity while avoiding model overfitting. 5000 tobacco shred images were selected and then randomly divided into training, validation, and test sets in an 8:1:1 ratio, resulting in a training set of 4000 images, a validation set of 500 images, and a test set of 500 images. The operating system was Windows 11, the CPU was an Intel(R) Core(TM) i7-13620H, the GPU was an NVIDIA RTX 4060, the compiler was Python 3.8, the deep learning framework was PyTorch 1.10.1, and CUDA version 11.3 was used. The experimental hyperparameters for deep learning are shown in Table 1 below.
[0082]
[0083] Table 1
[0084] 4. Evaluation Indicators: This invention uses mean average precision (mAP), precision (P), recall (R), and parameters (Params) as evaluation indicators for tobacco shred detection. The formulas defining these evaluation indicators are shown below:
[0085]
[0086]
[0087]
[0088]
[0089] Where TP represents the number of correctly detected targets, FP represents the number of incorrectly detected targets, FN represents the number of missed detections, AP is the area under the PR curve, mAP is the average AP when the IOU threshold is 0.5, n represents the total number of detected categories, and APi represents the sum of AP values for all categories.
[0090] 5. CM-YOLOv8n Model Construction: The CM-YOLOv8n model improves detection accuracy and enhances its adaptability in complex environments. Specific innovations are as follows:
[0091] (1) The C2f-FC module was designed to effectively improve the detection accuracy while reducing the number of parameters; at the same time, the MSCA module was designed to significantly enhance the ability to extract tobacco characteristics.
[0092] (2) The WIOU loss function is introduced to improve detection performance and generalization ability.
[0093] (3) A dataset for tobacco shred detection under complex conditions was established to support the measurement of tobacco shred width.
[0094] (4) The CM-YOLOv8n lightweight online detection method significantly improves the accuracy of tobacco width measurement.
[0095] 6. Results and Analysis: To verify the accuracy and operation of the tobacco detection model used in the tobacco width detection, the following are the verification experiments and results analysis.
[0096] (1) Ablation experiment: The CM-YOLOv8n model was optimized in three ways based on YOLOv8n: a C2f-FC lightweight network was designed in the backbone network; an MSCA attention mechanism module was designed in the backbone network; and a WIOU loss function was introduced. To verify the effectiveness of these three improved methods, an ablation experiment was conducted on the tobacco detection dataset. The results of the ablation experiment are shown in Table 2 below (where “√” indicates that the module was used; “×” indicates that the module was not used, and the bold part is the tobacco detection model constructed in this invention). Model 1 is the original YOLOv8n model, models 2 to 7 are all newly added modules, and model 8 is the CM-YOLOv8n model.
[0097]
[0098] Table 2
[0099] As shown in Table 2, compared to the original algorithm model, replacing the C2f module in the YOLOv8n feature extraction network with the C2f-FC module resulted in a 0.1% decrease in detection accuracy, a 0.8M decrease in parameter count, and a 2.0GFLOPs decrease in computational complexity compared to Model 1. This indicates that although the C2f-FC module slightly reduced detection accuracy, it effectively reduced redundant information in the model, thereby significantly reducing the overall parameter count and computational complexity. Model 3, which added the MSCA module before SPPF, showed an increase in detection accuracy, parameter count, and computational complexity of 0.7%, 0.12M, and 0.3GFLOPs respectively compared to Model 1. This demonstrates that the MSCA module significantly improved the detection accuracy of tobacco shreds while slightly increasing the parameter count and computational complexity. Model 4, which replaced the loss function with the WIOU loss function, showed a 0.6% improvement in detection accuracy compared to Model 1, while maintaining the same parameter count and computational complexity, proving that the WIOU loss function plays a positive role in improving the detection accuracy of tobacco shreds.
[0100] Models 5, 6, and 7 respectively added C2f-FC and MSCA, C2f-FC and WIOU, and MSCA and WIOU. Comparing these models, it can be seen that the CM-YOLOv8n model, while maintaining low parameter count and computational complexity, also features a lightweight design, ensuring high accuracy and significantly improving tobacco detection performance. Its mAP@0.5 is 92.4%, with 2.38M parameters and a computational complexity of 6.2 GFLOPs.
[0101] (2) Model Comparison: To evaluate the performance of the CM-YOLOv8n model, it was compared with mainstream object detection algorithms proposed in previous studies (Faster R-CNN, SSD, YOLOv5s, YOLOv7, and YOLOv8n). The experimental results of the comparison of different network models are shown in Table 3 below:
[0102]
[0103] Table 3
[0104] As shown in Table 3 above, the two-stage object detection algorithm Faster R-CNN has a large computational cost and number of parameters, as well as a large model weight file, making it unsuitable for the lightweight real-time detection requirements of this dataset. YOLOv8n outperforms the other two single-stage networks in terms of parameter count and model size, and its mAP@0.5 is higher than SSD and YOLOv7. YOLOv5s has slightly higher detection accuracy than YOLOv8n, but its parameter count and model size are much larger. Compared to the original YOLOv8n, CMW-YOLOv8n has fewer parameters and model weights, and its mAP@0.5 and other metrics are superior. In summary, the CMW-YOLOv8n model demonstrates its superiority in multiple aspects.
[0105] (3) Comparison of test results: such as Figure 8 The image shows the visualization results of different models. Based on the model comparison and validation results, YOLOv5s, YOLOv8n, and CM-YOLOv8n models were selected to detect tobacco shreds, and three samples from the test set were randomly chosen for testing. Due to the large number of tobacco shred targets, a confidence threshold of 0.7 and an IoU threshold of 0.7 were set to remove targets with low confidence. The detection results are shown below. Figure 7 As shown in the figure, although YOLOv5s is slightly better than CM-YOLOv8n in some confidence levels, YOLOv5s suffers from a significant problem of missed detections and has poor target bounding box localization accuracy. While YOLOv8n has fewer missed detections, its detection confidence is lower than that of CM-YOLOv8n. The comparison results show that CM-YOLOv8n effectively reduces missed detections while exhibiting higher confidence and more accurate target localization.
[0106] 7. Verification of Tobacco Shred Width: To verify the accuracy and reliability of the measurement results, 50g of tobacco shreds were taken from each of five groups of samples with widths of 0.8mm, 0.9mm, 1.0mm, 1.1mm, and 1.2mm, respectively, using the quartering method. The tobacco shreds were then evenly spread on a conveyor belt. An industrial camera was fixed directly above the conveyor belt, with the lens perpendicular to the belt surface at a distance of 700mm. The light source brightness was adjusted to ensure that the acquired tobacco shred images were clear and free of background interference. The camera continuously acquired images at a frame rate of 5fps, and the entire process was repeated 10 times, with the tobacco shred positions varying each time to reduce random errors. Finally, the average tobacco shred width was calculated using the variable diameter circle method and Zhang Zhengyou calibration method in industry standard YC / T 606-2024. The calculation results are shown in Table 4 below.
[0107]
[0108] Table 4
[0109] According to Table 4, for different tobacco samples, the average width detected online was lower than that detected manually, and all errors were within the cutting width deviation range (≤±0.1mm) specified in the cigarette manufacturing process specifications. However, the average width detected online for the five groups of samples was slightly higher than the cutter's set value. This was mainly because the tobacco shreds would become dense or piled up during the detection process, interfering with the online detection results and leading to higher measured values. Nevertheless, compared to manual detection, online detection can more comprehensively and objectively reflect the true state of the tobacco shreds in actual production, effectively avoiding subjective errors and operational instability that occur with manual measurement. It accurately reflects the width variation pattern after tobacco shredding, laying the foundation for subsequent real-time online detection.
[0110] In summary, to address the issue of low width measurement accuracy due to inaccurate detection of dense, overlapping, and disordered tobacco shreds, a lightweight online tobacco shred detection method based on CM-YOLOv8n is proposed. Experimental results show that this method exhibits excellent detection accuracy, achieving an mAP of 92.4%. Compared to the original YOLOv8n model, the CM-YOLOv8n model significantly improves tobacco shred detection performance: precision increased by 3.1 percentage points, recall by 2.4 percentage points, and mAP@0.5 by 2.5 percentage points, while the model size decreased by 0.62 MB. Furthermore, to achieve rapid detection and high-precision measurement of tobacco shred width, this study designed an online tobacco shred width detection system based on the CM-YOLOv8n framework. Experimental verification shows that the system's width measurement error is within the process specification range (≤±0.1 mm), meeting the process production standards and providing a solid technical foundation for real-time online detection.
[0111] Method Detailed Implementation 7:
[0112] like Figure 9The diagram illustrates the process of an online tobacco width detection method based on YOLOv8 proposed in this invention in a practical application scenario. First, the tobacco in the acquired original image is segmented using a watershed method to obtain several tobacco images. Then, a target detection model is called to process and mark the original images of these tobacco images to filter and extract tobacco regions. Next, the image is converted to the HSV color space, and red regions of different hues are detected using preset dual threshold ranges (H: 0-10 and 160-180, S / V: 100-255). After bitwise OR operation fusion, an initial binary mask is generated. Subsequently, a morphological opening operation is performed using a 3×3 elliptic kernel to effectively eliminate noise interference and smooth tobacco edges. Finally, a standardized binary mask is output (target region 255 / background 0). This optimized mask serves as core data, providing a precise centerline extraction basis for subsequent thinning algorithms and supporting region verification during dynamic inscribed circle detection. Accurate measurement of tobacco width is achieved by iteratively calculating the maximum inscribed circle diameter. Next, the pixel values of the tobacco shreds in the image are calculated using the variable diameter circle method proposed in "YC / T 606-2024 Image Method for Detection of Tobacco Shred Length, Width, and Curl". Based on this, and combined with Zhang Zhengyou's calibration method, a spatial relationship model between the tobacco shred width and image pixel units is established. Finally, through a proportional transformation relationship, the pixel distances in the image are accurately converted into dimensions in actual physical space, thereby achieving high-precision measurement of the tobacco shred width.
[0113] Detailed implementation of the device:
[0114] Another aspect of the present invention proposes an online tobacco width detection device based on YOLOv8, including a processor, which is used to execute the above-described online tobacco width detection method based on YOLOv8. For specific implementation of the device, please refer to Specific Implementation Method 1-7, which will not be repeated here.
[0115] In summary, to address the problem of inaccurate detection of tobacco shreds in dense, overlapping, and disordered conditions, leading to low width measurement accuracy, an online tobacco shred width detection method and device based on YOLOv8 is proposed. This involves constructing a tobacco shred detection model based on YOLOv8n (C2f-Faster-CGLU-MSCA-YOLOv8n, CM-YOLOv8n). Firstly, a C2f-Faster-CGLU network (C2f-FC) is designed, which processes some channels of the input features through partial convolution and dynamically filters important feature channels using a gating mechanism, significantly reducing the number of model parameters and computational complexity. Secondly, a Multi-Scale Convolutional Attention (MSCA) module is designed and placed before the Spatial Pyramid Pooling-Fast (SPPF) module in the backbone network. This enhances the model's ability to perceive tobacco shred features through multi-scale feature aggregation and channel attention mechanisms. Finally, the Wise-IOU loss function was introduced, and the detection performance and generalization ability were improved by dynamically adjusting the focusing mechanism and balancing sample weights. Experimental results show that the CM-YOLOv8n model achieves an accuracy of 90.7%, a recall of 86.6%, and an mAP@0.5 of 92.4%, with only 2.38M parameters and 6.2 GFLOPs. Compared with other advanced algorithms, this model has the fewest parameters and significantly reduced floating-point operations, demonstrating higher detection efficiency and stronger generalization ability. Based on this method, an online tobacco width detection system was designed, achieving rapid detection and high-precision measurement of tobacco width. The system's detection error is within ±0.1mm, meeting manufacturing standards and laying a solid foundation for the application of subsequent real-time online detection systems, thus enabling more accurate detection of tobacco width.
Claims
1. A method for online detection of tobacco shred width based on YOLOv8, characterized in that, include: 1) Define the boundaries of several tobacco strands in the original image containing several tobacco strands; 2) Call the pre-built YOLOv8-based tobacco detection model to extract several tobacco regions containing complete single tobacco shreds from the segmented image after dividing the tobacco shred boundaries; 3) Calculate the pixel distance value in the width direction of each tobacco region in the corresponding image; 4) Based on the pre-obtained spatial relationship model between tobacco width and image pixel units, determine the tobacco width corresponding to the pixel distance value in the width direction of all tobacco regions in the corresponding image.
2. The online tobacco width detection method based on YOLOv8 according to claim 1, characterized in that, A tobacco detection model based on YOLOv8 was constructed using the following method: Based on the YOLOv8 model architecture, a network structure is built in the backbone network to perform convolution operations on the target channels of the input features, wherein the target channels are feature channels dynamically selected through a gating mechanism. Before the SPPF module in the backbone network, a multi-scale convolutional attention module is added to capture local details and global contextual information in tobacco images. The original loss function is replaced with a target loss function that balances sample weights through a dynamic adjustment focusing mechanism to generate the YOLOv8-based tobacco detection model.
3. The online tobacco width detection method based on YOLOv8 according to claim 2, characterized in that, By integrating the C2f and FasterBlock modules and embedding activation functions for dynamically filtering feature channels, the network structure for performing convolution operations on the target channels of the input features is generated.
4. The online tobacco width detection method based on YOLOv8 according to claim 2, characterized in that, The multi-scale convolutional attention module includes a channel attention block for weighting each channel of the feature map, a spatial attention block for weighting the spatial location of the feature map, and a multi-scale convolutional block for extracting multi-scale features.
5. The online tobacco width detection method based on YOLOv8 according to claim 2, characterized in that, The target loss function is expressed by the following formula: ; Specifically, , , ; in, The target loss function; r is the non-monotonic focusing coefficient; β represents the outlier; δ is the gradient gain; α is the balance factor. The monotonic focusing coefficient, The normalization factor, i.e., the dynamic moving average, is indicated by the superscript. This indicates that a separation operation is being performed; The penalty items representing WIOU; This is due to IOU loss.
6. The online tobacco width detection method based on YOLOv8 according to claim 1, characterized in that, The marker watershed algorithm is used to divide the boundaries of several tobacco strands from the original image containing several tobacco strands.
7. The online tobacco width detection method based on YOLOv8 according to claim 1, characterized in that, The pixel distance value in the width direction of each tobacco shred region in the corresponding image is calculated using the variable diameter circle method.
8. The online tobacco width detection method based on YOLOv8 according to claim 1, characterized in that, Based on a pre-obtained spatial relationship model between tobacco width and image pixel units, the tobacco width corresponding to the pixel distance value in the width direction of all tobacco regions in the corresponding image is determined by the following method: Based on Zhang Zhengyou's calibration method, a spatial relationship model between tobacco width and image pixel units is established; the pixel distance values in the width direction of the image are converted into the actual physical distance of the tobacco width through a proportional transformation relationship.
9. The online tobacco width detection method based on YOLOv8 according to claim 1, characterized in that, The spatial relationship model is expressed by the following formula: ; Where y is the actual physical size; x is the pixel distance value; and K is the conversion factor.
10. An online tobacco width detection device based on YOLOv8, characterized in that, Includes a processor for executing the online tobacco width detection method based on YOLOv8 as described in any one of claims 1-9.