A method, device and equipment for counting black pigs based on color difference threshold

By converting BGR images to the Lab color space and performing channel separation and enhancement processing, combined with superpixel segmentation and optical flow field calculation, the accuracy problem of black pig counting in low-contrast and dynamic scenes was solved, and stable counting under different lighting conditions was achieved.

CN120708166BActive Publication Date: 2025-10-28厦门农芯数字科技有限公司
View PDF 2 Cites 0 Cited by

Patent Information

Application Number
CN202511196586.8
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2025-08-26
Publication Date
2025-10-28
Estimated Expiration
2045-08-26

AI Technical Summary

Technical Problem

Existing technologies struggle to accurately count black pigs in low-contrast and dynamic scenes. Traditional methods perform poorly under varying lighting conditions and complex backgrounds. Deep learning methods are computationally complex and costly, making them difficult to implement in real time.

Method used

The BGR image was converted to the Lab color space, channel separation and enhancement were performed, and black pig counting was achieved by combining superpixel segmentation and optical flow field calculation through color difference threshold segmentation and motion compensation.

Benefits of technology

It accurately counts the number of black pigs in low-contrast and dynamic scenes, improves the stability and accuracy of counting, adapts to different lighting conditions, and reduces computational complexity and cost.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN120708166B_ABST
    Figure CN120708166B_ABST
Patent Text Reader

Abstract

This invention discloses a method, apparatus, and device for counting black pigs based on a color difference threshold, comprising: converting a BGR image to the Lab color space and performing channel separation; enhancing and weighting the resulting a-channel and b-channel images to obtain a color difference enhanced image; binarizing the color difference enhanced image based on a threshold segmentation strategy determined by the L-channel image to obtain a binarized image; converting the BGR image to an RGB image and performing superpixel segmentation to generate a mask image including multiple superpixel regions; merging regions with the binarized image to obtain a target mask image; calculating the optical flow field of the current frame and the previous frame to obtain a displacement vector; mapping the target mask image of the previous frame to the current frame based on the displacement vector to obtain a compensation mask image; calculating the overlap rate between the superpixel regions of the current frame and the compensation mask image; merging and statistically analyzing adjacent regions with an overlap rate greater than a threshold to obtain the black pig counting result.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This invention relates to the field of pig farm management technology, and in particular to a method, apparatus, and equipment for counting black pigs based on color difference thresholds. Background Technology

[0002] In the pig farming industry, black pigs have attracted much attention due to their superior meat quality and high market demand, but accurate counting in their automated management faces many challenges. Current pig counting methods mainly include manual counting, RFID ear tag identification, traditional image processing (color segmentation), deep learning (such as YOLO and Faster R-CNN), and background subtraction. However, these existing methods have significant limitations in practical applications.

[0003] However, traditional color segmentation methods rely on RGB / HSV thresholds, which are difficult to effectively segment when black pigs are similar to dark backgrounds (such as mud or shadows), and fixed thresholds are difficult to adapt to different lighting conditions. While deep learning methods show some effectiveness in complex scenes, data acquisition costs for diverse black pig breeds are high, and the computational complexity of the models is high, making real-time operation on embedded devices difficult. Background subtraction is suitable for detecting moving objects, but black pigs are easily misclassified as background when stationary. Furthermore, detection methods based on deep neural networks have weak generalization ability in black pig identification, traditional image processing methods based on color segmentation have poor robustness, and background subtraction is susceptible to motion blur and occlusion. All of these factors make it difficult for existing technologies to meet the requirements of low-cost, high-efficiency real-time black pig counting. Summary of the Invention

[0004] In view of this, the purpose of this invention is to propose a black pig counting method, device and apparatus based on color difference threshold, which aims to solve the problems of difficulty in accurately counting black pigs in low contrast and dynamic scenes in the prior art.

[0005] To achieve the above objectives, the present invention provides a method for counting black pigs based on a color difference threshold, the method comprising:

[0006] The BGR image containing the black pig herd was converted to the Lab color space and the channels were separated to obtain the L channel image, the a channel image, and the b channel image.

[0007] The a-channel image and the b-channel image are enhanced, and the enhanced images are then weighted and fused to obtain a color difference enhanced image.

[0008] The color difference enhancement image is binarized based on the threshold segmentation strategy determined by the L-channel image to obtain a binarized image.

[0009] The BGR image is converted into an RGB image and superpixel segmentation is performed to generate a mask image that includes multiple superpixel regions. The region is then merged with the binarized image to obtain the optimized target mask image.

[0010] By calculating the optical flow fields of the current frame and the previous frame, a displacement vector is obtained. Based on the displacement vector, the target mask image of the previous frame is mapped to the current frame to obtain a compensation mask image.

[0011] Calculate the overlap rate between the superpixel region and the compensation mask in the current frame, merge adjacent regions with an overlap rate greater than a threshold, and count the number of merged regions that meet the preset conditions as the black pig counting result.

[0012] Preferably, the step of enhancing the a-channel image and the b-channel image, and then weighted and fused the enhanced images to obtain a color difference enhanced image, includes:

