• 【多媒体技术与实践】使用OpenCV处理图像(实验三.上)


    1:图像直方图

    将原彩色图像转成灰度图像,得到该灰度图像的灰度直方图,并对灰度直方图进行直方图均衡化,将原图、灰度图、直方图及均衡化后的直方图一起拼接为一张图片

    1. import cv2
    2. import numpy as np
    3. import matplotlib.pyplot as plt
    4. # 读取原彩色图像
    5. img = cv2.imread(r'input.jpg')
    6. # 将原彩色图像转换为灰度图像
    7. gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    8. # 获取灰度直方图
    9. hist, bins = np.histogram(gray_img.flatten(), 256, [0, 256])
    10. # 进行直方图均衡化
    11. equ_img = cv2.equalizeHist(gray_img)
    12. # 获取均衡化后的灰度直方图
    13. equ_hist, bins = np.histogram(equ_img.flatten(), 256, [0, 256])
    14. # 绘制原图、灰度图、直方图和均衡化后的直方图
    15. fig, axs = plt.subplots(2, 2)
    16. axs[0, 0].imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
    17. axs[0, 0].set_title('Original Image')
    18. axs[0, 1].imshow(gray_img, cmap='gray')
    19. axs[0, 1].set_title('Grayscale Image')
    20. axs[1, 0].hist(gray_img.flatten(), 256, [0, 256])
    21. axs[1, 0].set_title('Original Histogram')
    22. axs[1, 1].hist(equ_img.flatten(), 256, [0, 256])
    23. axs[1, 1].set_title('Equalized Histogram')
    24. plt.tight_layout()
    25. # 保存拼接后的图片
    26. plt.savefig(r'out.jpg')

    eg.

    2:图像变换

    对作品一的灰度图像进行傅里叶变换,转成频域图像,对该频域图像分别进行低通和高通滤波后做傅里叶逆变换还原,得到两幅图像,将灰度图像、频域图像、低通还原图像及高通还原图像一起拼接为一张图片

    1. import cv2
    2. import numpy as np
    3. import matplotlib.pyplot as plt
    4. # 读取原彩色图像
    5. img = cv2.imread(r'input.jpg')
    6. # 将原彩色图像转换为灰度图像
    7. gray_image = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    8. # 进行傅里叶变换
    9. f_transform = np.fft.fft2(gray_image)
    10. fshift = np.fft.fftshift(f_transform)
    11. magnitude_spectrum = 20 * np.log(np.abs(fshift))
    12. # 创建一个低通滤波器(示例中使用方形掩码)
    13. rows, cols = gray_image.shape
    14. crow, ccol = rows // 2, cols // 2
    15. low_pass_filter = np.zeros((rows, cols), np.uint8)
    16. low_pass_filter[crow-30:crow+30, ccol-30:ccol+30] = 1
    17. # 应用低通滤波器
    18. fshift_low = fshift * low_pass_filter
    19. # 创建一个高通滤波器(高通滤波器是低通滤波器的逆)
    20. high_pass_filter = 1 - low_pass_filter
    21. # 应用高通滤波器
    22. fshift_high = fshift * high_pass_filter
    23. # 傅里叶逆变换还原
    24. img_low = np.fft.ifftshift(fshift_low)
    25. img_low = np.fft.ifft2(img_low)
    26. img_low = np.abs(img_low)
    27. img_high = np.fft.ifftshift(fshift_high)
    28. img_high = np.fft.ifft2(img_high)
    29. img_high = np.abs(img_high)
    30. # 调整图像位置
    31. result_image = np.zeros((rows*2, cols*2), dtype=np.uint8)
    32. result_image[0:rows, 0:cols] = gray_image
    33. result_image[0:rows, cols:] = magnitude_spectrum
    34. result_image[rows:, 0:cols] = img_low
    35. result_image[rows:, cols:] = img_high
    36. # 显示并保存拼接后的图像
    37. cv2.imwrite(r'output.jpg', result_image)
    38. plt.imshow(result_image, cmap='gray')
    39. plt.axis('off')
    40. plt.show()

    eg.

    3:图像平滑

    从作品一的原彩色图像中的任意位置截取一块大小为 300*400 的图像块,然后添加高斯噪声,并用任意一种平滑方法(均值滤波、高斯滤波、中值滤波)对图像进行平滑处理,将原图截取的图像块、加噪图像及平滑图像一起拼接为一张图片

    1. import cv2
    2. import numpy as np
    3. # 读取原彩色图像
    4. original_image = cv2.imread(r'input.jpg')
    5. # 截取图像块
    6. x, y = 100, 200 # 起始坐标,请根据需要修改
    7. roi = original_image[y:y+300, x:x+400]
    8. # 添加高斯噪声
    9. mean = 0
    10. stddev = 25 # 调整噪声的强度
    11. gaussian_noise = np.random.normal(mean, stddev, roi.shape).astype(np.uint8)
    12. noisy_roi = cv2.add(roi, gaussian_noise)
    13. # 使用均值滤波对图像进行平滑处理
    14. smoothed_mean = cv2.blur(noisy_roi, (5, 5)) # 调整内核大小
    15. # 创建一个空白的拼接图像
    16. result_image = np.zeros((300, 1200, 3), dtype=np.uint8)
    17. # 将原图截取的图像块、加噪图像和平滑图像拼接在一起
    18. result_image[0:300, 0:400] = roi
    19. result_image[0:300, 400:800] = noisy_roi
    20. result_image[0:300, 800:1200] = smoothed_mean
    21. # 保存拼接后的图像
    22. cv2.imwrite(r'output.jpg', result_image)

    eg.

  • 相关阅读:
    Java idea编译器工程out目录修改
    解析几何@平面上点到直线的距离@点到平面的距离@空间中点到直线的距离
    容器化 | 在 KubeSphere 中部署 MySQL 集群
    基于Keilv5新建STM32F030工程
    为什么 Go 语言 struct 要使用 tags
    第八章 设计zrlog项目接口自动化测试框架(8.5章节)
    【Qt】QTabWidget如何添加控件到Tab页水平位置
    linux笔记8--安装软件
    微机原理与技术接口笔记
    pytest+allure生成测试报告
  • 原文地址:https://blog.csdn.net/m0_65787507/article/details/133841899