[R package] Segment and Measure Colored Objects in Images (Feat. colorcapture)
In my previous post, I introduced the R code I developed to capture green color. This updated R function, colorcapture(), replaces the earlier greencapture().
1. Install the pacakge
Before installing colorcapture(), please download Rtools (https://cran.r-project.org/bin/windows/Rtools), and install the following package.
□ colorcapture()
if(!require(remotes)) install.packages("remotes")
if (!requireNamespace("colorcapture", quietly = TRUE)) {
remotes::install_github("agronomy4future/colorcapture", force= TRUE)
}
library(remotes)
library(colorcapture)
2. Basic code
colorcapture(
input_folder= r"(C:/Users/agron/Desktop/Coding_Output)", # designate a folder for saved images
output_folder= r"(C:/Users/agron/Desktop/Coding_Output/output)", # designate a folder for output images
image_real_cm= c(24, 24) # set up the actual frame size
)
If you want to change other criteria, type ?colorcapture to see detailed information about the colorcapture() function.

The default code is set to capture the yellow color. If you run the code, it will detect the yellow surface area. The frame size was 24 cm × 24 cm.
colorcapture(
input_folder= r"(C:/Users/agron/Desktop/Coding_Output)", # designate a folder for saved images
output_folder= r"(C:/Users/agron/Desktop/Coding_Output/output)", # designate a folder for output images
image_real_cm= c(24, 24) # set up the actual frame size
)

and the image will be output as shown below.

This code also outputs the area, length, and width data as a .csv file.


■ Capturing green pepper
Even though the default code is configured to capture yellow, we can modify the HSV range. To determine the appropriate range, I developed the hsv4opencv() package.
if(!require(remotes)) install.packages("remotes")
if (!requireNamespace("hsv4opencv", quietly = TRUE)) {
remotes::install_github("agronomy4future/hsv4opencv", force= TRUE)
}
library(remotes)
library(hsv4opencv)
#
hsv4opencv(lower= c(30, 25, 30), upper= c(90, 255, 255))

Let’s test this HSV range.
colorcapture(
input_folder= r"(C:/Users/agron/Desktop/Coding_Output)",
output_folder= r"(C:/Users/agron/Desktop/Coding_Output/output)",
lower_hsv= c(30L, 25L, 30L),
upper_hsv= c(90L, 255L, 255L),
image_real_cm= c(20, 20)
)


■ Capturing potato tubers
Now, let’s capture the potato tubers. Their color ranges from dark black to reddish. Let’s determine the optimal HSV range.
if(!require(remotes)) install.packages("remotes")
if (!requireNamespace("hsv4opencv", quietly = TRUE)) {
remotes::install_github("agronomy4future/hsv4opencv", force= TRUE)
}
library(remotes)
library(hsv4opencv)
#
hsv4opencv(lower= c(0, 50, 50), upper= c(22, 255, 255))

if(!require(remotes)) install.packages("remotes")
if (!requireNamespace("colorcapture", quietly = TRUE)) {
remotes::install_github("agronomy4future/colorcapture", force= TRUE)
}
library(remotes)
library(colorcapture)
colorcapture(
input_folder= r"(C:/Users/agron/Desktop/Coding_Output)", # designate a folder for saved images
output_folder= r"(C:/Users/agron/Desktop/Coding_Output/output)", # designate a folder for output images
image_real_cm = c(20, 20),
# HSV segmentation
lower_hsv = c(0L, 50L, 50L),
upper_hsv = c(22L, 255L, 255L),
# morphological cleaning
k_open = c(7L, 7L), # stronger open to remove small soil specks
k_close = c(7L, 7L), # smoother potato outlines
# object filtering
min_component_area_px = 3000L, # reject very small connected components
object_min_area_cm2 = 2, # minimum realistic potato size
rel_min_frac_of_largest = 0.08, # keep objects >8% of largest
max_keep = 10L # upto 10 objects can be detected
)


We aim to develop open-source code for agronomy ([email protected])
© 2022 – 2025 https://agronomy4future.com – All Rights Reserved.
Last Updated: 12/Oct/2025