Chromatic aberration threshold-based black pig counting method, device and equipment

By converting BGR images to 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 in the existing technology is solved, and stable counting is achieved under different lighting conditions.

CN120708166AActive Publication Date: 2025-09-26厦门农芯数字科技有限公司
View PDF 4 Cites 0 Cited by

Patent Information

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

AI Technical Summary

Technical Problem

Existing technologies have difficulty accurately counting black pigs in low-contrast and dynamic scenes. Traditional methods are ineffective under conditions of changing lighting and complex backgrounds. Deep learning methods are computationally complex and costly, making them difficult to run in real time.

Method used

The BGR image is converted to Lab color space, and channel separation and enhancement processing are performed. Combined with superpixel segmentation and optical flow field calculation, black pig counting is achieved through color difference threshold segmentation and motion compensation.

Benefits of technology

The system can accurately count black pigs in low-contrast and dynamic scenes, improve the counting stability and accuracy, adapt to different lighting conditions, and reduce computational complexity and cost.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN120708166A_ABST
    Figure CN120708166A_ABST
Patent Text Reader

Abstract

The invention discloses a color difference threshold-based black pig counting method, device and equipment, and the method comprises the steps: converting a BGR image into a Lab color space, carrying out the channel separation, carrying out the enhancement processing of an obtained a channel image and a b channel image, and carrying out the weight fusion, and obtaining a color difference enhanced image; performing binarization processing on the chromatic aberration enhanced image based on a threshold segmentation strategy determined by the L-channel image to obtain a binarized image; converting the BGR image into an RGB image, performing super-pixel segmentation, generating a mask image corresponding to a plurality of super-pixel regions, and performing region merging in combination with the binarized image to obtain a target mask image; calculating optical flow fields of the current frame and the previous frame to obtain a displacement vector, and mapping a target mask pattern of the previous frame to the current frame according to the displacement vector to obtain a compensation mask pattern; and calculating the overlapping rate of the superpixel region of the current frame and the compensation mask pattern, and carrying out merging and statistics on adjacent regions with the overlapping rate greater than a threshold value to obtain a black pig counting result.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the technical field of pig farm management, and in particular to a method, device and equipment for counting black pigs based on a color difference threshold. Background Art

[0002] In the pig farming sector, black pigs have attracted significant attention due to their superior meat quality and high market demand. However, accurate pig counting in automated management faces numerous challenges. Current pig counting methods primarily include manual counting, RFID ear tag recognition, 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 application.

[0003] However, traditional color segmentation methods rely on RGB / HSV thresholds, making it difficult to effectively segment black pigs when they resemble dark backgrounds (such as mud or shadows). Furthermore, fixed thresholds are difficult to adapt to varying lighting conditions. While deep learning methods have some effectiveness in complex scenarios, the high cost of data collection for the diverse black pig breeds and the high computational complexity of the models make them difficult to run in real time on embedded devices. Background subtraction methods are suitable for monitoring moving objects, but black pigs can be easily misidentified as background when stationary. Furthermore, deep neural network-based detection methods have weak generalization capabilities for black pig identification, traditional image processing methods based on color segmentation have poor robustness, and background subtraction methods are susceptible to motion blur and occlusion. These factors make it difficult for existing technologies to meet the needs of low-cost, high-efficiency, real-time black pig counting. Summary of the Invention

[0004] In view of this, the purpose of the present invention is to propose a black pig counting method and device based on color difference threshold, aiming to solve the problem that the existing technology is difficult to accurately count black pigs in low contrast and dynamic scenes.

[0005] To achieve the above object, the present invention provides a black pig counting method based on color difference threshold, the method comprising: Convert the BGR image containing the black pig group to Lab color space and perform channel separation to obtain L channel image, a channel image and b channel image; Performing enhancement processing on the a-channel image and the b-channel image, and performing weighted fusion on the enhanced images to obtain a chromatic aberration enhanced image; Binarization is performed on the chromatic aberration enhanced image based on a threshold segmentation strategy determined by the L channel image to obtain a binary image; Convert the BGR image into an RGB image and perform superpixel segmentation to generate a mask image corresponding to multiple superpixel regions, combine the regions with the binarized image, and obtain an optimized target mask image; A displacement vector is obtained by calculating the optical flow field of the current frame and the previous frame, 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; The overlap rate between the superpixel area of ​​the current frame and the compensation mask image is calculated, adjacent areas with an overlap rate greater than a threshold are merged, and the number of areas in the merged area that meet the preset conditions is counted as the black pig counting result.

