\[ \begin{align}\begin{aligned}\newcommand\blank{~\underline{\hspace{1.2cm}}~}\\% Bold symbols (vectors) \newcommand\bs[1]{\mathbf{#1}}\\% Differential \newcommand\dd[2][]{\mathrm{d}^{#1}{#2}} % use as \dd, \dd{x}, or \dd[2]{x}\\% Poor man's siunitx \newcommand\unit[1]{\mathrm{#1}} \newcommand\num[1]{#1} \newcommand\qty[2]{#1~\unit{#2}}\\\newcommand\per{/} \newcommand\squared{{}^2} \newcommand\cubed{{}^3} % % Scale \newcommand\milli{\unit{m}} \newcommand\centi{\unit{c}} \newcommand\kilo{\unit{k}} \newcommand\mega{\unit{M}} % % Percent \newcommand\percent{\unit{{\kern-4mu}\%}} % % Angle \newcommand\radian{\unit{rad}} \newcommand\degree{\unit{{\kern-4mu}^\circ}} % % Time \newcommand\second{\unit{s}} \newcommand\s{\second} \newcommand\minute{\unit{min}} \newcommand\hour{\unit{h}} % % Distance \newcommand\meter{\unit{m}} \newcommand\m{\meter} \newcommand\inch{\unit{in}} \newcommand\foot{\unit{ft}} % % Force \newcommand\newton{\unit{N}} \newcommand\kip{\unit{kip}} % kilopound in "freedom" units - edit made by Sri % % Mass \newcommand\gram{\unit{g}} \newcommand\g{\gram} \newcommand\kilogram{\unit{kg}} \newcommand\kg{\kilogram} \newcommand\grain{\unit{grain}} \newcommand\ounce{\unit{oz}} \newcommand\pound{\unit{lbs}} % % Temperature \newcommand\kelvin{\unit{K}} \newcommand\K{\kelvin} \newcommand\celsius{\unit{{\kern-4mu}^\circ C}} \newcommand\C{\celsius} \newcommand\fahrenheit{\unit{{\kern-4mu}^\circ F}} \newcommand\F{\fahrenheit} % % Area \newcommand\sqft{\unit{sq\,\foot}} % square foot % % Volume \newcommand\liter{\unit{L}} \newcommand\gallon{\unit{gal}} % % Frequency \newcommand\hertz{\unit{Hz}} \newcommand\rpm{\unit{rpm}} % % Voltage \newcommand\volt{\unit{V}} \newcommand\V{\volt} \newcommand\millivolt{\milli\volt} \newcommand\mV{\milli\volt} \newcommand\kilovolt{\kilo\volt} \newcommand\kV{\kilo\volt} % % Current \newcommand\ampere{\unit{A}} \newcommand\A{\ampere} \newcommand\milliampereA{\milli\ampere} \newcommand\mA{\milli\ampere} \newcommand\kiloampereA{\kilo\ampere} \newcommand\kA{\kilo\ampere} % % Resistance \newcommand\ohm{\Omega} \newcommand\milliohm{\milli\ohm} \newcommand\kiloohm{\kilo\ohm} % correct SI spelling \newcommand\kilohm{\kilo\ohm} % "American" spelling used in siunitx \newcommand\megaohm{\mega\ohm} % correct SI spelling \newcommand\megohm{\mega\ohm} % "American" spelling used in siunitx % % Capacitance \newcommand\farad{\unit{F}} \newcommand\F{\farad} \newcommand\microfarad{\micro\farad} \newcommand\muF{\micro\farad} % % Inductance \newcommand\henry{\unit{H}} \newcommand\H{\henry} \newcommand\millihenry{\milli\henry} \newcommand\mH{\milli\henry} % % Power \newcommand\watt{\unit{W}} \newcommand\W{\watt} \newcommand\milliwatt{\milli\watt} \newcommand\mW{\milli\watt} \newcommand\kilowatt{\kilo\watt} \newcommand\kW{\kilo\watt} % % Energy \newcommand\joule{\unit{J}} \newcommand\J{\joule} % % Composite units % % Torque \newcommand\ozin{\unit{\ounce}\,\unit{in}} \newcommand\newtonmeter{\unit{\newton\,\meter}} % % Pressure \newcommand\psf{\unit{psf}} % pounds per square foot \newcommand\pcf{\unit{pcf}} % pounds per cubic foot \newcommand\pascal{\unit{Pa}} \newcommand\Pa{\pascal} \newcommand\ksi{\unit{ksi}} % kilopound per square inch \newcommand\bar{\unit{bar}} \end{aligned}\end{align} \]

Apr 28, 2026 | 544 words | 5 min read

5.2.1. Task 1#

Learning Objectives#

  • Understand the components of the HSV (Hue, Saturation, Value) color space.

  • Implement the mathematical conversion from the RGB to the HSV color space.

  • Apply a pixel-by-pixel transformation to an entire image.

  • Gain experience with manually implementing color space conversions using mathematical formulas.

Introduction#

Images can be represented in various color spaces, each with its own advantages for different applications. The RGB color space is commonly used for display and storage, while the HSV color space is often preferred for tasks involving color manipulation and analysis because it separates color information (hue) from intensity (saturation and value). The HSV color space is more intuitive than RGB for human perception and often more robust for computer vision tasks like object detection.

The HSV components are:

  • Hue (\(H\)): Color type (e.g., red, yellow, blue) represented as an angle from \(0^\circ\) to \(360^\circ\)

  • Saturation (\(S\)): Color intensity/purity, ranging from \(0\) (gray) to \(1\) (pure color)

  • Value (\(V\)): Color brightness, ranging from \(0\) (black) to \(1\) (full brightness)

This task will help you understand both the mathematical foundations of color space conversions and practical implementation techniques for image processing.

Task Instructions#

You will create two user-defined functions to convert RGB images to HSV:

  1. Helper function: Convert a single pixel from RGB to HSV

  2. Image converter: Apply pixel conversion to entire images

Deliverable Reminder

Create flowcharts for all functions and save them in tp1_team_1_teamnumber.pdf. The flowcharts for these two UDFs should also be included in your final report.

UDF1: rgb_to_hsv#

Write a function named rgb_to_hsv that converts a single RGB pixel to HSV.

  • Input: \(R\), \(G\), \(B\) (integers from \(0\) to \(255\))

  • Return: \(H\), \(S\), \(V\) (scaled to appropriate ranges)

1: Normalize RGB values to \([0,1]\) range:

(5.1)#\[\begin{split}\begin{align} R' &= R/255 \\ G' &= G/255 \\ B' &= B/255 \end{align}\end{split}\]

2: Calculate intermediate values:

(5.2)#\[\begin{split}\begin{align} C_{\max} &= \max(R', G', B') \quad \text{(maximum component)} \\ C_{\min} &= \min(R', G', B') \quad \text{(minimum component)} \\ \Delta &= C_{\max} - C_{\min} \quad \text{(difference)} \end{align}\end{split}\]

3: Calculate Hue (\(H\)) based on which channel is maximum:

(5.3)#\[\begin{split}H' = \begin{cases} 0, & \text{if } \Delta = 0 \\ \left(60^\circ \times \frac{G' - B'}{\Delta} \right) \pmod{360^\circ}, & \text{if } C_{max} = R' \\ \left(60^\circ \times \frac{B' - R'}{\Delta} + 120^\circ \right) \pmod{360^\circ}, & \text{if } C_{max} = G' \\ \left(60^\circ \times \frac{R' - G'}{\Delta} + 240^\circ \right) \pmod{360^\circ}, & \text{if } C_{max} = B' \end{cases}\end{split}\]
  • The result \(H'\) will be an angle in degrees \([0^\circ, 360^\circ]\).

  • If \(\Delta=0\), the color is grayscale, so set Hue to \(0\).

4: Calculate Saturation (\(S\)):

(5.4)#\[\begin{split}S' = \begin{cases} 0, & \text{if } C_{max} = 0 \\ \frac{\Delta}{C_{max}}, & \text{if } C_{max} \neq 0 \end{cases}\end{split}\]
  • Result: \(S'\) in \([0, 1]\). If \(C_{max} = 0\) (black), then \(S' = 0\).

5: Calculate Value (\(V\)):

(5.5)#\[ V' = C_{max}\]

6: Rescale to \([0,255]\) range for output:

(5.6)#\[\begin{split}\begin{align} H &= \left(\frac{H'}{360}\right) \times 255 \\ S &= S' \times 255 \\ V &= V' \times 255 \end{align}\end{split}\]

Note

Return these as floating-point values. Integer conversion happens in convert_to_hsv when casting to np.uint8.

Note

These conversion formulas are based upon the official cpython colorsys source code. Using built in RGB to HSV conversion functions will not yield matching outputs.

UDF2: convert_to_hsv#

Write a function named convert_to_hsv that converts RGB images to HSV by applying rgb_to_hsv to every pixel.

  • Input: rgb_image (NumPy array, 3-channel RGB, data type np.uint8)

  • Return: hsv_image (NumPy array, 3-channel HSV, data type np.uint8)

Note

The output array must have the same shape as input (e.g., \(100 \times 100 \times 3\)) and be cast to np.uint8 with pixel values in range \([0, 255]\).

Main method: main#

In your main method:

  1. Load image: Prompt for file path and load using load_img

  2. Preprocess: Rescale to np.uint8 \([0,255]\) range (see Section 15.2.3)

  3. Standardize size: Resize and pad to \(100\times100\) using functions from Section 15.2.3

  4. Inspect pixel: Prompt for coordinates and print RGB values

  5. Convert: Apply your convert_to_hsv function to the image

  6. Compare: Print HSV values at the same coordinates

  7. Visualize: Display the resulting HSV image

Testing: Use the provided reference images to verify your implementation.

Table 5.3 Test Images#

Image File Name

Description

ref_col_raw0.png

A color image

ref_col_raw1.png

A grayscale image

Sample Output#

Use the values in Table 5.4 below to test your program.

Table 5.4 Test Cases#

Case

image_path

Coordinates (x, y)

1

ref_col_raw0.png

50, 50

2

ref_col_raw1.png

0, 0

3

ref_col_raw0.png

20, 20

4

ref_col_raw1.png

12, 34

Ensure your program’s output matches the provided samples exactly. This includes all characters, white space, and punctuation. In the samples, user input is highlighted like this for clarity, but your program should not highlight user input in this way.

Case 1 Sample Output

$ python3 tp1_team_1_teamnumber.py Enter the path of the RGB image you want to convert to hsv: ref_col_raw0.png Enter the coordinates of the pixel you want inspect: 50, 50 RGB values of the (50, 50) pixel: R=255, G=255, B=255 Converting ref_col_raw0.png to HSV... HSV values of the (50, 50) pixel: H=0, S=0, V=255

output hsv ref col raw0

Fig. 5.1 Case_1_output_hsv_ref_col_raw0.png#

Case 2 Sample Output

$ python3 tp1_team_1_teamnumber.py Enter the path of the RGB image you want to convert to hsv: ref_col_raw1.png Enter the coordinates of the pixel you want inspect: 0, 0 RGB values of the (0, 0) pixel: R=2, G=2, B=2 Converting ref_col_raw1.png to HSV... HSV values of the (0, 0) pixel: H=0, S=0, V=2

output hsv ref col raw1

Fig. 5.2 Case_2_output_hsv_ref_col_raw1.png#

Case 3 Sample Output

$ python3 tp1_team_1_teamnumber.py Enter the path of the RGB image you want to convert to hsv: ref_col_raw0.png Enter the coordinates of the pixel you want inspect: 20, 20 RGB values of the (20, 20) pixel: R=255, G=255, B=255 Converting ref_col_raw0.png to HSV... HSV values of the (20, 20) pixel: H=0, S=0, V=255

output hsv ref col raw0

Fig. 5.3 Case_3_output_hsv_ref_col_raw0.png#

Case 4 Sample Output

$ python3 tp1_team_1_teamnumber.py Enter the path of the RGB image you want to convert to hsv: ref_col_raw1.png Enter the coordinates of the pixel you want inspect: 12, 34 RGB values of the (12, 34) pixel: R=1, G=2, B=1 Converting ref_col_raw1.png to HSV... HSV values of the (12, 34) pixel: H=85, S=127, V=2

output hsv ref col raw1

Fig. 5.4 Case_4_output_hsv_ref_col_raw1.png#

Table 5.5 Deliverables#

Deliverables

Description

tp1_team_1_teamnumber.pdf

Flowchart(s) for this task.

tp1_team_1_teamnumber.py

Your completed Python code.