Abstract

The traditional canny edge detection algorithm has its limitations in the aspect of antinoise interference, and it is susceptible to factors such as light. To solve these defects, the Canny algorithm based on morphological improvement was proposed and applied to the detection of agricultural products. First, the algorithm uses the open and close operation of morphology to form a morphological filter instead of the Gaussian filter, which can remove the image noise and strengthen the protection of image edge. Second, the traditional Canny operator is improved to increase the horizontal and vertical templates to 45° and 135° to improve the edge positioning of the image. Finally, the adaptive threshold segmentation method is used for rough segmentation, and on this basis, double detection thresholds are used for further segmentation to obtain the final edge points. The experimental results show that compared with the traditional algorithm applied to the edge detection of agricultural products, this algorithm can effectively avoid the false contour caused by illumination and other factors and effectively improve the antinoise interference while more accurate and fine detection of the edge of real agricultural products.

1. Introduction

Nowadays, computer technology is developing rapidly. For example, [1] applied new technology to agricultural products, the application level of computer technology in agricultural products’ informatization and intelligence has also been greatly improved, and the inspection and management of agricultural products also need to combine computer vision technology. In order to improve the management level, a large number of agricultural product images must be preprocessed. Among them, many methods are particularly important, such as image denoising and image edge detection. For example, the application of machine vision discussed in [2, 3] shows that, compared with other objects, although the structure of agricultural products is simple, a variety of mixed agricultural products are still difficult to detect and identify. The edge contains most of the information of the image and is the starting point of computer vision. Applying computer vision technology to agricultural products and edge detection is a good method. Although the structure of agricultural products is simple, its edge information or appearance will change under the influence of human or natural environment and other factors. The edge detection can identify the contour of agricultural products more accurately and lay a foundation for the subsequent classification of agricultural products, fruit and vegetable picking robot, and quality detection of agricultural products. Zhou and Ding [4] first proposed a design of an edge detection system for agricultural product images based on DM642, which laid a foundation for detection.

The reliability and accuracy of edge detection contribute to the description of image features and other image-related works. For example, the traditional differential edge detection algorithms proposed in [5], such as Sobel and Log, are traditional detection methods that use high-frequency signals as the extraction method. The edges extracted by these algorithms are relatively rough, although the edges of the image can be effectively detected without noise. But when there is noise, since the noise and edges of the image are both high-frequency signals, the noise is sometimes detected as edges. To solve this problem, John Canny proposed a more accurate edge detection method in 1986. This method uses a relatively high accuracy operator, the Canny edge detection operator, in which the large signal-to-noise ratio is an important characteristic. Therefore, the Canny operator has a wide range of applications in the field of digital image processing. Wang et al. [6] proposed a road edge detection method based on Canny operator color features. However, this algorithm also has some shortcomings. It will be affected by lighting changes. The Gaussian filter in the denoising part uses fixed parameters, and the parameters are modified manually. Therefore, the extracted edge information will be lost and too smooth, the edge of the image will become blurred, the restriction is relatively large, and the denoising effect is not obvious.

In recent years, many scholars have proposed an improved algorithm based on the Canny operator. Xue et al. [7] proposed a method different from that of traditional Canny, which adopts double thresholds according to the image instead of the traditional algorithm that uses mean and variance to determine the threshold. Li et al. [8] adopted a method that can improve the weakness of the traditional Canny algorithm (such as weak edge) and use other filters to replace the Gaussian filter in the Canny algorithm to smooth the image. With the development of image processing technology, mathematical morphology has been widely used. There are not only research studies on agricultural image segmentation, such as the research on plant leaf image segmentation based on mathematical morphology proposed in [9], but also research studies on edge detection. Mathematical morphology has also made great achievements in image denoising and enhancement, morphological filter has gradually entered people’s field of vision, Zhang and Tao [10] proposed a morphological method and the combination of fractal theory to image denoising processing of agricultural products, by choosing appropriate structural elements for morphological open and close operation, and the noise that can effectively remove noise is part of the high-frequency part. Therefore, a compound morphological filter is proposed in this study, which replaces the Gaussian filter used for smoothing images in the traditional Canny algorithm. At the same time, 45° and 135° directions are added on the basis of original direction to calculate the amplitude and gradient of the template. Finally, the adaptive threshold segmentation method is used for rough segmentation. In addition, the double detection threshold method is used for further segmentation based on rough segmentation. In the edge detection of agricultural products, the improved algorithm proposed in this study not only has a good antinoise effect but also can maintain the edge and details of the extracted contour.

2. Fundamentals of Traditional Canny Algorithms

