Automated Image Sorting System Design
Automated Image Sorting System Design
SEMESTER 251
i
ABSTRACT
In this document, the design and implementation of the automated image processing-based
sorting system would be elaborated with a view to offering a cost-effective as well as scalable
solution for Small Medium Enterprises (SMEs). In this system, a Decoupled Master-Slave control
system would be employed. Additionally, a computer vision application would be used as the
“Master,” whereas an Arduino Uno R3 would be used as the “Slave.”
To overcome issues associated with the RGB color space in situations where lighting is
not controlled, the system converts the color space into Hue-Saturation-Value (HSV) space. This
is also achieved in order to segregate chromaticity from lightness. Secondly, Centroid Tracking
algorithms are used in order to automatically avoid double-counting, while Bucket Sorting algo-
rithms are used in order to schedule asynchronous hardware detection triggers into well-defined
time slots.
Therefore, this technique is an efficient electromechanical system. It is capable of clas-
sifying and sorting items into certain categories while also increasing processing needs with its
design.
ii
TABLE OF CONTENTS
1 Introduction 1
2 Objectives 2
3 Theoretical Construction 2
3.1 The Physics of Color Perception in Machine Vision . . . . . . . . . . . . . . . . . 2
3.2 Mathematical Derivation of Color Space Transformation . . . . . . . . . . . . . . 3
3.3 Morphological Image Processing Theory . . . . . . . . . . . . . . . . . . . . . . . 4
3.3.1 Dilation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
3.3.2 Erosion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
3.4 Master-Slave Control Topology . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
3.5 Bucket Sorting Algorithm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
3.6 Centroid Tracking . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
3.7 Integration of Centroid Tracking with Bucket Sorting . . . . . . . . . . . . . . . . 7
4 Methodology 7
4.1 System Architecture Strategy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
4.2 Image Processing Algorithm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
4.2.1 Computer Vision Algorithm . . . . . . . . . . . . . . . . . . . . . . . . . 8
4.2.2 The Segmentation Pipeline . . . . . . . . . . . . . . . . . . . . . . . . . . 8
4.3 Protocol for Communication . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
4.4 Software Algorithm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
4.4.1 Data Storage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
4.4.2 Double Counting Prevention Using Centroid Tracking . . . . . . . . . . . 10
4.4.3 Bucket Sorting Algorithm Implementation . . . . . . . . . . . . . . . . . 11
5 System Design 13
5.1 System Architecture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
5.1.1 Microcontroller . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
iii
5.1.2 Vision Sensor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
5.1.3 Circuit Construction Components . . . . . . . . . . . . . . . . . . . . . . 14
5.1.4 Power Supply . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
5.1.5 Actuators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
5.1.6 Mechanical Structure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
6 Implementation 16
6.1 General Mechanical System Design . . . . . . . . . . . . . . . . . . . . . . . . . 16
6.2 Power Management and Isolation . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
6.3 Sensor Configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
6.4 Hardware Implementation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
6.5 Circuit Topology and Interfacing . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
6.5.1 Actuator Interfacing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
6.5.2 Signal Integrity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
6.6 Mechanical Configuration and Environment . . . . . . . . . . . . . . . . . . . . . 19
6.7 Software Implementation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
7 Technical Challenges 20
7.1 Environmental and Optical Constraints . . . . . . . . . . . . . . . . . . . . . . . . 20
7.2 Computational and AI Model Limitations . . . . . . . . . . . . . . . . . . . . . . 21
7.3 Real-Time Processing and Latency Constraints . . . . . . . . . . . . . . . . . . . 22
7.3.1 The Latency Budget . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
7.3.2 Hardware Bottlenecks on Edge Devices . . . . . . . . . . . . . . . . . . . 24
7.4 Mechanical and Actuation Synchronization . . . . . . . . . . . . . . . . . . . . . 24
7.4.1 Actuation Timing and Mechanical Response . . . . . . . . . . . . . . . . 24
7.4.2 Conveyor Belt Dynamics and Vibration . . . . . . . . . . . . . . . . . . . 25
7.4.3 Spatial Variance and Reachability . . . . . . . . . . . . . . . . . . . . . . 26
8 Evaluation 26
8.1 System Features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
8.2 Economic Impact . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
iv
8.3 Environmental Impact . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
9 Conclusion 28
10 References 29
v
LIST OF FIGURES
vi
Ho Chi Minh City University of Technology
FACULTY OF ENGINEERING
1 Introduction
1
Ho Chi Minh City University of Technology
FACULTY OF ENGINEERING
2 Objectives
This document provides an in-depth account of a sorting solution that is based on an au-
tomated image processing system with the intention of integrating the theoretical algorithms de-
veloped in computer vision to a practical electromechanical system. This document was created
because small and medium-sized enterprises need a cost-effective, scalable, and enhanced method
of sorting products where the use of high-end industrial sorters is not an option due to their pro-
hibitive costs. [3]
The primary objectives of this analysis are:
3 Theoretical Construction
This chapter will introduce the theoretical background for the design of the automated sort-
ing system. Topics will include color perception theory for machine vision systems, mathematical
transformation of color spaces, morphological processing of images, and the algorithms used for
control and data management during the real-time processing for object classification.
To design the sorting system efficiently, the color accuracy needs to be considered. Digital
imaging typically indicates color through an RGB (Red, Green, Blue) triplet; the RGB colour
2
Ho Chi Minh City University of Technology
FACULTY OF ENGINEERING
space is difficult to use for reliably detecting objects under various lighting situations.
The RGB model is an additive colour model but considers both chromaticity (colour infor-
mation) and luminance (brightness information) combined. In optimal situations, a given object
has an identified colour regardless of the amount of light illuminating it. However, if a red object
were situated in some sort of shadow or low light, the shadow would likely affect the object’s R, G,
and B values so that the colour representation of the object’s pixel may no longer qualify as being
”red,” and this may result in the algorithm failing to identify the pixel as ”red.”
In response to these issues, the proposed colour space within the sorting system will use
a Hue-Saturation-Value (HSV) colour space; this method allows the RGB cube to be converted
to a non-linear cylindrical coordinate system, which successfully separates chromaticity and lu-
minance. Each image is represented in a 3-dimensional matrix by creating a vector of each pixel
p(x, y). [7]
Let R′ , G′ , B′ be the normalized RGB values. We define the following maximums and
deltas:
Cmax = max(R′ , G′ , B′ )
Cmin = min(R′ , G′ , B′ )
∆ = Cmax −Cmin
The Value (V) component, which corresponds to the brightness and is simply the maximum
channel intensity:
V = Cmax
The Saturation (S) component constitutes the purity of the color (spectral width) and is
calculated as:
0, if Cmax = 0
S=
∆ , if C ̸= 0
Cmax max
The Hue (H) component represents the dominant spectral wavelength and is calculated
based on which RGB channel has the maximum value:
0◦ , if ∆ = 0
60◦ × G′ −B′ mod 6 , if Cmax = R′
H= ′∆ ′
◦ B −R
60 ×
+2 , if Cmax = G′
′∆ ′
60◦ × R −G + 4 ,
if Cmax = B′
∆
3
Ho Chi Minh City University of Technology
FACULTY OF ENGINEERING
This mathematical transformation allows the control algorithm to define a target color (e.g.,
Red) as a range of angles on the Hue circle (e.g., H ∈ [0◦ , 20◦ ] ∪ [340◦ , 360◦ ]), while permitting
the Value (V ) to fluctuate significantly. This ensures that the system remains operational even if
the ambient lighting changes from 150 lux to 800 lux, provided the camera sensor is not being
saturated. [8]
Due to the color segmentation process, any binary image that is created often includes
”salt,” which are random pixels because of the thermal effects on sensors, and ”pepper,” the whites-
pace areas between parts of a model due to the presence of a reflection. This noise is cleared up by
the mathematical morphology method used by our system in analyzing a physical model’s struc-
tural and topological properties using math to help identify and partially resolve these issues.
The fundamental operators are Erosion and Dilation. Let A be the binary set representing
the detected object pixels, and B be the structuring element (a small matrix, typically 3 × 3 or
5 × 5). [7]
3.3.1 Dilation
Dilation adds pixels to the boundaries of objects in an image. The value of the output pixel
is the maximum value of all pixels in the neighborhood. In a binary image, a pixel is set to 1 if any
of the neighboring pixels have the value 1. Morphological dilation makes objects more visible and
fills in small holes in objects. Lines appear thicker, and filled shapes appear larger [7].
3.3.2 Erosion
Erosion removes pixels on object boundaries. The value of the output pixel is the minimum
value of all pixels in the neighborhood. In a binary image, a pixel is set to 0 if any of the neighbor-
ing pixels have the value 0. Morphological erosion removes floating pixels and thin lines so that
only substantive objects remain. Remaining lines appear thinner and shapes appear smaller [7].
4
Ho Chi Minh City University of Technology
FACULTY OF ENGINEERING
• Master computer vision (Master): The master handles all non-determinate tasks. The
length of time taken to process images will be determined by the complexity of the scene,
existing background tasks, and USB speed. If the master was responsible for directly con-
trolling the stepper and servo motors, the jitters produced by the delays in image processing
would impart unwanted motion to the machinery.
• Slave microcontroller (Slave): The microcontroller handles all determinate and real-time
tasks. The slave microcontroller executes either a bare-metal firmware loop or Real-Time
Operating System (RTOS) structure for determining the exact timing of the PWM signals
that control the servo motors (e.g., it produces a PWM signal every 20 ms, which is equal to
50 Hz) independent of the state of the master microcontroller.
The communication link between the Master and Slave is the serial UART interface that
serves to synchronize the two. The bottlenecks to overcome when using the communication link
are related to the latency and bandwidth between communications (when the sorting command has
to be sent from the master to the slave before the object can pass through the sorting mechanism).
5
Ho Chi Minh City University of Technology
FACULTY OF ENGINEERING
Following object classification via the Arduino UNO R3 and OpenCV system, effective
data management is required to quantify objects within specific time intervals. While comparison-
based sorting algorithms offer general flexibility, they are strictly limited to a theoretical lower
bound time complexity of θ (n log(n)). To overcome this limitation and ensure real-time per-
formance, this framework utilizes Bucket Sorting, a non-comparison-based method capable of
achieving linear time complexity θ (n) by exploiting specific data properties. [9]
In this continuous vision-based system, Bucket Sorting is essential for organizing Red,
Green, and Blue detections into deterministic time windows. This segmentation manages asyn-
chronous, high-frequency input, thereby facilitating precise throughput measurement, trend analy-
sis, and synchronization with host-side control modules.
The sequential Bucket Sort (BS) algorithm is a classical sorting technique designed for
datasets whose values are uniformly distributed within a known range [1, n]. The fundamental
concept of this algorithm is to divide the input range into several equal-sized subintervals, referred
to as ”buckets”, based on its value. Since the buckets are defined to have identical ranges, the
number of elements assigned to each bucket is expected to be approximately equal, with an average
of nb elements per bucket.
The size of each bucket and its associated subinterval boundaries are calculated using Equa-
tions (1) − (3):
max − min + 1
s=
b
Bucketstart (B) = min + Bs, where B = 0, 1, 2, ..., b − 1
Centroid tracking is employed to maintain the identity of detected objects across consecu-
tive video frames and to prevent redundant counting in the color-based sorting system.
The centroid coordinates are derived as follows:
xmin + xmax
Cx =
2
ymin + ymax
Cy =
2
After object detection using color thresholding and contour extraction, the centroid of each
detected object is calculated from its bounding box. The tracking algorithm compares the centroids
6
Ho Chi Minh City University of Technology
FACULTY OF ENGINEERING
in the current frame with those from the previous frame using Euclidean distance: [10]
q
d= (x2 − x1 )2 + (y2 − y1 )2
Objects with the minimum centroid distance are assumed to correspond to the same phys-
ical entity and are assigned a persistent unique identifier. This identifier is maintained throughout
the object’s presence in the field of view. By counting objects only once per unique identifier
and within a predefined time interval or spatial region, the system effectively eliminates double
counting.
The bucket sorting is closely tied with the centroid tracking algorithm in ensuring efficient
data aggregation in the color sorting system. After assigning a unique identifier to each object via
the centroid tracking algorithm, its color is detected along with its timestamp.
Contrary to storing all object data in a list without organization, buckets of fixed sizes,
indexed to each color category, are assigned. Once an object is noted as a new entry, indexed by
its unique identifier and status, it is placed within the bucket assigned for its specific color. Each
bucket is responsible for storing a running total as well as timestamp data, such as detecting a num-
ber of items within a fixed timeframe. This bucket organization enables constant-time processing
without scanning all data multiple times. In linking both algorithms, accurate real-time object
counts, double counts, as well as efficient data transmission of summarized colors, are attained.
4 Methodology
2. The Slave Node (Arduino): Responsible for real-time actuation. It operates on a deter-
ministic loop to generate Pulse Width Modulation (PWM) signals for the servo motor SG90
upon receiving triggers from the host.
The objective of this project is to design and build an automated system capable of classi-
fying objects based on color and physically sorting them using a mechanical actuator. The system
7
Ho Chi Minh City University of Technology
FACULTY OF ENGINEERING
utilizes OpenCV for image processing and color detection and an Arduino Uno to control a servo
motor based on data received via serial communication.
The core of the detection mechanism relies on computer vision. The processing pipeline
transforms raw video data into binary sorting commands through the following stages:
Color Space Transformation (RGB vs. HSV): Standard webcams capture images in the
RGB (Red-Green-Blue) color space. However, RGB is inherently correlated with light intensity.
A red object in a shadow has significantly different RGB values than the same object in bright
light, making thresholding unreliable. To mitigate issues, the system converts input frames to the
HSV (Hue, Saturation, Value) color space. [11]
• Hue (H): Represents the dominant color wavelength. This channel remains relatively stable
even if lighting conditions fluctuate.
By performing thresholding primarily on the Hue channel, the system achieves robustness against
ambient lighting variations [7].
Figure 2: Relationship between the RGB color and the HSV color systems [2]
The following is the order in which the algorithm processes video frames:
8
Ho Chi Minh City University of Technology
FACULTY OF ENGINEERING
1. Region of Interest (ROI) Extraction: The algorithm crops the video frame to a particular
sub-window (black platform mentioned above) in order to maximize computational effi-
ciency. This raises the processing frame rate (FPS) and eliminates unnecessary background
noise. [7]
2. In-Range Thresholding: OpenCV is used to apply a band-pass filter. While some pixels
are set to 0 (Black), those that fall within the calibrated HSV range (LowerRed ≤ Pixel ≤
U pperRed ) are set to 255 (White). A Binary Mask is created as a result. [7]
3. Morphological Filtering: Because of sensor flaws, raw binary masks frequently contain
noise.
4. Contour Analysis: To determine the boundaries of the white areas, the algorithm makes use
of the algorithm ‘[Link]‘ [12] [13].
5. Area Filtering: This method applies a geometric constraint. Valid objects are only contours
with an area A > Athreshold (1000 pixels). This stops tiny artifacts like dust and glare from
setting off the sorter.
• Data Payload: The protocol employs single-byte character flags (’r’, ’g’, ’b’) instead of
string parsing to reduce latency.
• Flow Control: To stop buffer overflow on the Arduino’s serial buffer, the Python script
applies a period delay (500 ms) blocking following a command.
A Python-based program utilizing the OpenCV library was developed and interfaced with
an Arduino Uno R3 to perform color classification. Detected objects are categorized into three
distinct classes: RED, BLUE, and GREEN. In addition to being converted into control signals
and transmitted to the Arduino, the detected color data are identified by Centroid Tracking and
organized according to color and time. The processed results are displayed on the computer screen.
This program is illustrated in three steps: Stored data, Sorting, Counting (Fig. 3).
9
Ho Chi Minh City University of Technology
FACULTY OF ENGINEERING
To manage and store the color detection results efficiently, a bucket-based data structure is
implemented in Python using the “defaultdict” module, which enables automatic initialization of
empty data containers. Each bucket is assigned to a specific color category, namely RED, GREEN,
and BLUE.
During operation, a timestamp is generated for every detected object, and the corresponding
time information is appended to the appropriate color bucket. This approach allows the system to
organize detection events according to both color and time, thereby facilitating subsequent analysis
of object counts within predefined time intervals and enabling structured data transmission and
visualization on the host computer. Each detection event is represented as a tuple:
Ei = (ti , ci )
Where ti is the timestamp of the detection and ci denotes the detected color label.
To prevent double counting of objects in consecutive video frames, the system assigns a
unique identification (ID) to each detected object using a centroid-based tracking approach. The
fundamental principle of this method is to track the spatial position of an object across successive
frames by computing the centroid of its detected contour or bounding box. By monitoring centroid
displacement over time, the system can determine whether a newly detected object corresponds to
an existing tracked object or represents a new entry.
10
Ho Chi Minh City University of Technology
FACULTY OF ENGINEERING
In the initial stage, objects are detected using OpenCV-based image processing techniques,
and their contours are extracted. The centroid of each detected object is calculated from the contour
moments. A tracking algorithm then compares the current centroid positions with those from
the previous frame using a distance metric, such as Euclidean distance. If the distance between
two centroids falls below a predefined threshold, the object is considered the same and retains its
assigned ID. Otherwise, a new unique ID is generated and assigned to the object.
Each tracked object ID is associated with a timestamp and a time interval bucket. In this
report, the event Ei (RED, GREEN, BLUE) are set with ID (1,2,3) respectively. During counting,
an object is counted only once within a predefined time interval by checking whether its ID has
already been registered in that interval. This mechanism ensures that objects appearing in multiple
frames are not repeatedly counted, thereby maintaining accurate object counts.
The implementation primarily relies on the OpenCV library for object detection, contour
extraction, and centroid calculation, while Python data structures such as dictionaries and sets are
used to manage object IDs and interval-based counting records. This combination of centroid
tracking and ID-based validation provides a reliable and computationally efficient solution for
eliminating duplicate counts in real-time color-sorting applications.
To enable efficient real-time analysis of detection data, a bucket sorting strategy is em-
ployed. The buckets are predefined based on the set of target colors recognized by the system.
Initially, the detection data are organized into buckets according to specified time intervals.
Color detection is performed in the HSV color space using predefined threshold ranges,
where red objects are detected within hue intervals of 0–10 and 170–180, green objects within
36–86, and blue objects within 94–126, with corresponding saturation and value constraints to
ensure robustness against noise and illumination variation. The continuous data stream produced
by the vision-based detection system is divided into discrete time intervals of fixed duration, such
as 1s, 5s, or 10s.
For each detected object, the system records the corresponding detection timestamp. Using
this timestamp, the detection event is then assigned to the appropriate time bucket representing the
interval in which it occurs. This procedure ensures that all detection events within the same time
window are grouped together, enabling structured and efficient temporal analysis in subsequent
processing stages.
Subsequently, the color information within each time-based bucket is analyzed to determine
the distribution of detected colors over time. After the detection events have been grouped into
their respective time-based buckets, a second processing stage is performed to analyze the color
information within each interval. For each time bucket, the stored color labels corresponding to
the detected objects are examined and categorized into predefined classes, namely RED, GREEN,
11
Ho Chi Minh City University of Technology
FACULTY OF ENGINEERING
and BLUE.
Each bucket stores timestamps corresponding to detection events of its associated color:
Where
Bcolor = [t1 ,t2 , ...,tn ]
12
Ho Chi Minh City University of Technology
FACULTY OF ENGINEERING
5 System Design
In this part, the components used in the projects are shown in detail.
5.1.1 Microcontroller
The Arduino Uno R3 serves as the embedded controller. It was chosen for its native PWM
(Pulse Width Modulation) capabilities and ease of serial interfacing, which is cost-efficient for this
scale of project. It handles the signal processing from the computer and drives the servo motors.
13
Ho Chi Minh City University of Technology
FACULTY OF ENGINEERING
Figure 5: Webcam
Wires and a breadboard MB-102 (830 holes, 165x55x10 mm) are used to connect all of the
electrical components, allowing for modular prototyping and testing of the circuit topology before
final soldering.
14
Ho Chi Minh City University of Technology
FACULTY OF ENGINEERING
A 5V DC Source is used to supply a sustainable electric source for two (2) SG90 servos.
This is critical for isolating the inductive load of the motors from the logic power of the microcon-
troller.
Figure 7: 5V DC Source
5.1.5 Actuators
Two (2) SG90 servos are used to control the rotation angle of the slide and the platform.
These are small, lightweight servos capable of roughly 1.6kg-cm torque, sufficient for sorting light
objects.
15
Ho Chi Minh City University of Technology
FACULTY OF ENGINEERING
A black platform with a 1 W LED is used to position products for webcam-based detection.
A slide transports products from the detection area to the correct sorting box. The components are
connected following the above procedure and can be displayed in a block diagram in Fig. 9.
6 Implementation
This section details the physical realization of the design, including hardware assembly,
environmental setup, and software tuning.
1. Detection Zone: A static area where the object passes through. This area is illuminated by a
16
Ho Chi Minh City University of Technology
FACULTY OF ENGINEERING
fixed light source to ensure color consistency is displayed. At this part, a small black square
platform serves as a detection zone and a 1W LED as a fixed light. After being detected,
the product is delivered onto the second part by connecting the platform with Servo Motor
SG90 to spin into the slide.
2. Actuation Zone: Located immediately after the detection zone. This contains a slide con-
trolled by an actuator using Servo Motor SG90.
3. Collection Zone: Three distinct bins or channels positioned at specific angles relative to the
actuator (e.g., left for red, right for green, center for blue).
The Power Isolation Issue: During initial testing, the system experienced sudden micro-
controller resets when the servo motor activated. Analysis revealed that the startup current of
the Servo (approximately 600 mA to 1 A) exceeded the current limit of the USB port (500 mA),
causing a voltage drop on the Arduino’s 5 V rail.
So the power distribution was separated into three discrete parts:
• Servo Power: Connected to an external 5V/2A DC power supply, which isolates the sensi-
tive microprocessor logic from voltage spikes generated by the motors.
• Common Ground: A critical connection was established between the negative terminal of
the external supply and the Arduino GND pin. Without this common ground, the PWM
control signal can float and cause erratic motor behavior (jitter).
The sensor was mounted on a rigid stand at a height of 30 cm, utilizing a ”Top-Down”
perspective to minimizes occlusion and simplifies the geometry of the objects to 2D shapes. The
webcam was equipped with one small crane on the platform to adjust the distances from camera to
objects for better detection when the environment light changes.
The hardware implementation phase focuses on the integration of the sensing, processing,
and actuation subsystems into a cohesive electromechanical unit. This section describes the cir-
cuit topology, power management strategy, and mechanical configuration used to ensure system
stability.
17
Ho Chi Minh City University of Technology
FACULTY OF ENGINEERING
The electrical design bridges the logic level (TTL) of the microcontroller with the power
level of the actuators. The circuit diagram is illustrated in Fig. 10.
The system utilizes two micro-servos (TowerPro SG90). Unlike DC motors which require
H-Bridge drivers, these servos are driven directly via Pulse Width Modulation (PWM).
• Dispensing Platform: Connected to digital Pin 6. This servo operates the release mecha-
nism, rotating the inspection platform to drop the object into the slide after image processing
is complete.
• Sorting Actuator: The sorting actuator is connected to pin 9 (digital pin connector). This
servo controls the angle (0◦ , 45◦ , 90◦ ) of the slide to navigate the product to the correct
category.
18
Ho Chi Minh City University of Technology
FACULTY OF ENGINEERING
To eliminate signal float causing servo jitter (resulting from the control line connecting
to an MCU’s GPIOs), the control lines are directly coupled to the MCU’s GPIOs, providing a
way to send the control signals as necessary while preventing accidental disconnection by using
the internal timer of the ATmega328P (Timer1 for Pin 9 and Timer0 for Pin 6) to produce PWM
signals that accurately maintain a consistent servo position even if the serial communication is
interrupted.
As mentioned, the mechanical components are divided into three individual parts. This
primary physical updating is constructed to minimize vibration and control lighting variables.
• Optical Isolation: The semi-open matte black housing encloses the detection zone via foam
board. This design provides a two-fold function, as it functions as a contrasting background
for the background subtraction algorithms and also allows for less entry of outside ambient
light (sunlight/room lights), thus ensuring the 1W LED is the primary source of illumination.
• Camera Installing: The webcam is mounted on a rigid cantilever arm that is situated 30 cm
above the detection zone (nadir view) and can be adjusted less or more by 5 cm depending
on the environmental changes. The cantilever arm is mechanically affixed to the base to
eliminate any relative movement between the camera coordinate system and the platform
coordinate system, which would require the recalibration of the Region of Interest (ROI).
• Sorting Slide: The actuation slide is set at a 35◦ angle respect to the ground surface. This
gravity-assisted design reduces the complexity of the system by removing the need for a
conveyor belt. Once the object is inspected, gravity transports it to the appropriate bin by the
slide with a servo under it.
As far as the accurate timing control for the sorting operation is concerned, a Python-
based application was designed and implemented with the help of OpenCV. This application has
been designed in a manner such that the information about the detection of different colors has
been split into three different categories, which are RED, GREEN, and BLUE. The bucket sort
algorithm was employed in the application at hand, given that the categories for the data and the
time intervals are already known in advance.
In order to eliminate duplicate counting, centroid tracking was employed to follow objects
in the video frames. The centroid for each detected object is determined with the help of its
19
Ho Chi Minh City University of Technology
FACULTY OF ENGINEERING
rectangular bounding box, with a comparison of the centroid with the centroid of the previous
frame being done using Euclidean distance. If the distance is below a certain threshold, the object is
tracked in order to identify and assign a new label to the distinct object. A data handling procedure
involving ”defaultdict” in Python was employed to deal with this output. Lastly, a visualization
aid is created which aims to refresh a graph chart at each interval with values placed on top of the
video output so that we are able to track its performance in ensuring this process works while at
the same time arranging the objects.
7 Technical Challenges
While the theoretical design of an AI-based color classification system demonstrates sig-
nificant potential for industrial automation, the transition from a controlled simulation to a physical
prototype faces numerous difficulties. These challenges are not merely software-related; they en-
compass a complex interplay between optical physics, mechanical latency, and computational con-
straints. In a real-world environment, the system must maintain high accuracy despite fluctuating
variables that are difficult to model in a training dataset. This section analyzes the critical bottle-
necks of the proposed system, specifically focusing on environmental inconsistencies, real-time
processing requirements, and the mechanical synchronization necessary for accurate sorting.
One of the most prevalent challenges in computer vision applications is the dependency
on consistent illumination. In a controlled simulation, analyzed images mostly possess uniform
brightness values. However, realistically, ambient lighting conditions fluctuate unpredictably due
to external factors such as time of day, shadows cast by nearby humans or machinery, or the
flickering of lightbulbs due to AC current. This phenomenon is known as the ”Color Constancy
Problem” and it can drastically alter the RGB (Red-Green-Blue) values captured by the sensor.
For example, an object that appears completely red (R = 255, G = 0, B = 0) under neutral
white light may appear brown under low-light conditions. The camera sensor differs from the
human eye regarding color perception because it processes the reflected photons perceived linearly.
If the light source changes, the reflected spectrum changes, leading to misclassification by the AI
model.
20
Ho Chi Minh City University of Technology
FACULTY OF ENGINEERING
Furthermore, surface reflectivity poses a significant hurdle. Industrial products often pos-
sess glossy or metallic surfaces that generate specular reflections (glare). These high-intensity
highlights saturate the camera sensor, causing ”clipping” where the pixel data is lost. The AI
model, trained on perfect images, may misinterpret these white glare spots as defects or entirely
different colors.
To quantify the difficulty of distinguishing colors under varying conditions, we must ana-
lyze the mathematical distance between color vectors. In a standard 3-D RGB space, the difference
between two colors is calculated using the Euclidean Distance metric:
q
D = (R2 − R1 )2 + (G2 − G1 )2 + (B2 − B1 )2
Where (R1 , G1 , B1 ) represents the reference color and (R2 , G2 , B2 ). As illumination de-
creases, the values of R, G, B drop closer to zero, effectively compressing the distance D between
distinct colors. This compression reduces the ”margin of error” for the classifier, making it statis-
tically more likely to confuse dark blue with black or dark red with brown.
21
Ho Chi Minh City University of Technology
FACULTY OF ENGINEERING
Beyond physical hardware, the AI model itself faces significant challenges when deployed
in a production environment. A primary difficulty in industrial AI is ensuring the model ”general-
izes” correctly, meaning it must conceptually understand a defect rather than simply memorizing
specific pixel patterns from training photos. The transition from a static dataset to a dynamic en-
vironment exposes two critical weaknesses in standard deep learning approaches: the inability to
adapt to new environments (Domain Shift) and the statistical bias caused by uneven data (Data
Imbalance).
• Domain Shift (Contextual Gaps): This phenomenon occurs when the AI model’s per-
formance plummets upon encountering a real-life subject within a complex or noisy back-
ground. This is due to the training datasets that rely heavily on static images captured under
perfect lighting configurations. The system may learn to associate valid features with that
impeccable background, resulting in failure when introduced to the ”noise” of a real factory
floor.
• Data Imbalance: In an industrial setting, defects are rare. A production line might produce
1000 ”good” red cubes for every 1 ”defective” chipped cube. An AI model trained on such
a dataset can achieve near-perfect accuracy (99.9%) by simply guessing ”good” every single
time, completely ignoring the defects. This ”Accuracy Paradox” makes collecting a suffi-
cient volume of ”rare case” data a logistical challenge, often requiring extensive manual data
augmentation or annotation.
The fundamental constraint is defined by the physical transport time. Given a conveyor belt
velocity v and a distance d from the camera sensor to the sorting actuator, the maximum allowable
time window (tmax ) for the entire classification pipeline is:
d
tmax =
v
22
Ho Chi Minh City University of Technology
FACULTY OF ENGINEERING
For example, if the camera is positioned 50cm (0.5m/s) upstream from the sorting arm and the belt
moves at 0.5m/s, the total system latency must be strictly less than 1s. While this appears generous,
the total system latency (ttotal ) is the aggregate sum of distinct processing stages, creating potential
bottlenecks:
ttotal = tcap + ttrans + tin f + tact
Where:
• tcap (Capture Latency): The time required for the sensor to expose the frame and digi-
tize the signal. In low-light environments, the necessary increase in exposure time directly
inflates this value.
• ttrans (Transmission Latency): The time required to transfer raw pixel data from the camera
interface to the processing unit (CPU/GPU). High-resolution streams over standard USB
2.0 buses often suffer from bandwidth throttling compared to direct CSI (Camera Serial
Interface) connections.
• tin f (Inference Latency): The computational time required for the Neural Network to prop-
agate the input and generate a probability score. This is typically the dominant component
of the delay.
• tact (Actuation Delay): The mechanical response time required for the servo motor or pneu-
matic piston to transit from a neutral state to the active sorting position.
23
Ho Chi Minh City University of Technology
FACULTY OF ENGINEERING
To minimize transmission latency (ttrans ), industrial systems typically employ ”Edge Com-
puting” where processing occurs locally on the machine rather than in the cloud. However, this
introduces severe hardware constraints, as the prototype utilizes a Single-Board Computer (SBC)
with limited thermal and power headroom.
Deep learning models require substantial matrix multiplication operations. On a standard
CPU, a complex architecture like ResNet-50 may require 200 − 300ms per image, effectively cap-
ping the maximum conveyor speed. To achieve real-time performance (e.g., > 10 items per sec-
ond), the system must utilize Model Quantization (reducing precision from 32-bit floating point
to 8-bit integers) or switch to lightweight architectures such as MobileNetV2. While these opti-
mizations significantly reduce tin f , they often incur a penalty in classification accuracy, forcing a
difficult engineering trade-off between throughput and reliability.
Furthermore, the operating system introduces scheduling jitter. Standard General Purpose
Operating Systems (GPOS), such as standard Linux, do not guarantee deterministic task schedul-
ing. Background processes can momentarily preempt the Python control script, causing variable
delays. In a worst-case scenario, a 50ms OS-induced lag could cause the sorting arm to trigger
late, striking the object off-center or jamming the mechanism.
The final and perhaps most volatile variable in the system architecture is the physical in-
terface between the digital control unit and the mechanical sorting apparatus. While the software
logic operates in a deterministic environment, the mechanical components are subject to friction,
inertia, and wear, leading to non-linear behaviors that are difficult to compensate for in code.
The sorting mechanism (typically a servo-controlled arm) does not move instantaneously.
When the microcontroller transmits a Pulse Width Modulation (PWM) signal to the servo motor,
there is a distinct physical delay before the arm reaches the target position. This is governed by the
motor’s Mechanical Time Constant (τmech ).
If a servo has a rated slew rate of 0, 10sec/60◦ , and the arm must rotate to reject an object,
the theoretical travel time is approximately. However, this assumes the motor is already in motion.
In reality, overcoming static friction and rotor inertia adds an unpredictable start-up delay.
If the conveyor belt moves at a velocity v = 0.5m/s, a seemingly negligible delay results in
24
Ho Chi Minh City University of Technology
FACULTY OF ENGINEERING
A 10mm error is often sufficient to cause the sorting arm to strike the edge of the object rather
than its center of mass. This off-center impact can impart torque, causing the object to spin out of
control or jam against the chassis rather than being cleanly diverted into the rejection bin.
Low-cost conveyor systems rarely maintain a constant velocity. Factors such as motor
slippage, uneven belt tension, and variable load weights cause the instantaneous velocity v(t) to
fluctuate around the nominal target speed. Since simple sorting algorithms typically rely on a fixed
timestamp (e.g., ”Wait 2s after detection to actuate”), any variation in v(t) directly invalidates this
timing calculation.
Furthermore, the mechanical rotation of the drive motor creates high-frequency vibrations
that propagate through the machine frame. If the camera module is rigidly mounted without vibra-
tion dampening, these micro-movements manifest as Motion Blur.
For CMOS sensors (standard in embedded cameras), vibration exacerbates the Rolling
Shutter Effect. Because the sensor scans the image line-by-line, high-frequency vibration during
the scan causes the image to appear skewed or wobbled (”Jello effect”). This geometric distortion
changes the apparent aspect ratio of the object, potentially causing the AI to misclassify a square
product as a deformed rectangle or an unknown blob.
25
Ho Chi Minh City University of Technology
FACULTY OF ENGINEERING
Unlike a simulation where objects are instantiated in the exact center of the frame, objects
on a real conveyor belt arrive with random positional variance and orientation. An object may drift
to the extreme left or right of the belt. This introduces a Reachability Constraint:
• Mechanical Limit: If the sorting arm has a fixed length, objects on the far side of the belt
may be physically out of range.
• Leverage Limit: Even if the arm touches the object, it may not have sufficient leverage to
push it fully into the bin.
Consequently the vision system cannot simply classify ”Color.” It must also calculate the object’s
centroid coordinates (x, y) to determine if the object is within the valid ”Actuation Window”. If
an object is classified as ”Defect” but is physically unreachable, the system must log a ”Missed
Sort” error rather than attempting a futile actuation.
8 Evaluation
The feature of the Master-Slave control topology that makes it ideal for this object sorting
system is its ability to decouple high-level decision-making from low-level hardware actuation.
This is made possible by assigning non-determinate image processing tasks to the host computer
while reserving the slave microcontroller for real-time deterministic tasks. The Master node can
handle complex matrix operations and logic, making it perfect for heavy computational loads,
while the Slave ensures precise timing for motor control.
One of the key features of the vision system is the implementation of the Hue-Saturation-
Value (HSV) color space transformation. This means that the system separates chromaticity from
luminance, allowing the algorithm to define target colors as a range of angles on the Hue circle.
As a result, the objects sorting system can remain operational and accurate even when ambient
lighting conditions fluctuate, reducing the chances of errors caused by shadows or glare.
The Arduino Uno R3 module is a cost-effective option for the slave node. It is chosen for
its native Pulse Width Modulation (PWM) capabilities and ease of serial interfacing, making it a
budget-friendly choice for projects that require precise motor control. This also means that the
microcontroller can independently execute the exact timing of PWM signals to control the servo
motors without being affected by the processing jitter of the master computer.
One of the most significant advantages of the Centroid Tracking integration is its ability to
maintain the identity of detected objects across consecutive video frames. This makes it ideal for
26
Ho Chi Minh City University of Technology
FACULTY OF ENGINEERING
use in object sorting systems where preventing redundant counting is critical for inventory accu-
racy. The algorithm’s ability to compare centroid distances effectively eliminates double counting,
ensuring that each unique physical entity is processed and logged exactly once.
Another crucial feature of the hardware design is the power isolation strategy. By con-
necting the servo motors to an external 5V/2A DC power supply, the system isolates sensitive
microprocessor logic from voltage spikes. Additionally, the establishment of a common ground
prevents the PWM control signals from floating, making the actuators reliable for use in dynamic
mechanical environments where jitter must be minimized.
The implementation of this automated sorting solution can have a tremendous effect on
the economic system, particularly for small and medium-sized enterprises (SMEs). Automation
can significantly reduce the barrier to entry for efficient manufacturing, which in turn provides a
cost-effective and scalable method for sorting products where high-end industrial sorters are too
expensive.
Automation can also increase the speed and accuracy of the sorting task via the Master-
Slave architecture, which can lead to a decrease in the amount of time and resources required to
manage inventory. Finally, the system utilizes affordable components like the Arduino Uno R3 and
standard webcams, resulting in substantial cost savings for businesses compared to proprietary
industrial equipment. All of these benefits lead to increased efficiency and competitiveness for
smaller market players, ultimately resulting in a positive impact on the economic system.
Automated sorting systems can also reduce the environmental impact of manufacturing by
minimizing waste through precise classification. The system utilizes the HSV color space to ensure
robust detection regardless of lighting, which in turn reduces the amount of falsely rejected goods
or sorting errors that generate waste.
Additionally, the system employs efficient algorithms like Bucket Sorting to maximize
computational efficiency, which helps reduce the energy load required for processing large datasets.
Finally, the system’s components have been chosen with stability in mind, such as the power isola-
tion circuit that prevents hardware burnout and extends the lifespan of the electronic components,
ensuring they do not become electronic waste prematurely.
27
Ho Chi Minh City University of Technology
FACULTY OF ENGINEERING
9 Conclusion
This project proved the success of the implementation of an automated sorting system
where computer vision is combined with microcontroller control. Through the use of the Master-
Slave system, it was clear that the non-deterministic process of vision algorithms was separated
from the deterministic process of the servo motors. The use of the HSV color space proved to
enhance the process of object detection, unlike the RGB space. Also, the combination of both the
Centroid Tracking Algorithm and the Bucket Sorting Algorithm proved effective in the manage-
ment of streams of data with no repetition, especially in a continuous flow process.
However, a key observation brought to light during the transition from designing a the-
oretical prototype to designing a prototype that can be physically realized involves a number of
critical technical challenges. Conditions in a real-world setting, particularly the ”Color Constancy
Problem” and specular reflection (glare) for shiny surfaces, are relatively difficult challenges that
could result in errors of misclassification. Moreover, there are very demanding latency constraints
in the system, and when calculated cumulatively for image sensing, network transmission, deep AI
analysis, and mechanical control execution, it has to be within the physical transport time of the
belt conveyor for proper sorting.
Despite these challenges, the system shows the potential of AI-based color classification
algorithms for industrial automation tasks. In the future, there should be a concentration on opti-
mizations related to edge computing for lower latency and the integration of vibration reduction
and light control for increased reliability under dynamic environmental conditions.
28
Ho Chi Minh City University of Technology
FACULTY OF ENGINEERING
10 References
[2] A. Palananda and W. Kimpan, “Turbidity of coconut oil determination using the mamoh
method in image processing,” IEEE Access, 2021.
[3] H. Kopetz, Real-Time Systems: Design Principles for Distributed Embedded Applications.
Springer Science & Business Media, 2011.
[4] S. Dohta, T. Akagi, and M. A. A. Sani, “Control and analysis of simple-structured robot arm
using flexible pneumatic cylinders,” International Journal of ADVANCED AND APPLIED
SCIENCES, vol. 4, pp. 151–157, 12 2017.
[5] I. Sulistiyowati, H. M. Ichsan, and I. Anshory, “Object sorting conveyor with detection
color using ESP-32 camera python based on Open-CV,” Journal of Electrical Engineering
and Computer Sciences (JEECS), vol. 9, no. 1, pp. 61–68, Jun. 2024. [Online]. Available:
[Link]
[7] R. C. Gonzalez and R. E. Woods, Digital Image Processing, 4th ed. Pearson, 2018.
[8] A. R. Smith, “Color gamut transform pairs,” SIGGRAPH Computer Graphics, vol. 12, no. 3,
pp. 12–19, 1978.
[9] T. H. Cormen, C. E. Leiserson, R. L. Rivest, and C. Stein, Introduction to Algorithms, 3rd ed.
MIT Press, 2009.
[10] A. Yilmaz, O. Javed, and M. Shah, “Object tracking: A survey,” ACM Computing Surveys
(CSUR), vol. 38, no. 4, pp. 13–es, 2006.
[11] R. Szeliski, Computer Vision: Algorithms and Applications, 2nd ed. Springer Nature, 2022.
[12] GeeksforGeeks, “Find and draw contours using opencv - python,” jul 2025,
accessed: 2025-12-21. [Online]. Available: [Link]
find-and-draw-contours-using-opencv-python/
[13] S. Suzuki and K. Be, “Topological structural analysis of digitized binary images by border
following,” Computer Vision, Graphics, and Image Processing, vol. 30, no. 1, pp. 32–46,
1985.
29