[0006] Preferably, the enhancing the a-channel image and the b-channel image and weighted fusion of the enhanced images to obtain a chromatic aberration enhanced image comprises: 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 spliced ​​to obtain an a-channel enhanced image and a b-channel enhanced image; The a-channel enhanced image and the b-channel enhanced image are weightedly fused according to the weight distribution value to obtain the chromatic aberration enhanced image.

[0007] Preferably, the threshold segmentation strategy determined based on the L channel image is used to perform binarization processing on the chromatic aberration enhanced image to obtain a binarized image, including: By calculating the average brightness value of the L channel image, when the average brightness value is less than a preset value, it is determined that the current scene is a low-brightness scene, and the chromatic aberration enhanced image is binarized using a Gaussian adaptive threshold method to obtain the binarized image after illumination compensation; Otherwise, the chromatic aberration enhanced image is binarized using the Otsu automatic threshold method to obtain the binarized image.

[0008] Preferably, the step of converting the BGR image into an RGB image and performing superpixel segmentation to generate a mask image corresponding to a plurality of superpixel regions, combining the binarized image with the region merging to obtain an optimized target mask image comprises: Using the SLIC algorithm to perform superpixel segmentation on the RGB image based on the set number of segmentation blocks and compactness parameters, and generating a mask image corresponding to multiple superpixel regions; By traversing each of the superpixel areas, calculating the foreground overlap ratio between the superpixel area and the foreground pixels in the binary image, marking the area where the foreground overlap ratio is greater than a preset ratio value as the area to be merged, and merging the spatially adjacent areas to be merged to obtain the target mask map.

[0009] Preferably, obtaining the displacement vector by calculating the optical flow field of the current frame and the previous frame includes: The displacement vector is obtained by calculating the optical flow fields of the current frame and the previous frame by adopting the Farneback algorithm and setting the pyramid scaling factor to 0.5.

[0010] Preferably, mapping the target mask image of the previous frame to the current frame according to the displacement vector to obtain a compensation mask image includes: Performing coordinate mapping calculation based on the displacement vector and the coordinates of the current frame to obtain a coordinate mapping matrix of the previous frame; Based on the coordinate mapping matrix, a bilinear interpolation method is used to perform compensation calculation on the non-integer coordinates in the target mask image of the previous frame to obtain the compensated mask image of the current frame.

[0011] Preferably, calculating the overlap rate between the superpixel area of ​​the current frame and the compensation mask image includes: The overlap rate is obtained by calculation according to the preset formula: overlap rate = |superpixel area of ​​the current frame |compensation mask map| / |superpixel area of ​​the current frame |compensation mask map|.

[0012] Preferably, the counting of the number of regions after the merger that meet the preset conditions as the black pig counting result includes: Calculate the foreground pixel ratio of each merged area, and select the area where the foreground pixel ratio is greater than the pixel threshold as the valid target area; The number of the effective target areas is counted to obtain the black pig counting result.

[0013] To achieve the above-mentioned object, the present invention further provides a black pig counting device based on color difference threshold, the device comprising: A preprocessing unit is used to convert the BGR image containing the black pig group into the Lab color space and perform channel separation to obtain an L channel image, an a channel image, and a b channel image; an enhancement unit, configured to perform enhancement processing on the a-channel image and the b-channel image, and perform weighted fusion on the enhanced images to obtain a chromatic aberration enhanced image; A binarization unit, configured to perform binarization processing on the chromatic aberration enhanced image based on a threshold segmentation strategy determined by the L channel image to obtain a binarized image; a segmentation unit, configured to convert the BGR image into an RGB image and perform superpixel segmentation to generate a mask image corresponding to a plurality of superpixel regions, and perform region merging on the binarized image to obtain an optimized target mask image; a mapping unit, configured to obtain a displacement vector by calculating the optical flow fields of the current frame and the previous frame, and 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 area of ​​the current frame and the compensation mask image, merge adjacent areas with an overlap rate greater than a threshold, and count the number of areas in the merged area that meet the preset conditions as the black pig counting result.

[0014] In order to achieve the above-mentioned objectives, the present invention also proposes a black pig counting device based on 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 color difference threshold as described in the above embodiment.

[0015] In order to achieve the above objectives, the present invention also proposes a computer-readable storage medium, on which a computer program is stored. The computer program 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 embodiment.

[0016] In order to achieve the above objectives, the present invention also proposes a computer program product, including a computer program / instruction, which, when executed by a processor, implements the steps of a black pig counting method based on color difference threshold as described in the above embodiment.