Compared with the differential algorithm, the Canny algorithm has these three characteristics in the process of proposing. These three characteristics are good detection performance, low probability of edge recognition as nonedge, and high positioning accuracy. And the detected edge point is as close to the center of the actual edge as possible; when the response times of the same edge are low, the response probability generated by a single edge is reduced, and the response of false edge is suppressed to the greatest extent.

The implementation of the traditional Canny algorithm mainly includes four aspects. First, the algorithm uses the Gaussian filter to smooth the image; second, perform further calculations on the gradient magnitude and direction of the image based on the smoothed image; third, the interference in the local area in the algorithm is performed by nonmaximum suppression technology; fourth, the removal of false edges is carried out through the set high and low threshold technology; and finally, the real edges after the connection are obtained. The specific steps of the algorithm are as follows.

2.1. Gaussian Filtering Smooth Image

In the current research on image processing and edge detection at home and abroad, there are many types of filters related to image processing and noise processing. It is found through experiments that the Gaussian filter is mainly used to eliminate Gaussian noise among many filters, and it is a linear filter.

In [11], the filtering process of the Gaussian filter is given, and the filtering process of the Gaussian filter can be calculated using the following equation.

The function of the standard deviation of the Gaussian function in formula (1) is to control the degree of smoothness.

2.2. Calculate the Gradient Magnitude and Direction

The function f (x, y) is taken as the representation of the image, and the rate of change of the image is expressed by differentiation. The grayscale of the image is calculated by using the first-order discrete difference operator, and the gradient amplitude G and angle of the pixel in the image are calculated according to the gradients in the X and Y directions. In [12], the calculation formula is provided as shown in the following equation.

2.3. Nonmaximum Suppression of Gradient Amplitude

The nonmaximum suppression method refers to the search for the local maximum in the image detection process, that is, the suppression of maximum value elements. The neighborhood is the so-called local, and the dimension and size of the neighborhood are two variables that can be changed. Its purpose is to accurately locate the edge, and its essence is to suppress the interference in the local area while retaining the point with the largest local amplitude change. Its specific operation process is to find as many edge points as possible in the image f(x, y) in the 3 × 3 neighborhood and compare the gradient magnitude G(x, y) of the point (x, y) on it with two adjacent pixels along the gradient direction. There are two main comparison results: if the gradient magnitude of the pixel point is less than the relationship, then the point is not an edge point; if it is greater than the relationship, the point is an edge point.

2.4. Double Threshold Detection and Edge Connection

Th represents the high threshold in the double threshold. Tl represents the low threshold in the double threshold. The traditional Canny algorithm uses these two thresholds to process the image processed in the third step. The gradient amplitude of this point (x, y) is less than Tl, indicating that this point is not an edge point; if the gradient magnitude of the point (x, y) is greater than Th, it means that the point is an edge point; if the gradient value is between the high and low thresholds, that is, Tl < G(x, y) < Th, further judgment is needed, that is, to find a point near (x, y) that is greater than Th; if it is greater than that, the point is an edge point. Otherwise, it is not an edge point. The above description is the traditional Canny algorithm, which can better evaluate the quality of the edge.

3. Improved Canny Edge Detection Algorithm Based on Morphology

The traditional Canny operator has two most obvious problems when detecting image edges. First, the filter part adopts the Gaussian filter, and the parameters of the Gaussian filter are not easy to set, and it lacks adaptability in the process of image edge detection. To solve this problem, many scholars have made improvements; among them, Wu et al. [13] replaces the Gaussian filter with the median filter. Second, when the influence of noise is large, false edges will be detected, and the edges will be lost in small parts where the gray level change is not obvious. In view of the obvious shortcomings of the traditional Canny algorithm in the above two aspects, this study makes some improvements. The morphological filter is used as the improved filter and replaces the Gaussian filter in the traditional algorithm, will originally be in the horizontal direction and vertical direction the Sobel operator to increase both directions to calculate the direction to extend along the x, y, 45°, and 135° of the four directions, and then calculate the gradient and the size of the amplitude; easy to lost, the real edge phenomenon is solved, the threshold value calculating algorithm is improved, and the disadvantages of artificial selection are solved. The flow chart of the improved algorithm is shown in Figure 1.

3.1. Compound Morphological Filtering Denoising

