import cv2 as cv
import numpy as np
# Set the file path for the original image.
# Don't forget to replace 'Pandora.png' with your own image!
file_path = 'Pandora.png'
# Use OpenCV's imread function to read the image.
# The image data is stored as number bricks in the 'source_picture' variable.
source_picture = cv.imread(file_path)
# Check if the image was successfully loaded. If not, print a warning!
if source_picture is None:
print(f"Warning: Oops! Could not load the image.")
print(f"Please check if the file path is correct: {file_path}")
else:
# We'll create a "copy" of the original image called 'modification_img'.
# This way, our modifications won't affect the original picture,
# and you can see a side-by-side comparison!
modification_img = source_picture.copy()
# Now, we'll start messing with every single small brick (pixel) of the image!
# 'modification_img.shape[0]' is the height of the image (how many rows of pixels).
for y in range(modification_img.shape[0]):
# 'modification_img.shape[1]' is the width of the image
# (how many columns of pixels).
for x in range(modification_img.shape[1]):
# Get the color values for the pixel at the current (y, x) coordinates.
# 'pixel' will be a list containing three numbers for blue, green, and red.
pixel = modification_img[y, x]
# For each color (blue, green, red), we subtract its original value from 255.
# Why 255? Because the color values range from 0 to 255.
# This turns it into its "complementary color"!
pixel[0] = 255 - pixel[0] # Blue (B)
pixel[1] = 255 - pixel[1] # Green (G)
pixel[2] = 255 - pixel[2] # Red (R)
# Start an infinite loop to display the modified image window.
while True:
# Use OpenCV's imshow function to display both the original and modified images.
cv.imshow('Original Picture', source_picture)
cv.imshow('Modification Picture (Inverted)', modification_img)
# Changed the window name to be clearer
# The cv.waitKey(1) function "pauses" your program
# for 1 millisecond to check for a key press.
key = cv.waitKey(1)
# Check if the pressed key is the ESC key (the key code for ESC is 27).
if key & 0xFF == 27:
break # If you press the ESC key, exit the loop and close the display.
# Finally, call cv.destroyAllWindows() to close all image windows created
# by OpenCV and free up computer resources.
cv.destroyAllWindows()
import cv2 as cv # Import the OpenCV module
# Set the file path for the original image.
# Don't forget to replace 'Pandora.png' with your own image!
file_path = 'Pandora.png'
# Use OpenCV's imread function to read the image.
source_picture = cv.imread(file_path)
# Check if the image was successfully loaded.
if source_picture is None:
print(f"Warning: Oops! Could not load the image.")
print(f'Please check if the path is correct: {file_path}')
else:
# Now, we'll "cut" a region from the original 'source_picture'.
# We're cropping the area:
# Height (y-axis): From row 100 to row 100 + 200 = 300 (not including row 300).
# Width (x-axis): From column 100 to column 100 + 300 = 400
# (not including column 400).
# This will give us a new image with
# a height of 200 pixels and a width of 300 pixels.
crop_image = source_picture[100 : 100 + 200, 100 : 100 + 300]
# --- Image Display Loop ---
# Enter an infinite loop to keep the image windows open.
while True:
# Display the original image in a window named 'Original Picture'.
cv.imshow('Original Picture', source_picture)
# Display the cropped image in a window named 'Cropped Picture'.
cv.imshow('Cropped Picture', crop_image)
# The cv.waitKey(1) function pauses the program for
# 1 millisecond to check for a key press.
key = cv.waitKey(1)
# If the ESC key is pressed (number 27), break the loop and end the display.
if key & 0xFF == 27:
break
# --- Release Resources ---
# Finally, close all image windows created by OpenCV to free up computer resources.
cv.destroyAllWindows()
def warpAffine(src: cv2.typing.MatLike,
# This is the "original image" you want to move.
M: cv2.typing.MatLike,
# This is a crucial "transformation matrix,"
# which contains the "secret formula" for how the image moves.
dsize: cv2.typing.Size,
# This is the "new image's size" after the movement (width, height).
dst: cv2.typing.MatLike | None = ...,
# The moved image will be stored here
# (usually you don't set this yourself; the function returns it).
flags: int = ...,
# Some extra flags, like the interpolation method,
# which affects image quality.
borderMode: int = ...,
# How to handle the image's edges when they go out of bounds
# (e.g., what color to fill them with).
borderValue: cv2.typing.Scalar = ...)
# The color value to fill the border with.
-> cv2.typing.MatLike: ...
import cv2 as cv
import numpy as np # We need NumPy to handle the point coordinates
# Set the file path for the original image.
# Don't forget to replace 'Pandora.png' with your own image!
file_path = 'Pandora.png'
# Use OpenCV's imread function to read the image.
source_picture = cv.imread(file_path)
# Check if the image was successfully loaded.
if source_picture is None:
print(f"Warning: Oops! Could not load the image.")
print(f'Please check if the path is correct: {file_path}')
else:
# --- 1. Define the "three reference points" on the original image ---
# We'll choose the top-left, top-right, and bottom-left corners as reference points.
# The coordinates must be of type float32 (np.float32).
src_points = np.float32([
[0.0, 0.0],
# Point 1: Top-left corner of the original image (x=0, y=0)
[source_picture.shape[1] - 1.0, 0.0],
# Point 2: Top-right corner (x=far right, y=0)
[0.0, source_picture.shape[0] - 1.0]
# Point 3: Bottom-left corner (x=0, y=far bottom)
])
# --- 2. Define where these reference points should go "after the move" ---
# Here, we'll make the whole image move slightly to the bottom right,
# with a bit of a squeeze and tilt.
dst_points = np.float32([
[0.0, source_picture.shape[0] * 0.1],
# Point 1 moves: y-axis moves down by 10% of the image's height
[source_picture.shape[1] * 0.9, 0.0],
# Point 2 moves: x-axis moves left by 10% of the image's width
[source_picture.shape[1] * 0.2, source_picture.shape[0] * 0.9]
# Point 3 moves: x-axis moves right by 20%, y-axis moves down by 10%
])
# --- 3. Calculate the image's "movement formula" (Transformation Matrix M) ---
# cv.getAffineTransform will automatically calculate a 2x3 transformation matrix
# (warp_matrix)
# for us based on src_points and dst_points,
# which contains the commands for how to
# translate, rotate, and scale the image.
warp_matrix = cv.getAffineTransform(src_points, dst_points)
# --- 4. Apply the "movement formula" to make the image really move! ---
# The cv.warpAffine function will take the original image (source_picture)
# and move it to a new position based on the warp_matrix we calculated.
# (source_picture.shape[1], source_picture.shape[0])
# specifies the size of the new image after the move.
# Here, we'll keep it the same size as the original.
modified_image = cv.warpAffine(source_picture, warp_matrix,
(source_picture.shape[1], source_picture.shape[0]))
# --- Image Display Loop ---
# Enter an infinite loop to keep the image windows open.
while True:
# Display the original image.
cv.imshow('Original Picture', source_picture)
# Display the moved image.
cv.imshow('Moved Picture', modified_image)
# Changed the window name to be clearer!
# The cv.waitKey(1) function pauses the program for
# 1 millisecond to check for a key press.
key = cv.waitKey(1)
# If the ESC key is pressed (number 27), break the loop and end the display.
if key & 0xFF == 27:
break
# --- Release Resources ---
# Finally, close all image windows created by OpenCV to free up computer resources.
cv.destroyAllWindows()
def flip(src: cv2.typing.MatLike,
# This is the "original image" you want to flip.
flipCode: int,
# This number determines how the image will be flipped!
dst: cv2.typing.MatLike | None = ...)
# The flipped image will be stored here (usually you don't set this yourself;
# the function returns it).
-> cv2.typing.MatLike: ...
import cv2 as cv
# Set the file path for the original image.
# Don't forget to replace 'Pandora.png' with your own image!
file_path = 'Pandora.png'
# Use OpenCV's imread function to read the image.
source_picture = cv.imread(file_path)
# Check if the image was successfully loaded.
if source_picture is None:
print(f"Warning: Oops! Could not load the image.")
print(f"Please check if the path is correct: {file_path}")
else:
# Here, we'll directly call the cv.flip function to flip the image.
# source_picture is the image we want to flip.
# 1 stands for a horizontal flip (left-to-right).
# modified_image will receive the flipped image data.
# Note: cv.flip returns a new image by default, so you don't need to use .copy()!
modified_image = cv.flip(source_picture, 1) # **Horizontal** flip (flipCode=1)
# --- Image Display Loop ---
# Enter an infinite loop to keep the image windows open.
while True:
# Display the original image.
cv.imshow('Original Picture', source_picture)
# Display the flipped image.
cv.imshow("Flipped Picture (Horizontal)", modified_image)
# Changed the window name to be clearer!
# The cv.waitKey(1) function pauses the program for
# 1 millisecond to check for a key press.
key = cv.waitKey(1)
# If the ESC key is pressed (number 27), break the loop and end the display.
if key & 0xFF == 27:
break
# --- Release Resources ---
# Finally, close all image windows created by OpenCV to free up computer resources.
cv.destroyAllWindows()
def cvtColor(src: cv2.typing.MatLike,
# This is the "original image" you want to change colors for.
code: int,
# This "conversion code" determines which
# color mode to convert from and to!
dst: cv2.typing.MatLike | None = ...,
# The color-changed image will be stored here
# (usually you don't set this yourself; the function returns it).
dstCn: int = ...,
# The number of channels for the output image
# (usually you don't set this; the function figures it out).
hint: AlgorithmHint = ...)
# A hint for the conversion algorithm (usually the default is fine).
-> cv2.typing.MatLike: ...
import cv2 as cv
# Set the file path for the original image.
# Don't forget to replace 'Pandora.png' with your own image!
file_path = 'Pandora.png'
# Use OpenCV's imread function to read the image.
source_picture = cv.imread(file_path)
# Check if the image was successfully loaded.
if source_picture is None:
print(f"Warning: Oops! Could not load the image.")
print(f" Please check if the path is correct: {file_path}")
else:
# --- Image Grayscaling! ---
# Use the cv.cvtColor function to convert the original color image (source_picture)
# to a grayscale image.
# cv.COLOR_BGR2GRAY is the conversion code,
# which means "convert from BGR color mode to grayscale mode."
# The grayscale image will only have one color channel (representing brightness),
# no more red, green, and blue channels!
modified_image = cv.cvtColor(source_picture, cv.COLOR_BGR2GRAY)
# --- Image Display Loop ---
# Enter an infinite loop to keep the image windows open.
while True:
# Display the original color image.
cv.imshow('Original Picture (Color)', source_picture)
# Display the converted grayscale image.
cv.imshow("Modified Picture (Grayscale)", modified_image)
# Changed the window name to be clearer!
# The cv.waitKey(1) function pauses the program for 1 millisecond to check for
# a key press.
key = cv.waitKey(1)
# If the ESC key is pressed (number 27), break the loop and end the display.
if key & 0xFF == 27:
break
# --- Release Resources ---
# Finally, close all image windows created by OpenCV to free up computer resources.
cv.destroyAllWindows()
def threshold(src: cv2.typing.MatLike,
# This is the "grayscale image" you want to process
# (Note: it must be a grayscale image!).
thresh: float,
# This is your "threshold value." Pixels brighter than this value will
# be processed.
maxval: float,
# This is the "maximum brightness value" for pixels that
# exceed the threshold (usually 255 for white).
type: int,
# This is the "threshold type," which tells the function
# how to classify pixels based on the threshold.
dst: cv2.typing.MatLike | None = ...)
-> tuple[float, cv2.typing.MatLike]: ...
import cv2 as cv
# Set the file path for the original image.
# Don't forget to replace 'Pandora.png' with your own image!
file_path = 'Pandora.png'
# Use OpenCV's imread function to read the image.
source_picture = cv.imread(file_path)
# Check if the image was successfully loaded.
if source_picture is None:
print(f"Warning: Oops! Could not load the image. ")
print(f"lease check if the path is correct: {file_path}")
else:
# --- 1. First, turn the color image into a grayscale image! ---
# cvtColor is our "color-changing magician," converting BGR color to grayscale.
convert_img = cv.cvtColor(source_picture, cv.COLOR_BGR2GRAY)
# --- 2. Perform image binarization! ---
# Use the cv.threshold function to binarize the grayscale image.
# convert_img: The input grayscale image.
# 200: This is the "threshold value" we set. Pixels with brightness higher than 200.
# 255: Pixels that exceed the threshold will become 255 (white).
# cv.THRESH_BINARY: This is the threshold type. It means "if pixel brightness > 200,
# it becomes 255; otherwise, it becomes 0."
# ret: The returned actual threshold value (here, it's 200).
# modified_image: This is the binarized black and white image.
# The pixels inside will only have values of 0 or 255.
ret, modified_image = cv.threshold(convert_img, 200, 255, cv.THRESH_BINARY)
# --- Image Display Loop ---
# Enter an infinite loop to keep the image windows open.
while True:
# Display the original color image.
cv.imshow('Original Picture (Color)', source_picture)
# Display the binarized black and white image.
cv.imshow("Modified Picture (Binary)", modified_image)
# Changed the window name to be clearer!
# The cv.waitKey(1) function pauses the program for 1 millisecond to check for
# a key press.
key = cv.waitKey(1)
# If the ESC key is pressed (number 27), break the loop and end the display.
if key & 0xFF == 27:
break
# --- Release Resources ---
# Finally, close all image windows created by OpenCV to free up computer resources.
cv.destroyAllWindows()
def Canny(image: cv2.typing.MatLike,
# This is the "grayscale image" you want to detect edges on
# (Note: it must be a grayscale image!).
threshold1: float,
# This is the first "low threshold value."
threshold2: float,
# This is the second "high threshold value."
edges: cv2.typing.MatLike | None = ...,
# The detected edge image will be stored here
# (you usually don't set this yourself).
apertureSize: int = ...,
# A parameter that affects the fineness of the edge detection
# (usually the default is fine).
L2gradient: bool = ...)
# The method for calculating the gradient (usually the default is fine).
-> cv2.typing.MatLike: ...
import cv2 as cv
# Set the file path for the original image.
# Don't forget to replace 'Pandora.png' with your own image!
file_path = 'Pandora.png'
# Use OpenCV's imread function to read the image.
source_picture = cv.imread(file_path)
# Check if the image was successfully loaded.
if source_picture is None:
print(f"Warning: Oops! Could not load the image.")
print(f" Please check if the path is correct: {file_path}")
else:
# --- 1. First, turn the color image into a grayscale image! ---
# cvtColor is our "color-changing magician," converting BGR color to grayscale.
convert_img = cv.cvtColor(source_picture, cv.COLOR_BGR2GRAY)
# --- 2. Perform Canny edge detection! ---
# Use the cv.Canny function to find the edges of the grayscale image.
# convert_img: The input grayscale image.
# 50: This is the "low threshold value."
# 150: This is the "high threshold value."
# modified_image: This is the detected edge image,
# where edges are displayed as white lines.
modified_image = cv.Canny(convert_img, 50, 150)
# --- Image Display Loop ---
# Enter an infinite loop to keep the image windows open.
while True:
# Display the original color image.
cv.imshow('Original Picture (Color)', source_picture)
# Display the Canny edge-detected image.
cv.imshow('Edge Detected Picture', modified_image)
# Changed the window name to be clearer!
# The cv.waitKey(1) function pauses the program for
# 1 millisecond to check for a key press.
key = cv.waitKey(1)
# If the ESC key is pressed (number 27), break the loop and end the display.
if key & 0xFF == 27:
break
# --- Release Resources ---
# Finally, close all image windows created by OpenCV to free up computer resources.
cv.destroyAllWindows()