[0017] Beneficial effects: The above scheme, by converting the BGR image to Lab color space and separating the channels, can better utilize color information differences to distinguish black pigs from the background. It then enhances and weighted-fuses the a and b channel images to produce a color-difference-enhanced image, further highlighting the characteristics of black pigs. Binarization is performed based on the L channel, determining a threshold segmentation strategy to adapt to different lighting conditions and accurately extract black pig regions. Subsequent steps such as superpixel segmentation, region merging, and optical flow field calculation effectively address issues such as mutual occlusion among pigs, allowing accurate counting of black pigs in low-contrast scenes and dynamic changes, providing reliable data for breeding management. Furthermore, the Farneback algorithm is used to calculate the optical flow field to obtain a displacement vector, which is then mapped from the previous frame's target mask to the current frame to achieve motion compensation. Combined with the calculation of the overlap rate between the superpixel region and the compensated mask and region merging, the counting method maintains stable and accurate counting results in dynamic scenes of black pig movement, meeting the needs of monitoring pig activity in actual breeding environments.

[0018] By using the CLAHE method to contrast enhance the a and b channel images and setting appropriate cropping limits and tile grid sizes, the color difference contrast between the target area and the background can be effectively improved while avoiding excessive noise enhancement, making image details clearer and enhancing image quality. This provides better input for subsequent segmentation and recognition operations, and improves the performance and stability of the entire counting system. Moreover, by weighting the enhanced a and b channel images through weighted fusion of weight distribution values, the difference characteristics between black pigs and the background can be highlighted, channel information can be reasonably integrated, the effect of color difference enhancement can be further optimized, the accuracy of subsequent segmentation steps can be enhanced, and the black pig area can be extracted more accurately.

[0019] The scene brightness is determined by calculating the average brightness value of the L channel image, and the threshold segmentation method is automatically selected according to different brightness conditions. That is, the Gaussian adaptive threshold method is used in low-brightness scenes to effectively compensate for insufficient lighting conditions, ensuring that the black pig area can still be accurately segmented at night or in dimly lit environments, expanding the applicability of the counting method in different lighting environments; in non-low-brightness scenes, the Otsu automatic threshold method is used to automatically find the threshold that minimizes the intra-class variance of the foreground and background classes, achieving precise binarization processing of the chromatic aberration enhanced image and obtaining an accurate initial binary image, laying a good foundation for subsequent target extraction and counting, and improving the accuracy of the entire counting process.

[0020] Using the SLIC algorithm to perform superpixel segmentation on an RGB image and generate a mask image, the image is segmented into multiple, regionally consistent superpixel regions, more closely resembling the actual outline features of the black pig. By combining the binary image with the superpixel region and calculating the overlap ratio between foreground pixels, and merging eligible adjacent regions, this effectively removes noise interference and completes the broken edges of the black pig target area, resulting in a complete, continuous, and accurate target mask image, improving counting accuracy.

[0021] The coordinate mapping calculation of the previous frame is performed by using the displacement vector and the coordinates of the current frame. Based on this matrix, bilinear interpolation is performed on the non-integer coordinates in the target mask image of the previous frame. This can more accurately map the target mask image of the previous frame to the current frame and achieve accurate motion compensation. It helps to maintain the continuity and consistency of the target area during the movement of black pigs, improve the stability of counting results in dynamic scenes, and reduce counting errors caused by pig movement.

[0022] A preset formula is used to calculate the overlap ratio between the superpixel region of the current frame and the compensation mask image, providing an objective and quantitative method to assess the degree of overlap between the superpixel region and the target region of the previous frame. Region merging based on this overlap ratio ensures that the merged region better conforms to the actual shape and distribution characteristics of black pigs, improving the accuracy of target region extraction and, in turn, the reliability of the counting results. Furthermore, by calculating the foreground pixel ratio of the merged region and selecting regions with a ratio greater than a pixel threshold as valid target regions, it is possible to effectively remove regions that do not conform to black pig characteristics and prevent non-black pig regions from interfering with the counting results. Finally, the number of valid target regions is counted to obtain the black pig counting result, which has stronger anti-interference capabilities and higher accuracy, providing more precise data support for breeding management and production decisions. BRIEF DESCRIPTION OF THE DRAWINGS

[0023] In order to more clearly illustrate the embodiments of the present invention or the technical solutions in the prior art, the following briefly introduces the drawings required for use in the embodiments or the description of the prior art. Obviously, the drawings described below are only some embodiments of the present invention. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying any creative work.

[0024] Figure 1 A flowchart of a black pig counting method based on color difference thresholds provided by one embodiment of the present invention.

[0025] Figure 2 A schematic diagram of the visualization results after channel separation and CLAHE enhancement provided by one embodiment of the present invention.

[0026] Figure 3 A schematic diagram of the visualization result of binarization processing of a chromatic aberration enhanced image provided by an embodiment of the present invention.