In recent years, the methods of image denoising of agricultural products have emerged in an endless stream. Contour transform [14] plays a very good role in the denoising of agricultural products. In this study, a morphological filter is used to replace the Gaussian filter for denoising. Structural elements with different denoising abilities were combined with branches, blades, and arc-shaped structures in agricultural products for denoising. The virtual noise generated by agricultural products under illumination or their own factors can be removed by small structural elements in morphology to better protect the details of agricultural products. For agricultural products with complex structure, it can be filtered by large structural elements. Therefore, the composite morphological filter proposed in this study can well remove the noise interference in the process of agricultural product edge detection and can also highlight the contour and remove unnecessary false contour.

Suppose F(x, y) is a grayscale image of m × n and its morphological structure element is B(s, t), the basic operations of morphology on this image mainly include 4 operations. They should be referred to as equations (3)∼(6) in the main text.Dilation operationErosion operationOpen operationClosed operation

Isolated burrs in the image can be filtered out by equation (5) because an important feature of open operation is that it can effectively suppress the positive peak noise with small structural elements. Wang et al. [15] explained the basic operation of morphology; while keeping the size of the target unchanged, the open operation can remain roughly the same and play a smoothing role. However, if the distance between the structural elements is far greater than the noise points, the image quality after operation will be very poor. The core of this operation is to remove the bright details smaller than the structural elements on the premise of keeping the overall bright features unchanged. The closed operation is the inverse operation of the open operation, which can remove the dark details smaller than the structural element under the condition that the features remain unchanged and fill the narrow cracks between the image objects, so that the processing effect is close to the desired result. On the basis of basic morphological operation, we can construct a morphological filtering operator, which is also reflected in [16]. In the newly constructed operator morphology operation, the structure element is a key factor, so it is necessary to make a reasonable choice, in image smoothing or edge detection, the appropriate structural elements are first selected, and then, the image is sequential open and close combined operation.

The two commonly used morphological filter operators are as follows:

For the above two morphological filtering operators, A1 represents the image filtered after opening operation first and then closing operation, and A2 represents the image filtered after closing operation first and then opening operation. The new composite filter in this study is constructed on the basis of the above two filtering operators (7) and (8), and the filter can be represented by the following equation.

In order to remove noise more effectively and obtain a good smoothing effect, appropriate structural elements should be selected while combining the opening and closing operations, which can not only denoise but also maintain edge details. Therefore, after experiments, we choose the sum of structural elements represented by the following formula.

In this study, formula (11) is used to construct a composite filter with multiple structural elements.

Structural elements B1 and B2 have different denoising capabilities due to different scale structures. Smaller scales have weaker denoising capabilities, but they have better effects in image detail protection; on the contrary, the large-scale structural elements can denoise well but protect the image details not well. Therefore, we considered to use a composite morphological filter to avoid the shortcomings of a single structural element and to effectively filter out the noise while maintaining the detailed information of the image. According to the signal-to-noise ratio of the open-close filter and the closed-open filter under different intensities of salt and pepper noise, we can see that these two filters cannot effectively filter the noise in the figure. Therefore, after many experimental studies, we finally decided to fuse the two at a ratio of 10% and 90% to obtain a composite morphological filter. And it can be seen through experiments that the signal-to-noise ratio of the filter under salt and pepper noise is originally higher than the other two filters; this study adopts the method adopted in [17] to obtain the result comparison chart as shown in Figure 2.

3.2. Calculation Method of Gradient Amplitude

In the traditional Canny algorithm, the core of calculation is to apply the finite difference method to the template with the grid number of 2 × 2 to calculate the gradient amplitude of the image. In the calculation process, because it is not in an ideal state, there will always be some environmental factors, such as noise, so this algorithm can carry out edge positioning but can not get a good detection effect. Pandey et al. improved the existing problem by combining the traditional Sobel operator with other programmers. The improved method has the advantages of suppressing noise and improving edge position, but the disadvantage is that it increases the complexity of computing environment. In this study, based on the method in [18, 19], gradients in the directions of 45° and 135° are added on the basis of one-step template to reduce the calculation amount. Finally, the template is extended to a 3 × 3 domain to calculate the gradient angle and amplitude of the image. Through the application in the experiment, a good effect has been obtained. The template is shown in Figure 3.

Gradient components in four directions are , , , and . Formulas (12) and (13) can be obtained from the template in Figure 2 through the gradient components:

3.3. Improvement of Threshold Calculation Algorithm

The traditional Canny edge detection algorithm has great drawbacks in the selection of thresholds. Its disadvantage is that it is difficult to set the appropriate threshold, and the setting of threshold directly affects the extraction of edge information. If the threshold is too large, it can effectively suppress the false edge information, but the edge information will be lost. If the threshold is too small, the false edge information will increase while the edge information is retained. For some special objects, we not only need to consider the overall characteristics but also the preservation of local information. Therefore, the original threshold selection method is not good. For example, Yang et al. [20] used the relationship between the pixel gray value and the change of the cumulative number of pixels in the traditional Otsu algorithm to select the ratio of the pixel gray value to a certain cumulative number of pixels as the adjustment threshold. In this study, double threshold detection and improved Otsu are combined to achieve this step.

