A visually assisted dairy sheep counting method and system

By performing threshold segmentation and template matching on grayscale images of dairy sheep flocks, combined with edge detection technology, overlapping dairy sheep regions were screened and analyzed, solving the problem of inaccurate dairy sheep counts and achieving accurate dairy sheep number statistics.

CN118628984BActive Publication Date: 2024-11-26神木市畜牧业发展中心
View PDF 7 Cites 0 Cited by

Patent Information

Application Number
CN202411095969.1
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2024-08-12
Publication Date
2024-11-26
Estimated Expiration
2044-08-12

AI Technical Summary

Technical Problem

Existing methods cannot accurately count the number of dairy sheep, mainly due to overlapping and ghosting phenomena that lead to inaccurate counting of dairy sheep in the image.

Method used

By acquiring grayscale images of dairy sheep flocks, threshold segmentation is performed to obtain dairy sheep regions. Template matching and edge detection techniques are used to filter out overlapping dairy sheep regions. Finally, the number of dairy sheep is accurately counted through shape similarity analysis of edge line groups.

Benefits of technology

It effectively eliminated the interference of overlapping and trailing images of dairy sheep on the counting of dairy sheep, and achieved accurate counting of dairy sheep.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN118628984B_ABST
    Figure CN118628984B_ABST
Patent Text Reader

Abstract

The present invention relates to the field of image analysis, and in particular to a method and system for counting the number of dairy sheep under visual assistance. The method first performs threshold segmentation on a grayscale image of a dairy sheep herd to obtain multiple dairy sheep areas in the grayscale image, performs template matching on each dairy sheep area and a template, and screens out overlapping dairy sheep areas from all dairy sheep areas; performs edge detection on the overlapping dairy sheep areas to obtain multiple edge lines; uses any two edge lines as edge line groups in the overlapping dairy sheep areas, and obtains the shape similarity of the edge line groups according to the difference in pixel point position distribution between the edge lines in the edge line groups; counts the number of dairy sheep according to the shape similarity of each edge line group in the overlapping dairy sheep areas, the number of edge lines, and the number of non-overlapping dairy sheep areas in the grayscale image. The present invention can eliminate the interference of the dairy sheep overlap phenomenon and the smear in the image on the number counting, and accurately count the number of dairy sheep.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The invention relates to the field of image analysis, and in particular to a vision-assisted dairy sheep counting method and system. Background Art

[0002] Dairy sheep are a breed of sheep specifically raised for milk production. Regularly counting the number of dairy sheep can promptly detect possible health problems in the dairy sheep herd. It can also better adjust production and sales strategies and control the degree of consumption of environmental resources by the herd, helping managers to develop better management plans.

[0003] Since dairy sheep are usually raised in open pastures, related technologies usually use drones to photograph dairy sheep herds, and count the number of dairy sheep based on machine vision technologies such as image segmentation. However, due to the overlap of multiple dairy sheep, the rapid movement of some dairy sheep, and the jitter that occurs when drones are photographed, there are ghost images of dairy sheep in the images, which makes it impossible to accurately count the number of dairy sheep using existing methods. Summary of the invention

[0004] In order to solve the technical problem that the number of dairy sheep cannot be accurately counted using the existing method, the purpose of the present invention is to provide a method and system for counting the number of dairy sheep with visual assistance. The technical scheme adopted is as follows:

[0005] Obtain a grayscale image of a dairy sheep herd; perform threshold segmentation on the grayscale image to obtain multiple dairy sheep regions in the grayscale image;

[0006] For each dairy sheep region, scaling each template according to the size difference between the dairy sheep region and each template to obtain an adjustment template; matching the dairy sheep region with each adjustment template, and screening out overlapping dairy sheep regions from all the dairy sheep regions;

[0007] Perform edge detection on the overlapping dairy sheep regions to obtain multiple edge lines in the overlapping dairy sheep regions; for each overlapping dairy sheep region, any two edge lines constitute an edge line group, and according to the difference in pixel point position distribution between edge lines in the edge line group, obtain the shape similarity of the edge line group;

[0008] The number of dairy sheep is counted according to the shape similarity of each edge line group in each overlapping dairy sheep area, the number of edge lines, and the number of non-overlapping dairy sheep areas in the grayscale image.

[0009] Furthermore, scaling each template according to the size difference between the dairy sheep area and each template to obtain the adjusted template includes:

[0010] Select any one of the horizontal and vertical directions as the direction to be measured;

[0011] The maximum value of the number of pixels in the dairy sheep area in the direction to be tested is used as the numerator, the maximum value of the number of pixels in each template in the direction to be tested is used as the denominator, and the ratio is used as the scaling ratio of each template;

[0012] Based on the scaling ratio, each template is scaled to obtain an adjusted template.

[0013] Further, scaling each template based on the scaling ratio to obtain an adjusted template includes:

[0014] Use any template as the target template;

[0015] If the scaling ratio of the target template is greater than a value of 1, interpolation processing is performed on the target template, and the target template is enlarged by the scaling ratio to obtain an adjusted template;

[0016] If the scaling ratio of the target template is less than a value of 1, interpolation processing is performed on the target template, and the target template is reduced by the scaling ratio to obtain an adjusted template;