[0027] Figure 4 A schematic diagram of the visualization results of superpixel segmentation of an RGB image using the SLIC algorithm provided by one embodiment of the present invention.

[0028] Figure 5 A schematic structural diagram of a black pig counting device based on color difference threshold provided by one embodiment of the present invention.

[0029] The realization of the objectives of the invention, the functional features and advantages will be further explained in conjunction with the embodiments and with reference to the accompanying drawings. DETAILED DESCRIPTION

[0030] In order to make the purpose, technical solutions and advantages of the embodiments of the present invention clearer, the technical solutions in the embodiments of the present invention will be clearly and completely described below in conjunction with the drawings in the embodiments of the present invention. Obviously, the described embodiments are part of the embodiments of the present invention, not all of the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without making creative work 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 drawings is not intended to limit the scope of the invention for which protection is sought, but merely represents selected embodiments of the present invention. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without making creative work are within the scope of protection of the present invention.

[0031] The present invention is described in detail below with reference to the embodiments.

[0032] Reference Figure 1 The figure is a flow chart of a black pig counting method based on color difference threshold provided by one embodiment of the present invention.

[0033] In this embodiment, the method includes: S11, converting the BGR image containing the black pig group into the Lab color space and performing channel separation to obtain an L channel image, an a channel image, and a b channel image.

[0034] S12, performing enhancement processing on the a-channel image and the b-channel image, and performing weighted fusion on the enhanced images to obtain a chromatic aberration enhanced image.

[0035] Furthermore, in step S12, the a-channel image and the b-channel image are enhanced, and the enhanced images are weightedly fused to obtain a chromatic aberration enhanced image, including: S12-1, using CLAHE, setting a cropping limit of 2.0 and a tile grid size of 8×8, respectively performing contrast enhancement processing on the a-channel image and the b-channel image, and then stitching them together to obtain an a-channel enhanced image and a b-channel enhanced image; S12-2, performing weighted fusion on the a-channel enhanced image and the b-channel enhanced image according to the weight distribution value to obtain the chromatic aberration enhanced image.

[0036] In this embodiment, the method is implemented based on the segmentation model obtained by pre-training yolov8. The segmentation model can be deployed on the edge device to achieve real-time processing, which can reduce the dependence on labeled data and is suitable for small and medium-sized farms. Specifically, the farm screen is captured in real time by the camera, and the BGR image containing the black pig group in the video stream is obtained. The input BGR image is converted to the Lab color space to obtain a Lab format image. The Lab format image is separated into three independent channels to obtain the L channel image (brightness information), the a channel image (red and green information) and the b channel image (blue and yellow information). Figure 2 Schematic diagram of channel separation and visualization results after CLAHE enhancement.

[0037] Use CLAHE to perform contrast enhancement on the a-channel image and the b-channel image respectively, obtaining the a-channel enhanced image and the b-channel enhanced image, that is, the a-channel enhanced image a_eq = clahe.apply(a_channel), the b-channel enhanced image b_eq = clahe.apply(b_channel); wherein, the image enhancement technology CLAHE (Contrast Limited Adaptive Histogram Equalization) contrast-limited adaptive histogram equalization is used to enhance the a-channel image and the b-channel image in the Lab color space by setting the clipping limit to 2.0 and the tile grid size to 8×8. While maintaining color details, this process effectively improves the color difference contrast between the target area and the background, enhancing the stability of subsequent segmentation. That is, clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8, 8)), 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 small blocks, equalizes each block separately, and finally splices them together.

[0038] By weightedly fusing the a-channel enhanced image and the b-channel enhanced image (for example, assigning weights of 0.7 and 0.3), we obtain a color difference enhanced image; that is, delta_img = cv2.addWeighted(a_eq, 0.7, b_eq, 0.3, 0). This can highlight the difference between the black pig (which is usually reddish / yellowish) and other objects such as green vegetation / gray ground.

