This repository contains a Python script designed as a computer vision pipeline to measure the total area of leaves and count/measure distinct red target objects present in a set of images.
The script leverages the OpenCV library for robust image processing and color-based segmentation, and Pandas for structured data storage and analysis. It processes images sequentially, isolates the desired objects (leaves and targets), calculates their real-world areas in square centimeters (
To run the analysis script, you will need:
- Python (3.x)
- The following Python libraries:
-
opencv-python(cv2) numpypandasmatplotlibpathlib
-
- A global constant
IMAGE_FILES_PATH(the file path to the directory containing your input images). - A global constant
A3_SHEET_AREA(the known real-world area, in$\text{cm}^2$ , of the background sheet used for scaling—e.g.,$623.7 \text{ cm}^2$ for an A3 sheet).
It is important to keep the leaves separated during the scanning so that the system can isolate them (if they are overlapped the system sees these leaves as one).
The core script executes a recursive process over all .jpg files in the input folder:
- Color Space: Images are converted from BGR to the HSV (Hue, Saturation, Value) color space, which is ideal for isolating colors regardless of lighting variations.
- Spatial Resolution: A conversion factor (
pixel_spatial_res_cm) is calculated using the knownA3_SHEET_AREAto accurately translate pixel areas into real-world square centimeters.
The script uses predefined HSV ranges to create binary masks that isolate key image components:
- Red Targets: Identified using dual HSV ranges to account for the wrap-around nature of the red color on the Hue scale. In this version the red square has known area and it is used to check the system error level.
- White Background: Isolated using a high Value and low Saturation range.
- Leaves (Foreground): Created by combining the Red Target and White Background masks into a single Background Mask, and then inverting it to find