[0017] If the scaling ratio of the target template is equal to the value 1, the target template is not scaled.

[0018] Further, the matching of the dairy sheep region with each adjustment template and screening out overlapping dairy sheep regions from all the dairy sheep regions comprises:

[0019] Performing template matching between the dairy sheep region and each adjustment template to obtain a matching degree between the dairy sheep region and each adjustment template;

[0020] The maximum value of the matching degree between the dairy sheep region and all the adjustment templates is taken as the non-overlapping possibility of the dairy sheep region;

[0021] The dairy sheep areas whose non-overlapping probability is less than a preset first threshold are regarded as overlapping dairy sheep areas.

[0022] Furthermore, obtaining the shape similarity of the edge line group according to the difference in pixel point position distribution between edge lines in the edge line group includes:

[0023] Taking any edge line group in the overlapping dairy sheep area as the target edge line group, respectively obtaining the eight-chain code sequence of two edge lines in the target edge line group, and taking the eight-chain code sequence of the shorter edge line of the two edge lines as the short sequence, and taking the eight-chain code sequence of the longer edge line of the two edge lines as the long sequence;

[0024] The number of elements contained in the short sequence is taken as a reference number, and a sequence consisting of adjacent reference number elements in the long sequence is taken as a subsequence of the long sequence;

[0025] According to the difference of the elements at the same position between the short sequence and each subsequence, the sequence similarity between the short sequence and each subsequence is obtained;

[0026] The maximum value of the sequence similarities between the short sequence and all subsequences is taken as the shape similarity of the target edge line group.

[0027] Furthermore, obtaining the sequence similarity between the short sequence and each subsequence according to the difference of the elements at the same position between the short sequence and each subsequence includes:

[0028] Take any subsequence as the target subsequence, and obtain the element difference value at each position between the short sequence and the target subsequence according to the difference of the elements at the same position between the short sequence and the target subsequence;

[0029] Using a negative exponential function with the natural constant e as the base, the accumulated values ​​of the element differences at all positions between the short sequence and the target subsequence are mapped to obtain the sequence similarity between the short sequence and the target subsequence.

[0030] Further, counting the number of dairy sheep according to the shape similarity of each edge line group in each overlapping dairy sheep area, the number of edge lines, and the number of non-overlapping dairy sheep areas in the grayscale image includes:

[0031] In each overlapping dairy sheep region, the edge line group whose shape similarity is greater than a preset second threshold is used as the smear edge line group of the overlapping dairy sheep region;

[0032] According to the difference between the number of all edge lines in each overlapping dairy sheep area and the number of all the smear edge line groups, the number of dairy sheep in each overlapping dairy sheep area is obtained;

[0033] The accumulated value of the number of dairy sheep in all overlapping dairy sheep areas is taken as the first number, the number of all non-overlapping dairy sheep areas in the grayscale image is taken as the second number, and the sum of the first number and the second number is taken as the actual number of dairy sheep in the grayscale image.

[0034] Furthermore, the step of performing threshold segmentation on the grayscale image to obtain a plurality of dairy sheep regions in the grayscale image comprises:

[0035] Performing threshold segmentation on the grayscale image to obtain an initial binary image, and performing an opening operation on the initial binary image to obtain an adjusted binary image;

[0036] The connected region composed of pixels with grayscale values ​​of 1 in the adjusted binary image corresponds to the region in the grayscale image as a dairy sheep region in the grayscale image.

[0037] The present invention also proposes a visually assisted dairy sheep counting system, the system comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, the processor implementing any one of the steps of a visually assisted dairy sheep counting method when executing the computer program.

[0038] The present invention has the following beneficial effects:

[0039] The present invention takes into account that due to the overlap of dairy sheep and the smearing of dairy sheep in the image, the existing method cannot accurately count the number of dairy sheep. Therefore, the present invention first performs threshold segmentation on the grayscale image of the dairy sheep herd to obtain multiple dairy sheep areas in the grayscale image; considering that the overlap and smearing of multiple dairy sheep in the grayscale image will reduce the accuracy of subsequent counting, and considering the problem that the size difference between the template image and the dairy sheep area in the grayscale image is large, the present invention first scales each template, and then performs template matching on the dairy sheep area and each adjustment template, and screens out overlapping dairy sheep areas from all dairy sheep areas, considering that although there are multiple dairy sheep overlapping in the overlapping dairy sheep area As well as the overlapping phenomenon of dairy sheep and afterimages, the edges between the dairy sheep and between the dairy sheep and the afterimages are relatively obvious. Therefore, the present invention first performs edge detection on the overlapping dairy sheep area to obtain multiple edge lines in the overlapping dairy sheep area, wherein each edge line in the overlapping dairy sheep area represents the edge of a dairy sheep or the edge of a afterimage. Considering that the shapes and directions of the edges of the dairy sheep and the afterimages are highly similar, while the edge shapes of the dairy sheep are less similar, the shape similarity obtained can reflect the possibility that two edge lines in the edge line group are the edges of the afterimage and the edges of the dairy sheep body that produces the afterimage, thereby eliminating the interference of the overlapping dairy sheep and the afterimages in the image on the number counting, and accurately counting the number of dairy sheep. BRIEF DESCRIPTION OF THE DRAWINGS

