• OpenCV-图像基础处理


    目录

    1 彩色(RGB)图像

    2 灰度图像

    3 黑白图像


    1 彩色(RGB)图像

    • 使用cv2.imread()函数加载RGB图像;
    • 使用cv2.split()函数分割颜色通道;
    • 将BGR颜色格式转换成RGB颜色格式;
    • 使用matplotlib或cv2.imshow()函数可视化图像。
    1. import cv2
    2. from matplotlib import pyplot as plt
    3. import numpy as np
    4. # 加载RGB图片
    5. img_OpenCV = cv2.imread(r"demo.jpeg")
    6. print(type(img_OpenCV), img_OpenCV.shape) # (484, 515, 3)
    7. # 分割颜色通道,opencv使用BGR颜色格式
    8. b, g, r = cv2.split(img_OpenCV)
    9. # imread()方法返回ndarray对象,因此也可以使用切片分割颜色通道
    10. B = img_OpenCV[:, :, 0]
    11. G = img_OpenCV[:, :, 1]
    12. R = img_OpenCV[:, :, 2]
    13. print((b == B).all(), (g == G).all(), (r == R).all()) # True True True
    14. # 转换成RGB颜色格式
    15. img_matplotlib = cv2.merge([r, g, b])
    16. img_ndarray = img_OpenCV[:, :, ::-1]
    17. print((img_matplotlib == img_ndarray).all()) # True
    18. # 使用matplotlib可视化图片
    19. plt.subplot(121)
    20. plt.imshow(img_OpenCV)
    21. plt.subplot(122)
    22. plt.imshow(img_matplotlib)
    23. # plt.show()
    24. # 使用opencv可视化图片
    25. cv2.imshow('BGR image', img_OpenCV)
    26. cv2.imshow('RGB image', img_matplotlib)
    27. # 拼接图片
    28. img_connect = np.concatenate((img_OpenCV, img_matplotlib), axis=1)
    29. cv2.imshow('BGR image-RGB image', img_connect)
    30. cv2.waitKey(0) # 等待键盘动作
    31. cv2.destroyAllWindows() # 关闭窗口

    2 灰度图像

    • 使用cv2.imread()函数加载灰度图像;
    • 图像切片。
    1. import cv2
    2. # 加载灰度图片
    3. img_OpenCV = cv2.imread(r"demo.png", cv2.IMREAD_GRAYSCALE)
    4. print(type(img_OpenCV), img_OpenCV.shape) # (484, 515)
    5. cv2.imshow('BGR image', img_OpenCV)
    6. # 图片切片
    7. img_split = img_OpenCV[100:200, 250:350]
    8. cv2.imshow("BGR image", img_split)
    9. cv2.waitKey(0) # 等待键盘动作
    10. cv2.destroyAllWindows() # 关闭窗口

    3 黑白图像

    • 将灰度图像转换成黑白图像;
    • 使用cv2.threshold()函数二值化图像;
    • 使用cv2.adaptiveThreshold()函数二值化图像。
    1. """
    2. @Title: Black And White
    3. @Time: 2024/3/13
    4. @Author: Michael Jie
    5. """
    6. import cv2
    7. # 加载灰度图片
    8. img_OpenCV = cv2.imread(r"demo.png", cv2.IMREAD_GRAYSCALE)
    9. print(type(img_OpenCV), img_OpenCV.shape) # (484, 515)
    10. print(img_OpenCV)
    11. # 二值化图像
    12. ret, img_binary = cv2.threshold(
    13. # 图像
    14. img_OpenCV,
    15. # 阈值
    16. thresh=200,
    17. # 填充色
    18. maxval=255,
    19. # 填充类型
    20. type=cv2.THRESH_BINARY # 小于阈值置0,大于阈值置填充色
    21. )
    22. cv2.imshow('BlackAndWhite image', img_binary)
    23. # 自适应阈值
    24. img_binary1 = cv2.adaptiveThreshold(
    25. # 图像
    26. img_OpenCV,
    27. # 填充色
    28. maxValue=255,
    29. # 填充类型
    30. adaptiveMethod=cv2.ADAPTIVE_THRESH_MEAN_C,
    31. # 二值化方法,计算阈值
    32. thresholdType=cv2.THRESH_BINARY,
    33. # 邻域大小,奇数
    34. blockSize=11,
    35. # 从计算的平均值或加权平均值中减去常数
    36. C=8
    37. )
    38. cv2.imshow('BlackAndWhite image1', img_binary1)
    39. cv2.waitKey(0) # 等待键盘动作
    40. cv2.destroyAllWindows() # 关闭窗口
    41. """
    42. 阈值类型 小于等于阈值 大于阈值
    43. THRESH_BINARY 0 maxval
    44. THRESH_BINARY_INV maxval 0
    45. THRESH_TRUNC 保持原样 thresh
    46. THRESH_TOZERO 0 保持原样
    47. THRESH_TOZERO_INV 保持原样 0
    48. """
    49. """
    50. 二值化方法
    51. ADAPTIVE_THRESH_MEAN_C 邻域的平均值
    52. ADAPTIVE_THRESH_GAUSSIAN_C 邻域值的加权和
    53. """

  • 相关阅读:
    MySQL笔记下
    编译过程 & 学习 CMake 文档的前置知识
    利用Appuploader上架IPA步骤
    云计算专业创新人才培养体系的探索与实践
    Double 4 VR仿真情景实训教学系统在商务谈判课堂上的应用
    EasyDSS视频直播点播平台出现断流该如何重连?具体步骤是什么?
    算法小题3
    SSM+图书馆电子文件资源管理 毕业设计-附源码091426
    redis变慢原因记录
    odoo16前端框架源码阅读——env.js
  • 原文地址:https://blog.csdn.net/2401_82889064/article/details/136672852