Animal Husbandry Water Pollution Detection Method Based on Machine Vision
Through machine vision-based methods, the possibility of algae and the threshold segmentation algorithm is adjusted, the problem of misidentification of algae stacks as water areas is solved, and the accuracy of water pollution detection in animal husbandry is improved.
Patent Information
- Application Number
- CN202411036593.7
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2024-07-31
- Publication Date
- 2024-11-26
- Estimated Expiration
- 2044-07-31
AI Technical Summary
In the existing method, in the detection of water pollution in animal husbandry, the algae stack is close to the color of the water body, resulting in the algae stack being misidentified as a water area, which reduces the image segmentation effect and thus makes the detection inaccurate.
Using a machine vision-based method, by obtaining the grayscale image of the water area, calculating the algae possibilities of each pixel point, combining the dividing line distance and grayscale value difference, the threshold segmentation algorithm is adjusted to improve segmentation accuracy and ensure accurate identification of algae and water areas.
It improves the recognition accuracy and segmentation accuracy of algae pixel points, and enhances the accuracy of water pollution detection in animal husbandry.
Smart Images

Figure CN118570242B_ABST
Abstract
Description
Technical Field
[0001] The invention relates to the technical field of threshold segmentation, and in particular to a method for detecting animal husbandry water pollution based on machine vision. Background Art
[0002] The feces and urine produced during livestock and poultry breeding contain a large amount of organic matter. If they are discharged directly into the water without treatment, the eutrophication of the water will lead to the proliferation of algae and the formation of water blooms. These organisms will consume a large amount of dissolved oxygen in the water, causing the death of fish and other economic livestock products in the water, destroying the ecological balance and causing economic losses. Water pollution detection is an important part of the prevention and control of water eutrophication. It can detect whether the water is in a polluted state in a timely and effective manner so that people can take corresponding measures in time.
[0003] The existing method uses the threshold segmentation algorithm to divide the algae and water in the livestock water images. Because the algae in the water body will have a more serious stacking phenomenon, the color of the algae stacking area is closer to the water area, resulting in the algae stacking area being mistakenly identified as water area, reducing the image segmentation effect, and thus making the livestock water pollution detection inaccurate. Summary of the invention
[0004] In order to solve the technical problem that the algae stacking area is mistakenly identified as water body due to the color of the algae stacking area being close to the water body, resulting in inaccurate detection of livestock water pollution, the purpose of the present invention is to provide a livestock water pollution detection method based on machine vision, and the technical scheme adopted is as follows:
[0005] The present invention proposes a method for detecting water pollution in animal husbandry based on machine vision, which comprises:
[0006] Obtaining a water area grayscale image obtained by graying the water area RGB image of the livestock water body, and an analysis value of each pixel in the water area grayscale image;
[0007] According to the gray value of each pixel in the gray image of the water area and the analysis value, a first possible degree of algae at each pixel in the gray image of the water area is obtained;
[0008] Obtaining a boundary line in the grayscale image of the water area; obtaining the algae stacking degree of each pixel in the grayscale image of the water area according to the distance between each pixel in the grayscale image of the water area and the boundary line, and the grayscale value difference between the pixel and the pixel on the boundary line and the analysis value difference;
[0009] Combining the difference between the grayscale distribution in the preset original window and the preset extended window of each pixel in the water area grayscale image, and the grayscale distribution of the pixel in the preset extended window and the algae stacking degree, the second possible degree of algae for each pixel in the water area grayscale image is obtained;
[0010] According to the first possibility degree of the algae and the second possibility degree of the algae, the segmentation threshold of the water area grayscale image obtained by using the threshold segmentation algorithm is adjusted to obtain a correction threshold of each pixel in the water area grayscale image;
[0011] Pollution detection is performed on livestock water bodies based on the modified threshold.
[0012] Furthermore, the method for obtaining the first possible degree of algae at each pixel in the water area grayscale image includes:
[0013] Calculate the mean gray value of all pixels in the water area gray image and record it as the overall gray value;
[0014] The calculation formula for the first possible degree of algae at each pixel in the water grayscale image is as follows:
[0015] ; In the formula, is the first possible degree of the algae at the ith pixel in the grayscale image of the water area; is the gray value of the i-th pixel in the water area gray image; is the overall gray value; is the analysis value of the i-th pixel in the water area grayscale image; is the mean of the analysis values of all pixels in the water area grayscale image; exp is an exponential function with the natural constant e as the base; Norm is a normalization function; is a fixed value.
[0016] Furthermore, the method for obtaining the algae stacking degree of each pixel in the grayscale image of the water area based on the distance between each pixel in the grayscale image of the water area and the boundary line, and the grayscale value difference between the pixel and the pixel on the boundary line and the analysis value difference includes:
[0017] In the grayscale image of the water area, any pixel point is selected as a test point, and a straight line with a first preset direction through the test point is drawn as a first analysis line; a straight line with a second preset direction through the test point is drawn as a second analysis line; the first analysis line and the second analysis line are perpendicular to each other;
[0018] The first analysis line and the second analysis line are recorded as lines to be tested; for each line to be tested, when there are at least two intersections between the line to be tested and the demarcation line, the Euclidean distance between each intersection and the point to be tested is calculated, and the intersection corresponding to the smallest Euclidean distance is recorded as the target point under the line to be tested; when there is one intersection between the line to be tested and the demarcation line, the intersection is recorded as the target point under the line to be tested; when there is no intersection between the line to be tested and the demarcation line, the intersection between the line to be tested and the preset boundary of the grayscale image of the water area is recorded as the target point under the line to be tested;
[0019] Record the target point under the first analysis line of the point to be measured as the target edge point of the point to be measured; record the Euclidean distance between the point to be measured and the target edge point as the first distance of the point to be measured, and record the Euclidean distance between the point to be measured and the target point under the second analysis line as the second distance of the point to be measured;
[0020] The algae stacking degree of the test point is obtained by combining the first distance of the test point, the second distance, and the gray value difference and analysis value difference between the test point and the target edge point.
[0021] Furthermore, the calculation formula of the algae stacking degree at the test point is as follows:
[0022] ; In the formula, is the algae stacking degree of the test point; d is the test point; D1 is the first distance of the test point; D2 is the second distance of the test point; is the gray value of the point to be tested; is the gray value of the target edge point of the point to be measured; m is the target edge point of the point to be measured; is the mean of the analysis values of the pixel points in the preset original window of the point to be tested; is the mean value of the grayscale values of the pixels in the preset original window of the point to be tested; is the analysis value of the point to be tested; The analysis value of the target edge point of the point to be measured; is the first preset positive number; exp is an exponential function with the natural constant e as the base; Norm is a normalization function.
[0023] Furthermore, the method of combining the difference in grayscale distribution between a preset original window and a preset extended window of each pixel in the water area grayscale image, and the grayscale distribution of the pixel in the preset extended window and the algae stacking degree to obtain the second possible degree of algae for each pixel in the water area grayscale image includes:
[0024] For each pixel in the water grayscale image, a preset extended window of the pixel is divided into different sub-windows, and the preset original window of the pixel is one of the sub-windows; the size of the preset extended window is larger than the size of the preset original window;
[0025] For each of the sub-windows, the average of the grayscale values of the pixels in the sub-window is taken as the comprehensive grayscale value of the sub-window;
[0026] Calculate the sum of squares of the differences between the comprehensive grayscale values of the preset original window of each pixel in the water area grayscale image and the remaining sub-windows in the preset extended window except the preset original window, as the extended grayscale difference of each pixel in the water area grayscale image; obtain the sum of squares of the differences between the grayscale values of each pixel in the water area grayscale image and the remaining pixel points in the preset original window except the pixel point, as the original grayscale difference of each pixel in the water area grayscale image;
[0027] Counting the number of pixels whose grayscale values in a preset extended window of each pixel in the water area grayscale image are greater than the overall grayscale value, and recording it as the analysis number of each pixel in the water area grayscale image;
[0028] The second possible degree of algae at the pixel point in the grayscale image of the water area is obtained by combining the extended grayscale difference of each pixel point, the original grayscale difference, the analysis quantity, the grayscale distribution of the pixel point in the preset extended window of the pixel point, and the algae stacking degree.
[0029] Furthermore, the calculation formula of the second possibility degree of the algae at the pixel point in the water area grayscale image is as follows:
[0030] ; In the formula, is the second possible degree of the algae at the ith pixel in the grayscale image of the water area; is the extended grayscale difference of the i-th pixel in the water area grayscale image; is the original grayscale difference of the i-th pixel in the water area grayscale image; is the number of analyzed pixels of the ith pixel in the water grayscale image; N is the total number of pixels in the preset extended window of each pixel in the water grayscale image; The algae stacking degree of the nth pixel point in the preset extended window of the i-th pixel point in the water area grayscale image; is the gray value of the nth pixel in the preset extended window of the ith pixel in the water area gray image; is the overall gray value; A positive number for the second preset.
[0031] Furthermore, the method of adjusting the segmentation threshold of the water area grayscale image obtained by using the threshold segmentation algorithm according to the first possibility degree of the algae and the second possibility degree of the algae to obtain a correction threshold of each pixel in the water area grayscale image includes:
[0032] Normalizing the result of weighted summation of the first possibility degree of algae and the second possibility degree of algae to obtain the final possibility degree of algae at each pixel in the grayscale image of the water area;
[0033] The maximum inter-class variance method is used to obtain the segmentation threshold for the grayscale values of pixels in the water grayscale image;
[0034] For each pixel in the grayscale image of the water area, the final possible degree of the algae at the pixel is negatively correlated to obtain a correction coefficient of the pixel; and the correction threshold of each pixel in the grayscale image of the water area is obtained based on the correction coefficient and the segmentation threshold.
[0035] Furthermore, the method for detecting pollution of livestock water bodies based on the modified threshold value comprises:
[0036] For each pixel in the grayscale image of the water area, when the grayscale value of the pixel is greater than the correction threshold, the pixel is recorded as an algae pixel; the number of algae pixels in the grayscale image of the water area is used as the numerator, and the total number of pixels in the grayscale image of the water area is used as the denominator to obtain the ratio as the pollution degree;
[0037] When the pollution degree is less than a preset pollution threshold, the livestock water body is not polluted; when the pollution degree is greater than or equal to the preset pollution threshold, the livestock water body is polluted.
[0038] Furthermore, the method for obtaining the analysis value of each pixel in the water area grayscale image is:
[0039] The channel value of each pixel point in the water area RGB image under the preset first color channel is used as the analysis value of the pixel point at the same position of the corresponding pixel point in the water area grayscale image.
[0040] Furthermore, the method for obtaining the boundary line in the water area grayscale image includes:
[0041] Convert the water area RGB image into an HSV image; if the channel value of each pixel point in the HSV image under all second color channels is within the preset color range of the corresponding second color channel, then use the corresponding pixel point as a demarcation point;
[0042] The connected domain formed by the pixel points at the same position of the water area grayscale image corresponding to the water area RGB image at the dividing point is used as the dividing line.
[0043] The present invention has the following beneficial effects:
[0044] In the embodiment of the present invention, since the color of the algae area is closer to that of the normal water area, the grayscale of the algae area in the water area grayscale image is closer to that of the normal water area, and the accuracy of grayscale value analysis alone is low. However, the algae in this scheme has a specific color, and subsequent analysis is performed in combination with the analysis value; the grayscale of the algae area in the water area grayscale image is slightly higher than that of the normal water area, and the analysis value of the pixel point in the algae area is significantly greater than the analysis value of the pixel point in the normal water area, and the two are analyzed in combination to improve the accuracy of algae pixel recognition; there is a clear dividing line between the algae and the water area, and the color characteristics of the pixel points in the algae area and the normal water area are different depending on the distance from the dividing line. Combined with the distance between the pixel point and the dividing line, the grayscale value difference between the pixel point and the pixel point on the dividing line and the analysis value difference, the algae stacking degree can be obtained. It reflects the degree of algae stacking more accurately; the algae stacking positions in the algae area are discretely distributed in point form, and there are differences in the grayscale distribution of pixels in local areas of different sizes from the pixels, while normal waters do not have the above characteristics. The second possibility degree of algae is obtained by combining the difference in grayscale distribution between the preset original window and the preset extended window of each pixel in the water grayscale image, and combining the grayscale distribution of the pixels in the preset extended window with the algae stacking degree; the first possibility degree of algae and the second possibility degree of algae analyze the possibility that the pixel is an algae pixel from the aspects of color features and the degree of grayscale chaos in the local area of the pixel, so that the adjusted correction threshold of the pixel is more suitable for its own grayscale situation, thereby improving the segmentation accuracy of the algae area and normal waters, thereby increasing the accuracy of livestock water pollution detection. BRIEF DESCRIPTION OF THE DRAWINGS
[0045] 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.
[0046] Figure 1 A method flow chart of a method for detecting water pollution in animal husbandry based on machine vision provided by one embodiment of the present invention;
[0047] Figure 2 A partial schematic diagram of a grayscale image of a water area of a livestock water body provided by an embodiment of the present invention;
[0048] Figure 3 A schematic diagram of the distribution of test lines of test points provided by an embodiment of the present invention;
[0049] Figure 4 A schematic diagram of a preset extended window division of pixels provided by an embodiment of the present invention. DETAILED DESCRIPTION
[0050] 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 the specific implementation method, structure, features and effects of a livestock water pollution detection method based on machine vision proposed by the present invention in combination 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.
[0051] 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.
[0052] The specific scenario targeted by the present invention is as follows: when performing pollution detection on water images, due to the accumulation of green algae in the water, the grayscale value of the green algae in the image and the grayscale value of the water area are relatively close, and the image cannot be correctly segmented, which in turn affects the judgment of the degree of water pollution.
[0053] The following is a detailed description of a specific method for detecting livestock water pollution based on machine vision provided by the present invention in conjunction with the accompanying drawings.
[0054] See also Figure 1 , which shows a method flow chart of a method for detecting livestock water pollution based on machine vision provided by an embodiment of the present invention, the method comprising:
[0055] Step S1: obtaining a water area grayscale image obtained by graying the water area RGB image of the livestock water body, and an analysis value of each pixel point in the water area grayscale image.
[0056] Specifically, a camera is mounted on a drone, and images of livestock water bodies are photographed by the camera on the drone from above, which are recorded as water body RGB images. The water body RGB images are grayscaled to obtain water body grayscale images. Figure 2 A partial schematic diagram of a grayscale image of a water body of animal husbandry provided by an embodiment of the present invention is shown in FIG. Figure 2 As shown, Figure 2 The left half area is the algae area, and the right half area is normal water area.
[0057] Normal waters generally appear blue or cyan, because light radiates through the water surface to the bottom of the water, and the light reflected by the water surface is less than the reflection value of the algae area, so the brightness value of the algae area in the water RGB image is higher than the brightness value of the normal water. However, due to the darker color of the algae stacking area, the grayscale value of the pixel points in the algae stacking position and the normal water area is relatively close, and the normal water area cannot be divided from the algae area through the regional segmentation algorithm. In order to increase the grayscale difference between the algae area and the normal water area, because the algae in this solution has a specific color, the channel value of each pixel point in the water RGB image under the preset first color channel is used as the analysis value of the corresponding pixel point at the same position in the water grayscale image. It should be noted that the pixels in the water RGB image correspond one to one to the pixels in the water grayscale image.
[0058] It should be noted that the first color channel includes: R color channel, G color channel, and B color channel; since this scheme is aimed at analyzing green algae in waters, the first color channel is preset as the G color channel in this embodiment, and the implementer can set it according to the specific situation. In the embodiment of the present invention, a weighted average grayscale algorithm is used for grayscale processing; wherein, the weighted average grayscale algorithm is a technical means well known to those skilled in the art. In other embodiments of the present invention, other image acquisition devices and image preprocessing algorithms may also be selected. Both image acquisition and image preprocessing algorithms are technical means well known to those skilled in the art and are not limited here.
[0059] Step S2: Obtain the first possible degree of algae at each pixel in the grayscale image of the water area according to the grayscale value and analysis value of each pixel in the grayscale image of the water area.
[0060] Specifically, since the channel value difference between the algae area and the normal water area in the RGB image of the water area is only obvious in the G color channel, the grayscale of the algae area in the grayscale image of the water area is slightly higher than that of the normal water area; because this scheme is aimed at green algae analysis, the analysis value of the pixel in the algae area is significantly greater than the analysis value of the pixel in the normal water area. Therefore, on the basis of grayscale value analysis, this scheme also considers the analysis value to improve the accuracy of algae pixel recognition.
[0061] According to the gray value and analysis value of each pixel in the water gray image, the first possible degree of algae at each pixel in the water gray image is obtained; the calculation formula of the first possible degree of algae at each pixel in the water gray image is as follows:
[0062]
[0063] In the formula, is the first possible degree of algae at the ith pixel in the grayscale image of the water area; is the gray value of the i-th pixel in the water area gray image; is the overall grayscale value, that is, the mean grayscale value of all pixels in the water area grayscale image; is the analysis value of the i-th pixel in the water area grayscale image; is the mean of the analysis values of all pixels in the grayscale image of the water area; exp is an exponential function with the natural constant e as the base; Norm is the normalization function.
[0064] It should be noted that the embodiment of the present invention combines the grayscale value of a single pixel point compared to the overall grayscale value of the water area grayscale image, and the analysis value of a single pixel point compared to the overall analysis value of the water area grayscale image to analyze the possibility that the pixel point is located in the algae area. It is known that the grayscale value and analysis value of the pixel point at the unstacked position in the algae area are higher, and the grayscale value of the pixel point at the stacked position in the algae area and in the normal water area is lower; is a fixed value, when and The larger the value, the greater the possibility that the i-th pixel point is in the unstacked position in the algae area, and the first possibility of algae is On the contrary, the greater the probability that the i-th pixel point is in the stacking position or normal water area in the algae area, the greater the first probability of algae. The smaller. May be less than 0, to avoid Influence The correlation between The value range of is adjusted to be greater than 0.
[0065] Step S3: Obtain the boundary line in the grayscale image of the water area; obtain the algae stacking degree of each pixel in the grayscale image of the water area based on the distance between each pixel in the grayscale image of the water area and the boundary line, as well as the grayscale value difference and analysis value difference between the pixel and the pixel on the boundary line.
[0066] Specifically, considering that there is a clear dividing line between algae and water area, and the farther the algae is from the dividing line, the more likely it is that the grayscale of the stacking position will be lower due to the algae stacking phenomenon, and then it will be closer to the grayscale of normal water area, causing errors in threshold segmentation. In order to reduce the impact of the algae stacking phenomenon on threshold segmentation, normal water area and algae area are roughly distinguished. The embodiment of the present invention performs threshold segmentation in the HSV color space to obtain the dividing line, specifically: converting the water area RGB image into an HSV image; if the channel value of each pixel point in the HSV image under all second color channels is within the preset color range of the corresponding second color channel, the corresponding pixel point is used as the dividing point; the connected domain formed by the pixel points corresponding to the dividing point in the water area grayscale image corresponding to the water area RGB image is used as the dividing line. Among them, the method of converting an RGB image to an HSV image is a well-known technology for those skilled in the art and will not be repeated here.
[0067] It should be noted that the second color channel includes: H color channel, S color channel, V color channel; the preset color range of the H color channel is , the preset color range of the S color channel is , the preset color range of the V color channel is The implementer can set the preset color range of each second color channel according to the specific situation. The pixels in the water area RGB image, the HSV image corresponding to the water area RGB image, and the water area grayscale image correspond one to one.
[0068] In the water area RGB image, the closer the algae are to the normal water area, i.e. the dividing line, the brighter the color, and the farther the algae are, the darker the color. The algae farther away from the dividing line have a greater degree of stacking. Therefore, combining the distance between each pixel point and the dividing line in the water area grayscale image, as well as the grayscale value difference and analysis value difference between the pixel point and the pixel point on the dividing line, the algae stacking degree can more accurately reflect the degree of algae stacking.
[0069] Preferably, the specific method for obtaining the stacking degree of algae is as follows: randomly select a pixel point in the grayscale image of the water area and record it as the test point; draw a straight line through the test point with a first preset direction as the first analysis line; draw a straight line through the test point with a second preset direction as the second analysis line; the first analysis line and the second analysis line are perpendicular to each other; the first analysis line and the second analysis line are recorded as the test line; for each test line, when there are at least two intersections between the test line and the boundary line, calculate the Euclidean distance between each intersection and the test point, and record the intersection corresponding to the smallest Euclidean distance as the target point under the test line; when the test line and the boundary line have at least two intersections ... Euclidean distance between each intersection and the test point, and record the Euclidean distance between each intersection and the test point as the target point under the test line; when the test line and the boundary line have at least two intersections, calculate the Euclidean distance between each intersection and the test point, and record the Euclidean distance between each intersection and the test point, and record the Euclidean distance between each intersection and the target point under the test line. When there is an intersection point between the line to be tested, the intersection point is recorded as the target point under the line to be tested; when there is no intersection point between the line to be tested and the dividing line, the intersection point between the line to be tested and the preset boundary of the grayscale image of the water area is recorded as the target point under the line to be tested; the target point under the first analysis line of the point to be tested is recorded as the target edge point of the point to be tested; the Euclidean distance between the point to be tested and the target edge point is recorded as the first distance of the point to be tested, and the Euclidean distance between the point to be tested and the target point under the second analysis line is recorded as the second distance of the point to be tested; the first distance and the second distance of the point to be tested, as well as the grayscale value difference and the analysis value difference between the point to be tested and the target edge point are combined to obtain the algae stacking degree of the point to be tested.
[0070] It should be noted that the first preset direction is perpendicular to the water flow direction of the livestock water body, and the second preset direction is parallel to the water flow direction of the livestock water body. In the embodiment of the present invention, the water flow direction is vertically upward, then the first preset direction is the horizontal direction, and the second preset direction is the vertical direction. Figure 3 A schematic diagram of the distribution of the test lines of the test points provided by an embodiment of the present invention is shown in FIG. Figure 3 As shown, Figure 3The middle curve L is the dividing line, point d is the point to be measured, straight line L1 and straight line L2 are the lines to be measured of the point to be measured d, wherein straight line L1 is the first analysis line, straight line L2 is the second analysis line, and point m is the target edge point of the point to be measured d.
[0071] It is known that algae are usually distributed downstream in the water flow and aggregated in relatively stable or slow water flow areas. In this embodiment, the water flow direction is vertically upward. Figure 3 There is algae in the lower left corner. The presence of algae will reduce the water flow around the algae, making it easier for the algae to grow. As the algae grows, the width of the algae area along the direction of the water flow gradually increases. Therefore, there is only one intersection between the first analysis line L1 and the dividing line L; the gradual increase in the width of the algae area along the direction of the water flow is an overall trend, and it is not the case that the slope of each point on the dividing line L is equal. Therefore, the second analysis line L2 and the dividing line L may have one intersection, at least two intersections, or no intersection. Among them, the situation where the second analysis line L2 has no intersection with the dividing line L is caused by the small camera shooting area. In the case where the second analysis line L2 has no intersection with the dividing line L, because the water flow direction in the embodiment of the present invention is vertically upward, the width of the algae area along the direction of the water flow gradually increases, so the lower boundary of the image is closer to the edge of the algae area; therefore, in the embodiment of the present invention, the lower boundary of the grayscale image of the water area is selected as the preset boundary, such as Figure 3 As shown, Figure 3 The square area in the middle is the grayscale image of the water area, the four sides of the square area are the four boundaries of the grayscale image of the water area, and the straight line XL is the lower boundary of the grayscale image of the water area; if the water flow direction in other embodiments of the present invention is vertically downward, the preset boundary is the upper boundary of the grayscale image of the water area. The boundary of the image is the line segment formed by the outermost pixel points in the image.
[0072] The calculation formula of the algae stacking degree at the test point is as follows:
[0073]
[0074] In the formula, is the algae stacking degree of the test point; d is the test point; D1 is the first distance of the test point; D2 is the second distance of the test point; is the gray value of the point to be tested; is the gray value of the target edge point of the point to be measured; m is the target edge point of the point to be measured; The mean value of the analysis values of the pixels in the preset original window of the point to be tested; is the mean value of the grayscale values of the pixels in the preset original window of the point to be tested; is the analytical value of the point to be tested; is the analysis value of the target edge point of the point to be measured; is the first preset positive number, with an empirical value of 0.1, which is used to prevent the denominator from being zero, resulting in meaningless fractions; exp is an exponential function with the natural constant e as the base; Norm is a normalization function.
[0075] It should be noted that the point to be measured is located at the center of its preset original window, and the size of the preset original window is , implementers can set it according to specific circumstances.
[0076] In the RGB image of the water area, the closer the algae are to the normal water area, i.e. the boundary line, the brighter the color is, and the farther the algae are, the darker the color is. The channel value of the pixel points between the test point and its target edge point in the horizontal direction under the G color channel shows a downward trend, while the channel value of the pixel points between the pixel points in the normal water area and its target edge point in the horizontal direction under the G color channel is relatively close. When the value is larger, it means that the test point is in the algae area and is far away from the dividing line. The farther away from the dividing line, the greater the distribution density of algae. The algae stacking degree at the test point is greater. The bigger; when The smaller the value, the more likely the test point is in normal waters, and the smaller the algae stacking degree at the test point is. The smaller. The unit grayscale change of the distance from the test point to the target edge point is presented. When the value is larger, the unit grayscale change of the distance from the test point to the target edge point is greater, and the possibility that the test point is located at the algae stacking position is greater. The bigger. Reflects the ratio of the channel value and grayscale value of the neighboring pixel points of the test point, that is, the pixel points in the preset original window under the G color channel; When the value is larger, the channel value of the pixel point in the preset original window of the test point under the G color channel is more important, which means that the color of the test point is closer to green in the RGB image of the water area, and the possibility that the test point is located in the algae area is greater. Because algae stacking will occur in the algae area, but not in the normal water area, the possibility of algae stacking at the location of the test point is greater, and the algae stacking degree is higher. The bigger.
[0077] The method for obtaining the stacking degree of algae at each pixel point in the grayscale image of the water area is the same as the method for obtaining the stacking degree of algae at the point to be measured; the stacking degree of algae at each pixel point in the grayscale image of the water area is obtained.
[0078] Step S4: Combining the difference in grayscale distribution between the preset original window and the preset extended window of each pixel in the water grayscale image, as well as the grayscale distribution of the pixel in the preset extended window and the algae stacking degree, the second possible degree of algae for each pixel in the water grayscale image is obtained.
[0079] Specifically, the stacking positions of algae in the algae area are distributed discretely in the form of dots. Then, in the algae area, the grayscale distribution of the surrounding pixels that are far away from the pixel is relatively chaotic, while the grayscale of the surrounding pixels in the normal water area is relatively uniform. In order to analyze the grayscale distribution of the surrounding pixels of the pixel, that is, in the preset extended window, the present embodiment divides the preset extended window of each pixel in the grayscale image of the water area into different sub-windows, and the preset original window of the pixel is one of the sub-windows. The size of all sub-windows in the preset extended window of the pixel is the same. In the embodiment of the present invention, since the size of the preset original window is , then the preset expansion window size is the empirical value , implementers can set it according to specific circumstances. Figure 4 A schematic diagram of dividing a preset extended window of pixels provided by an embodiment of the present invention is shown in FIG. Figure 4 As shown, Figure 4 The medium grayscale square is the preset original window of pixel b, the large square composed of 9 small squares is the preset extended window of pixel b, and the 9 small squares are all sub-windows in the preset extended window of pixel b. It should be noted that the pixel is located at the center of its preset extended window.
[0080] The difference in grayscale distribution between the preset original window and the preset extended window of the pixel point, as well as the grayscale distribution characteristics of the pixel point in the preset extended window are analyzed from the local area of the pixel point, combined with the algae stacking degree, so that the second possibility degree of algae can more accurately reflect the possibility that the pixel point is an algae pixel point.
[0081] Preferably, the specific method for obtaining the second possibility degree of algae is: for each sub-window, the mean of the grayscale values of the pixels in the sub-window is taken as the comprehensive grayscale value of the sub-window; the sum of the squares of the differences between the comprehensive grayscale values of the preset original window and the remaining sub-windows in the preset extended window except the preset original window of each pixel in the water area grayscale image is calculated as the extended grayscale difference of each pixel in the water area grayscale image; the sum of the squares of the differences between the grayscale values of each pixel in the water area grayscale image and the remaining pixels in the preset original window except the pixel is obtained as the original grayscale difference of each pixel in the water area grayscale image; the number of pixels in the preset extended window of each pixel in the water area grayscale image whose grayscale value is greater than the overall grayscale value is counted, and recorded as the analysis number of each pixel in the water area grayscale image; the second possibility degree of algae of the pixel in the water area grayscale image is obtained by combining the extended grayscale difference, the original grayscale difference, the analysis number of each pixel, the grayscale distribution of the pixel in the preset extended window of the pixel, and the algae stacking degree.
[0082] The calculation formula for the second possible degree of algae at each pixel in the water grayscale image is as follows:
[0083]
[0084]
[0085]
[0086] In the formula, is the second possible degree of algae at the ith pixel in the grayscale image of the water area; is the extended grayscale difference of the i-th pixel in the water area grayscale image; is the mean value of the grayscale value of the pixel points in the preset original window of the i-th pixel point in the water area grayscale image; The mean value of the grayscale values of the pixels in the kth sub-windows except the preset original window of the pixel in the preset extended window of the i-th pixel in the water grayscale image; K is the total number of sub-windows in the preset extended window of each pixel in the water grayscale image; is the original grayscale difference of the i-th pixel in the water area grayscale image; is the gray value of the i-th pixel in the water area gray image; is the gray value of the rth pixel points other than the pixel point in the preset original window of the i-th pixel point in the water gray image; R is the total number of pixels in the preset original window of each pixel point in the water gray image; is the number of analyzed pixels of the ith pixel in the water grayscale image; N is the total number of pixels in the preset extended window of each pixel in the water grayscale image; is the algae stacking degree of the nth pixel in the preset extended window of the i-th pixel in the water grayscale image; is the gray value of the nth pixel in the preset extended window of the ith pixel in the water area gray image; is the overall gray value; The second preset positive number takes the empirical value 1, which is used to prevent the analysis of quantity When it is 0, it will be considered and Meaningless; Norm is the normalization function.
[0087] It should be noted that the positions of algae stacking in the algae area are distributed discretely in the form of points, but because the degree of algae stacking of pixels closer to the normal waters in the algae area is less than that of pixels farther from the normal waters, and because the size of the preset original window is small, the preset original window of the pixels at the algae stacking position is more likely to be included in the algae stacking area, but the original extended window is too large, and the preset extended window of the pixels at the algae stacking position cannot be completely included in the algae stacking area. In summary, the grayscale of the surrounding pixels of the pixels farther from the normal waters in the algae area is more chaotic, but the grayscale of the adjacent pixels is more uniform, and the grayscale of the surrounding pixels of the pixels in the normal waters and the adjacent pixels in the algae area are all relatively uniform. Among them, the surrounding pixels of the pixel in this scheme are the pixels in the preset extended window of the pixel, and the adjacent pixels of the pixel are the pixels in the preset original window of the pixel.
[0088] when When the value is larger, the grayscale distribution in the preset extended window of the i-th pixel is more chaotic, but the grayscale distribution in the preset original window is more uniform. The i-th pixel is more likely to be a pixel farther away from the normal water area in the algae area, and the second possibility of algae is The bigger; when When the value is smaller, the grayscale distribution in the preset extended window and the preset original window of the i-th pixel is more uniform. The greater the possibility that the i-th pixel is a pixel near the normal water area and a pixel in the normal water area, the smaller the possibility that the i-th pixel is an algae pixel. The smaller.
[0089] The grayscale of splashes or waves in normal waters is higher than that in calm waters, so the grayscale distribution of the pixel points at the splash or wave position and the surrounding pixel points is relatively chaotic; the stacked positions of algae in the algae area are discretely distributed in a dotted manner, making the grayscale distribution of the surrounding pixel points in the algae area relatively chaotic, while the grayscale distribution of the surrounding pixel points in normal waters is relatively uniform. Therefore, the grayscale in the preset extended window between the pixel points in the algae area and the pixel points at the splash position is relatively chaotic, and the grayscale value in the window is greater than the overall grayscale value The more pixels there are, the more analysis The grayscale value in the preset extended window of the pixel point at the calm water position is more uniform, and the grayscale value in the window is greater than the overall grayscale value. The fewer the number of pixels, the more analysis is done. The less. This embodiment is Measures the grayscale disorder of pixels within the preset extended window. and When the value is larger, the probability that the i-th pixel is in the algae area or the splash position is greater, and the second probability of algae is The larger the number, the greater the and The smaller the value, the greater the possibility that the i-th pixel is in calm waters. The smaller.
[0090] Under normal circumstances, the algae stacking degree of pixels far away from the water area is higher, the algae stacking degree of pixels close to the water area is lower, and the algae stacking degree of pixels in normal water area is the lowest; The greater the algae stacking degree, the greater the possibility that the pixel in the preset expansion window of the i-th pixel is in the algae area, making the i-th pixel in the algae area more likely, and the algae second possibility degree The bigger.
[0091] Step S5: According to the first possibility degree of algae and the second possibility degree of algae, the segmentation threshold of the water area grayscale image obtained by using the threshold segmentation algorithm is adjusted to obtain a modified threshold value for each pixel in the water area grayscale image.
[0092] The first possibility degree of algae analyzes the possibility that the pixel is an algae pixel from the aspect of color characteristics, and the second possibility degree of algae analyzes the possibility that the pixel is an algae pixel from the degree of grayscale chaos in the local area of the pixel. The segmentation threshold is adjusted based on the first possibility degree of algae and the second possibility degree of algae, so that the corrected threshold of the pixel is more suitable for its own grayscale situation, thereby improving the accuracy of the division between the algae area and the normal water area.
[0093] Preferably, the embodiment of the present invention normalizes the result of weighted summation of the first possibility degree of algae and the second possibility degree of algae to obtain the final possibility degree of algae at each pixel in the grayscale image of the water area. The calculation formula for the final possibility degree of algae at each pixel in the grayscale image of the water area is as follows:
[0094]
[0095] In the formula, is the final possible degree of algae at the ith pixel in the water grayscale image; is the first possible degree of algae at the ith pixel in the grayscale image of the water area; is the second possible degree of algae at the ith pixel in the grayscale image of the water area; is the first preset coefficient; is the second preset coefficient; Norm is the normalization function.
[0096] It should be noted that the first possibility of algae obtained based on the grayscale change in the entire image has a certain degree of randomness, and the second possibility of algae obtained based on the stacking of algae is more accurate than the first possibility of algae. Less than the second preset coefficient ; The first preset coefficient in the embodiment of the present invention Take the empirical value 0.4, the second preset coefficient The empirical value is 0.6, and implementers can set it according to specific circumstances.
[0097] In the embodiment of the present invention, the result of weighted summing the first possibility degree of algae and the second possibility degree of algae using the Norm function is: Normalization is performed. In the embodiment of the present invention, other The normalization method, such as function transformation, maximum and minimum normalization, Sigmoid function and other normalization methods, is not limited here.
[0098] The possibility that the pixel point presenting the final probability of algae is an algae pixel point, the pixel point with a larger final probability of algae should have a smaller threshold value during threshold segmentation to avoid the pixel point with a lower gray value at the algae stacking position being mistakenly identified as a water pixel point; the pixel point with a smaller final probability of algae should have a larger threshold value during threshold segmentation to avoid the pixel point in the water area with a gray value close to the algae stacking position being mistakenly identified as an algae pixel point.
[0099] The grayscale values of the pixels in the water grayscale image are used to obtain the segmentation threshold using the maximum inter-class variance method; for each pixel in the water grayscale image, the final possible degree of the algae of the pixel is negatively correlated to obtain the correction coefficient of the pixel; based on the correction coefficient and the segmentation threshold, the correction threshold of each pixel in the water grayscale image is obtained. Among them, the maximum inter-class variance method is a well-known technology for those skilled in the art and will not be described in detail here.
[0100] A specific example of the calculation formula for the correction threshold of each pixel in the water grayscale image is as follows:
[0101]
[0102] In the formula, is the correction threshold of the i-th pixel in the water grayscale image; is the final possible degree of algae at the ith pixel in the water grayscale image; is the correction coefficient of the i-th pixel in the water grayscale image; To obtain the segmentation threshold of water grayscale image using the maximum inter-class variance method.
[0103] It should be noted that since the grayscale of the algae stacking area is slightly larger than that of the normal water area, in order to avoid the algae stacking area being misidentified as water area, the correction threshold of the pixel points in the algae stacking area should be smaller; When the value is larger, the probability that the i-th pixel is in the green algae area is greater, making The smaller the value, the more correct the threshold value. The smaller.
[0104] Step S6: Perform pollution detection on livestock water bodies based on the modified threshold.
[0105] For each pixel in the grayscale image of the water area, when the grayscale value of the pixel is greater than the correction threshold, the pixel is recorded as an algae point; the algae pixel in the grayscale image of the water area; the ratio obtained by dividing the total number of algae pixel points in the grayscale image of the water area by the denominator is used as the pollution degree; when the pollution degree is less than the preset pollution threshold, the livestock water body is not polluted; when the pollution degree is greater than or equal to the preset pollution threshold, the livestock water body is polluted.
[0106] It should be noted that the preset pollution threshold is an empirical value of 0.3, and implementers can set it according to specific circumstances. After determining that the livestock water body is polluted, the pollution degree of the livestock water body can be determined according to the dangerous level range of the pollution degree, which can be specifically set as follows: When the pollution degree of animal husbandry water is slightly polluted; when the pollution degree is When the pollution level of animal husbandry water is moderate, At that time, the pollution level of livestock water bodies was severe.
[0107] So far, the present invention is completed.
[0108] In summary, in an embodiment of the present invention, the first possibility of algae is obtained according to the grayscale value and analysis value of the pixel in the grayscale image of the water area, the stacking degree of algae of the pixel is obtained according to the distance between the pixel and the boundary line in the image, and the difference between the grayscale value of each pixel and the pixel on the boundary line and the difference between the analysis value, and the second possibility of algae of the pixel in the grayscale image of the water area is obtained by combining the difference in grayscale distribution between the pixel and the preset expansion window; based on the first possibility of algae and the second possibility of algae, the segmentation threshold in the grayscale image of the water area obtained by the threshold segmentation algorithm is adjusted to obtain the correction threshold of the pixel; and the pollution detection of the livestock water area is performed based on the correction threshold. The present invention adaptively obtains the correction threshold of each pixel according to the degree of algae stacking, increases the image segmentation effect, and improves the accuracy of livestock water pollution detection.
[0109] 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.
[0110] 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 detecting livestock water pollution based on machine vision, characterized in that: The method includes: Obtaining a water area grayscale image obtained by graying the water area RGB image of the livestock water body, and an analysis value of each pixel in the water area grayscale image; The method for obtaining the analysis value of each pixel in the water area grayscale image is: The channel value of each pixel point in the water area RGB image under the preset first color channel is used as the analysis value of the pixel point at the same position of the corresponding pixel point in the water area grayscale image; According to the gray value of each pixel in the gray image of the water area and the analysis value, a first possible degree of algae at each pixel in the gray image of the water area is obtained; Obtaining a boundary line in the grayscale image of the water area; obtaining the algae stacking degree of each pixel in the grayscale image of the water area according to the distance between each pixel in the grayscale image of the water area and the boundary line, and the grayscale value difference between the pixel and the pixel on the boundary line and the analysis value difference; Combining the difference between the grayscale distribution in the preset original window and the preset extended window of each pixel in the water area grayscale image, and the grayscale distribution of the pixel in the preset extended window and the algae stacking degree, the second possible degree of algae for each pixel in the water area grayscale image is obtained; According to the first possibility degree of the algae and the second possibility degree of the algae, the segmentation threshold of the water area grayscale image obtained by using the threshold segmentation algorithm is adjusted to obtain a correction threshold of each pixel in the water area grayscale image; Conducting pollution detection on livestock water bodies based on the modified threshold value; The method for obtaining the algae stacking degree of each pixel in the water area grayscale image according to the distance between each pixel in the water area grayscale image and the boundary line, and the grayscale value difference between the pixel and the pixel on the boundary line and the analysis value difference comprises: In the grayscale image of the water area, any pixel point is selected as a test point, and a straight line with a first preset direction through the test point is drawn as a first analysis line; a straight line with a second preset direction through the test point is drawn as a second analysis line; the first analysis line and the second analysis line are perpendicular to each other; The first analysis line and the second analysis line are recorded as lines to be tested; for each line to be tested, when there are at least two intersections between the line to be tested and the demarcation line, the Euclidean distance between each intersection and the point to be tested is calculated, and the intersection corresponding to the smallest Euclidean distance is recorded as the target point under the line to be tested; when there is one intersection between the line to be tested and the demarcation line, the intersection is recorded as the target point under the line to be tested; when there is no intersection between the line to be tested and the demarcation line, the intersection between the line to be tested and the preset boundary of the grayscale image of the water area is recorded as the target point under the line to be tested; Record the target point under the first analysis line of the point to be measured as the target edge point of the point to be measured; record the Euclidean distance between the point to be measured and the target edge point as the first distance of the point to be measured, and record the Euclidean distance between the point to be measured and the target point under the second analysis line as the second distance of the point to be measured; Acquire the algae stacking degree of the test point by combining the first distance of the test point, the second distance, and the gray value difference and analysis value difference between the test point and the target edge point; The calculation formula of the algae stacking degree at the test point is as follows: ; In the formula, is the algae stacking degree of the test point; d is the test point; D1 is the first distance of the test point; D2 is the second distance of the test point; is the gray value of the point to be tested; is the gray value of the target edge point of the point to be measured; m is the target edge point of the point to be measured; is the mean of the analysis values of the pixel points in the preset original window of the point to be tested; is the mean value of the grayscale values of the pixels in the preset original window of the point to be tested; is the analysis value of the point to be tested; The analysis value of the target edge point of the point to be measured; is the first preset positive number; exp is an exponential function with the natural constant e as the base; Norm is a normalization function.
2. The method for detecting livestock water pollution based on machine vision according to claim 1, characterized in that: The method for obtaining the first possible degree of algae at each pixel in the water area grayscale image comprises: Calculate the mean gray value of all pixels in the water area gray image and record it as the overall gray value; The calculation formula for the first possible degree of algae at each pixel in the water grayscale image is as follows: ; In the formula, is the first possible degree of the algae at the ith pixel in the grayscale image of the water area; is the gray value of the i-th pixel in the water area gray image; is the overall gray value; is the analysis value of the i-th pixel in the water area grayscale image; is the mean of the analysis values of all pixels in the water area grayscale image; exp is an exponential function with the natural constant e as the base; Norm is a normalization function; is a fixed value.
3. The method for detecting livestock water pollution based on machine vision according to claim 2 is characterized in that: The method of combining the difference between the grayscale distribution in the preset original window and the preset extended window of each pixel in the water area grayscale image, and the grayscale distribution of the pixel in the preset extended window and the algae stacking degree to obtain the second possible degree of algae for each pixel in the water area grayscale image includes: For each pixel in the water grayscale image, a preset extended window of the pixel is divided into different sub-windows, and the preset original window of the pixel is one of the sub-windows; the size of the preset extended window is larger than the size of the preset original window; For each of the sub-windows, the average of the grayscale values of the pixels in the sub-window is taken as the comprehensive grayscale value of the sub-window; Calculate the sum of squares of the differences between the comprehensive grayscale values of the preset original window of each pixel in the water area grayscale image and the remaining sub-windows in the preset extended window except the preset original window, as the extended grayscale difference of each pixel in the water area grayscale image; obtain the sum of squares of the differences between the grayscale values of each pixel in the water area grayscale image and the remaining pixel points in the preset original window except the pixel point, as the original grayscale difference of each pixel in the water area grayscale image; Counting the number of pixels whose grayscale values in a preset extended window of each pixel in the water area grayscale image are greater than the overall grayscale value, and recording it as the analysis number of each pixel in the water area grayscale image; The second possible degree of algae at the pixel point in the grayscale image of the water area is obtained by combining the extended grayscale difference of each pixel point, the original grayscale difference, the analysis quantity, the grayscale distribution of the pixel point in the preset extended window of the pixel point, and the algae stacking degree.
4. The method for detecting livestock water pollution based on machine vision according to claim 3 is characterized in that: The calculation formula of the second possibility degree of the algae at the pixel point in the water area grayscale image is as follows: ; In the formula, is the second possible degree of the algae at the ith pixel in the grayscale image of the water area; is the extended grayscale difference of the i-th pixel in the water area grayscale image; is the original grayscale difference of the i-th pixel in the water area grayscale image; is the number of analyzed pixels of the ith pixel in the water grayscale image; N is the total number of pixels in the preset extended window of each pixel in the water grayscale image; The algae stacking degree of the nth pixel point in the preset extended window of the i-th pixel point in the water area grayscale image; is the gray value of the nth pixel in the preset extended window of the ith pixel in the water area gray image; is the overall gray value; A positive number for the second preset.
5. The method for detecting livestock water pollution based on machine vision according to claim 1, characterized in that: The method of adjusting the segmentation threshold of the water area grayscale image obtained by using a threshold segmentation algorithm according to the first possibility degree of the algae and the second possibility degree of the algae to obtain a correction threshold of each pixel in the water area grayscale image includes: Normalizing the result of weighted summation of the first possibility degree of algae and the second possibility degree of algae to obtain the final possibility degree of algae at each pixel in the grayscale image of the water area; The maximum inter-class variance method is used to obtain the segmentation threshold for the grayscale values of pixels in the water grayscale image; For each pixel in the grayscale image of the water area, negative correlation mapping is performed on the final possible degree of the algae at the pixel to obtain a correction coefficient of the pixel; based on the correction coefficient and the segmentation threshold, a correction threshold for each pixel in the grayscale image of the water area is obtained; The calculation formula for the correction threshold of each pixel in the water grayscale image is as follows: In the formula, is the correction threshold of the i-th pixel in the water grayscale image; is the final possible degree of algae at the ith pixel in the water grayscale image; is the correction coefficient of the i-th pixel in the water grayscale image; To obtain the segmentation threshold of water grayscale image using the maximum inter-class variance method.
6. The method for detecting livestock water pollution based on machine vision according to claim 1, characterized in that: The method for detecting pollution of livestock water bodies based on the modified threshold value comprises: For each pixel in the grayscale image of the water area, when the grayscale value of the pixel is greater than the correction threshold, the pixel is recorded as an algae pixel; the number of algae pixels in the grayscale image of the water area is used as the numerator, and the total number of pixels in the grayscale image of the water area is used as the denominator to obtain the ratio as the pollution degree; When the pollution degree is less than a preset pollution threshold, the livestock water body is not polluted; when the pollution degree is greater than or equal to the preset pollution threshold, the livestock water body is polluted.
7. The method for detecting livestock water pollution based on machine vision according to claim 1, characterized in that: The method for obtaining a boundary line in a water area grayscale image comprises: Convert the water area RGB image into an HSV image; if the channel value of each pixel point in the HSV image under all second color channels is within the preset color range of the corresponding second color channel, then use the corresponding pixel point as a demarcation point; The connected domain formed by the pixel points at the same position of the water area grayscale image corresponding to the water area RGB image at the dividing point is used as the dividing line.