In 1979, Otsu proposed a maximum difference between classes (Otsu), also known as the Otsu algorithm, which can automatically select the threshold. This algorithm is not only computationally simple but also recognized as the leader in threshold selection algorithms. This method will not be affected by external factors, such as image brightness and contrast. Therefore, this feature has achieved the position of this algorithm in image segmentation in the image processing industry. The Otsu method is an in-depth study of image processing problems, and the core of this method is to use the idea of clustering. This method can divide the research object into two parts according to the number of gray levels. If the variance between classes is large, it means that the difference between the background and the foreground of the image is greater. When the difference becomes smaller, it is the opposite situation when the foreground is considered to be the wrong one for the background. When the variance between classes takes the maximum value, it means that the probability of error is also the smallest under this result.

Suppose the pixels of an image are n, which is the number of gray values i, is the probability of i appearing. The image is divided into two categories and by the threshold k, and are divided into the probability generated by and , and the corresponding average values are and , respectively. Let the average value of the entire gray scale be ; then, you can use equation (14) to calculate the variance between and .

3.4. Improvement of the Canny Algorithm for Edge Detection of Agricultural Products

Since morphological filters have been widely used, Salehi et al. [21] applied morphological filters to fault classification and fault phase selection of transmission lines. Morphological filter was introduced in this study. At first, the compound morphological algorithm was used to denoise. According to the combination of the structure of the object itself and the morphological structural elements, the circular arc and angular shape of the structure of the agricultural product combined with structural elements of different sizes have different denoising abilities. For small-scale structural elements, although its denoising ability is weak, it can filter out unnecessary noise generated by small agricultural products under the light or agricultural products’ own factors. And at the same time, it has a good detail protection effect for such agricultural products. For the large-scale structural elements, the noise in the slightly complex agricultural products can be filtered out. Therefore, the composite morphological filter proposed in this study combines these two factors to remove noise from agricultural products and maintain details at the same time. In the case of adding artificial salt and pepper noise, the composite filter set in this study combined with open and closed filter can remove the noise more effectively. In the calculation method of gradient amplitude, the two directions of the traditional algorithm are changed into four directions, which can make the edge positioning more accurate, and play a better detection effect for objects such as agricultural products. Ruslau and Pratama [22] discussed various types of edge detection. In this study, the adaptive threshold segmentation method was adopted for rough segmentation based on the characteristics of agricultural products. As agricultural products contain special edges of branches, leaves, and fruit stems, double detection thresholds were used for further segmentation, and the edges were finally linked.

4. Results and Discussion

In order to verify that the proposed algorithm is more effective than the traditional Canny algorithm and other algorithms to some extent, a simulation experiment is carried out under the same experimental software MATLAB 2021B. In this study, representative fruits and agricultural products are selected as the research objects. The selected fruits and agricultural products may have unnecessary contours due to sunshine. The algorithm comparison experiment results in the case of no noise are shown in Figure 4.

In the absence of noise, it can be seen from the figure that although the traditional algorithm can describe the edge of the approximate contour of agricultural products, the edge contour is not connected, which increases the difficulty of identification when there are many kinds of agricultural products. The proposed method can be seen that the method of [23] although it can clearly describe the outline of agricultural products, but due to the unnecessary lighting and form false contour can also be very clear description, account and edge profile are not real, and pure simple morphological edge detection used to describe the image is easy to produce false edges. The improved algorithm can not only remove the redundant false edges but also clearly depict the real edges of each object in various agricultural products. In order to further illustrate the reliability of the algorithm, the evaluation criteria proposed by Hu et al. [24] were adopted in this study to evaluate the edge detection effect of the algorithm. The total number of pixel edges found is A, and B is the number of 8 connected domains. Then, the ratio is compared. The smaller the B/A ratio, the fewer discontinuity points, the better the edge connectivity, and the higher the linear integrity. The statistical results are given in Table 1.

It can be seen from Table 1 that the B/A value of the algorithm in this study is much smaller than that of the traditional Canny algorithm, indicating that the algorithm has high accuracy. Although the algorithm proposed in [23] is close to the experimental value of the algorithm in this study, the algorithm in this study is superior to the method in the literature in recognition of real scenes based on the combination of experimental images. In the same experimental environment, in order to further demonstrate the reliability of the algorithm in this study, a comparative experiment was carried out under the condition of adding noise. We added 0.01 pepper and salt noise to the image of agricultural products to form a noise image. The comparison experiment effect chart is shown in Figure 5.

