# Weed Detection and Classification with Computer Vision Using a Limited Image Dataset
## 2 Materials and Methods
### Filtering the Images  
Due to the presence of numerous distractors in the images, the initial step involved cleaning and preprocessing the images. It was essential to identify the main crop and eliminate any background clutter, as well as smaller or overlapping weeds. The pixel-level noise had to be removed first. In [17], classification experiments were conducted with two types of weeds, measuring noise variance from no noise to 0.05 sigma noise across six steps. Their results showed that without noise, the classification accuracy was 98.4% without noise filtering and 98.29% with noise mean filtering, indicating nearly identical performance. However, even a small amount of noise, such as 0.01 sigma, reduced classification accuracy to 91.32% without noise filtering and 96.23% with noise reduction, highlighting the significant impact of noise on performance. Furthermore, with larger noise levels, such as 0.05 sigma, classification accuracy decreased to 87.61% without filtering and increased to 92.85% with filtering, demonstrating a consistent 5% difference attributable to noise filtering. Various methods, including blur, median, and Gaussian filters, are commonly employed to address noise in images, albeit at the cost of slight image blurring. Figure 1: The 6 classes: (**a**) bluegrass sample; (**b**) white goosefoot sample; (**c**) lettuce sample; (**d**) sedge sample; (**e**) corn sample; (**f**) Cirsium setosum sample. To remove pixel-level noise from the image, first, a 5 \(\times\) 5 Gaussian filter was employed. Given that the RGB color space may not be optimal for emphasizing green areas, the images were converted to the HSV color space. In the HSV image, the green areas were isolated, and all other pixels were set to black. The green color was defined by a range in HSV color space: lower_green = (30, 40, 40) and upper_green = (80, 255, 255). This process created a black-and-white mask highlighting the green regions. Figure 2b shows the mask, and it contains many other parts that are related to different plants. Subsequently, clusters of connected pixels within this mask were identified. Each cluster has a different color, as shown in Figure 2c. Figure 2: Cont. From the identified clusters, the largest one was selected as the object of interest for further analysis. In this workflow, the focus was exclusively on this object. However, it was also plausible to consider the second largest object, followed by subsequent ones, for classification purposes. This approach allowed for a more comprehensive analysis, leveraging multiple objects within the image for classification tasks. Based on the largest cluster, the region of interest was extracted from the image and stored for later use (Figure 2d). Additionally, this region was resized to a 256 \(\times\) 256-pixel image. The shape parameters of the plant were analyzed on this resized image (Figure 2e,f). After creating the mask from the image, small black spots may appear on the plant, potentially due to diseases, soil residue, or dried leaf fragments. It is possible to close these spots using erosion and dilation techniques, an important step for contour analysis. In Figure 3, the main steps of image processing are depicted. The process began with an RGB image, which is shown in the top left corner. The Gaussian filter was applied to eliminate pixel-based noise. Subsequently, the image was transformed into the HSV color space to facilitate the extraction of the green mask, using the range previously specified. These image masks encompassed all green components, not just the primary object. The connected pixels were calculated, and an artificial coloring image was generated. The largest connected area was selected, and its color was used as a mask to isolate a single plant. In our dataset, these steps were enough to locate and extract a single plant. Occasionally, other small plants might adhere to the mask, but their proportion relative to the larger plant was minimal, causing no significant issues.