# HSV Color-Space-Based Automated Object Localization for Robot Grasping without Prior Knowledge
## 4 HSV Color-Space-Based Automated Object Feature Extraction
### HSV Color-Space-Based Automated Object Localization Algorithm
#### 4.2.2 HSV Color-Based ROI Random Sampling and HSV Color Similarity Comparison  
In this study, we propose an HSV color-space-based ROI random sampling and HSV color similarity algorithm to automatically recognize and accurately reflect the shape features of objects that are affected by lighting conditions, shadows, reflections, etc. ROI random sampling is used to automatically recognize the shape of an object from complex and volatile external environment changes such as shadows, reflections, or lighting interferences, etc. in factories. ROI random sampling is advantageous for accurate positioning and recognition of an object because it responds quickly and easily to some changes in the lighting environment without any human intervention or prior learning. HSV color similarity can be defined as the extent of color matching with the reference ROI by calculating the distances for H, S, and V between the colors of the randomly retrieved ROI and reference ROI. The definition of the similarity threshold means the boundary value of whether the HSV color similarity matches. Algorithm 2 presents the algorithm of the HSV color-based ROI random sampling and similarity. The ROI\({}_{\text{sampling\_max}}\) variable is the ROI max count of the ROI random sampling. ROI\({}_{\text{sampling\_i}}\) represents the initial value of the number of sampling ROIs. Similarity threshold refers to the threshold of HSV color similarity. The ROI\({}_{\text{Templist}}\) variable is a temporary array list that registers random sampling ROIs with high HSV color similarity, compared to the reference ROI ROI random sampling randomly searches for the ROI to detect the location of the product in various places inside the 3D printer from the maximum ROI. ROI random sampling extracts the position values of the searched random ROI. Then, the RGB of the random ROI is converted into an HSV, and the HSV value is extracted. ```
1:Start ROI Random Sampling and Similarity Comparison in ROI\({}_{\text{max\_region}}\)
2:Set ROI\({}_{\text{sampling\_max}}\) = Max\({}_{\text{sample\_count}}\)
3:For ROI\({}_{\text{sampling\_i}}\) = 0: ROI\({}_{\text{sampling\_max}}\)
4: Extract Random ROI\({}_{\text{sampling\_i}}\) location info from bounding box (x\({}_{\text{start}}\), y\({}_{\text{start}}\), x\({}_{\text{end}}\), y\({}_{\text{end}}\))
5:RO\({}_{\text{sampling\_i}}\) \(\rightarrow\) Transformation from RGB to HSV through Equations (4)-(6)
6:Picking automated HSV Color Extraction (HSV values) from ROI\({}_{\text{sampling\_i}}\)
7:Compute HSV Color Similarity for ROI\({}_{\text{ref\_region}}\) between ROI\({}_{\text{sampling\_i}}\)
8: Retrieve HSV Color Values \(\rightarrow\) (H1, S1, V1 of ROI\({}_{\text{ref\_region}}\) and H0, S0, V0 ROI\({}_{\text{sampling\_i}}\))
9:Compute HSV Color Distance by Equation (7)
10:IF(Similarity\({}_{\text{threshold}}\) > Distance) then
11: Set HSV Upper(max) and Low(min) bound threshold from ROI\({}_{\text{sampling\_i}}\)
12: Add HSV value, Upper/Lower bound threshold of ROI\({}_{\text{sampling\_i}}\) in ROI\({}_{\text{Templist}}\)
13:Else
14:RO\({}_{\text{sampling\_i}}\) = ROI\({}_{\text{sampling\_i}}\) + 1
15:IF(ROI\({}_{\text{sampling\_i}}\) > Max\({}_{\text{sample\_count}}\)) then
16: Action ROI\({}_{\text{Templist}}\) Merge Operation
17:Else
18:Continue:
19:End
```  
**Algorithm 2** ROI Random Sampling and Similarity Comparison  
The HSV color similarity compares the similarity between the HSV color of the reference ROI and the searched HSV color value of the random ROI. HSV color similarity is used to accurately classify product shape (position, color) as the HSV value of a random ROI can be a shape property inside the object or a background property outside the object shape due to changes in the external environment (lighting condition, reflection, illumination, etc.). The HSV color similarity algorithm compares the HSV value of the reference ROI with the HSV color value of the random ROI and stores only the HSV value of the random ROI closest to the distance.