Basic Image Processing with OpenCV: Reading, Displaying, and Manipulating Images in Python
This tutorial introduces basic image processing techniques using OpenCV in Python, covering image reading, displaying, grayscale conversion, cropping, resizing, rotation, flipping, and saving, with step‑by‑step code examples and explanations to help beginners apply these operations in real projects.
Goal: Learn basic image processing techniques.
Learning content: OpenCV basics, practice using OpenCV for image reading, display, and basic processing.
Code examples include the following steps:
1. Import required libraries
import cv2 import numpy as np import matplotlib.pyplot as plt2. Image reading
# 读取图像 image_path = 'path_to_your_image.jpg' # 替换为你的图像路径 image = cv2.imread(image_path) # 检查图像是否成功读取 if image is None: print("图像读取失败,请检查路径是否正确。") else: print("图像读取成功!")3. Image display
# 使用 OpenCV 显示图像 cv2.imshow('原图', image) cv2.waitKey(0) cv2.destroyAllWindows() # 使用 Matplotlib 显示图像 plt.imshow(cv2.cvtColor(image, cv2.COLOR_BGR2RGB)) plt.title('原图') plt.axis('off') plt.show()4. Image basic information
# 获取图像的基本信息 height, width, channels = image.shape print(f"图像高度: {height} 像素") print(f"图像宽度: {width} 像素") print(f"图像通道数: {channels}")5. Image grayscale conversion
# 将图像转换为灰度图像 gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) # 显示灰度图像 cv2.imshow('灰度图', gray_image) cv2.waitKey(0) cv2.destroyAllWindows() # 使用 Matplotlib 显示灰度图像 plt.imshow(gray_image, cmap='gray') plt.title('灰度图') plt.axis('off') plt.show()6. Image cropping
# 裁剪图像 cropped_image = image[100:400, 100:400] # 显示裁剪后的图像 cv2.imshow('裁剪图', cropped_image) cv2.waitKey(0) cv2.destroyAllWindows() # 使用 Matplotlib 显示裁剪后的图像 plt.imshow(cv2.cvtColor(cropped_image, cv2.COLOR_BGR2RGB)) plt.title('裁剪图') plt.axis('off') plt.show()7. Image resizing
# 缩放图像 resized_image = cv2.resize(image, (width // 2, height // 2)) # 显示缩放后的图像 cv2.imshow('缩放图', resized_image) cv2.waitKey(0) cv2.destroyAllWindows() # 使用 Matplotlib 显示缩放后的图像 plt.imshow(cv2.cvtColor(resized_image, cv2.COLOR_BGR2RGB)) plt.title('缩放图') plt.axis('off') plt.show()8. Image rotation
# 旋转图像 center = (width // 2, height // 2) angle = 45 scale = 1.0 rotation_matrix = cv2.getRotationMatrix2D(center, angle, scale) rotated_image = cv2.warpAffine(image, rotation_matrix, (width, height)) # 显示旋转后的图像 cv2.imshow('旋转图', rotated_image) cv2.waitKey(0) cv2.destroyAllWindows() # 使用 Matplotlib 显示旋转后的图像 plt.imshow(cv2.cvtColor(rotated_image, cv2.COLOR_BGR2RGB)) plt.title('旋转图') plt.axis('off') plt.show()9. Image flipping
# 翻转图像 flipped_image = cv2.flip(image, 1) # 1 表示水平翻转,0 表示垂直翻转,-1 表示水平和垂直翻转 # 显示翻转后的图像 cv2.imshow('翻转图', flipped_image) cv2.waitKey(0) cv2.destroyAllWindows() # 使用 Matplotlib 显示翻转后的图像 plt.imshow(cv2.cvtColor(flipped_image, cv2.COLOR_BGR2RGB)) plt.title('翻转图') plt.axis('off') plt.show()10. Image saving
# 保存处理后的图像 output_path = 'processed_image.jpg' cv2.imwrite(output_path, flipped_image) print(f"处理后的图像已保存到 {output_path}")Summary: After completing these exercises, you should be able to perform basic image processing tasks with OpenCV, including reading, displaying, converting to grayscale, cropping, resizing, rotating, flipping, and saving images.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
