Python图像处理示例

环境准备
pip install opencv-python pillow numpy matplotlib scikit-image
图像读取与显示
import cv2
import numpy as np
from PIL import Image
import matplotlib.pyplot as plt

# 读取图像
img = cv2.imread('image.jpg')
# OpenCV使用BGR格式,转换为RGB用于显示
img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)

# 显示图像
plt.imshow(img_rgb)
plt.axis('off')
plt.show()
几何变换示例
def resize_image(img_path, new_width, new_height):
    img = cv2.imread(img_path)
    resized = cv2.resize(img, (new_width, new_height), interpolation=cv2.INTER_LINEAR)
    return resized

def rotate_image(img, angle):
    height, width = img.shape[:2]
    center = (width//2, height//2)
    rotation_matrix = cv2.getRotationMatrix2D(center, angle, 1.0)
    rotated = cv2.warpAffine(img, rotation_matrix, (width, height))
    return rotated
滤波算法示例
def apply_filters(img_path):
    img = cv2.imread(img_path)

    # 均值滤波
    mean_filtered = cv2.blur(img, (5, 5))

    # 高斯滤波
    gaussian_filtered = cv2.GaussianBlur(img, (5, 5), 0)

    # 中值滤波
    median_filtered = cv2.medianBlur(img, 5)

    # 双边滤波(保边去噪)
    bilateral_filtered = cv2.bilateralFilter(img, 9, 75, 75)

    return {
        'original': img,
        'mean': mean_filtered,
        'gaussian': gaussian_filtered,
        'median': median_filtered,
        'bilateral': bilateral_filtered
    }
边缘检测示例
def edge_detection(img_path):
    img = cv2.imread(img_path, 0)  # 读取为灰度图

    # Sobel 边缘检测
    sobel_x = cv2.Sobel(img, cv2.CV_64F, 1, 0, ksize=3)
    sobel_y = cv2.Sobel(img, cv2.CV_64F, 0, 1, ksize=3)
    sobel_combined = np.sqrt(sobel_x**2 + sobel_y**2)

    # Canny 边缘检测
    canny_edges = cv2.Canny(img, threshold1=50, threshold2=150)

    # Laplacian 边缘检测
    laplacian = cv2.Laplacian(img, cv2.CV_64F)

    return {
        'sobel': sobel_combined,
        'canny': canny_edges,
        'laplacian': laplacian
    }
形态学操作示例
def morphological_operations(img_path):
    img = cv2.imread(img_path, 0)  # 读取为灰度图

    # 定义结构元素
    kernel = np.ones((5, 5), np.uint8)

    # 腐蚀
    erosion = cv2.erode(img, kernel, iterations=1)

    # 膨胀
    dilation = cv2.dilate(img, kernel, iterations=1)

    # 开运算(先腐蚀后膨胀)
    opening = cv2.morphologyEx(img, cv2.MORPH_OPEN, kernel)

    # 闭运算(先膨胀后腐蚀)
    closing = cv2.morphologyEx(img, cv2.MORPH_CLOSE, kernel)

    # 梯度形态学
    gradient = cv2.morphologyEx(img, cv2.MORPH_GRADIENT, kernel)

    return {
        'erosion': erosion,
        'dilation': dilation,
        'opening': opening,
        'closing': closing,
        'gradient': gradient
    }
色彩空间转换示例
def color_space_conversions(img_path):
    img = cv2.imread(img_path)

    # BGR 转 RGB
    img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)

    # BGR 转 HSV
    img_hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)

    # BGR 转灰度
    img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

    # BGR 转 LAB
    img_lab = cv2.cvtColor(img, cv2.COLOR_BGR2LAB)

    # BGR 转 YCrCb
    img_ycrcb = cv2.cvtColor(img, cv2.COLOR_BGR2YCrCb)

    return {
        'rgb': img_rgb,
        'hsv': img_hsv,
        'gray': img_gray,
        'lab': img_lab,
        'ycrcb': img_ycrcb
    }