[0039] Furthermore, the network architecture of this segmentation model consists of a backbone network (Backbone), a neck network (Neck), and a head network (Head). The backbone network uses a modified CSPDarknet structure, including multiple C2f modules (Cross Stage Partial Network) to enhance feature extraction capabilities. It introduces the SPPF (Spatial Pyramid Pooling - Fast) module to fuse multi-scale features through max pooling of different sizes. The neck network (Neck) adopts the PANet (Path Aggregation Network) structure, aggregating multi-scale features through top-down and bottom-up paths. It also enhances the Feature Pyramid Network (FPN) to fuse semantic information from different levels. The detection branch of the head network (Head) predicts bounding box coordinates (x, y, w, h), object confidence, and category probability. The segmentation branch generates mask features through an instance segmentation decoder. The output is a binary mask for each object, typically with a resolution of 1 / 4 or 1 / 8 of the original image. The training process is simplified by directly predicting bounding box center points rather than anchor box offsets. Classification losses include: BCEWithLogitsLoss, bounding box loss (combined with 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 (such as FP16), and export to ONNX format, it supports edge device deployment and can run on low-cost hardware such as Raspberry Pi.

[0040] The training process of the segmentation model includes: (1) training data preparation, specifically: obtaining images containing black pigs as training datasets. The training dataset format is image JPEG / PNG format, and the labeled training data is in COCO format (JSON) or YOLO format (txt file), including bounding box coordinates and masks (polygonal or RLE encoding); data enhancement methods are divided into basic enhancement and advanced enhancement. Basic enhancement includes random scaling, rotation, flipping, and brightness / contrast adjustment; advanced enhancement includes Mosaic (4-image stitching), 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 domain analysis).

[0041] S13, performing binarization processing on the chromatic aberration enhanced image based on the threshold segmentation strategy determined by the L channel image to obtain a binarized image.

[0042] Furthermore, in step S13, the chromatic aberration enhanced image is binarized using the threshold segmentation strategy determined based on the L channel image to obtain a binarized image, including: By calculating the average brightness value of the L channel image, when the average brightness value is less than a preset value, it is determined that the current scene is a low-brightness scene, and the chromatic aberration enhanced image is binarized using a Gaussian adaptive threshold method to obtain the binarized image after illumination compensation; Otherwise, the chromatic aberration enhanced image is binarized using the Otsu automatic threshold method to obtain the binarized image.

[0043] In this embodiment, the average brightness value of the L channel image is calculated, and it is determined whether the current scene is in a low-brightness environment based on the average brightness value; if it is a low-brightness scene (such as an average brightness value less than 30), the chromatic aberration enhanced image is binarized using the Gaussian adaptive threshold method to obtain a binary image after illumination compensation; otherwise, the Otsu automatic threshold method is used to find a threshold T so that the intra-class variance of the foreground and background is minimized, and the image less than T is the foreground, and the image greater than T is the background, that is, the chromatic aberration enhanced image is binarized using the Otsu automatic threshold method to obtain a binary image. Figure 3 As shown in the figure, the output binary 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 pigs are extracted from the complex background, providing the prerequisite for tasks such as group counting and behavior analysis.

[0044] S14, converting the BGR image into an RGB image and performing superpixel segmentation to generate a mask image corresponding to multiple superpixel regions, combining the binarized image with the region merging to obtain an optimized target mask image.

[0045] Furthermore, in step S14, the BGR image is converted into an RGB image and superpixel segmentation is performed to generate a mask image corresponding to a plurality of superpixel regions, and the region is merged in combination with the binarized image to obtain an optimized target mask image, including: S14-1, using the SLIC algorithm to perform superpixel segmentation on the RGB image based on the set number of segmentation blocks and compactness parameters, to generate a mask image corresponding to multiple superpixel regions; S14-2, by traversing each of the superpixel areas, calculating the foreground overlap ratio between the superpixel area and the foreground pixels in the binary image, marking the area where the foreground overlap ratio is greater than a preset ratio value as the area to be merged, and merging the spatially adjacent areas to be merged to obtain the target mask map.

[0046] Reference Figure 4 As shown. In this embodiment, the original image in BGR format is converted into an RGB image in RGB format to prepare for subsequent superpixel segmentation. The RGB image is then subjected to superpixel segmentation using the SLIC algorithm, which sets the number of segmentation blocks to 100 and the compactness parameter to 10, obtaining superpixel segmentation results for multiple superpixel regions. A mask image is generated for each superpixel region by traversing all superpixel regions. The overlap ratio of foreground pixels between the superpixel region and the binary image is then calculated. Regions with an overlap ratio greater than a preset value (e.g., 50%) are considered to belong to the foreground and merged, thus completing the broken edges and obtaining N complete and continuous target regions. This process eliminates isolated noise points and preserves the complete pig outline. The merging operation essentially utilizes the "regional consistency" property of superpixels to "correct and complete" the initial binary image, making the target region of the black pig more complete and continuous while eliminating irrelevant noise. Ultimately, a target binary image that is closer to the true target is obtained.

[0047] S15 , obtaining a displacement vector by calculating the optical flow fields of the current frame and the previous frame, and mapping the target mask image of the previous frame to the current frame according to the displacement vector to obtain a compensation mask image.

