From pixels to measurements: segmentation, features and detection.
Overview
Computer vision turns images into numbers: which pixels are plant, how big is a fruit, where is a weed. In agriculture most tasks are one of three kinds: segmentation (mark the pixels of interest), classification (name what is in an image) and detection (find and locate objects).
Key concepts
Colour spaces
RGB mixes colour and brightness. HSV separates hue from brightness, which helps for fruit ripeness, but it is not immune to light: in our study the detected size of ripe tomatoes shrank below about 3,000 lx.
Vegetation indices
Simple band arithmetic such as Excess Green (ExG = 2g − r − b on chromatic coordinates) separates green plants from soil with almost no training data.
Feature-based vs deep learning
Hand-made features (shape, colour histogram, texture) plus a classical classifier need few images; CNNs need thousands but learn the features themselves.
Evaluation
Report precision, recall and F1 on images that were not used for training, and look at the confusion matrix, not only one number.
Code examples
Segmenting green plants from soil with the Excess Green index and Otsu thresholding
import cv2, numpy as np
img = cv2.imread("field.jpg").astype(np.float32)
b, g, r = cv2.split(img / img.sum(axis=2, keepdims=True).clip(1e-6)) # chromatic coordinates
exg = 2 * g - r - b # Excess Green index
exg8 = cv2.normalize(exg, None, 0, 255, cv2.NORM_MINMAX).astype(np.uint8)
_, mask = cv2.threshold(exg8, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
cv2.imwrite("plant_mask.png", mask)
Detecting ripe (red) pixels in HSV colour space
import cv2
hsv = cv2.cvtColor(cv2.imread("tomato.jpg"), cv2.COLOR_BGR2HSV)
# red wraps around hue 0, so two ranges; thresholds are examples, tune them on your own images
mask = cv2.inRange(hsv, (0, 100, 60), (10, 255, 255)) | cv2.inRange(hsv, (170, 100, 60), (180, 255, 255))
print("ripe pixels:", int((mask > 0).sum()))
Case study
Case studies from my work: how few images a feature-based classifier needs, and how illumination changes colour-based tomato detection.
References from my work
2024Q2
Weed Detection and Classification with Computer Vision Using a Limited Image Dataset
L. Moldvai, P. Á. Mesterházi, G. Teschner, A. Nyéki
Applied Sciences 2024, 14(11), 4839
Abstract
How few training images are enough? Traditional feature-based computer vision (shape, distance-transform, colour-histogram and texture features) with six classifiers was tested on 5–160 images per weed type in a four-class problem. With 160 images per class a 94.56 % recall was reached, close to deep-learning accuracy with a tenth of the data.
The Effect of Illumination on HSV Colour Segmentation for Ripe Tomatoes based on Machine Vision
B. Ambrus, A. Nyéki, G. Teschner, L. Moldvai
Chemical Engineering Transactions 2024, 114, 829–834
Abstract
Ripe-tomato detection by HSV colour segmentation was measured under 15 artificial illumination levels. Results are stable at or above about 3,000 lx; below that the detected fruit size shrinks, and near 1,600 lx only about half of the real size is measured, so controlled lighting is critical for precise measurement.
Innovative computer vision methods for tomato (Solanum lycopersicum) detection and cultivation: a review
L. Moldvai, A. Nyéki
Discover Applied Sciences 2025, 7, 975
Abstract
A review of recent machine vision, deep learning and AI work in tomato production: fruit classification, counting and yield estimation, plant health monitoring (weeds, pests, leaf diseases, fruit disorders), vehicle navigation and harvesting robots, with a focus on the image processing algorithms of the last two years.