直方图处理示例
def histogram_processing(img_path):
    img = cv2.imread(img_path, 0)  # 读取为灰度图

    # 计算直方图
    hist, bins = np.histogram(img.flatten(), 256, [0, 256])

    # 直方图均衡化
    equalized = cv2.equalizeHist(img)

    # 创建 CLAHE 对象(限制对比度自适应直方图均衡化)
    clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8, 8))
    clahe_result = clahe.apply(img)

    return {
        'histogram': hist,
        'equalized': equalized,
        'clahe': clahe_result
    }
阈值处理示例
def thresholding(img_path):
    img = cv2.imread(img_path, 0)  # 读取为灰度图

    # 简单阈值
    ret, thresh_binary = cv2.threshold(img, 127, 255, cv2.THRESH_BINARY)

    # 自适应阈值(高斯)
    adaptive_gaussian = cv2.adaptiveThreshold(
        img, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C,
        cv2.THRESH_BINARY, 11, 2
    )

    # 自适应阈值(均值)
    adaptive_mean = cv2.adaptiveThreshold(
        img, 255, cv2.ADAPTIVE_THRESH_MEAN_C,
        cv2.THRESH_BINARY, 11, 2
    )

    # Otsu 阈值
    ret, otsu = cv2.threshold(img, 0, 255,
                              cv2.THRESH_BINARY + cv2.THRESH_OTSU)

    return {
        'binary': thresh_binary,
        'adaptive_gaussian': adaptive_gaussian,
        'adaptive_mean': adaptive_mean,
        'otsu': otsu
    }
轮廓检测示例
def contour_detection(img_path):
    img = cv2.imread(img_path)
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

    # 阈值处理
    ret, thresh = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY)

    # 查找轮廓
    contours, hierarchy = cv2.findContours(
        thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE
    )

    # 绘制轮廓
    contour_img = img.copy()
    cv2.drawContours(contour_img, contours, -1, (0, 255, 0), 2)

    # 查找轮廓的边界框
    bounding_boxes = [cv2.boundingRect(c) for c in contours]

    return {
        'contours': contours,
        'hierarchy': hierarchy,
        'contour_image': contour_img,
        'bounding_boxes': bounding_boxes
    }
霍夫变换直线检测示例
def hough_line_detection(img_path):
    img = cv2.imread(img_path)
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

    # 边缘检测
    edges = cv2.Canny(gray, 50, 150, apertureSize=3)

    # 标准霍夫变换
    lines = cv2.HoughLines(edges, 1, np.pi / 180, 200)

    # 概率霍夫变换
    lines_prob = cv2.HoughLinesP(
        edges, 1, np.pi / 180, threshold=100,
        minLineLength=100, maxLineGap=10
    )

    # 在原图上绘制检测到的直线
    result = img.copy()
    if lines_prob is not None:
        for line in lines_prob:
            x1, y1, x2, y2 = line[0]
            cv2.line(result, (x1, y1), (x2, y2), (0, 255, 0), 2)

    return {
        'edges': edges,
        'lines': lines,
        'lines_probabilistic': lines_prob,
        'result': result
    }
频域处理示例
def frequency_domain_processing(img_path):
    img = cv2.imread(img_path, 0)  # 读取为灰度图

    # 快速傅里叶变换
    f_transform = np.fft.fft2(img)
    f_shift = np.fft.fftshift(f_transform)

    # 计算幅度谱
    magnitude_spectrum = 20 * np.log(np.abs(f_shift) + 1)

    # 创建高通滤波器
    rows, cols = img.shape
    crow, ccol = rows // 2, cols // 2
    mask = np.ones((rows, cols), np.uint8)
    r, c = 30, 30  # 截止频率
    mask[crow-r:crow+r, ccol-r:ccol+r] = 0

    # 应用高通滤波
    f_shift_masked = f_shift * mask
    f_ishift = np.fft.ifftshift(f_shift_masked)
    img_back = np.fft.ifft2(f_ishift)
    img_back = np.abs(img_back)

    return {
        'magnitude_spectrum': magnitude_spectrum,
        'highpass_result': img_back,
        'mask': mask
    }