[0040] In order to more clearly illustrate the technical solutions and advantages in the embodiments of the present invention or the prior art, the drawings required for use in the embodiments or the prior art descriptions are briefly introduced below. 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 creative work.

[0041] Figure 1 A flow chart of a method for counting the number of dairy sheep with visual assistance provided by one embodiment of the present invention;

[0042] Figure 2 A schematic diagram of an initial binary image provided by an embodiment of the present invention;

[0043] Figure 3 A flow chart of a method for obtaining an adjustment template provided by an embodiment of the present invention;

[0044] Figure 4 A flow chart of a method for obtaining shape similarity of edge line groups provided by an embodiment of the present invention;

[0045] Figure 5 A flow chart of a method for counting the number of dairy sheep provided by one embodiment of the present invention. DETAILED DESCRIPTION

[0046] In order to further explain the technical means and effects adopted by the present invention to achieve the predetermined invention purpose, the following is a detailed description of a visually assisted dairy sheep counting method and system according to the present invention, its specific implementation, structure, features and effects, in conjunction with the accompanying drawings and preferred embodiments. In the following description, different "one embodiment" or "another embodiment" does not necessarily refer to the same embodiment. In addition, specific features, structures or characteristics in one or more embodiments may be combined in any suitable form.

[0047] Unless defined otherwise, all technical and scientific terms used herein have the same meaning as commonly understood by one of ordinary skill in the art to which this invention belongs.

[0048] The specific scheme of a visually assisted dairy sheep counting method and system provided by the present invention is described in detail below with reference to the accompanying drawings.

[0049] See also Figure 1 , which shows a flow chart of a visually assisted dairy sheep counting method provided by an embodiment of the present invention, the method comprising:

[0050] Step S1: obtaining a grayscale image of a dairy sheep herd; performing threshold segmentation on the grayscale image to obtain a plurality of dairy sheep regions in the grayscale image.

[0051] The embodiment of the present invention first uses a drone to shoot a flock of dairy sheep in a pasture and collects an original image. Since the original image is generally a multi-channel RGB image, in order to reduce the amount of calculation of subsequent image processing and improve the processing speed, in one embodiment of the present invention, the collected original image is gray-scaled and converted into a single-channel gray-scale image. It should be noted that gray-scale processing is a technical means well known to those skilled in the art and will not be described in detail here.

[0052] Since the hair on the surface of dairy sheep is generally white, which is quite different from the background grayscale, the embodiment of the present invention can first perform threshold segmentation on the grayscale image to preliminarily obtain multiple dairy sheep areas in the grayscale image. Subsequently, each dairy sheep area can be analyzed to screen out dairy sheep areas with overlapping dairy sheep and ghosting phenomena.

[0053] Preferably, in one embodiment of the present invention, the method for acquiring multiple dairy sheep regions in a grayscale image specifically includes:

[0054] First, perform threshold segmentation on the grayscale image to obtain the initial binary image. Figure 2 , which is a schematic diagram of an initial binary image provided by an embodiment of the present invention, wherein the background grass has noise due to the reflection phenomenon, and after the image is binarized, there are tiny holes in the area representing the dairy sheep, which is not conducive to subsequent analysis. Therefore, the embodiment of the present invention further needs to perform an opening operation on the initial binary image to obtain an adjusted binary image.

[0055] In one embodiment of the present invention, the grayscale image is segmented using the existing Otsu threshold segmentation algorithm. In other embodiments of the present invention, the threshold segmentation may also be performed using an existing adaptive threshold segmentation algorithm, which is not limited here.

[0056] Among them, the opening operation is a technical means well known to those skilled in the art and will not be elaborated here.

[0057] After threshold segmentation and opening operation, the pixel points with gray value 1 represent the dairy sheep in the image. Therefore, the connected area composed of the pixel points with gray value 1 in the adjusted binary image can be regarded as a dairy sheep area in the gray image.

[0058] So far, multiple dairy sheep regions in the grayscale image are obtained.

[0059] Step S2: for each dairy sheep region, scale each template according to the size difference between the dairy sheep region and each template to obtain an adjusted template; match the dairy sheep region with each adjusted template, and screen out overlapping dairy sheep regions from all dairy sheep regions.

[0060] Considering that there are a large number of dairy sheep in the ranch, it is inevitable that multiple dairy sheep will overlap with each other during the shooting process. At the same time, due to the rapid movement of some dairy sheep and the shaking of the drone during the shooting process, there is a ghosting phenomenon of dairy sheep in the collected grayscale image, and there is overlap between the ghosting and the main body. Therefore, there are two situations for each dairy sheep area obtained above. The first situation is: the dairy sheep area represents an area composed of a single dairy sheep, and there is no overlap of dairy sheep and overlap of dairy sheep with ghosts. The second situation is: the dairy sheep area represents an area composed of multiple overlapping dairy sheep or overlapping dairy sheep with ghosts. For the second situation, due to the overlap of multiple dairy sheep and the influence of ghosts, it is impossible to accurately count the number of dairy sheep in the dairy sheep area with overlapping phenomena. Therefore, it is necessary to first extract the dairy sheep area with overlapping phenomena to facilitate subsequent further analysis.

