We use image processing to help computers understand and change pictures. This is the first step to teach computers to see like humans.
First image processing program in Computer Vision
import cv2 # Load an image from file image = cv2.imread('path_to_image.jpg') # Show the image in a window cv2.imshow('Image Window', image) cv2.waitKey(0) cv2.destroyAllWindows()
Use cv2.imread() to load an image from your computer.
cv2.imshow() opens a window to display the image. cv2.waitKey(0) waits for a key press to close the window.
import cv2 # Load and show a color image image = cv2.imread('flower.jpg') cv2.imshow('Color Image', image) cv2.waitKey(0) cv2.destroyAllWindows()
import cv2 # Load image in grayscale (black and white) gray_image = cv2.imread('flower.jpg', cv2.IMREAD_GRAYSCALE) cv2.imshow('Grayscale Image', gray_image) cv2.waitKey(0) cv2.destroyAllWindows()
import cv2 # Resize image to half its size image = cv2.imread('flower.jpg') small_image = cv2.resize(image, (image.shape[1]//2, image.shape[0]//2)) cv2.imshow('Small Image', small_image) cv2.waitKey(0) cv2.destroyAllWindows()
This program loads a color image, changes it to black and white, shows both, and prints their sizes.
import cv2 # Load an image image = cv2.imread('flower.jpg') # Convert to grayscale gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) # Show original and grayscale images cv2.imshow('Original Image', image) cv2.imshow('Grayscale Image', gray_image) cv2.waitKey(0) cv2.destroyAllWindows() # Print image shapes print(f'Original image shape: {image.shape}') print(f'Grayscale image shape: {gray_image.shape}')
Make sure the image file path is correct or the program will not load the image.
Color images have 3 channels (blue, green, red), grayscale has 1 channel.
Use cv2.destroyAllWindows() to close all image windows properly.
Image processing lets computers open and change pictures.
Use OpenCV functions like imread, imshow, and cvtColor to work with images.
Showing images and printing their size helps understand what the program does.
Practice
imread do in an image processing program?Solution
Step 1: Understand the purpose of
The functionimreadimreadis used to load an image from a file into the program's memory.Step 2: Differentiate from other functions
Functions likeimshowdisplay images, andcvtColorchanges image colors, so they do not read files.Final Answer:
It reads an image file and loads it into the program. -> Option BQuick Check:
imread = load image [OK]
- Confusing imread with imshow
- Thinking imread changes image colors
- Assuming imread saves images
img using OpenCV?Solution
Step 1: Recall the OpenCV display function
The correct function to show an image iscv2.imshow, which takes a window name and the image variable.Step 2: Check the syntax of options
Only cv2.imshow('Window', img) usescv2.imshowwith correct parameters: a string window name and the image.Final Answer:
cv2.imshow('Window', img) -> Option DQuick Check:
imshow = show image [OK]
- Using non-existent functions like display or showimage
- Forgetting the window name argument
- Swapping argument order
import cv2
img = cv2.imread('photo.jpg')
print(img.shape)Solution
Step 1: Understand what
In OpenCV,img.shapereturnsimg.shapegives the dimensions of the image as a tuple: (height, width, number of color channels).Step 2: Differentiate from other outputs
It does not print pixel values or file size, andshapeis a valid attribute for images loaded byimread.Final Answer:
It prints the dimensions of the image as (height, width, channels). -> Option CQuick Check:
img.shape = image size [OK]
- Expecting pixel data instead of shape
- Thinking shape is a method, not attribute
- Confusing file size with image dimensions
import cv2
img = cv2.imread('image.png')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
cv2.imshow('Gray Image')
cv2.waitKey(0)
cv2.destroyAllWindows()Solution
Step 1: Check the usage of
The functioncv2.imshowcv2.imshowrequires two arguments: a window name and the image to display. Here, the image argument is missing.Step 2: Verify other function calls
cv2.cvtColorcorrectly converts color images,waitKey(0)waits indefinitely, anddestroyAllWindowsis correctly placed after showing images.Final Answer:
Missing the image argument in cv2.imshow function. -> Option AQuick Check:
imshow needs image argument [OK]
- Forgetting the image argument in imshow
- Misunderstanding waitKey argument
- Calling destroyAllWindows too early
Solution
Step 1: Understand the task steps
The program must first read the image, then convert it to grayscale, and finally save the new image.Step 2: Match functions to steps
cv2.imread()reads the image,cv2.cvtColor()converts color spaces, andcv2.imwrite()saves the image to a file.Final Answer:
cv2.imread() -> cv2.cvtColor() -> cv2.imwrite() -> Option AQuick Check:
Read -> Convert -> Save = imread, cvtColor, imwrite [OK]
- Trying to save before reading
- Showing image before converting
- Mixing order of functions
Ready to prove it?
Pass the test and this pattern moves unknown