特征提取示例
def feature_extraction(img_path):
    from skimage import feature, measure

    img = cv2.imread(img_path, 0)  # 读取为灰度图

    # Canny 边缘检测
    edges = feature.canny(img, sigma=1)

    # Harris 角点检测
    corners = feature.corner_harris(img)
    corner_coords = feature.corner_peaks(corners, min_distance=5)

    # SIFT 特征点检测
    sift = cv2.SIFT_create()
    keypoints, descriptors = sift.detectAndCompute(img, None)

    # ORB 特征点检测
    orb = cv2.ORB_create()
    orb_keypoints, orb_descriptors = orb.detectAndCompute(img, None)

    # 绘制特征点
    feature_img = cv2.drawKeypoints(
        img, keypoints, None,
        flags=cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS
    )

    return {
        'edges': edges,
        'corners': corner_coords,
        'sift_keypoints': keypoints,
        'sift_descriptors': descriptors,
        'orb_keypoints': orb_keypoints,
        'feature_image': feature_img
    }
图像金字塔与多尺度检测示例
def image_pyramid(img_path):
    img = cv2.imread(img_path)

    # 高斯金字塔
    gaussian_pyramid = [img]
    for i in range(4):
        img = cv2.pyrDown(img)
        gaussian_pyramid.append(img)

    # 拉普拉斯金字塔
    laplacian_pyramid = []
    for i in range(len(gaussian_pyramid) - 1, 0, -1):
        expanded = cv2.pyrUp(gaussian_pyramid[i])
        laplacian = cv2.subtract(gaussian_pyramid[i-1], expanded)
        laplacian_pyramid.append(laplacian)

    return {
        'gaussian': gaussian_pyramid,
        'laplacian': laplacian_pyramid
    }
完整示例:综合图像处理流程
def complete_image_processing_pipeline(img_path):
    """
    完整的图像处理流程示例
    """
    # 1. 读取图像
    img = cv2.imread(img_path)
    img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)

    # 2. 预处理 - 去噪
    denoised = cv2.bilateralFilter(img, 9, 75, 75)

    # 3. 转换为灰度图
    gray = cv2.cvtColor(denoised, cv2.COLOR_BGR2GRAY)

    # 4. 增强对比度 - 直方图均衡化
    equalized = cv2.equalizeHist(gray)

    # 5. 边缘检测
    edges = cv2.Canny(equalized, 50, 150)

    # 6. 形态学操作 - 填充小孔洞
    kernel = np.ones((3, 3), np.uint8)
    dilated_edges = cv2.dilate(edges, kernel, iterations=2)
    eroded_edges = cv2.erode(dilated_edges, kernel, iterations=1)

    # 7. 查找轮廓
    contours, _ = cv2.findContours(
        eroded_edges, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE
    )

    # 8. 绘制结果
    result = img_rgb.copy()
    cv2.drawContours(result, contours, -1, (0, 255, 0), 2)

    return {
        'original': img_rgb,
        'denoised': denoised,
        'gray': gray,
        'equalized': equalized,
        'edges': edges,
        'processed_edges': eroded_edges,
        'result': result,
        'contour_count': len(contours)
    }

# 使用示例
if __name__ == "__main__":
    result = complete_image_processing_pipeline('sample.jpg')
    print(f"检测到 {result['contour_count']} 个轮廓")

    # 显示结果
    import matplotlib.pyplot as plt
    plt.figure(figsize=(15, 10))
    plt.subplot(2, 4, 1)
    plt.imshow(result['original'])
    plt.title('原始图像')
    plt.subplot(2, 4, 4)
    plt.imshow(result['result'])
    plt.title('最终结果')
    plt.tight_layout()
    plt.show()