使用 Python 和 OpenCV 库直接查看数字图像属性。这将巩固你对图像尺寸、颜色通道、数据类型和像素值的理解。首先,请确保你已经安装了 OpenCV (pip install opencv-python),并且在你的 Python 脚本或 Jupyter Notebook 文件所在的目录中准备好一个图像文件(我们称之为 example_image.jpg)。加载图像我们首先导入 OpenCV 库,然后使用 imread 函数加载图像,这个函数我们已经在上一节中简单提及过。import cv2 import numpy as np # 常用与 OpenCV 结合使用 # 从文件加载图像 # 标志 '1' 以彩色(BGR 格式)加载。使用 '0' 加载灰度图像。 image_path = 'example_image.jpg' # 替换为你的图像文件路径 img = cv2.imread(image_path, 1) # 检查图像是否成功加载 if img is None: print(f"错误:无法读取图像文件:{image_path}") else: print("图像加载成功!") # 接下来的步骤中我们将在此处添加更多代码如果图像正确加载,img 变量现在将以 NumPy 数组的形式保存图像数据。如果出现错误信息,请再次确认图像文件路径是否正确,并且文件确实存在。检查图像尺寸 (Shape)图像大小是最基本属性之一。在 NumPy 数组中,图像大小存储在 shape 属性中。# 确保图像已加载,再继续 if img is not None: # 获取图像尺寸 height, width, channels = img.shape print(f"图像高度:{height} 像素") print(f"图像宽度:{width} 像素") print(f"颜色通道数:{channels}") # 计算总像素数 total_pixels = height * width print(f"总像素数:{total_pixels}")当你用彩色图像运行这段代码时,img.shape 会返回一个包含三个值的元组:(height, width, channels)。高度: 像素行数。宽度: 像素列数。通道数: 每个像素的值数量。对于 OpenCV 加载的标准彩色图像,这个值是 3,表示蓝色、绿色和红色(BGR)值。如果是灰度图像呢? 如果你以灰度模式加载图像(cv2.imread(image_path, 0)),shape 属性将只包含两个值:(height, width)。由于每个像素只有一个强度值,因此没有颜色通道的第三个维度。你需要稍微调整代码来处理这种情况:# 以灰度模式加载 img_gray = cv2.imread(image_path, 0) if img_gray is not None: # 检查尺寸 - 如果尝试解包 3 个值会引发错误 if len(img_gray.shape) == 2: height, width = img_gray.shape channels = 1 # 灰度图像有 1 个通道 print(f"(灰度) 高度: {height}, 宽度: {width}, 通道数: {channels}") # 处理可能意外加载的彩色图像(使用标志 0 时不应发生) elif len(img_gray.shape) == 3: height, width, channels = img_gray.shape print(f"(彩色?) 高度: {height}, 宽度: {width}, 通道数: {channels}") else: print(f"错误:无法读取图像文件:{image_path}")检查数据类型数字图像使用特定的数值数据类型存储像素值。这决定了每个像素分量的可能值范围。OpenCV 默认通常使用 uint8(无符号 8 位整数)。# 确保图像已加载,再继续 if img is not None: # 获取图像数组的数据类型 data_type = img.dtype print(f"图像数据类型:{data_type}")输出很可能是 uint8。这意味着每个像素的每个颜色分量(B、G、R)都由一个介于 0 到 255(含)之间的整数表示。在执行图像操作时,了解数据类型很重要,因为操作可能依赖于这个范围。访问像素值我们可以使用数组索引访问特定像素的值。请记住,图像使用一个坐标系统,其原点 (0, 0) 位于左上角。第一个索引表示行(y 坐标,即高度),第二个索引表示列(x 坐标,即宽度)。# 确保图像已加载,再继续 if img is not None: # 定义坐标 (y, x) - 记住 y 是行,x 是列 px_y, px_x = 50, 100 # 示例坐标 # 检查坐标是否在图像范围内 height, width, _ = img.shape if 0 <= px_y < height and 0 <= px_x < width: # 访问 (y, x) 处的像素值 pixel_value = img[px_y, px_x] print(f"({px_y}, {px_x}) 处的像素值:{pixel_value}") # 对于彩色图像 (BGR),pixel_value 是一个数组 [蓝色, 绿色, 红色] blue = pixel_value[0] green = pixel_value[1] red = pixel_value[2] print(f" 蓝色: {blue}, 绿色: {green}, 红色: {red}") else: print(f"坐标 ({px_y}, {px_x}) 超出图像边界 ({height}x{width})。") # 灰度图像示例(如果 img_gray 已加载) if 'img_gray' in locals() and img_gray is not None: height_g, width_g = img_gray.shape if 0 <= px_y < height_g and 0 <= px_x < width_g: pixel_intensity = img_gray[px_y, px_x] print(f"({px_y}, {px_x}) 处的灰度强度:{pixel_intensity}") else: print(f"灰度坐标 ({px_y}, {px_x}) 超出边界 ({height_g}x{width_g})。") 对于彩色图像,img[y, x] 返回一个包含该特定像素的 B、G 和 R 值的列表或数组(请记住 OpenCV 默认使用 BGR 顺序,而不是 RGB)。对于灰度图像,img_gray[y, x] 返回一个表示强度值的单个整数。完整示例以下是一个结合了这些步骤的完整脚本:import cv2 import numpy as np # --- 配置 --- image_path = 'example_image.jpg' # 将此改为你的图像文件 pixel_coordinate_y = 50 # 如有需要,更改此坐标 pixel_coordinate_x = 100 # 如有需要,更改此坐标 # --- 加载图像 --- # 以彩色加载 img_color = cv2.imread(image_path, 1) # 以灰度加载 img_gray = cv2.imread(image_path, 0) print(f"--- 彩色图像 ({image_path}) 的属性 ---") if img_color is not None: # 尺寸 height, width, channels = img_color.shape print(f"尺寸 (高x宽x通道): {height} x {width} x {channels}") print(f"总像素数: {height * width}") # 数据类型 print(f"数据类型: {img_color.dtype}") # 像素值 if 0 <= pixel_coordinate_y < height and 0 <= pixel_coordinate_x < width: bgr_value = img_color[pixel_coordinate_y, pixel_coordinate_x] print(f"({pixel_coordinate_y}, {pixel_coordinate_x}) 处的 BGR 值: {bgr_value}") else: print(f"坐标 ({pixel_coordinate_y}, {pixel_coordinate_x}) 超出边界。") else: print("错误:无法加载彩色图像。") print(f"\n--- 灰度图像 ({image_path}) 的属性 ---") if img_gray is not None: # 尺寸 height_g, width_g = img_gray.shape print(f"尺寸 (高x宽): {height_g} x {width_g}") print(f"总像素数: {height_g * width_g}") # 数据类型 print(f"数据类型: {img_gray.dtype}") # 像素值 if 0 <= pixel_coordinate_y < height_g and 0 <= pixel_coordinate_x < width_g: intensity = img_gray[pixel_coordinate_y, pixel_coordinate_x] print(f"({pixel_coordinate_y}, {pixel_coordinate_x}) 处的强度值: {intensity}") else: print(f"坐标 ({pixel_coordinate_y}, {pixel_coordinate_x}) 超出边界。") else: print("错误:无法加载灰度图像。") 试一试:用你自己的图像运行脚本。更改 pixel_coordinate_y 和 pixel_coordinate_x 的值,以查看图像的不同区域。尝试靠近角落(例如 0, 0)和靠近中心的坐标。使用图像编辑工具找到特定像素的颜色,并查看它是否与脚本报告的 BGR 值匹配(请记住 BGR 顺序)。尝试加载不同的图像文件类型(例如 .png),查看属性是否保持一致。这项动手实践能让你具体感受图像是如何用数字表示的,为下一章做准备,届时我们将开始操作这些像素值以执行基本的图像处理。