[0013] Using CLAHE, and by setting the cropping limit to 2.0 and the tile grid size to 8×8, the a-channel image and the b-channel image are contrast-enhanced and then stitched together to obtain the a-channel enhanced image and the b-channel enhanced image.

[0014] The enhanced image of channel a and the enhanced image of channel b are weighted and fused according to the weight allocation values ​​to obtain the color difference enhanced image.

[0015] Preferably, the binarization process of the chromatic difference enhancement image based on the threshold segmentation strategy determined by the L-channel image to obtain a binarized image includes:

[0016] By calculating the average brightness value of the L-channel image, if the average brightness value is less than a preset value, the current scene is determined to be a low-brightness scene. The Gaussian adaptive thresholding method is used to binarize the color difference enhancement image to obtain the binarized image after illumination compensation.

[0017] Otherwise, the Otsu automatic thresholding method is used to binarize the color difference enhancement image to obtain the binarized image.

[0018] Preferably, the step of converting the BGR image to an RGB image and performing superpixel segmentation to generate a mask image including multiple superpixel regions, and then merging the regions in conjunction with the binarized image to obtain an optimized target mask image, includes:

[0019] The SLIC algorithm is used to perform superpixel segmentation on the RGB image based on the set number of segmentation blocks and compactness parameters, generating a mask image that includes multiple superpixel regions.

[0020] By traversing each superpixel region, the foreground overlap ratio between the superpixel region and the foreground pixels in the binarized image is calculated. Regions with a foreground overlap ratio greater than a preset ratio are marked as regions to be merged, and spatially adjacent regions to be merged are merged to obtain the target mask image.

[0021] Preferably, the step of obtaining the displacement vector by calculating the optical flow fields of the current frame and the previous frame includes:

[0022] The displacement vector is obtained by calculating the optical flow field of the current frame and the previous frame using the Farneback algorithm and setting the pyramid scaling factor to 0.5.

[0023] Preferably, the step of mapping the target mask image of the previous frame to the current frame based on the displacement vector to obtain the compensated mask image includes:

[0024] Based on the displacement vector and the coordinates of the current frame, coordinate mapping calculation is performed to obtain the coordinate mapping matrix of the previous frame;

[0025] Based on the coordinate mapping matrix, the non-integer coordinates in the target mask image of the previous frame are compensated by bilinear interpolation to obtain the compensated mask image of the current frame.

[0026] Preferably, calculating the overlap rate between the superpixel region and the compensation mask map in the current frame includes:

[0027] The overlap rate is calculated according to the preset formula: overlap rate = |superpixel region of the current frame| / |superpixel region of the current frame|.

[0028] Preferably, the number of regions that meet the preset conditions after statistical merging, as the black pig counting result, includes:

[0029] Calculate the foreground pixel ratio of each merged region, and select regions whose foreground pixel ratio is greater than a pixel threshold as valid target regions;

[0030] The number of valid target areas is counted to obtain the black pig count result.

[0031] To achieve the above objectives, the present invention also provides a black pig counting device based on a color difference threshold, the device comprising:

[0032] The preprocessing unit is used to convert the BGR image containing the black pig herd to the Lab color space and perform channel separation to obtain the L channel image, the a channel image, and the b channel image.

[0033] An enhancement unit is used to enhance the a-channel image and the b-channel image, and then perform weighted fusion on the enhanced images to obtain a color difference enhanced image;

[0034] The binarization unit is used to perform binarization processing on the color difference enhancement image based on the threshold segmentation strategy determined by the L-channel image to obtain a binarized image.

[0035] The segmentation unit is used to convert the BGR image into an RGB image and perform superpixel segmentation to generate a mask image that includes multiple superpixel regions. The region is then merged with the binarized image to obtain an optimized target mask image.

[0036] The mapping unit is used to calculate the optical flow field of the current frame and the previous frame to obtain a displacement vector, and to map the target mask image of the previous frame to the current frame according to the displacement vector to obtain a compensation mask image.

[0037] The counting unit is used to calculate the overlap rate between the superpixel region and the compensation mask in the current frame, merge adjacent regions with an overlap rate greater than a threshold, and count the number of merged regions that meet the preset conditions as the black pig counting result.

[0038] To achieve the above objectives, the present invention also proposes a black pig counting device based on a color difference threshold, comprising a processor, a memory, and a computer program stored in the memory, wherein the computer program is executed by the processor to implement the steps of a black pig counting method based on a color difference threshold as described in the above embodiments.

[0039] To achieve the above objectives, the present invention also proposes a computer-readable storage medium storing a computer program that is executed by a processor to implement the steps of a black pig counting method based on color difference threshold as described in the above embodiments.

[0040] To achieve the above objectives, the present invention also proposes a computer program product, including a computer program / instructions, which, when executed by a processor, implement the steps of a black pig counting method based on color difference threshold as described in the above embodiments.

[0041] Beneficial effects:

[0042] The above scheme, by converting the BGR image to the Lab color space and separating the channels, can better utilize color information differences to distinguish black pigs from the background. Then, the a and b channel images are enhanced and weighted to obtain a color difference enhanced image, further highlighting the black pig characteristics. Binarization processing based on the L channel threshold segmentation strategy can adapt to different lighting conditions and accurately extract the black pig region. Subsequent steps such as superpixel segmentation, region merging, and optical flow field calculation effectively solve the problem of pigs occluding each other, thus accurately counting the number of black pigs in low-contrast scenes and dynamic changes, providing reliable data for breeding management. At the same time, the Farneback algorithm is used to calculate the optical flow field to obtain the displacement vector, and this is used to map the target mask image of the previous frame to the current frame to achieve motion compensation. Combined with the overlap rate calculation of the superpixel region and the compensation mask image and region merging, the counting method maintains a stable and accurate counting effect in dynamic scenes of black pig movement, meeting the monitoring needs of pig activities in actual breeding environments.

[0043] By employing the CLAHE method to enhance the contrast of the a and b channel images, and setting appropriate cropping constraints and tile grid sizes, the color difference contrast between the target region and the background can be effectively improved. This avoids excessive noise enhancement, resulting in clearer image details and improved image quality. This provides better input for subsequent segmentation and recognition operations, improving the performance and stability of the entire counting system. Furthermore, by weighting and fusing the enhanced a and b channel images with weighted values, the differences between the black pig and the background can be highlighted. Reasonable fusion of channel information further optimizes the effect of the color difference enhancement image, enhancing the accuracy of subsequent segmentation steps and facilitating more precise extraction of the black pig region.

[0044] The scene brightness is determined by calculating the average brightness value of the L-channel image. The threshold segmentation method is automatically selected according to different brightness conditions. In low-brightness scenes, the Gaussian adaptive thresholding method is used, which can effectively compensate for insufficient lighting and ensure that the black pig area can still be accurately segmented in nighttime or low-light environments, thus expanding the applicability of the counting method under different lighting conditions. In non-low-brightness scenes, the Otsu automatic thresholding method is used, which can automatically find the threshold that minimizes the intra-class variance of the foreground and background, achieving accurate binarization of the color difference enhanced image and obtaining an accurate initial binary image. This lays a good foundation for subsequent target extraction and counting, improving the accuracy of the entire counting process.

[0045] Using the SLIC algorithm to perform superpixel segmentation on RGB images and generate a mask image can divide the image into multiple superpixel regions with regional consistency, more closely resembling the actual contour features of the black pig. By combining the binarized image with the calculation of the overlap ratio between the superpixel regions and foreground pixels, and merging adjacent regions that meet the conditions, noise interference can be effectively removed, the broken edges of the black pig target area can be filled in, and a complete, continuous, and accurate target mask image can be obtained, improving the accuracy of counting.

[0046] By calculating the coordinate mapping matrix of the previous frame through the displacement vector and the coordinates of the current frame, and then using bilinear interpolation on the non-integer coordinates in the target mask map of the previous frame based on this matrix, the target mask map of the previous frame can be mapped to the current frame more accurately, achieving accurate motion compensation. This helps to maintain the continuity and consistency of the target area during the movement of the black pig, improves the stability of the counting results in dynamic scenes, and reduces the counting error caused by the movement of the pig.

[0047] This method employs a pre-defined formula to calculate the overlap rate between the superpixel region of the current frame and the compensation mask image, providing an objective and quantitative approach to assess the degree of overlap between the superpixel region and the target region of the previous frame. Region merging based on this overlap rate ensures that the merged region better reflects the actual shape and distribution characteristics of black pigs, improving the accuracy of target region extraction and thus enhancing the reliability of the counting results. Furthermore, by calculating the proportion of foreground pixels in the merged region and selecting regions with a proportion greater than a pixel threshold as valid target regions, regions that do not conform to the characteristics of black pigs can be effectively removed, avoiding interference from non-black pig regions on the counting results. Finally, the number of valid target regions is counted to obtain the black pig counting results, exhibiting stronger anti-interference capabilities and higher accuracy, providing more precise data support for breeding management and production decisions. Attached Figure Description

[0048] To more clearly illustrate the technical solutions in the embodiments of the present invention or the prior art, the drawings used in the description of the embodiments or the prior art will be briefly introduced below. Obviously, the drawings described below are only some embodiments of the present invention. For those skilled in the art, other drawings can be obtained based on these drawings without creative effort.

[0049] Figure 1 This is a flowchart illustrating a black pig counting method based on color difference threshold provided in an embodiment of the present invention.

[0050] Figure 2 This is a schematic diagram illustrating the visualization results of channel separation and CLAHE enhancement provided in an embodiment of the present invention.

[0051] Figure 3This is a schematic diagram illustrating the visualization result of binarizing a color difference enhanced image according to an embodiment of the present invention.

[0052] Figure 4 This is a schematic diagram illustrating the visualization results of superpixel segmentation of an RGB image using the slic algorithm, provided in an embodiment of the present invention.

[0053] Figure 5 This is a schematic diagram of a black pig counting device based on a color difference threshold, provided in an embodiment of the present invention.

[0054] The realization of the invention's objective, its functional characteristics, and advantages will be further explained in conjunction with the embodiments and with reference to the accompanying drawings. Detailed Implementation