[0061] The embodiment of the present invention first analyzes any dairy sheep area, and obtains templates of the front, back, side and various postures of the dairy sheep from the database, so as to facilitate the subsequent extraction of the dairy sheep area with overlapping phenomena through template matching. The value range of the number of templates is generally an integer of 50 to 100. In one embodiment of the present invention, the number of templates is set to 50. The specific numerical value of the number of templates can also be set by the implementer according to the specific implementation scenario, which is not limited here. Due to the large difference in body shape between the dairy sheep and the overlapping phenomenon, the dairy sheep area with overlapping phenomena is relatively large, resulting in a large size difference between the dairy sheep area and each template, which reduces the accuracy of subsequent template matching analysis. Therefore, the embodiment of the present invention first scales each template according to the size difference between the dairy sheep area and each template to obtain an adjusted template. The adjusted template can be used to perform template matching with the dairy sheep area to accurately screen out the overlapping dairy sheep area in the grayscale image.

[0062] Preferably, in one embodiment of the present invention, the method for obtaining the adjustment template specifically includes:

[0063] See also Figure 3 , which shows a flow chart of a method for obtaining an adjustment template provided by an embodiment of the present invention.

[0064] Step S201: Select any one of the horizontal direction and the vertical direction as the direction to be measured.

[0065] First, any one of the horizontal direction or the vertical direction is selected as the direction to be tested. For example, the horizontal direction can be selected as the direction to be tested. Subsequently, the dairy sheep area and the template can be analyzed in the direction to be tested.

[0066] Step S202: taking the maximum value of the number of pixels in the dairy sheep area in the direction to be measured as the numerator, taking the maximum value of the number of pixels in each template in the direction to be measured as the denominator, and taking the ratio as the scaling ratio of each template.

[0067] Since there is a large size difference between the dairy sheep area and each template, in order to reduce this difference and improve the accuracy of subsequent template matching, the embodiment of the present invention takes the maximum value of the number of pixels in the dairy sheep area in the direction to be measured as the numerator, and the maximum value of the number of pixels of each template in the direction to be measured as the denominator, and takes the ratio as the scaling ratio of each template. Subsequently, each template can be scaled based on the scaling ratio, so that the size of the adjusted template is more consistent with that of the dairy sheep area, thereby improving the accuracy of subsequent template matching.

[0068] The expression of the scaling ratio of each template can be specifically, for example, as follows:

[0069] ;

[0070] in, Indicates Templates about Scaling ratio of dairy sheep areas; Indicates The maximum number of pixels in the dairy sheep area in the direction to be tested; Indicates The maximum number of pixels of the template in the direction to be tested. Since the template must have pixels in the direction to be tested, .

[0071] Step S203: scaling each template based on the scaling ratio to obtain an adjusted template.

[0072] After obtaining the scaling ratio of each template, the size of the template can be adjusted based on the scaling ratio to obtain an adjusted template.

[0073] Preferably, in one embodiment of the present invention, the method for obtaining the adjustment template further comprises:

[0074] First, any template is used as the target template. If the scaling ratio of the target template is greater than 1, it means that the size of the target template is smaller than the dairy sheep area. In this case, the target template needs to be interpolated and enlarged at the scaling ratio to obtain an adjusted template.

[0075] If the scaling ratio of the target template is less than 1, it means that the size of the target template is larger than the dairy sheep area, and the target template needs to be interpolated and reduced by the scaling ratio to obtain an adjusted template;

[0076] If the scaling ratio of the target template is equal to 1, it means that the sizes of the target template and the dairy sheep area are relatively consistent, and the target template is not scaled. At this time, it can be considered that the target template is still itself after adjustment, that is, the adjustment template corresponding to the target template is the target template itself.

[0077] In the embodiment of the present invention, a bilinear interpolation algorithm or a nearest neighbor interpolation algorithm may be used to perform interpolation processing on the template to implement scaling processing on the template, which is not limited here.

[0078] By using the same method as above, each template can be adjusted to obtain multiple adjusted templates.

[0079] For the dairy sheep area without overlapping phenomenon in the grayscale image, that is, this dairy sheep area only represents a single dairy sheep and there is no ghosting phenomenon, it is more matched with the adjustment template. For the dairy sheep area with overlapping phenomenon, the matching degree between it and the adjustment template is low. Therefore, the dairy sheep area can be template matched with each adjustment template, and the overlapping dairy sheep area can be screened out from all dairy sheep areas. Subsequently, the overlapping dairy sheep area can be analyzed to accurately analyze the number of dairy sheep in the overlapping dairy sheep area.

[0080] Preferably, in one embodiment of the present invention, the method for obtaining overlapping dairy sheep areas further comprises:

[0081] First, template matching is performed on the dairy sheep region and each adjustment template to obtain the matching degree between the dairy sheep region and each adjustment template. The matching degree is obtained by template matching, which is a technical means well known to those skilled in the art and will not be elaborated here.