[0048] Furthermore, in step S15, it includes: S15-1, calculating the optical flow fields of the current frame and the previous frame by using the Farneback algorithm and setting the pyramid scaling factor to 0.5, to obtain the displacement vector; S15-2, performing coordinate mapping calculation based on the displacement vector and the coordinates of the current frame to obtain a coordinate mapping matrix of the previous frame; S15-3, based on the coordinate mapping matrix, using bilinear interpolation to perform compensation calculation on the non-integer coordinates in the target mask image of the previous frame to obtain the compensated mask image of the current frame.

[0049] In this embodiment, the pig's motion in consecutive video frames can cause the segmented superpixel region maps to be misaligned between adjacent frames (e.g., after the pig moves, the same object is segmented into different superpixel regions). Motion compensation uses the pixel displacement estimated by the optical flow field to "map" the superpixel region map of the previous frame to the current frame according to the displacement relationship, generating a compensated mask map, warped_segments. This achieves temporal alignment of the segmented regions and improves object consistency in dynamic scenes. In other words, 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 super-pixel area map, the optical flow field flow of the current frame and the previous frame (including the displacement vector (u, v) of each pixel are processed, the floating-point displacement of the optical flow field is converted into integer coordinates, and the noise points with abnormally large displacements are filtered out; the Farneback algorithm is used to calculate the optical flow field of the current frame and the previous frame in the video based on the set pyramid scaling factor (such as 0.5) to obtain the displacement vector; the coordinate mapping calculation is performed based on the displacement vector and the coordinates of the current frame 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 used Bilinear interpolation solves the problem of the previous frame's coordinates being non-integer and unable to directly index pixel values ​​by calculating and mapping them through bilinear interpolation. The previous frame's coordinates obtained through the coordinate mapping matrix may be non-integer, while the image pixel coordinates are usually integers. To accurately obtain the pixel values ​​at the corresponding positions in the previous frame, methods such as bilinear interpolation are required. The coordinate mapping matrix provides a coordinate basis for bilinear interpolation. The coordinates determined by this matrix are interpolated based on the surrounding integer coordinate pixel values ​​to obtain more accurate mapped pixel values, which are used for subsequent operations such as constructing a compensated superpixel area map.

[0050] S16, calculating the overlap rate between the superpixel area of ​​the current frame and the compensation mask image, merging adjacent areas with an overlap rate greater than a threshold, and counting the number of areas in the merged area that meet the preset conditions as the black pig counting result.

[0051] Furthermore, in step S16, it includes: S16-1, calculating the overlap ratio according to a preset formula: overlap ratio = |superpixel area of ​​current frame||compensation mask image| / |superpixel area of ​​current frame||compensation mask image|; S16-2, calculating the foreground pixel ratio of each merged region, and selecting regions where the foreground pixel ratio is greater than a pixel threshold as valid target regions; S16-3, counting the number of the effective target areas to obtain the black pig counting result.

[0052] In this embodiment, the overlap rate of the superpixel region map curr_segments of the current frame and the compensated compensation mask map warped_segments is calculated, and the formula is overlap rate = |superpixel region of the current frame ∪compensation mask map| / |compensation mask map ∩compensation mask map|; if the overlap rate is greater than a threshold (such as 30%), the adjacent superpixels are merged (adjacent superpixels are regions that are spatially adjacent to each other in the superpixel segmentation result), and ultimately N independent regions are still maintained, and the labels are reallocated. The reallocation of labels means that when the overlap rate of two adjacent superpixels is greater than a threshold, it is considered that the two superpixels belong to the same target area or have a strong correlation, and they need to be merged into one area as a valid target area, and then a new label is assigned to the merged area to indicate that they are a whole, which is convenient for subsequent operations such as identification and processing of image areas.

[0053] After motion compensation and superpixel merging, each valid target region can better correspond to a black pig's body part or the entire pig. If a valid target region primarily contains black pig pixels (e.g., more than 50% of the area belongs to black pigs), it is considered a "black pig-related region." By counting the number of valid target regions that meet these conditions, the number of black pigs can be estimated. The introduction of superpixel segmentation based on the SLIC algorithm effectively optimizes the black pig boundary contours and reduces fragmentation misclassifications caused by noise or ground interference. Furthermore, combined with inter-frame motion estimation and compensation mechanisms, target counting errors caused by mutual or partial occlusion of pigs can be corrected, improving the stability of black pig recognition in dynamic scenes.

[0054] Reference Figure 5 FIG2 is a schematic structural diagram of a black pig counting device based on color difference threshold provided by one embodiment of the present invention.