[0055] To make the objectives, technical solutions, and advantages of the embodiments of the present invention clearer, the technical solutions of the embodiments of the present invention will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only a part of the embodiments of the present invention, not all of them. Based on the embodiments of the present invention, all other embodiments obtained by those skilled in the art without creative effort are within the scope of protection of the present invention. Therefore, the following detailed description of the embodiments of the present invention provided in the accompanying drawings is not intended to limit the scope of the claimed invention, but merely to represent selected embodiments of the invention. Based on the embodiments of the present invention, all other embodiments obtained by those skilled in the art without creative effort are within the scope of protection of the present invention.

[0056] The present invention will be described in detail below with reference to the embodiments.

[0057] Reference Figure 1 The diagram shown is a flowchart of a black pig counting method based on color difference threshold provided in an embodiment of the present invention.

[0058] In this embodiment, the method includes:

[0059] S11, convert the BGR image containing the black pig herd to the Lab color space and perform channel separation to obtain the L channel image, the a channel image, and the b channel image.

[0060] S12, the a-channel image and the b-channel image are enhanced, and the enhanced images are weighted and fused to obtain a color difference enhanced image.

[0061] Further, in step S12, the enhancement processing of the a-channel image and the b-channel image, and the weighted fusion of the enhanced images to obtain a color difference enhanced image, includes:

[0062] S12-1, using CLAHE and setting the cropping limit to 2.0 and the tile grid size to 8×8, the a-channel image and the b-channel image are contrast-enhanced and then stitched together to obtain the a-channel enhanced image and the b-channel enhanced image.

[0063] S12-2, The enhanced image of channel a and the enhanced image of channel b are weighted and fused according to the weight allocation value to obtain the color difference enhanced image.

[0064] In this embodiment, the method is implemented based on a segmentation model obtained through pre-training YOLOv8. This segmentation model can be deployed on edge devices for real-time processing, reducing reliance on labeled data and making it suitable for small and medium-sized farms. Specifically, the method involves capturing real-time footage of the farm using a camera, obtaining BGR images of the black pig herd from the video stream, converting the input BGR images to the Lab color space to obtain Lab format images, and then separating the Lab format images into three independent channels: the L channel image (brightness information), the a channel image (red-green hue information), and the b channel image (blue-yellow hue information). Figure 2 The diagram shows the channel separation and the visualization results after CLAHE enhancement.

[0065] Contrast enhancement was performed on the a-channel and b-channel images using CLAHE, resulting in enhanced a-channel and b-channel images respectively. Specifically, the enhanced a-channel image is defined as `a_eq = clahe.apply(a_channel)`, and the enhanced b-channel image is defined as `b_eq = clahe.apply(b_channel)`. The CLAHE (Contrast Limited Adaptive Histogram Equalization) image enhancement technique was employed. By setting the clipping limit to 2.0 and the tile grid size to 8×8, the a-channel and b-channel images in the Lab color space were enhanced. This process effectively improved the color contrast between the target area and the background while preserving color details, thus enhancing the stability of subsequent segmentation. Specifically, `clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8, 8))`.

[0066] Among them, clipLimit=2.0: controls the upper limit of local contrast to prevent over-enhancement; tileGridSize=(8,8): divides the image into 8×8 blocks, equalizes each block separately, and then stitches them together.

[0067] By weighted fusion of the enhanced a-channel and b-channel images (e.g., weights of 0.7 and 0.3), a color difference enhanced image is obtained; that is, delta_img = cv2.addWeighted(a_eq, 0.7, b_eq, 0.3, 0). This highlights the difference between black pigs (typically reddish / yellowish tones) and other features such as green vegetation / gray ground.

[0068] Furthermore, the network architecture of this segmentation model includes a backbone network, a neck network, and a head network. The backbone network uses an improved CSPDarknet structure, containing multiple C2f (Cross Stage Partial Network) modules to enhance feature extraction capabilities; it also introduces an SPPF (Spatial Pyramid Pooling - Fast) module to fuse multi-scale features through max pooling of different sizes. The neck network employs a PANet (Path Aggregation Network) structure, aggregating multi-scale features through top-down and bottom-up paths; it also incorporates an enhanced feature pyramid network (FPN) to fuse semantic information from different levels. The head network's detection branch predicts bounding box coordinates (x, y, w, h), target confidence, and class probability; the segmentation branch generates mask features through a segmentation decoder; and outputs a binary mask for each target, typically with a resolution of 1 / 4 or 1 / 8 of the original image. The training process is simplified by directly predicting the bounding box center points, rather than based on anchor offsets. Classification losses include BCEWithLogitsLoss, bounding box loss (combining CIoU Loss and DistributionFocal Loss (DFL)), and mask loss (using Dice Loss + BCE Loss to balance foreground and background segmentation accuracy). Efficient inference: Through model pruning, quantization (e.g., FP16), and export to ONNX format, it supports deployment on edge devices and can run on low-cost hardware such as Raspberry Pi.