[0082] Then, the greater the matching degree between the dairy sheep area and the adjustment template, the higher the possibility that there is no overlap in the dairy sheep area. Therefore, the maximum value of the matching degree between the dairy sheep area and all the adjustment templates is taken as the non-overlapping possibility of the dairy sheep area. The greater the non-overlapping possibility, the greater the possibility that there is no overlap in the dairy sheep area. The non-overlapping possibility of each dairy sheep area in the grayscale image can be obtained by the above method.

[0083] Finally, through the above analysis, it can be seen that the smaller the non-overlapping possibility is, the greater the possibility of overlapping in the dairy sheep area. Therefore, the dairy sheep area with a non-overlapping possibility less than the preset first threshold can be used as an overlapping dairy sheep area. In one embodiment of the present invention, the preset first threshold is set to 0.7. The specific value of the preset first threshold can also be set by the implementer according to the specific implementation scenario, and is not limited here.

[0084] At this point, multiple overlapping dairy sheep regions in the grayscale image have been screened out.

[0085] Step S3: performing edge detection on the overlapping dairy sheep areas to obtain multiple edge lines in the overlapping dairy sheep areas; for each overlapping dairy sheep area, an edge line group is formed by any two edge lines, and the shape similarity of the edge line group is obtained according to the difference in pixel point position distribution between the edge lines in the edge line group.

[0086] Since there are multiple overlapping dairy sheep or overlapping dairy sheep and afterimages in the overlapping dairy sheep areas, it is impossible to accurately count the number of dairy sheep contained in the overlapping dairy sheep areas. Therefore, it is necessary to analyze each overlapping dairy sheep area. Considering that although there are multiple overlapping dairy sheep or overlapping dairy sheep and afterimages in the overlapping dairy sheep areas, the edges between the dairy sheep and between the dairy sheep and the afterimages are relatively obvious, edge detection can be performed on each overlapping dairy sheep area first to obtain multiple edge lines in each overlapping dairy sheep area, wherein each edge line in the overlapping dairy sheep area represents the edge of a dairy sheep or the edge of a afterimage, thereby realizing the separation of overlapping dairy sheep. That is to say, an edge line in the overlapping dairy sheep area may be the edge of a dairy sheep or the edge of a afterimage.

[0087] In the embodiment of the present invention, the existing canny edge detection algorithm or Roberts edge detection algorithm or other edge detection algorithms may be used to perform edge detection on the overlapping dairy sheep area, which is not limited here.

[0088] Since each edge line in the overlapping dairy sheep area represents a dairy sheep or a trailing image, if there is only overlapping of multiple dairy sheep in the overlapping dairy sheep area, the number of edge lines in the overlapping dairy sheep area is equal to the number of dairy sheep it contains. In this case, it is only necessary to count the number of edge lines in the overlapping dairy sheep area to obtain the number of dairy sheep it contains. However, if there is trailing image in the overlapping dairy sheep area, that is, there is overlapping between dairy sheep and trailing images in the overlapping dairy sheep area, then an edge line in the overlapping dairy sheep area may also be the edge of the trailing image. Therefore, if the number of edge lines is directly used as the number of dairy sheep in the overlapping dairy sheep area, the result will be biased larger. Therefore, it is also necessary to analyze the number of trailing images in the overlapping dairy sheep area.

[0089] Since the afterimage is caused by the rapid relative motion between the dairy sheep and the drone due to the rapid movement of the dairy sheep or the shaking of the drone, usually, after the drone shooting is completed, each dairy sheep will produce at most one afterimage, and the afterimage is basically consistent with the movement and posture of the dairy sheep body that produces the afterimage, so the shape and direction of the edge between the afterimage and the body are basically the same. However, due to the different behaviors and postures of different dairy sheep, the shape or direction of the edges of different dairy sheep are quite different, and then the shape or direction of the edge between the afterimage and other dairy sheep except the body is also quite different. Therefore, the embodiment of the present invention first First, any overlapping dairy sheep area is analyzed, and any two edge lines in the overlapping dairy sheep area are taken as the edge line group in the overlapping dairy sheep area. Each edge line group is analyzed, and the shape similarity of the edge line group is obtained according to the difference in pixel position distribution between the two edge lines in the edge line group. The greater the shape similarity, the more similar the shapes or directions of the two edge lines in the edge line group are. For the two edge lines in the edge line group, one is the edge of the trailing image, and the other is the edge of the dairy sheep body that produces the trailing image. Subsequently, based on the shape similarity, the number of dairy sheep in the overlapping dairy sheep area can be accurately counted.

[0090] Preferably, in an embodiment of the present invention, a method for obtaining shape similarity of edge line groups specifically includes:

[0091] See also Figure 4 , which shows a flow chart of a method for obtaining shape similarity of edge line groups provided by an embodiment of the present invention.

[0092] Step S301: Take any edge line group in the overlapping dairy sheep area as the target edge line group, obtain the eight-chain code sequences of the two edge lines in the target edge line group respectively, and take the eight-chain code sequence of the shorter one of the two edge lines as the short sequence, and take the eight-chain code sequence of the longer one of the two edge lines as the long sequence.