[0055] In this embodiment, the device 20 includes: A preprocessing unit 21 is used to convert the BGR image containing the black pig group into a Lab color space and perform channel separation to obtain an L channel image, an a channel image, and a b channel image; The enhancement unit 22 is configured to perform enhancement processing on the a-channel image and the b-channel image, and perform weighted fusion on the enhanced images to obtain a chromatic aberration enhanced image; A binarization unit 23 is configured to perform binarization processing on the chromatic aberration enhanced image based on a threshold segmentation strategy determined by the L channel image to obtain a binarized image; The segmentation unit 24 is configured to convert the BGR image into an RGB image and perform superpixel segmentation to generate a mask image corresponding to a plurality of superpixel regions, and perform region merging on the binary image to obtain an optimized target mask image; A mapping unit 25 is configured to obtain a displacement vector by calculating the optical flow fields of the current frame and the previous frame, and 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 26 is used to calculate the overlap rate between the superpixel area of ​​the current frame and the compensation mask image, merge adjacent areas with an overlap rate greater than a threshold, and count the number of areas in the merged area that meet the preset conditions as the black pig counting result.

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

[0057] The embodiment of the present invention further provides a black pig counting device based on color difference threshold, which includes the black pig counting device based on color difference threshold as described above, wherein the black pig counting device based on color difference threshold can be used. Figure 5 The structure of the embodiment can be executed accordingly. Figure 1 The technical solution of the method embodiment shown has similar implementation principles and technical effects. For details, please refer to the relevant records in the above embodiments and will not be repeated here.

[0058] The device includes: a mobile phone, digital camera, 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, a processor, an input unit, a display unit, and a power supply.

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

[0060] The input unit can be used to receive input digital, character, or image information, and generate keyboard, mouse, joystick, optical, or trackball signal input related to user settings and function control. Specifically, the input unit of this embodiment can include not only a camera, but also a touch-sensitive surface (such as a touch display) and other input devices.

[0061] The display unit can be used to display information input by the user or information provided to the user and various graphical user interfaces of the device, which can be composed of graphics, text, icons, videos and any combination thereof. The display unit may include a display panel. Optionally, the display panel can be configured in the form of an LCD (Liquid Crystal Display), an OLED (Organic Light-Emitting Diode), etc. Furthermore, the touch-sensitive surface can cover the display panel. When the touch-sensitive surface detects a touch operation on or near it, it is transmitted to the processor to determine the type of touch event. The processor then provides a corresponding visual output on the display panel based on the type of touch event.

[0062] The embodiment of the present invention further provides a computer-readable storage medium, which may be a computer-readable storage medium included in the memory in the above embodiment; or a computer-readable storage medium that exists independently and is 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 black pig counting method based on color difference threshold is shown. The computer readable storage medium can be a read-only memory, a magnetic disk or an optical disk.

[0063] The embodiment of the present invention further provides a computer program product, including a computer program / instruction, which is loaded and executed by a processor to implement Figure 1 A black pig counting method based on color difference threshold is shown.

[0064] 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. For similar or identical parts between the various embodiments, reference can be made to each other. For system embodiments, device embodiments, and storage medium embodiments, since they are generally similar to the method embodiments, their descriptions are relatively simple. For relevant parts, reference can be made to the descriptions of the method embodiments.

[0065] Furthermore, in this document, the terms "comprises," "comprising," or any other variations thereof are intended to encompass non-exclusive inclusion, such that a process, method, article, or apparatus that includes a list of elements includes not only those elements but also other elements not explicitly listed, or elements inherent to such process, method, article, or apparatus. In the absence of further limitations, an element defined by the phrase "comprising a ..." does not preclude the presence of additional identical elements in the process, method, article, or apparatus that includes the element.

[0066] While the foregoing description shows 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. Rather, the present invention can be used in various other combinations, modifications, and environments, and can be modified within the scope of the present invention by the teachings herein or by techniques or knowledge in the relevant art. Modifications and variations made by those skilled in the art without departing from the spirit and scope of the present invention are intended to be within the scope of the appended claims.

Claims

1. A black pig counting method based on color difference threshold, characterized in that: The method comprises: Convert the BGR image containing the black pig group to Lab color space and perform channel separation to obtain L channel image, a channel image and b channel image; Performing enhancement processing on the a-channel image and the b-channel image, and performing weighted fusion on the enhanced images to obtain a chromatic aberration enhanced image; Binarization is performed on the chromatic aberration enhanced image based on a threshold segmentation strategy determined by the L channel image to obtain a binary image; Convert the BGR image into an RGB image and perform superpixel segmentation to generate a mask image corresponding to multiple superpixel regions, combine the regions with the binarized image, and obtain an optimized target mask image; A displacement vector is obtained by calculating the optical flow field of the current frame and the previous frame, 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; The overlap rate between the superpixel area of ​​the current frame and the compensation mask image is calculated, adjacent areas with an overlap rate greater than a threshold are merged, and the number of areas in the merged area that meet the preset conditions is counted as the black pig counting result.