[0069] The training process of the segmentation model includes: (1) Training data preparation, specifically: obtaining images containing black pig herds as training datasets. The training dataset is in JPEG / PNG format, and the labeled training data is in COCO format (JSON) or YOLO format (txt file), containing bounding box coordinates and masks (polygon or RLE encoding); data augmentation methods are divided into basic augmentation and advanced augmentation. Basic augmentation includes random scaling, rotation, flipping, and brightness / contrast adjustment; advanced augmentation includes Mosaic (4 images stitched together), MixUp (image fusion), and CopyPaste (mask copy and paste). (2) Pipeline optimization, specifically: divided into multi-threaded processing. Thread 1 is image acquisition (Camera API), thread 2 is color difference enhancement + segmentation, and thread 3 is counting logic (connected component analysis).

[0070] S13, the color difference enhancement image is binarized based on the threshold segmentation strategy determined by the L channel image to obtain a binarized image.

[0071] Further, in step S13, the threshold segmentation strategy determined based on the L-channel image is used to binarize the chromatic difference enhancement image to obtain a binarized image, including:

[0072] By calculating the average brightness value of the L-channel image, if the average brightness value is less than a preset value, the current scene is determined to be a low-brightness scene. The Gaussian adaptive thresholding method is used to binarize the color difference enhancement image to obtain the binarized image after illumination compensation.

[0073] Otherwise, the Otsu automatic thresholding method is used to binarize the color difference enhancement image to obtain the binarized image.

[0074] In this embodiment, the average brightness value of the L-channel image is calculated, and the current scene is determined to be in a low-brightness environment based on the average brightness value. If it is a low-brightness scene (e.g., average brightness value less than 30), the Gaussian adaptive thresholding method is used to binarize the color difference enhancement image to obtain a binarized image after illumination compensation. Otherwise, the Otsu automatic thresholding method is used to find a threshold T that minimizes the intra-class variance between the foreground and background. Values ​​less than T are foreground, and values ​​greater than T are background. In other words, the Otsu automatic thresholding method is used to binarize the color difference enhancement image to obtain a binarized image. (Refer to...) Figure 3 As shown, the output binarized image (the black pig area is white and the background is black) is the basic input for all subsequent processing. Its function is equivalent to a "target mask". Through pixel-level black and white segmentation, the black pig is extracted from the complex background, providing a prerequisite for tasks such as group counting and behavior analysis.

[0075] S14, the BGR image is converted into an RGB image and superpixel segmentation is performed to generate a mask image including multiple superpixel regions. The region is merged in combination with the binarized image to obtain the optimized target mask image.

[0076] Further, in step S14, converting the BGR image to an RGB image and performing superpixel segmentation to generate a mask image including multiple superpixel regions, and then merging the regions in conjunction with the binarized image to obtain an optimized target mask image, includes:

[0077] S14-1, Using the SLIC algorithm based on the set number of segmentation blocks and compactness parameters, the RGB image is superpixel segmented to generate a mask image including multiple superpixel regions;

[0078] S14-2, by traversing each superpixel region, the foreground overlap ratio between the superpixel region and the foreground pixels in the binarized image is calculated. Regions with a foreground overlap ratio greater than a preset ratio value are marked as regions to be merged, and spatially adjacent regions to be merged are merged to obtain the target mask image.

[0079] Reference Figure 4 As shown. In this embodiment, the original BGR format image is converted to an RGB format RGB image to prepare for subsequent superpixel segmentation; the slic algorithm is used to perform superpixel segmentation on the RGB image, including setting the number of segmentation blocks to 100 and the compactness parameter to 10, to obtain superpixel segmentation results for multiple superpixel regions; by traversing all superpixel regions, a mask image is generated for each superpixel region; then the overlap rate between the superpixel region and the foreground pixels of the binarized image is calculated, and regions with an overlap rate greater than a preset value (such as 50%) are considered to belong to the foreground and are merged, thus completing the broken edges and obtaining N complete and continuous target regions. Through the above process, isolated noise points can be eliminated and the complete outline of the pig body can be preserved. The merging operation essentially utilizes the "region consistency" characteristic of superpixels to "correct and complete" the initial binarized image, thereby making the target region of the black pig more complete and continuous, while removing irrelevant noise, and finally obtaining a target binarized image that is closer to the real target.

[0080] S15, by calculating the optical flow fields of the current frame and the previous frame, a displacement vector is obtained, and the target mask image of the previous frame is mapped to the current frame according to the displacement vector to obtain a compensation mask image.

[0081] Furthermore, step S15 includes:

[0082] S15-1, by employing the Farneback algorithm and setting the pyramid scaling factor to 0.5, the optical flow field of the current frame and the previous frame is calculated to obtain the displacement vector;

[0083] S15-2, Perform coordinate mapping calculation based on the displacement vector and the coordinates of the current frame to obtain the coordinate mapping matrix of the previous frame;

[0084] S15-3, Based on the coordinate mapping matrix, the non-integer coordinates in the target mask map of the previous frame are compensated by bilinear interpolation to obtain the compensated mask map of the current frame.