[0093] First, any edge line group in the overlapping dairy sheep area is taken as the target edge line group. Since the chain code can describe the shape and direction of the boundary, the eight-chain code sequences of the two edge lines in the target edge line group are first obtained respectively, wherein the corresponding eight-chain code sequence can be obtained along the edge line starting from the left end point or the right end point of the edge line. There is no limitation here. The eight-chain code is a technical means well known to those skilled in the art and will not be elaborated here. Considering that the lengths of the two edge lines in the target edge line, that is, the number of pixels on the edge line, are generally different, resulting in different lengths of the two eight-chain code sequences. Therefore, the eight-chain code sequence of the shorter of the two edge lines is taken as the short sequence, and the eight-chain code sequence of the longer of the two edge lines is taken as the long sequence, for the convenience of subsequent analysis.

[0094] Step S302: taking the number of elements contained in the short sequence as a reference number, and taking a sequence consisting of adjacent reference number elements in the long sequence as a subsequence of the long sequence.

[0095] Since the lengths of the short sequence and the long sequence are different, that is, the number of elements they contain is different, in order to accurately calculate and analyze the shape similarity of the two edge lines in the target edge line group, the number of elements contained in the short sequence is first taken as the reference number, and then the sequence composed of the adjacent reference number of elements in the long sequence is taken as a subsequence of the long sequence. At this time, the short sequence has the same length as each subsequence. Subsequently, based on the difference in elements between the short sequence and each subsequence, the similarity in shape or direction of the two edge lines in the target edge line group is accurately analyzed.

[0096] For example of subsequences: Assume that the length of the short sequence is 3, that is, the short sequence contains 3 elements, and the long sequence is , then the subsequences divided from the long sequence are , and .

[0097] Step S303: obtaining the sequence similarity between the short sequence and each subsequence according to the difference between the elements at the same position between the short sequence and each subsequence.

[0098] Since the elements in the eight-chain code sequence can reflect the direction of the position of each pixel point on the edge line, the greater the difference between the elements at the same position of the two eight-chain code sequences, the greater the difference in the shape and direction of the corresponding edge line. Therefore, the difference between the elements at the same position between the short sequence and each subsequence can be analyzed, and the obtained sequence similarity reflects the consistency between the short sequence elements and each subsequence elements, providing a data basis for the subsequent calculation of shape similarity.

[0099] Preferably, in one embodiment of the present invention, the method for obtaining the sequence similarity between the short sequence and each subsequence specifically includes:

[0100] First, any subsequence is taken as the target subsequence, and the element difference value at each position between the short sequence and the target subsequence is obtained according to the difference of the elements at the same position between the short sequence and the target subsequence. The larger the element difference value, the greater the difference of the elements at the same position between the short sequence and the target subsequence.

[0101] In one embodiment of the present invention, the absolute value of the difference between the elements at the same position of the short sequence and the target subsequence may be used as the element difference value at each position between the short sequence and the target subsequence to analyze the difference between the two.

[0102] The smaller the difference value of the element at each position between the short sequence and the target subsequence, the more similar the elements of the short sequence and the target subsequence are. Therefore, a negative exponential function with the natural constant e as the base can be used to map the cumulative value of the difference value of the element at all positions between the short sequence and the target subsequence to obtain the sequence similarity between the short sequence and the target subsequence.

[0103] The expression of sequence similarity between the short sequence and each subsequence can be specifically, for example, as follows:

[0104] ;

[0105] in, Indicates the sequence similarity between the short sequence and the target subsequence; Indicates the first The element at position Indicates the first The element at position Represents the first The difference value of the elements at each position; Represents the reference quantity, that is, the number of elements contained in the target subsequence; The natural index An exponential function with base .

[0106] By using the same method as above, the sequence similarity between the short sequence and each subsequence can be obtained.

[0107] Step S304: taking the maximum value of the sequence similarities between the short sequence and all subsequences as the shape similarity of the target edge line group.

[0108] The greater the sequence similarity between the short sequence and the subsequence, the more similar the shapes of some parts of the target edge line with the longer edge line are to the shapes of the shorter edge line. Therefore, the maximum value of the sequence similarity between the short sequence and all subsequences can be taken as the shape similarity of the target edge line group. The greater the shape similarity, the more similar the shapes or directions of the two edge lines in the target edge line group are.

[0109] The shape similarity of each edge line group in the overlapping dairy sheep area can be obtained by the same method as above.

[0110] Step S4: Count the number of dairy sheep according to the shape similarity of each edge line group in each overlapping dairy sheep area, the number of edge lines, and the number of non-overlapping dairy sheep areas in the grayscale image.

[0111] Since each edge line in the overlapping dairy sheep area represents a dairy sheep or a smear, and the greater the shape similarity, the more similar the shapes or directions of the two edge lines in the target edge line group are, for the two edge lines in the target edge lines, one is the edge of the smear, and the other is the edge of the dairy sheep body that produces the smear, and each non-overlapping dairy sheep area in the grayscale image represents a single dairy sheep, the shape similarity of each edge line group in each overlapping dairy sheep area, the number of edge lines and the number of non-overlapping dairy sheep areas in the grayscale image can be used to count the number of dairy sheep, thereby eliminating the interference of multiple overlapping dairy sheep and the overlap of dairy sheep and smears on the number counting.

[0112] Preferably, in one embodiment of the present invention, the method for counting the number of dairy sheep specifically includes:

[0113] See also Figure 5 , which shows a flow chart of a method for counting the number of dairy sheep provided by an embodiment of the present invention.

[0114] Step S401: In each overlapping dairy sheep region, an edge line group whose shape similarity is greater than a preset second threshold is used as a trailing edge line group of each overlapping dairy sheep region.

[0115] As the shape similarity is greater, for the two edge lines in the edge line group, one is the edge of the shadow, and the other is the edge of the dairy sheep body that produces the shadow, which further indicates that the edge line group contains the shadow and the edge line of the dairy sheep body that produces the shadow. Therefore, the edge line group in each overlapping dairy sheep area whose shape similarity is greater than the preset second threshold value can be used as the shadow edge line group of the overlapping dairy sheep area, and the number of shadow edge line groups is the number of shadows in the overlapping dairy sheep area.

[0116] The preset second threshold is set to 0.8, and the specific value of the preset second threshold can also be set by the implementer according to the specific implementation scenario, which is not limited here.

[0117] Step S402: Obtain the number of dairy sheep in each overlapping dairy sheep region according to the difference between the number of all edge lines and the number of all smear edge line groups in each overlapping dairy sheep region.

[0118] From the above analysis, it can be seen that the number of trailing edge line groups in the overlapping dairy sheep areas is equal to the number of trailing images appearing in the overlapping dairy sheep areas, and the number of edge lines in the overlapping dairy sheep areas represents the number of dairy sheep and trailing images in the overlapping dairy sheep areas. Therefore, based on the difference between the number of all edge lines and the number of all trailing edge line groups in each overlapping dairy sheep area, the number of dairy sheep in each overlapping dairy sheep area can be obtained, and the number of dairy sheep in each overlapping dairy sheep area can be accurately counted.

[0119] In one embodiment of the present invention, the difference between the number of all edge lines and the number of all trailing edge line groups in each overlapping dairy sheep area can be used as the number of dairy sheep in each overlapping dairy sheep area. Since the embodiment of the present invention assumes that each dairy sheep produces at most one trailing image, the difference between the number of all edge lines and the number of all trailing edge line groups in each overlapping dairy sheep area is greater than 0.

[0120] Step S403: taking the accumulated value of the number of dairy sheep in all overlapping dairy sheep areas as the first number, taking the number of all non-overlapping dairy sheep areas in the grayscale image as the second number, and taking the sum of the first number and the second number as the actual number of dairy sheep in the grayscale image.

[0121] Then, the accumulated value of the number of dairy sheep in all overlapping dairy sheep areas can be taken as the first number, which represents the number of dairy sheep contained in all overlapping dairy sheep areas in the grayscale image. Since each non-overlapping dairy sheep area represents a single dairy sheep, the number of all non-overlapping dairy sheep areas in the grayscale image can be taken as the second number, and then the sum of the first number and the second number can be taken as the actual number of dairy sheep in the grayscale image, thereby accurately counting the number of dairy sheep.

[0122] An embodiment of the present invention provides a visually assisted dairy sheep counting system, the system comprising a memory, a processor and a computer program, wherein the memory is used to store the corresponding computer program, the processor is used to run the corresponding computer program, and when the computer program runs in the processor, the method described in steps S1 to S4 can be implemented.

[0123] In summary, the embodiment of the present invention first obtains a grayscale image of a dairy sheep herd; performs threshold segmentation on the grayscale image to obtain multiple dairy sheep areas in the grayscale image; for each dairy sheep area, scales each template according to the size difference between the dairy sheep area and each template to obtain an adjustment template; matches the dairy sheep area with each adjustment template, and screens out overlapping dairy sheep areas from all dairy sheep areas; performs edge detection on the overlapping dairy sheep areas to obtain multiple edge lines in the overlapping dairy sheep areas; for each overlapping dairy sheep area, an edge line group is formed by any two edge lines, and the shape similarity of the edge line group is obtained according to the difference in pixel point position distribution between the edge lines in the edge line group; the number of dairy sheep is counted according to the shape similarity of each edge line group in each overlapping dairy sheep area, the number of edge lines, and the number of non-overlapping dairy sheep areas in the grayscale image.

[0124] It should be noted that the sequence of the above embodiments of the present invention is only for description and does not represent the advantages and disadvantages of the embodiments. The processes depicted in the accompanying drawings do not necessarily require the specific order or continuous order shown to achieve the desired results. In some embodiments, multitasking and parallel processing are also possible or may be advantageous.

[0125] The various embodiments in this specification are described in a progressive manner, and the same or similar parts between the various embodiments can be referenced to each other, and each embodiment focuses on the differences from other embodiments.

Claims