In the case of noise, it can be seen from Figure 5 that the traditional Canny algorithm has a better denoising effect than the algorithm in this study. Due to the particularity of some agricultural products, they have too much contour, and denoising also increases the difficulty of edge detection. The traditional Canny algorithm takes edge as noise filtering, while the improved algorithm can not only effectively remove noise but also retain the original edge contour of agricultural products. In order to illustrate the accuracy of the results, PSNR is introduced as the evaluation index, and the larger the value is, the better the performance of the algorithm is. The experimental results are given in Table 2.

The edge detection algorithm for agricultural products proposed in this study is compared with that without noise. In the absence of noise, the traditional Canny algorithm has no obvious effect in detecting agricultural products, and it is easy to overfilter the real edge under the influence of illumination. Compared with the algorithm provided in [23], the algorithm in this study can detect false edges and display the edge details of the target itself, which plays a more accurate role in the identification of agricultural products. In the case of noise in Figure 5, the traditional Canny algorithm cannot remove the noise very well and uses the noise as the edge detection. However, the algorithm in this study can remove the noise well and display the contour of agricultural products accurately, which lays a foundation for the subsequent identification of agricultural products. By comparing Figures 4 and 5, it can be seen that the algorithm proposed in this study is less affected by noise and more accurate in edge detection and positioning. Compared with the traditional Canny algorithm and the existing algorithm, it can detect the edge more clearly. Therefore, it has a better edge detection effect.

5. Conclusion and Future Work

The traditional Canny algorithm is easily affected by external noise and other factors, and it is difficult to detect the defects of false edge and poor adaptive ability. In order to reduce the impact of noise and meet the denoising environment of agricultural products, a morphological filter is used to replace the Gaussian filter in the traditional Canny algorithm and improve the Sobel operator by adding 45° and 135° in two directions. The gradient calculation is improved from the original two direction calculation to the four direction calculation, and the purpose of the improvement is to improve the positioning effect. Artificial selection of threshold has limitations; so in order to avoid coarse segmentation, adaptive threshold segmentation is used. On this basis, dual detection thresholds are used for further segmentation to obtain the final edge point. Through the analysis of the experimental results, it can be obtained that the improved algorithm in this study has a better antinoise effect and edge detection effect than the traditional Canny algorithm. Through the analysis of experimental results, it can be concluded that the improved algorithm in this study has a better antinoise effect and edge detection effect than the traditional Canny algorithm. Edge detection is the basis of image processing. In order to illustrate the universality of this algorithm, it is applied to the typical image detection. This is shown in Figure 6.

In this study, the algorithm is applied to the agricultural products and typical images to realize the optimization of denoising. The connection degree can also ensure not only the agricultural products in the complex scene but also the classic images, so that the edge detection effect is improved.

In this study, by applying the algorithm to the agricultural products and typical images in complex scenes, not only the optimization of denoising is realized but also the connectivity can be guaranteed, which improves the edge detection effect. The results of this algorithm can also be applied to the identification, classification, and detection of agricultural products and test the quality, size, and other defects of agricultural products. At the same time, it lays a foundation for the fruit and vegetable picking robot and the fruit and vegetable recognition and sorting robot. Through experiments, the algorithm can also be applied to general images, such as the recognition and detection of garbage classification. It can be seen from the experiment that the edge detection of the algorithm in this study is still not very ideal and cannot achieve a very perfect and accurate detection of eliminating false edges. Moreover, it also needs the support of hardware objects when it is applied to actual scenes such as robots. The researchers will also improve these practical problems in the following studies.

Data Availability

The data used to support the findings of this study are included within the article.

Conflicts of Interest

The authors declare that they have no conflicts of interest.

Acknowledgments

This research was funded by the National Natural Science Foundation of China (6192007, 61462008, 61751213, and 61866004), the Key projects of Guangxi Natural Science Foundation (2018GXNSFDA294001 and 2018GXNSFDA281009), the Natural Science Foundation of Guangxi (2018GXNSFAA294050 and 2017GXNSFAA198365), 2015 Innovation Team Project of Guangxi University of Science and Technology (gxkjdx201504), Innovation Project for College Students of Guangxi University of Science and Technology (GKYC201708), Research Fund of Guangxi Key Lab of Multi-source Information Mining & Security (MIMS19-04), and Guangxi Postgraduate Education Innovation Project (GKYC202106).