[0085] In this embodiment, the movement of the black pig in consecutive video frames may cause misalignment of the segmented superpixel region map between adjacent frames (e.g., after the black pig moves, the same object is segmented into different superpixel regions). Motion compensation, through pixel displacement estimated by the optical flow field, "maps" the superpixel region map of the previous frame to the current frame according to the displacement relationship, obtaining a compensated mask map `warped_segments`, achieving segmentation region alignment in the temporal dimension and improving target consistency in dynamic scenes. That is, the optical flow field describes the motion trajectory of objects in the scene by calculating the displacement vector of each pixel in the video between adjacent frames. Specifically, the segmented superpixel region map and the optical flow fields (including the displacement vectors (u,v) of each pixel) of the current and previous frames are processed. The floating-point displacements of the optical flow fields are converted to integer coordinates, and noise points with abnormally large displacements are filtered out. The Farneback algorithm, based on a set pyramid scaling factor (e.g., 0.5), calculates the optical flow fields of the current and previous frames in the video to obtain the displacement vectors. Coordinate mapping is then performed based on the displacement vectors and the coordinates of the current frame to obtain the coordinate mapping matrix of the previous frame. Based on the coordinate mapping matrix, non-integer coordinates in the target mask map of the previous frame are processed using... Bilinear interpolation addresses the issue of non-integer coordinates from the previous frame, which prevent direct indexing of pixel values. While the coordinates obtained from the coordinate mapping matrix may be non-integer, image pixel coordinates are typically integers. To accurately obtain the pixel value at the corresponding position in the previous frame, methods like bilinear interpolation are needed. The coordinate mapping matrix provides the coordinate basis for bilinear interpolation. Using the coordinates determined by this matrix, interpolation is performed based on the neighboring integer pixel coordinates, resulting in a more accurate mapped pixel value. This value is then used for subsequent operations such as constructing the compensated superpixel region map.

[0086] S16, calculate the overlap rate between the superpixel region and the compensation mask in the current frame, merge adjacent regions with an overlap rate greater than a threshold, and count the number of merged regions that meet the preset conditions as the black pig counting result.

[0087] Furthermore, step S16 includes:

[0088] S16-1, The overlap rate is calculated according to the preset formula: overlap rate = |superpixel region of the current frame|compensation mask| / |superpixel region of the current frame|compensation mask|.

[0089] S16-2, Calculate the foreground pixel ratio of each region after merging, and select regions whose foreground pixel ratio is greater than the pixel threshold as valid target regions;

[0090] S16-3, count the number of valid target areas to obtain the black pig count result.

[0091] In this embodiment, the overlap rate between the superpixel region map curr_segments of the current frame and the compensated mask map warped_segments is calculated using the formula: Overlap rate = |Current frame superpixel region| / |Compensated mask|. If the overlap rate is greater than a threshold (e.g., 30%), adjacent superpixels (adjacent superpixels are spatially adjacent regions in the superpixel segmentation result) are merged, ultimately maintaining N independent regions, and labels are reassigned. This reassignment means that when the overlap rate of two adjacent superpixels is greater than the threshold, these two superpixels are considered to belong to the same target region or have a strong correlation, and they need to be merged into one region as a valid target region. Then, a new label is assigned to this merged region to indicate that they are a whole, facilitating subsequent image region identification and processing operations.

[0092] After motion compensation and superpixel merging, each effective target region can better correspond to a part of the black pig's body or the entire black pig. If an effective target region mainly contains pixels of black pigs (e.g., more than 50% of the region belongs to black pigs), it can be regarded as a "black pig-related region." By counting the number of effective target regions that meet the above conditions, the number of black pigs can be estimated. The above superpixel segmentation based on the SLIC algorithm can effectively optimize the black pig boundary contour and reduce fragmentation misjudgments caused by noise or ground interference. In addition, by combining inter-frame motion estimation and compensation mechanisms, the target counting error caused by mutual or partial occlusion of pigs is corrected, improving the stability of black pig recognition in dynamic scenes.

[0093] Reference Figure 5 The diagram shown is a schematic diagram of a black pig counting device based on color difference threshold provided in an embodiment of the present invention.

[0094] In this embodiment, the device 20 includes:

[0095] Preprocessing unit 21 is used to convert the BGR image containing the black pig herd to the Lab color space and perform channel separation to obtain the L channel image, the a channel image and the b channel image;

[0096] Enhancement unit 22 is used to enhance the a-channel image and the b-channel image, and to perform weighted fusion of the enhanced images to obtain a color difference enhanced image;

[0097] Binarization unit 23 is used to perform binarization processing on the color difference enhancement image based on the threshold segmentation strategy determined by the L channel image to obtain a binarized image;

[0098] The segmentation unit 24 is used to convert the BGR image into an RGB image and perform superpixel segmentation to generate a mask image including multiple superpixel regions. The region is then merged with the binarized image to obtain an optimized target mask image.

[0099] The mapping unit 25 is used to obtain a displacement vector by calculating the optical flow field of the current frame and the previous frame, and to map the target mask image of the previous frame to the current frame according to the displacement vector to obtain a compensation mask image.

