# Classification Models for Nitrogen Concentration in Hop Leaves Using Digital Image Processing
## 2 Materials and Methods
### Image Processing  
The images were processed in the R computational environment, version 4.2.2 [28], using the pliman package (version 2.2.0) [29] to isolate the leaf area of hop plants and extract information through techniques such as enhancement, outlining, filtering, manipulation, and segmentation. The images were analyzed in terms of RGB color components, including pixel histograms, color matrices, and vegetative indices of interest. Thirteen quantitative variables were obtained: leaf area (A), perimeter (P), maximum and minimum diameters (Dmax and Dmin), mean values of red (R), green (G), and blue (B) color matrices, mean values of normalized color matrices (NR, NG, and NB), as well as the green-red ratio index (GRRI), modified photochemical reflectance index (MPRI), and percentage of yellow-brown area (YBA). The equations for obtaining the NR, NG, NB, GRRI, and MPRI indexes are presented in Equations (1)-(5), respectively.  
Figure 1: Images of leaves allocated to group 1 (**A**) and group 2 (**B**) obtained using a mobile phone, and leaves allocated to group 1 (**C**) and group 2 (**D**), obtained using a scanner.  
\[\text{NR} =\frac{\text{R}}{\text{R}+\text{G}+\text{B}} \tag{1}\] \[\text{NG} =\frac{\text{B}}{\text{R}+\text{G}+\text{B}}\] (2) \[\text{NB} =\frac{\text{G}}{\text{R}+\text{G}+\text{B}}\] (3) \[\text{GRRI} =\frac{\text{G}}{\text{R}}\] (4) \[\text{MPRI} =\frac{\text{R}-\text{G}}{\text{R}+\text{G}} \tag{5}\]  
The MPRI variable normalizes the distance between the R and G colors within a range from \(-1\) to \(1\). A value of \(-1\) represents a completely green leaf devoid of red, while a value of \(1\) represents a leaf that is absent of green and completely red.  
To calculate the percentage of yellow-brown leaf area, an adaptation of the measure_disease command from the pliman package was used. This function was originally developed to identify damaged areas in diseased leaves. A reference color palette was created for the green part of the leaf (Figure 2A), the yellow-brown region (Figure 2B), and the background of the image (Figure 2C).  
In the pliman package, the analyze_objects command was used to isolate the hop leaf and the reference square as distinct objects, allowing for the extraction of values for the variables A, P, Dmax, Dmin, R, G, B, NR, NG, NB, GRRI, and MPRI. For images obtained using a mobile phone (low resolution), the arguments index = \"G + R - B\", tolerance = 900, and invert = TRUE, were empirically defined as the optimal configuration for leaf segmentation and image processing performance, proving to be suitable for most images.  
However, in four cases, leaf recognition was inadequate, so it was necessary to define the additional filter and threshold arguments individually. The filter argument applies a median filter of a specific size to remove noise from the image, while threshold sets the cutoff value for converting grayscale images into binary images.  
The index argument defines the operation on color matrices that will be the basis for conversion into binary images, and the tolerance argument specifies a minimum object height for recognition. Finally, the invert argument was used to reverse the binary image, which was necessary for mobile phone images due to the black background.  
For images obtained using a scanner (high-resolution images), the arguments used were filter = 10, index = \"B\", tolerance = 900, and threshold = 0.6. The results were stored in two xlsx files: one for mobile phone images and another for scanner images. In these files, each row corresponds to an image, with the first column listing the file name, the last column indicating the group classification according to the nutritional analysis of the nitrogen concentration (group 1 or group 2), and the remaining columns containing the values of the evaluated variables.  
Figure 2: Reference palettes of the green part (**A**), yellow–brown region (**B**), and background (**C**).