2. The black pig counting method based on color difference threshold according to claim 1 is characterized in that: The enhancing process is performed on the a-channel image and the b-channel image, and weighted fusion is performed on the enhanced images to obtain a chromatic aberration enhanced image, including: 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 spliced ​​to obtain an a-channel enhanced image and a b-channel enhanced image; The a-channel enhanced image and the b-channel enhanced image are weightedly fused according to the weight distribution value to obtain the chromatic aberration enhanced image.

3. The black pig counting method based on color difference threshold according to claim 1 is characterized in that: The method of performing binarization processing on the chromatic aberration enhanced image based on the threshold segmentation strategy determined based on the L channel image to obtain a binarized image includes: By calculating the average brightness value of the L channel image, when the average brightness value is less than a preset value, it is determined that the current scene is a low-brightness scene, and the chromatic aberration enhanced image is binarized using a Gaussian adaptive threshold method to obtain the binarized image after illumination compensation; Otherwise, the chromatic aberration enhanced image is binarized using the Otsu automatic threshold method to obtain the binarized image.

4. The black pig counting method based on color difference threshold according to claim 1 is characterized in that: The step of converting the BGR image into an RGB image and performing superpixel segmentation to generate a mask image corresponding to a plurality of superpixel regions, combining the binarized image with the region merging to obtain an optimized target mask image comprises: Using the SLIC algorithm to perform superpixel segmentation on the RGB image based on the set number of segmentation blocks and compactness parameters, and generating a mask image corresponding to multiple superpixel regions; By traversing each of the superpixel areas, calculating the foreground overlap ratio between the superpixel area and the foreground pixels in the binary image, marking the area where the foreground overlap ratio is greater than a preset ratio value as the area to be merged, and merging the spatially adjacent areas to be merged to obtain the target mask map.

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

5.

6. The black pig counting method based on color difference threshold according to claim 1 is characterized in that: Mapping the target mask image of the previous frame to the current frame according to the displacement vector to obtain a compensation mask image includes: Performing coordinate mapping calculation based on the displacement vector and the coordinates of the current frame to obtain a coordinate mapping matrix of the previous frame; Based on the coordinate mapping matrix, a bilinear interpolation method is used to perform compensation calculation on the non-integer coordinates in the target mask image of the previous frame to obtain the compensated mask image of the current frame.

7. The black pig counting method based on color difference threshold according to claim 1 is characterized in that: Calculating the overlap rate between the superpixel region of the current frame and the compensation mask image includes: The overlap rate is obtained by calculation according to the preset formula: overlap rate = |superpixel area of ​​the current frame |compensation mask map| / |superpixel area of ​​the current frame |compensation mask map|.

8. The method for counting black pigs based on color difference threshold according to claim 1, characterized in that: The number of areas after the statistical merger that meet the preset conditions, as the black pig counting result, includes: Calculate the foreground pixel ratio of each merged area, and select the area where the foreground pixel ratio is greater than the pixel threshold as the valid target area; The number of the effective target areas is counted to obtain the black pig counting result.

9. A black pig counting device based on color difference threshold, characterized in that: The device comprises: A preprocessing unit is used to convert the BGR image containing the black pig group into the Lab color space and perform channel separation to obtain an L channel image, an a channel image, and a b channel image; an enhancement unit, configured to perform enhancement processing on the a-channel image and the b-channel image, and perform weighted fusion on the enhanced images to obtain a chromatic aberration enhanced image; A binarization unit, configured to perform binarization processing on the chromatic aberration enhanced image based on a threshold segmentation strategy determined by the L channel image to obtain a binarized image; a segmentation unit, configured to convert the BGR image into an RGB image and perform superpixel segmentation to generate a mask image corresponding to a plurality of superpixel regions, and perform region merging on the binarized image to obtain an optimized target mask image; a mapping unit, configured to obtain a displacement vector by calculating the optical flow fields of the current frame and the previous frame, and 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 area of ​​the current frame and the compensation mask image, merge adjacent areas with an overlap rate greater than a threshold, and count the number of areas in the merged area 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 method comprises 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 color difference threshold as claimed in any one of claims 1 to 8.

Citation Information

Patent Citations

  • Method for detecting mixed colors in pure-color image

    CN117853756A

  • National costume image recognition method and system based on tie-dyeing process

    CN119942116A

  • Black pig image segmentation method based on multi-feature fusion

    CN120471941A

  • System and method of counting livestock

    US20220104463A1