Mature strawberry fruit picking point positioning method with stems
By improving the YoLov5s-seg-master model and Canny edge detection technology, the problem of insufficient recognition accuracy of strawberry picking robots in the positioning of mature stem strawberry picking points is solved, and efficient positioning of strawberry picking points is achieved, improving the picking efficiency and accuracy sex.
Patent Information
- Application Number
- CN202410915703.0
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-07-09
- Publication Date
- 2024-11-01
AI Technical Summary
The existing strawberry picking robots have problems with insufficient identification accuracy and applicability in the picking point positioning of mature strawberries with stems. In particular, the semantic cutting module is limited to multi-scale detection and is easily disturbed by unripe fruit stems, resulting in picking efficiency. and the accuracy is not ideal.
Using the deep learning algorithm YoLov5s as the basis of the model, by improving the YoLov5s-seg-master model, combining Canny edge detection and image mask covering technology, we can identify the target instance segmentation of strawberry ripe fruits and stems, and calculate the specific (x,y) of the picking point. ) coordinates to achieve accurate positioning of strawberry picking points.
The recognition accuracy and robustness of strawberries and their fruit stems are improved, with an accuracy of 97%, ensuring the accurate picking points for automated picking and improving the picking efficiency.
Smart Images

Figure CN118887387A_ABST
Abstract
Description
Technical Field
[0001] The invention relates to the technical field of computer vision and target detection, and in particular to a method for locating a picking point of a mature strawberry fruit with a stem. Background Art
[0002] With the development of modern agriculture, strawberry, as a high value-added economic crop, has received widespread attention. However, the strawberry picking process requires a lot of human resources, especially in facility agriculture, where large-scale strawberry planting places high demands on picking efficiency. In order to improve the efficiency of strawberry picking, automated picking has become a research focus. By using image processing and machine vision technology, combined with artificial intelligence algorithms, an efficient strawberry picking positioning method can be developed to reduce labor intensity and improve picking efficiency and accuracy.
[0003] The Chinese invention patent with the patent number CN115984704A discloses a plant and fruit detection algorithm for a tomato picking robot. By establishing a multi-task convolutional neural network, using yolov5s as the basic network model, adding key point prediction parameters and semantic segmentation modules to the basic network model, to identify tomato fruits and stems, thereby helping the robot to pick tomatoes. However, in this process, the semantic segmentation module itself has limitations in multi-scale detection and cannot provide instance segmentation effects; the model itself lacks further analysis of the output stem image and further calculation and confirmation of the picking coordinates, and is easily disturbed by immature fruit stems. When applied to the positioning of the picking point of mature strawberries with stems, the accuracy and applicability in the actual picking process cannot reach the ideal level. Summary of the invention
[0004] Purpose of the invention: In response to the problems mentioned in the background technology, the present invention provides a method for locating the picking point of mature strawberry fruits with stems. The deep learning algorithm YoLov5s is used as the model basis to identify images of mature strawberries with stems, and the improved YoLov5s-seg-master model is used to perform target instance segmentation of the stems on the target image set of mature strawberry fruits and corresponding stems. The specific (x, y) coordinates of the strawberry picking points are analyzed and calculated to achieve picking point positioning.
[0005] Technical solution: The present invention provides a method for locating the picking point of mature strawberry fruits with stems, the method comprising the following steps:
[0006] S1 collects an image set of strawberry plants at maturity, annotates the red ripe strawberry fruits with stems in the strawberry plant images, and packages them with the original images to create a dataset;
[0007] S2 uses YoLov5s model to train and verify target recognition of ripe strawberry fruits with stems;
[0008] S2.1 Modify the Head module of the YoLov5s model to enhance the visualization of detecting ripe strawberries with stems;
[0009] S2.2 outputs a target image containing only ripe strawberry fruits and corresponding stems through the modified YoLov5s model;
[0010] S3 uses the labelme tool to annotate the output strawberry images with stems, convert and package them, and create a dataset;
[0011] S4 improved YoLov5s-seg-master model;
[0012] S4.1 creates an all-zero black background array in the Head module, adjusts and applies the prediction mask to obtain a binary image, and introduces Canny edge detection;
[0013] S4.2 uses the data set obtained in S3 to train the improved YoLov5s-seg-master model, obtains the final model for instance segmentation, identifies strawberry stems, and obtains an image set containing only stems;
[0014] S5 calculates the approximate rectangular features of the fruit stem image set, applies an image mask to cover and mark the center coordinates to determine the coordinates of the picking point.
[0015] Furthermore, the Yolov5 model described in S2 makes adaptive changes to the Input, Backbone and Head parts for the recognition of ripe strawberries with stems. The specific method is as follows:
[0016] Input part: Change the image size from 640x640 to 320x320, modify the input image size in the model configuration file, and retrain or fine-tune the model to adapt to the new input size;
[0017] Backbone part: modify the YOLOv5s configuration file, reduce the number of channels of the YOLOv5s model by half, and retrain or fine-tune the model to adapt to the reduction in the number of channels;
[0018] Head part: Modify the anchor box size in the configuration file and reduce the number of output channels of the prediction layer to the corresponding number of categories.
[0019] Furthermore, the adaptive changes also include: improving the SPP pyramid pooling structure to SPPF. SPPF optimizes the pooling operation, maintains the multi-scale feature fusion capability similar to SPP while reducing the amount of calculation. SPPF is applied to Backbone to enhance the multi-scale characteristics of the feature extraction stage.
[0020] Furthermore, the specific modification steps of the Head module to enhance the visual representation of the detection of ripe strawberries with stems are as follows:
[0021] Object detection: Use the trained YoLov5s model to process the input image, identify the strawberries in the image, and draw a detection box;
[0022] Content extraction: For each detection box, the strawberry part will be retained and the background outside the box will be ignored;
[0023] Background processing: Create a black image of the same size as the original image, and then paste the extracted strawberry content onto this black background.
[0024] Furthermore, the detailed steps of S4.1 are:
[0025] Create an all-zero array black_background with the same shape as the original image to be used as a black background; in the loop that processes the prediction results, for each detected object mask, resize it to match the image size and apply it to black_background; set save_img to save or display the black_background image with the drawn mask to facilitate stem shape calculation and picking point positioning.
[0026] Furthermore, the detailed steps of introducing Canny edge detection in the Head module are:
[0027] Define the Canny edge detection function: Create a function called Canny_demo that accepts the source image folder and the target output folder as parameters;
[0028] Check and create the destination folder: Inside the function, first check if the destination folder exists, if not, create it;
[0029] Traverse the images in the source folder: Use os.listdir to traverse all the files in the source folder and check whether each file is in image format;
[0030] Read and preprocess images: For each image file, use cv2.imread to read the image and use cv2.cvtColor to convert it from BGR color space to RGB and then to grayscale image;
[0031] Apply Gaussian blur: Apply Gaussian blur to the grayscale image to reduce image noise in preparation for Canny edge detection;
[0032] Perform Canny edge detection: Use cv2.Canny function to perform Canny edge detection on the blurred image and set appropriate thresholds to extract edges;
[0033] Save the processed image: Save the image after Canny edge detection to the target folder and record the processing information;
[0034] Calling the function: Finally, define the path of the source and destination folders and call the Canny_demo function to process the image.
[0035] Furthermore, the calculation steps of the S5 stem approximate rectangular feature are detailed as follows:
[0036] Define the processing function: Create a function called rectangle_approximation that accepts the source image folder and the target output folder as parameters;
[0037] Check and create the destination folder: Inside the function, first check if the destination folder exists, if not, create it;
[0038] Traverse the images in the source folder: Use os.listdir to traverse all the files in the source folder and check whether each file is in image format;
[0039] Read image and convert to grayscale: For each image file, use cv2.imread to read the image and use cv2.cvtColor to convert it to grayscale image;
[0040] Detect image contours: Use cv2.findContours function to detect contours in grayscale images;
[0041] Create a black background image: Create a completely black background image with the same size as the original image for each image;
[0042] Contour polygon approximation: traverse the detected contours and use the cv2.approxPolyDP function to perform polygon approximation on each contour;
[0043] Calculate the minimum area rectangle: Use cv2.minAreaRect to calculate the minimum area rectangle of the approximate polygon;
[0044] Determine the center and width of the rectangle: Extract the width, height and center point from the minimum area rectangle;
[0045] Draw a circle on a black background: On a black background, draw a blue solid circle with the center of the rectangle as the center and half the width as the radius;
[0046] Save the processed image: Save the image with the circles drawn on the black background to the destination folder.
[0047] Further, the method of applying the image mask to cover and mark the center coordinates of the circle to determine the coordinates of the picking point is as follows:
[0048] Define the mask and overlay functions: Create a function called
[0049] The mask_and_overlay_circles_with_coordinates_above function accepts the original image folder, the circle image folder, and the target output folder as parameters;
[0050] Check and create the destination folder: Inside the function, first check if the destination folder exists, if not, create it;
[0051] Traverse the image folder: Use os.listdir to traverse all the files in the original image folder and the circle image folder;
[0052] Read the image: For each image file, use cv2.imread to read the original strawberry image and the corresponding blue solid circle image;
[0053] Create a mask: Use cv2.inRange to create a mask that only contains the pixels of the blue solid circle, and use cv2.dilate to perform morphological dilation to fill the small holes in the mask;
[0054] Extract foreground and background: Use the mask and the complement of the mask to extract the background to be covered by the circle and the foreground of the circle from the original image respectively;
[0055] Cover circle: Cover the extracted blue circle foreground onto the background of the original image;
[0056] Mark the coordinates of the center of the circle: Use cv2.findContours and cv2.moments to find the centers of all circles and calculate their coordinates. Mark the coordinates of the center of each circle.
[0057] Save result image: Save the processed image to the target folder and record the processing information.
[0058] Beneficial effects:
[0059] 1. The present invention modifies the Head module of YoLov5s to improve the visualization effect of the detection results. The recognition rate of the trained YoLov5s model for ripe strawberries with stems reaches 97%.
[0060] 2. The present invention improves the YoLov5s-seg-master model, creates an all-zero black background array in the Head module, adjusts and applies the prediction mask to obtain a binary image, and introduces the Canny edge detection method, thereby further reducing the interference of other details in the process of instance segmentation and identifying strawberry stems, thereby improving the recognition accuracy of strawberries and their stems, especially the robustness in complex environments.
[0061] 3. The present invention calculates the approximate rectangular features of the fruit stem image set, applies an image mask to cover and mark the center coordinates to determine the coordinates of the picking point, and provides accurate picking points for automated picking by precisely locating the fruit stem, thereby improving picking efficiency. BRIEF DESCRIPTION OF THE DRAWINGS
[0062] Figure 1 Improved YOLO v5s network structure diagram for the present invention;
[0063] Figure 2 This is a schematic diagram of the confusion matrix after improving YoLov5s training in the present invention;
[0064] Figure 3 This is a schematic diagram of the confusion matrix after YoLov5s-seg-master training of the present invention;
[0065] Figure 4 This is a result diagram of YoLov5s-seg-master modified by the present invention identifying ripe strawberry stems;
[0066] Figure 5 Schematic diagram of YoLov5s-seg-master identifying ripe strawberry stems by introducing Canny edge detection in the present invention;
[0067] Figure 6 This is a schematic diagram of polygonal approximation and positioning of picking points in the present invention;
[0068] Figure 7 Positioning the strawberry picking point coordinate map for the final image mask of the present invention; DETAILED DESCRIPTION
[0069] The present application is further described in detail below in conjunction with the accompanying drawings. It is necessary to point out here that the following specific implementation methods are only used to further illustrate the present application and cannot be understood as limiting the scope of protection of the present application. Technical personnel in this field can make some non-essential improvements and adjustments to the present application based on the above application content.
[0070] (1) Collecting images of mature strawberry plants
[0071] The images in this embodiment were collected from the greenhouse of Haocheng Technology Co., Ltd. in Suyu District, Suqian City, Jiangsu Province, China. The "Hongyan" variety was selected for the experiment, and the specific collection time was April 2024. A total of 1050 images were collected under different occlusion levels, shooting distances, and lighting conditions to establish a strawberry data set. 80% of them were used as training sets and 20% as validation sets.
[0072] (2) Create a labeled dataset
[0073] Use labellmg to annotate the ripe strawberries with stems, convert the annotated files into txt format and package them with the original images for training with the YoLov5s program.
[0074] (3) YoLov5s performs target segmentation of ripe strawberry fruits with stems
[0075] YOLOv5 is a deep learning algorithm for target detection tasks. It has the advantages of excellent performance, high efficiency and accurate precision in the YOLO series. YOLOv5 consists of four parts: Input, Backbone, Neck and Head.
[0076] ①Input part
[0077] The input stage contains Mosaic data enhancement, adaptive image scaling, and adaptive anchor box calculation, which usually involves preprocessing the image, such as resizing and normalization, so that the model can process it.
[0078] ②Backbone
[0079] Backbone is the main part of YOLOv5, which is responsible for extracting features from the input image. The backbone network of YOLOv5 is based on CSP Darknet, which can effectively capture different scales and contextual information in the image. The Backbone of YOLOv5s usually uses the CSP (Cross Stage Partial) structure, which combines networks such as CSPDarknet53 (for YOLOv5l and YOLOv5x) or CSPDarknet (for YOLOv5s and YOLOv5m). These networks are improved versions of Darknet, designed specifically for YOLOv5 to provide better feature extraction capabilities.
[0080] ③Neck
[0081] The Neck part is between the Backbone and the Head. It is responsible for fusing and enhancing the features extracted by the Backbone, fusing feature maps from different scales, and enhancing feature representation. YOLOv5 uses PANet (Path Aggregation Network) as the neck network, which combines the top-down and bottom-up path feature fusion of FPN (Feature Pyramid Network), thereby enhancing the model's ability to detect objects of different scales.
[0082] ④Head part
[0083] Head is the output part of the model, which is responsible for predicting bounding boxes, categories and confidences based on the feature maps provided by Neck, and predicting bounding boxes and category scores. YOLOv5 uses a multi-scale prediction strategy method called anchors, predefines a set of bounding boxes of different scales and aspect ratios, and adjusts these bounding boxes during training to detect objects of different sizes at different feature scales and better fit the objects.
[0084] YOLOv5s is the "small" version in the YOLO v5 series. It is a network with the smallest depth and the smallest feature map width. Compared with other variants (such as YOLOv5m, YOLOv5l, and YOLOv5x), YOLOv5s has a smaller model size and lower computational complexity. Due to its smaller model size and lower computational complexity, YOLOv5s is mainly suitable for devices with more limited resources, such as mobile picking equipment. Although its performance may be slightly lower than that of the full YOLOv5 model, it can still provide high detection accuracy and real-time performance to a certain extent. In order to further improve the target detection speed of YOLOv5s, the lightweight improved YOLO v5s improves the four main parts of YOLOv5: Input, Backbone, Neck, and Head. The following is a detailed explanation of the common improvement strategies, principles, and processes for these parts:
[0085] YOLOv5s is the "small" version in the YOLO v5 series. It is a network with the smallest depth and the smallest feature map width. Compared with other variants (such as YOLOv5m, YOLOv5l, and YOLOv5x), YOLOv5s has a smaller model size and lower computational complexity. Due to its smaller model size and lower computational complexity, YOLOv5s is mainly suitable for devices with more limited resources, such as mobile picking equipment. Although its performance may be slightly lower than that of the full YOLOv5 model, it can still provide high detection accuracy and real-time performance to a certain extent. In order to further improve the target detection speed of YOLOv5s, the lightweight improved YOLO v5s improves the four main parts of YOLOv5: Input, Backbone, Neck, and Head. The following is a detailed explanation of the common improvement strategies, principles, and processes for these parts:
[0086] ①Input part
[0087] Improvement: Adjusted input image size (from 640x640 to 320x320).
[0088] Principle: Smaller input images mean that the model needs to process less data, thereby reducing the amount of computation.
[0089] Process: Change the input image size in the model configuration file and retrain or fine-tune the model to accommodate the new input size.
[0090] ②Backbone
[0091] Improvement: Reduce the number of channels of the model (reduce the number of channels of the YOLOv5s model by half).
[0092] Principle: Reducing the number of channels can reduce the number of parameters in the model, thereby reducing the amount of computation and storage requirements.
[0093] Process: Modify the YOLOv5s configuration file (such as yolov5s.yaml), adjust the width_multiple parameter (which controls the number of channels in the output feature map) to a smaller value, and adjust the number of channels of the convolutional layer accordingly in the code. Retrain or fine-tune the model to adapt to the reduction in the number of channels.
[0094] ③Neck
[0095] Improvement: Optimize the feature fusion structure and reduce the number of channels or levels of feature maps.
[0096] Principle: The Neck part is responsible for fusing feature maps of different scales. By reducing the number of channels or levels, the amount of calculation can be reduced.
[0097] Process: Adjust the structure of the Feature Pyramid Network (FPN) or Path Aggregation Network (PAN), reduce the number of channels or levels of the feature map, modify the configuration files and code to reflect these changes, and retrain or fine-tune the model to adapt to the changes in the Neck part.
[0098] ④Head part
[0099] Improvement: Reduce the number of output channels of the prediction layer (corresponding to the number of categories) or adjust the anchor boxes (such as halving the corresponding anchor boxes).
[0100] Principle: Reducing the number of output channels of the prediction layer can reduce the amount of calculation, while adjusting the anchor box can optimize the prediction ability of the model.
[0101] Process: Modify the anchor box size in the configuration file to make it more suitable for the new input size or task requirements, reduce the number of output channels of the prediction layer (if the number of categories is reduced), modify the code to reflect these changes, and retrain or fine-tune the model to adapt to the changes in the Head part.
[0102] SPP is a pyramid pooling structure that is mainly used to process feature maps of different scales. By performing pooling operations on feature maps of different scales, it can extract feature information of different scales, thereby enhancing the model's ability to detect multi-scale targets. In YOLOv5, SPP is often used in the Neck part, especially in the FPN (Feature Pyramid Network) or PAN (Path Aggregation Network) structure. These structures fuse feature maps of different scales through top-down and bottom-up paths, and SPP is usually used to further enhance the multi-scale characteristics of these feature maps. CSP is a Backbone structure in YOLOv5, which is mainly responsible for extracting features from the input image. The CSP structure combines networks such as CSPDarknet53 or CSPDarknet to provide better feature extraction capabilities. Although SPP and CSP differ in function (SPP is used for multi-scale feature fusion and CSP is used for feature extraction), they are both important components in the overall architecture of YOLOv5. Therefore, in addition to the above four improvements, this study also attempts to improve SPP to SPPF (SpatialPyramid Pooling-Fast). SPPF optimizes the pooling operation, maintains the multi-scale feature fusion capability similar to SPP, and reduces the amount of calculation. SPPF is applied to Backbone to enhance the multi-scale characteristics of the feature extraction stage, and work together to achieve efficient and accurate target detection and positioning. The Yolov5s network structure is shown in the figure Figure 1 shown.
[0103] The YoLov5s algorithm is used to detect strawberries. By modifying the Head module, only the content within the detection box is retained and the background outside the box is set to black.
[0104] ① First, we trained the YoLov5s model to recognize the features of ripe strawberries with stems. During the training process, we used a large number of strawberry images and corresponding labels to guide model learning. Once the model training is completed, we can apply the model to new images for object detection.
[0105] ② Next, in order to improve the visualization of the detection results, we modified the Head module of YoLov5s. The modified program will perform the following steps:
[0106] Object detection: Use the trained YoLov5s model to process the input image, identify the strawberries in the image, and draw a detection box.
[0107] Content extraction: For each detected box, the program will extract the content inside the box. This means that only the strawberry part within the box will be retained, while the background outside the box will be ignored.
[0108] Background processing: Set the background outside the detection box to black. This can be achieved by creating a black image of the same size as the original image and then pasting the extracted strawberry content onto this black background.
[0109] Output: Ultimately, the program will generate a new image that contains only the detected strawberries with a black background, making the strawberries more visually prominent. The trained YoLov5s model has a 97% recognition rate for ripe strawberries with stems ( Figure 2 ).
[0110] (4) Labeling using labelme tool
[0111] The output strawberry image is annotated with the fruit stem using the labelme tool, and then packaged and sent to the YoLov5s-seg-master instance segmentation program for training.
[0112] Using the Labelme tool for image annotation is an efficient method, especially for accurately identifying specific objects in an image, such as the stems of strawberries. With Labelme, we can draw bounding boxes on the strawberry image and label each strawberry stem. After the annotation is completed, the data is converted into YOLO format for use by the YoLov5s-seg-master instance segmentation program. YoLov5s-seg-master is an instance segmentation model based on YoLov5s that can identify and distinguish each strawberry instance in the image. Through training, the model can learn how to accurately segment the strawberry stems in the image, maintaining high accuracy even in complex backgrounds.
[0113] (5)YoLov5s-seg-master model
[0114] YOLOv5s-seg-master adds instance segmentation capabilities while keeping the backbone network CSP Darknet of YOLOv5 s unchanged. This is achieved by adding additional convolutional layers and upsampling layers to the Neck and Head parts of the model to generate a pixel-level mask with the same resolution as the input image. The specific operations are as follows:
[0115] ①Neck module
[0116] The Neck module is mainly responsible for multi-scale feature fusion of the features extracted by Backbone and passing these features to the prediction layer. In YOLOv5s-seg-master, the FPN structure is used to improve the effect of semantic segmentation. The FPN structure designs an aggregation strategy through multiple upsampling, splicing, points and dot products to better utilize multi-scale features. This feature fusion method can extract richer semantic information, allowing the model to more accurately identify objects of different scales.
[0117] ②Head module
[0118] The Head module is mainly responsible for the final regression prediction, that is, using the feature maps extracted by the Backbone and Neck parts to detect the location and category of the target and perform semantic segmentation. In YOLOv5s-seg-master, the Head part introduces a semantic segmentation head to convert the feature map into pixel-level semantic segmentation results.
[0119] ③Evaluation indicators
[0120] For instance segmentation tasks, new evaluation indicators are introduced to evaluate the performance of the model. In addition to traditional object detection evaluation indicators (such as mAP), evaluation indicators of pixel-level segmentation accuracy (such as mIoU) are also included to comprehensively evaluate the performance of the model on instance segmentation tasks.
[0121] (6) Improved YoLov5s-seg-master model
[0122] The YoLov5s-seg-master algorithm is used for instance segmentation to identify the strawberry stems, and the Head module is modified so that the output image only contains the stems, and the rest is set to a black background.
[0123] The standard YOLOv5s-seg-master usually outputs an image with an instance segmentation mask. In order to obtain a binary image in the experiment, which is convenient for the final shape calculation and picking point positioning, the Head module is modified so that its output is required by the experiment. The specific modifications to the Head module are as follows: create an all-zero array black_background with the same shape as the original image, which is used as a black background; in the loop that processes the prediction results, for each detected object mask, resize it to match the image size and apply it to black_background; set save_img to save or display the black_background image with the drawn mask instead of the original image. This modification enables the result to show the instance segmentation image more clearly, so that all masks are drawn on a uniform black background without being disturbed by other details in the original image. Therefore, this is an improvement for specific application scenarios or display needs to facilitate the subsequent stem shape calculation and picking point positioning.
[0124] After the model training is completed, we modify the Head module. The purpose of the modification is to retain only the strawberry stem part identified by the algorithm in the output image, and set the rest of the image to a black background. Figure 3 , the trained YoLov5s-seg-master has a recognition rate of up to 95% for the stems of ripe strawberries.
[0125] Canny edge detection is introduced in the Head module, and the processed instance segmentation output image becomes an image with only the outline of the stem.
[0126] First, we need to prepare instance segmentation images processed by the YoLov5s-seg-master algorithm. These images have already identified and segmented the strawberry stems, but there may still be interference information in the background. Our goal is to further clean up the image through Canny edge detection and keep only the outline of the stem, such as Figure 4-Figure 5 shown.
[0127] The following are the detailed steps for Canny edge detection using OpenCV library:
[0128] Define the Canny edge detection function: Create a function called Canny_demo that accepts the source image folder and the target output folder as parameters.
[0129] Check and create the destination folder: Inside the function, first check if the destination folder exists, if not, create it.
[0130] Iterate over the images in the source folder: Use os.listdir to iterate over all the files in the source folder and check if each file is in image format.
[0131] Reading and preprocessing images: For each image file, read the image using cv2.imread and convert it from BGR color space to RGB using cv2.cvtColor and then to grayscale image.
[0132] Apply Gaussian Blur: Applies Gaussian blur to the grayscale image to reduce image noise in preparation for Canny edge detection.
[0133] Perform Canny edge detection: Use cv2.Canny function to perform Canny edge detection on the blurred image and set appropriate threshold to extract the edges.
[0134] Save processed image: Save the image after Canny edge detection to the target folder and record the processing information.
[0135] Calling the function: Finally, define the path of the source and destination folders and call the Canny_demo function to process the image.
[0136] Through the above steps, we can get a series of images containing only the outline of the strawberry stem, with the background set to black and the edge of the stem clearly visible. These images can be used directly for further analysis, such as counting, measuring the width or length of the stem, etc., or as input data for machine learning models to achieve more advanced image analysis tasks.
[0137] (7) Calculation of stem approximate rectangular features
[0138] Detect contours in the image, perform polygonal approximation on each edge detected contour, and determine the features of the stem approximation rectangle: center, width, and height. Then calculate the minimum area and center point of the rectangle, and finally draw circles on a black background to represent these center points.
[0139] In the field of image processing and computer vision, contour detection is a key step in identifying and analyzing objects in an image. This step aims to use the cv2.approxPolyDP function in the OpenCV library to perform polygonal approximation on the contours in the image and calculate the minimum area rectangle and center point of the approximated contour. Ultimately, we will draw circles on a black background to represent these center points to achieve accurate positioning and measurement of specific objects (strawberry stems) in the image.
[0140] Here are the detailed steps:
[0141] Define the processing function: Create a function called rectangle_approximation that accepts the source image folder and the target output folder as parameters.
[0142] Check and create the destination folder: Inside the function, first check if the destination folder exists, if not, create it.
[0143] Iterate over the images in the source folder: Use os.listdir to iterate over all the files in the source folder and check if each file is in image format.
[0144] Read image and convert to grayscale: For each image file, read the image using cv2.imread and convert it to grayscale image using cv2.cvtColor.
[0145] Detect image contours: Use cv2.findContours function to detect contours in grayscale images.
[0146] Create Black Background Image: Creates a completely black background image with the same size as the original image for each image.
[0147] Contour polygon approximation: Traverse the detected contours and use the cv2.approxPolyDP function to perform polygon approximation on each contour.
[0148] Calculate the minimum area rectangle: Use cv2.minAreaRect to calculate the minimum area rectangle of the approximate polygon.
[0149] Determine rectangle center and width: Extract the width, height, and center point from the minimum area rectangle.
[0150] Draw a circle on a black background: On a black background, draw a blue solid circle with the center of the rectangle as the center and half the width as the radius.
[0151] Save the processed image: Save the image with the circles drawn on the black background to the destination folder.
[0152] By running this code, we can get a series of images with blue circles drawn on a black background, such as Figure 6 As shown in the figure, each circle represents the center point of a detected strawberry stem. These images can be directly used for further analysis, such as calculating the distribution and size of the stem. At the same time, the detailed information of the stem can also be recorded and saved to provide data support for scientific research or agricultural applications.
[0153] (8) Calculation of x and y coordinates of the picking point of the ripe strawberry stem
[0154] Perform image masking, overlay blue solid circles on the original image, and mark the coordinates of the center of each circle to determine the picking point.
[0155] Determining the picking point is a key task. This step uses image processing technology combined with the OpenCV library to overlay blue solid circles on the original image and mark the coordinates of the center of each circle to determine the picking point of the strawberry stem.
[0156] Here are the detailed steps:
[0157] Define the mask and overlay functions: Create a function called
[0158] The function mask_and_overlay_circles_with_coordinates_above accepts the original image folder, the circle image folder, and the target output folder as parameters.
[0159] Check and create the destination folder: Inside the function, first check if the destination folder exists, if not, create it.
[0160] Iterate through the image folders: Use os.listdir to iterate through all the files in both the original image folder and the circle image folder.
[0161] Read the image: For each image file, use cv2.imread to read the original strawberry image and the corresponding blue solid circle image.
[0162] Create a mask: Use cv2.inRange to create a mask that contains only the pixels of the blue solid circle. Then use cv2.dilate to perform morphological dilation to fill the small holes in the mask.
[0163] Extract foreground and background: Use the mask and the complement of the mask to extract the background to be covered by the circle and the foreground of the circle from the original image, respectively.
[0164] Overlay Circle: Overlay the extracted blue circle foreground onto the background of the original image.
[0165] Mark the center coordinates of the circle: Use cv2.findContours and cv2.moments to find the centers of all circles and calculate their coordinates. Then mark the coordinates of the center of each circle, such as Figure 7 shown.
[0166] Save the result image: Save the processed image to the target folder and record the processing information. The coordinates of the target sample strawberry in this embodiment are shown in Table 1:
[0167] Table 1
[0168]
[0169] This paper compares several algorithms for identifying strawberry stem features to prove the superiority of the basic algorithm of the present invention in locating the picking point of ripe strawberry fruits. The comparison results are shown in Table 2:
[0170] Table 2
[0171]
[0172] The above description does not limit the present invention in any form. According to the technical essence of the present invention, within the spirit and principles of the present invention, any simple modification, equivalent replacement and improvement made to the above embodiments shall still fall within the protection scope of the technical solution of the present invention.
Claims
1. A method for locating the picking point of mature strawberry fruits with stems, characterized in that: The method comprises the following steps: S1 collects an image set of strawberry plants at maturity, annotates the red ripe strawberry fruits with stems in the strawberry plant images, and packages them with the original images to create a dataset; S2 uses YoLov5s model to train and verify target recognition of ripe strawberry fruits with stems; S2.1 Modify the Head module of the YoLov5s model to enhance the visualization of detecting ripe strawberries with stems; S2.2 outputs a target image containing only ripe strawberry fruits and corresponding stems through the modified YoLov5s model; S3 uses the labelme tool to annotate the output strawberry images with stems, convert and package them, and create a dataset; S4 improved YoLov5s-seg-master model; S4.1 creates an all-zero black background array in the Head module, adjusts and applies the prediction mask to obtain a binary image, and introduces Canny edge detection; S4.2 uses the data set obtained in S3 to train the improved YoLov5s-seg-master model, obtains the final model for instance segmentation, identifies strawberry stems, and obtains an image set containing only stems; S5 calculates the approximate rectangular features of the fruit stem image set, applies an image mask to cover and mark the center coordinates to determine the coordinates of the picking point.
2. The method for locating the picking point of mature strawberry fruits with stems according to claim 1, characterized in that: The Yolov5 model described in S2 makes adaptive changes to the three parts of Input, Backbone, Neck and Head for the recognition of ripe strawberries with stems. The specific method is as follows: Input part: Change the image size from 640x640 to 320x320, modify the input image size in the model configuration file, and retrain or fine-tune the model to adapt to the new input size; Backbone part: modify the YOLOv5s configuration file, reduce the number of channels of the YOLOv5s model by half, and retrain or fine-tune the model to adapt to the reduction in the number of channels; Head part: Modify the anchor box size in the configuration file and reduce the number of output channels of the prediction layer to the corresponding number of categories.
3. The method for locating the picking point of mature strawberry fruits with stems according to claim 2, characterized in that: Adaptive changes also include: improving the SPP pyramid pooling structure to SPPF. SPPF optimizes the pooling operation, maintaining the multi-scale feature fusion capability similar to SPP while reducing the amount of calculation. SPPF is applied to Backbone to enhance the multi-scale characteristics of the feature extraction stage.
4. The method for locating the picking point of mature strawberry fruits with stems according to claim 1 or 2, characterized in that: The specific modification steps for the Head module to enhance the visual representation of detecting ripe strawberries with stems are as follows: Object detection: Use the trained YoLov5s model to process the input image, identify the strawberries in the image, and draw a detection box; Content extraction: For each detection box, the strawberry part will be retained and the background outside the box will be ignored; Background processing: Create a black image of the same size as the original image, and then paste the extracted strawberry content onto this black background.
5. The method for locating the picking point of mature strawberry fruits with stems according to claim 1 or 5, characterized in that: The detailed steps of S4.1 are: Create an all-zero array black_background with the same shape as the original image to be used as a black background; in the loop that processes the prediction results, for each detected object mask, resize it to match the image size and apply it to black_background; set save_img to save or display the black_background image with the drawn mask to facilitate stem shape calculation and picking point positioning.
6. The method for locating the picking point of mature strawberry fruits with stems according to claim 6, characterized in that: The detailed steps for introducing Canny edge detection in the Head module are: Define the Canny edge detection function: Create a function called Canny_demo that accepts the source image folder and the target output folder as parameters; Check and create the destination folder: Inside the function, first check if the destination folder exists, if not, create it; Traverse the images in the source folder: Use os.listdir to traverse all the files in the source folder and check whether each file is in image format; Read and preprocess images: For each image file, use cv2.imread to read the image and use cv2.cvtColor to convert it from BGR color space to RGB and then to grayscale image; Apply Gaussian blur: Apply Gaussian blur to the grayscale image to reduce image noise in preparation for Canny edge detection; Perform Canny edge detection: Use cv2.Canny function to perform Canny edge detection on the blurred image and set appropriate thresholds to extract edges; Save the processed image: Save the image after Canny edge detection to the target folder and record the processing information; Calling the function: Finally, define the path of the source and destination folders and call the Canny_demo function to process the image.
7. The method for locating the picking point of mature strawberry fruits with stems according to claim 1, characterized in that: The calculation steps of the approximate rectangular feature of the S5 stem are as follows: Define the processing function: Create a function called rectangle_approximation that accepts the source image folder and the target output folder as parameters; Check and create the destination folder: Inside the function, first check if the destination folder exists, if not, create it; Traverse the images in the source folder: Use os.listdir to traverse all the files in the source folder and check whether each file is in image format; Read image and convert to grayscale: For each image file, use cv2.imread to read the image and use cv2.cvtColor to convert it to grayscale image; Detect image contours: Use cv2.findContours function to detect contours in grayscale images; Create a black background image: Create a completely black background image with the same size as the original image for each image; Contour polygon approximation: traverse the detected contours and use the cv2.approxPolyDP function to perform polygon approximation on each contour; Calculate the minimum area rectangle: Use cv2.minAreaRect to calculate the minimum area rectangle of the approximate polygon; Determine the center and width of the rectangle: Extract the width, height and center point from the minimum area rectangle; Draw a circle on a black background: On a black background, draw a blue solid circle with the center of the rectangle as the center and half the width as the radius; Save the processed image: Save the image with the circles drawn on the black background to the destination folder.
8. The method for locating the picking point of mature strawberry fruits with stems according to claim 1, characterized in that: The method of S5 applying the image mask to cover and mark the center coordinates to determine the coordinates of the picking point is as follows: Define the mask and overlay functions: Create a function called mask_and_overlay_circles_with_coordinates_above that accepts the original image folder, the circle image folder, and the target output folder as parameters; Check and create the destination folder: Inside the function, first check if the destination folder exists, if not, create it; Traverse the image folder: Use os.listdir to traverse all the files in the original image folder and the circle image folder; Read the image: For each image file, use cv2.imread to read the original strawberry image and the corresponding blue solid circle image; Create a mask: Use cv2.inRange to create a mask that only contains the pixels of the blue solid circle, and use cv2.dilate to perform morphological dilation to fill the small holes in the mask; Extract foreground and background: Use the mask and the complement of the mask to extract the background to be covered by the circle and the foreground of the circle from the original image respectively; Cover circle: Cover the extracted blue circle foreground onto the background of the original image; Mark the coordinates of the center of the circle: Use cv2.findContours and cv2.moments to find the centers of all circles and calculate their coordinates. Mark the coordinates of the center of each circle. Save result image: Save the processed image to the target folder and record the processing information.
Citation Information
Patent Citations
Plant and fruit detection algorithm of tomato picking robot
CN115984704A
Cited By
Picking point positioning method and device based on image fusion, medium and equipment
CN121190572A
Methods, apparatus, media, and equipment for picking point localization based on image fusion
CN121190572B