1. A method for counting the number of dairy sheep with visual assistance, characterized in that: The method comprises: Obtain a grayscale image of a dairy sheep herd; perform threshold segmentation on the grayscale image to obtain multiple dairy sheep regions in the grayscale image; For each dairy sheep region, scaling each template according to the size difference between the dairy sheep region and each template to obtain an adjustment template; matching the dairy sheep region with each adjustment template, and screening out overlapping dairy sheep regions from all the dairy sheep regions; Perform edge detection on the overlapping dairy sheep regions to obtain multiple edge lines in the overlapping dairy sheep regions; for each overlapping dairy sheep region, any two edge lines constitute an edge line group, and according to the difference in pixel point position distribution between edge lines in the edge line group, obtain the shape similarity of the edge line group; Counting the number of dairy sheep according to the shape similarity of each edge line group in each overlapping dairy sheep area, the number of edge lines, and the number of non-overlapping dairy sheep areas in the grayscale image; The step of scaling each template according to the size difference between the dairy sheep region and each template to obtain the adjusted template comprises: Select any one of the horizontal and vertical directions as the direction to be measured; The maximum value of the number of pixels in the dairy sheep area in the direction to be tested is used as the numerator, the maximum value of the number of pixels in each template in the direction to be tested is used as the denominator, and the ratio is used as the scaling ratio of each template; Based on the scaling ratio, scaling each template to obtain an adjusted template; Scaling each template based on the scaling ratio to obtain an adjusted template includes: Use any template as the target template; If the scaling ratio of the target template is greater than a value of 1, interpolation processing is performed on the target template, and the target template is enlarged by the scaling ratio to obtain an adjusted template; If the scaling ratio of the target template is less than a value of 1, interpolation processing is performed on the target template, and the target template is reduced by the scaling ratio to obtain an adjusted template; If the scaling ratio of the target template is equal to the value 1, the target template is not scaled; The obtaining of the shape similarity of the edge line group according to the difference in the position distribution of the pixel points between the edge lines in the edge line group comprises: Taking any edge line group in the overlapping dairy sheep area as the target edge line group, respectively obtaining the eight-chain code sequence of two edge lines in the target edge line group, and taking the eight-chain code sequence of the shorter edge line of the two edge lines as the short sequence, and taking the eight-chain code sequence of the longer edge line of the two edge lines as the long sequence; The number of elements contained in the short sequence is taken as a reference number, and a sequence consisting of adjacent reference number elements in the long sequence is taken as a subsequence of the long sequence; According to the difference of the elements at the same position between the short sequence and each subsequence, the sequence similarity between the short sequence and each subsequence is obtained; The maximum value of the sequence similarities between the short sequence and all subsequences is used as the shape similarity of the target edge line group; The obtaining the sequence similarity between the short sequence and each subsequence according to the difference of the elements at the same position between the short sequence and each subsequence comprises: Take any subsequence as the target subsequence, and obtain the element difference value at each position between the short sequence and the target subsequence according to the difference of the elements at the same position between the short sequence and the target subsequence; Using a negative exponential function with the natural constant e as the base, the accumulated values ​​of the element differences at all positions between the short sequence and the target subsequence are mapped to obtain the sequence similarity between the short sequence and the target subsequence; The counting of the number of dairy sheep according to the shape similarity of each edge line group in each overlapping dairy sheep area, the number of edge lines, and the number of non-overlapping dairy sheep areas in the grayscale image comprises: In each overlapping dairy sheep region, the edge line group whose shape similarity is greater than a preset second threshold is used as the smear edge line group of the overlapping dairy sheep region; According to the difference between the number of all edge lines in each overlapping dairy sheep area and the number of all the smear edge line groups, the number of dairy sheep in each overlapping dairy sheep area is obtained; The accumulated value of the number of dairy sheep in all overlapping dairy sheep areas is taken as the first number, the number of all non-overlapping dairy sheep areas in the grayscale image is taken as the second number, and the sum of the first number and the second number is taken as the actual number of dairy sheep in the grayscale image.

2. A visually assisted dairy sheep counting method according to claim 1, characterized in that: The matching of the dairy sheep region with each adjustment template and screening out overlapping dairy sheep regions from all the dairy sheep regions comprises: Performing template matching between the dairy sheep region and each adjustment template to obtain a matching degree between the dairy sheep region and each adjustment template; The maximum value of the matching degree between the dairy sheep region and all the adjustment templates is taken as the non-overlapping possibility of the dairy sheep region; The dairy sheep areas whose non-overlapping probability is less than a preset first threshold are regarded as overlapping dairy sheep areas.

3. The visually assisted dairy sheep counting method according to claim 1, characterized in that: The grayscale image is subjected to threshold segmentation to obtain a plurality of dairy sheep regions in the grayscale image, comprising: Performing threshold segmentation on the grayscale image to obtain an initial binary image, and performing an opening operation on the initial binary image to obtain an adjusted binary image; The connected region composed of pixels with grayscale values ​​of 1 in the adjusted binary image corresponds to the region in the grayscale image as a dairy sheep region in the grayscale image.

4. A visually assisted dairy sheep counting system, the system comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, characterized in that: When the processor executes the computer program, the steps of the method according to any one of claims 1 to 3 are implemented.

Citation Information

Patent Citations

  • Image-recognition-technology-based cow milk yield auto-metering system and image recognition method therefor

    CN103177261A

  • Sheep body size detection method based on sheep top view

    CN107481243A

  • Printed matter ghosting detection method and system based on computer vision

    CN113888536A

  • Group-fed pig identity recognition method based on feature fusion

    CN114519864A

  • PCB production process online detection method based on edge detection

    CN115272346A