[0100] The counting unit 26 is used to calculate the overlap rate between the superpixel region and the compensation mask map in the current frame, merge adjacent regions with an overlap rate greater than a threshold, and count the number of regions that meet the preset conditions after merging, which is used as the black pig counting result.

[0101] Each unit module of the device 20 can execute the corresponding steps in the above method embodiment, so the details of each unit module will not be elaborated here. Please refer to the description of the corresponding steps above for details.

[0102] This invention also provides a black pig counting device based on a color difference threshold. The device includes the black pig counting apparatus based on a color difference threshold as described above, wherein the black pig counting apparatus based on the color difference threshold can employ... Figure 5 The structure of the embodiment, correspondingly, can be executed Figure 1 The technical solutions of the method embodiments shown are similar in implementation principle and technical effect. For details, please refer to the relevant records in the above embodiments, which will not be repeated here.

[0103] The device includes: a mobile phone, digital camera, or tablet computer, or other device with a camera function; or a device with an image processing function; or a device with an image display function. The device may include components such as a memory, processor, input unit, display unit, and power supply.

[0104] The memory can be used to store software programs and modules. The processor executes various functional applications and data processing by running the software programs and modules stored in the memory. The memory can mainly include a program storage area and a data storage area. The program storage area can store the operating system, application programs required for at least one function (such as image playback function), etc.; the data storage area can store data created according to the use of the device. In addition, the memory can include high-speed random access memory, and can also include non-volatile memory, such as at least one disk storage device, flash memory device, or other volatile solid-state storage device. Accordingly, the memory can also include a memory controller to provide access to the memory for the processor and input units.

[0105] The input unit can be used to receive input numerical, character, or image information, and to generate keyboard, mouse, joystick, optical, or trackball signal inputs related to user settings and function control. Specifically, in addition to a camera, the input unit of this embodiment may also include a touch-sensitive surface (e.g., a touch screen) and other input devices.

[0106] The display unit can be used to display information input by the user or information provided to the user, as well as various graphical user interfaces of the device. These graphical user interfaces can be composed of graphics, text, icons, video, and any combination thereof. The display unit may include a display panel, optionally configured as an LCD (Liquid Crystal Display), OLED (Organic Light-Emitting Diode), or other similar display panel. Furthermore, a touch-sensitive surface may cover the display panel. When the touch-sensitive surface detects a touch operation on or near it, it transmits the information to the processor to determine the type of touch event. Subsequently, the processor provides corresponding visual output on the display panel based on the type of touch event.

[0107] This invention also provides a computer-readable storage medium, which may be a computer-readable storage medium included in the memory described in the above embodiments; or it may be a standalone computer-readable storage medium not assembled into a device. The computer-readable storage medium stores at least one instruction, which is loaded and executed by a processor to implement... Figure 1 The method for counting black pigs based on color difference thresholds is shown. The computer-readable storage medium may be a read-only memory, a disk, or an optical disk, etc.

[0108] This invention also provides a computer program product, including a computer program / instructions, which are loaded and executed by a processor to achieve [the desired result]. Figure 1 This paper presents a black pig counting method based on color difference threshold.

[0109] It should be noted that the various embodiments in this specification are described in a progressive manner, with each embodiment focusing on the differences from other embodiments. Similar or identical parts between embodiments can be referred to interchangeably. For system embodiments, device embodiments, and storage medium embodiments, since they are basically similar to the method embodiments, the descriptions are relatively simple, and relevant parts can be referred to the descriptions in the method embodiments.

[0110] Furthermore, in this document, the terms "comprising," "including," or any other variations thereof are intended to cover non-exclusive inclusion, such that a process, method, article, or apparatus that comprises a list of elements includes not only those elements but also other elements not expressly listed, or elements inherent to such a process, method, article, or apparatus. Without further limitation, an element defined by the phrase "comprising one..." does not exclude the presence of other identical elements in the process, method, article, or apparatus that includes said element.

[0111] The foregoing description illustrates and describes preferred embodiments of the present invention. It should be understood that the present invention is not limited to the forms disclosed herein and should not be construed as excluding other embodiments. It can be used in various other combinations, modifications, and environments, and can be altered within the scope of the inventive concept by means of the foregoing teachings or techniques or knowledge in related fields. Any modifications and variations made by those skilled in the art that do not depart from the spirit and scope of the present invention should be within the protection scope of the appended claims.

Claims

1. A method for counting black pigs based on color difference threshold, characterized in that, The method includes: The BGR image containing the black pig herd was converted to the Lab color space and the channels were separated to obtain the L channel image, the a channel image, and the b channel image. The a-channel image and the b-channel image are enhanced, and the enhanced images are then weighted and fused to obtain a color difference enhanced image. The color difference enhancement image is binarized based on the threshold segmentation strategy determined by the L-channel image to obtain a binarized image. The BGR image is converted into an RGB image and superpixel segmentation is performed to generate a mask image that includes multiple superpixel regions. The region is then merged with the binarized image to obtain the optimized target mask image. By calculating the optical flow fields of the current frame and the previous frame, a displacement vector is obtained. Based on the displacement vector, the target mask image of the previous frame is mapped to the current frame to obtain a compensation mask image. Calculate the overlap rate between the superpixel region and the compensation mask in the current frame, merge adjacent regions with an overlap rate greater than a threshold, and count the number of merged regions that meet the preset conditions as the black pig counting result.

