M-1 Python
M-1 Python
Key capabilities:
Image Processing:
Filtering, color conversion, transformations (resize, crop, rotate), and geometric drawing.
Integration & Speed:
Fast numerical operations using NumPy, suitable for real-time computer vision tasks.
Explanation:
cv2.imread() loads the image in BGR format.
cv2.imshow() opens a window and shows the image.
cv2.waitKey(0) waits indefinitely for a key press.
cv2.destroyAllWindows() closes all windows created by OpenCV.
6. What are the different flags in cv2.imread() and what does each
one do?
Soln: The cv2.imread() function in OpenCV reads an image from the disk. It accepts
an optional flag parameter that specifies how the image should be read.
cv2.IMREAD_COLOR – Loads a color image (default).
8. Why does OpenCV use BGR instead of RGB? What are the
implications while accessing pixel values and manipulation?
Soln: Color Formats:
RGB stores color in the order Red, Green, Blue.
BGR stores color in the order Blue, Green, Red.
Most libraries (like Matplotlib, Pillow) use RGB, but OpenCV uses BGR by default.
It’s a legacy decision from older Windows systems and early camera APIs.
There is no technical benefit — just a default standard OpenCV follows.
Displaying OpenCV images using matplotlib (which expects RGB) causes wrong
colors (e.g., red becomes blue).
To fix this, convert using:
examples:
When detecting red color in an image, using incorrect channel indexing due to RGB-BGR
confusion will fail detection logic.
11. How can you access and modify individual pixel values
and color channels in OpenCV?
Soln: Accessing and modifying pixel values allows fine-grained image
manipulation, useful in tasks like masking, editing, or color enhancement.
12. How do you crop a region of interest (ROI) and flip an
image using OpenCV?
Soln: Cropping in OpenCV:
Cropping refers to extracting a specific region from an image, called ROI
(Region of Interest).
OpenCV allows cropping using slicing syntax:
y1:y2 defines the row range (height), and x1:x2 defines the column range
(width).
Example: cropped = img[100:300, 200:400] This extracts the portion from
y=100 to 300 and x=200 to 400.
Flipping in OpenCV:
Flipping creates a mirror image of the input, used for transformation and
augmentation.
OpenCV uses cv2.flip() function:
| Flip Code | Flip Direction |
| --------- | ---------------------------- |
| `0` | Vertical |
| `1` | Horizontal |
| `-1` | Both (vertical + horizontal) |
Example: flipped = cv2.flip(img, 1) # Flips the image horizontally
Cropping helps isolate parts of an image like faces, license plates, or objects.
Flipping is widely used in data augmentation to increase dataset variety in
machine learning and deep learning.
Use Case:
Gaussian blur is used in:
Noise reduction
Preprocessing before edge detection (e.g., Canny)
Smoothing effects in UI
Use Cases:
Face detection
Object tracking
OCR (Optical Character Recognition)
Image classification (as preprocessing)
20. How can you create a black image and draw basic
geometric shapes on it using OpenCV? Explain with code.
Soln: To create a black image and draw geometric shapes using OpenCV, we follow
two main steps:
Step 1: Create a Black Image
A black image is simply an array of zeros using NumPy with shape (height, width, 3)
where each pixel's BGR value is (0, 0, 0):
Step 2: Draw Geometric Shapes
Use OpenCV’s drawing functions to draw shapes on this black image:
Function Purpose
cv2.line() Draws a line between two points
cv2.rectangle() Draws a rectangle using two corners
cv2.circle() Draws a circle (can be filled)
cv2.ellipse() Draws an ellipse arc
Use Cases:
Visualizations in GUIs
Annotating images in object detection
Generating test patterns for computer vision
Explanation:
In grayscale images, each pixel is a single intensity value from 0 (black) to 255
(white).
The grayscale image reduces storage space and simplifies processing.