Images and Color in Python
English translation
Learning objectives
Compare visual appearance and array values to see how brightness, color, and size transformations change information needed for detection.
Execution conditions
Use opencv-python-headless, NumPy, and Matplotlib. A camera is not required for the basic activities.
From arrays to images
Starting conditions
Draw a blue rectangle in a 120×160 BGR image, and convert it to RGB, grayscale, and HSV.
Procedure
- Check the original array's shape, dtype, and range.
- Display the same image correctly in Matplotlib and compare with a deliberately altered channel order.
Results to retain
- Comparison images for each representation and an array summary.
import numpy as np
import matplotlib.pyplot as plt
import cv2 as cv
image=np.full((120,160,3),220,dtype=np.uint8);image[30:90,50:110]=(255,30,30)
rgb=cv.cvtColor(image,cv.COLOR_BGR2RGB);hsv=cv.cvtColor(image,cv.COLOR_BGR2HSV)
fig,axes=plt.subplots(1,2);axes[0].imshow(image);axes[0].set_title('BGR as RGB');axes[1].imshow(rgb);axes[1].set_title('Correct RGB')
print(image.shape,image.dtype,hsv[60,80])
Record results and self-check
Representation:
shape:
dtype/value range:
Interpretation:
Points to check
- Have you avoided displaying a BGR array as RGB?
- Did you check that H ranges from 0…179 in OpenCV uint8 HSV?
Find objects by color range
Starting conditions
Use the blue HSV range …130, S≥80, V≥50.
Procedure
- Create a mask using inRange and display it beside the original.
- Lower image brightness and detect regions using the same criterion.
- Add noise or a background with a similar color and examine the resulting errors.
Results to retain
- Original/mask comparison images and a detection table by condition.
mask=cv.inRange(hsv,np.array([100,80,50]),np.array([130,255,255]))
plt.figure();plt.imshow(mask,cmap='gray');print(np.count_nonzero(mask))
# TODO: Compare different brightness and background conditions.
Record results and self-check
Condition:
Detected pixel count:
Missed regions:
Falsely detected regions:
Points to check
- Did you keep the meanings of RGB and HSV channels distinct?
- Have you avoided judging accuracy solely by the number of detected pixels?
Pass coordinates to the next stage
Starting conditions
Calculate the center of the region detected in the mask in pixel coordinates.
Procedure
- Describe the relationship between row/column indices and x/y coordinates.
- Overlay the center point on the image to check its coordinates.
- Define the return value when no regions are detected.
Results to retain
- An image with the center marked and an example of handling missed detection.
yy,xx=np.nonzero(mask)
center=None if len(xx)==0 else (float(xx.mean()),float(yy.mean()))
print(center)
# TODO: Overlay the center on the image.
Record results and self-check
Scenario:
Center x:
Center y:
Detection valid:
Points to check
- If using 0,0 to indicate missed detection, have you distinguished it from an actual object at the upper left?
- Did you preserve the relationship of rows to y and columns to x?