2. The method for counting black pigs based on color difference threshold according to claim 1, characterized in that, The enhancement process for the a-channel image and the b-channel image, followed by weighted fusion of the enhanced images to obtain a color difference enhanced image, includes: Using CLAHE, and by setting the cropping limit to 2.0 and the tile grid size to 8×8, the a-channel image and the b-channel image are contrast-enhanced and then stitched together to obtain the a-channel enhanced image and the b-channel enhanced image. The enhanced image of channel a and the enhanced image of channel b are weighted and fused according to the weight allocation values ​​to obtain the color difference enhanced image.

3. The method for counting black pigs based on color difference threshold according to claim 1, characterized in that, The threshold segmentation strategy determined based on the L-channel image is used to binarize the chromatic difference enhancement image to obtain a binarized image, including: By calculating the average brightness value of the L-channel image, if the average brightness value is less than a preset value, the current scene is determined to be a low-brightness scene. The Gaussian adaptive thresholding method is used to binarize the color difference enhancement image to obtain the binarized image after illumination compensation. Otherwise, the Otsu automatic thresholding method is used to binarize the color difference enhancement image to obtain the binarized image.

4. The method for counting black pigs based on color difference threshold according to claim 1, characterized in that, The process of converting the BGR image to an RGB image and performing superpixel segmentation to generate a mask image containing multiple superpixel regions, and then merging the regions in conjunction with the binarized image to obtain an optimized target mask image, includes: The SLIC algorithm is used to perform superpixel segmentation on the RGB image based on the set number of segmentation blocks and compactness parameters, generating a mask image that includes multiple superpixel regions. By traversing each superpixel region, the foreground overlap ratio between the superpixel region and the foreground pixels in the binarized image is calculated. Regions with a foreground overlap ratio greater than a preset ratio are marked as regions to be merged, and spatially adjacent regions to be merged are merged to obtain the target mask image.

5. The method for counting black pigs based on color difference threshold according to claim 1, characterized in that, The step of obtaining the displacement vector by calculating the optical flow fields of the current frame and the previous frame includes: The displacement vector is obtained by calculating the optical flow field of the current frame and the previous frame using the Farneback algorithm and setting the pyramid scaling factor to 0.

5.

6. The method for counting black pigs based on color difference threshold according to claim 1, characterized in that, The step of mapping the target mask image of the previous frame to the current frame based on the displacement vector to obtain the compensated mask image includes: Based on the displacement vector and the coordinates of the current frame, coordinate mapping calculation is performed to obtain the coordinate mapping matrix of the previous frame; Based on the coordinate mapping matrix, the non-integer coordinates in the target mask image of the previous frame are compensated by bilinear interpolation to obtain the compensated mask image of the current frame.

7. The method for counting black pigs based on color difference threshold according to claim 1, characterized in that, The calculation of the overlap rate between the superpixel region and the compensation mask map in the current frame includes: The overlap rate is calculated according to the preset formula: Overlap rate = |Superpixel region of the current frame| / |Superpixel region of the current frame| / |Compensation mask|.

8. The method for counting black pigs based on color difference threshold according to claim 1, characterized in that, The number of regions that meet the preset conditions after statistical merging is used as the black pig counting result, including: Calculate the foreground pixel ratio of each merged region, and select regions whose foreground pixel ratio is greater than a pixel threshold as valid target regions; The number of valid target areas is counted to obtain the black pig count result.

9. A black pig counting device based on color difference threshold, characterized in that, The device includes: The preprocessing unit is used to convert the BGR image containing the black pig herd to the Lab color space and perform channel separation to obtain the L channel image, the a channel image, and the b channel image. An enhancement unit is used to enhance the a-channel image and the b-channel image, and then perform weighted fusion on the enhanced images to obtain a color difference enhanced image; The binarization unit is used to perform binarization processing on the color difference enhancement image based on the threshold segmentation strategy determined by the L-channel image to obtain a binarized image. The segmentation unit is used to convert the BGR image into an RGB image and perform superpixel segmentation to generate a mask image that includes multiple superpixel regions. The region is then merged with the binarized image to obtain an optimized target mask image. The mapping unit is used to calculate the optical flow field of the current frame and the previous frame to obtain a displacement vector, and to map the target mask image of the previous frame to the current frame according to the displacement vector to obtain a compensation mask image. The counting unit is used to calculate the overlap rate between the superpixel region and the compensation mask in the current frame, merge adjacent regions with an overlap rate greater than a threshold, and count the number of merged regions that meet the preset conditions as the black pig counting result.

10. A black pig counting device based on color difference threshold, characterized in that, The system includes a processor, a memory, and a computer program stored in the memory, the computer program being executed by the processor to implement the steps of a black pig counting method based on a color difference threshold as described in any one of claims 1 to 8.

Citation Information

Patent Citations

  • Method for detecting mixed colors in pure-color image

    CN117853756A

  • System and method of counting livestock

    US20220104463A1