OpenCV-图像基础处理

目录

[1 彩色(RGB)图像](#1 彩色(RGB)图像)

[2 灰度图像](#2 灰度图像)

[3 黑白图像](#3 黑白图像)


1 彩色(RGB)图像

  • 使用cv2.imread()函数加载RGB图像;
  • 使用cv2.split()函数分割颜色通道;
  • 将BGR颜色格式转换成RGB颜色格式;
  • 使用matplotlib或cv2.imshow()函数可视化图像。
python 复制代码
import cv2
from matplotlib import pyplot as plt
import numpy as np

# 加载RGB图片
img_OpenCV = cv2.imread(r"demo.jpeg")
print(type(img_OpenCV), img_OpenCV.shape)  # <class 'numpy.ndarray'> (484, 515, 3)

# 分割颜色通道,opencv使用BGR颜色格式
b, g, r = cv2.split(img_OpenCV)
# imread()方法返回ndarray对象,因此也可以使用切片分割颜色通道
B = img_OpenCV[:, :, 0]
G = img_OpenCV[:, :, 1]
R = img_OpenCV[:, :, 2]
print((b == B).all(), (g == G).all(), (r == R).all())  # True True True

# 转换成RGB颜色格式
img_matplotlib = cv2.merge([r, g, b])
img_ndarray = img_OpenCV[:, :, ::-1]
print((img_matplotlib == img_ndarray).all())  # True

# 使用matplotlib可视化图片
plt.subplot(121)
plt.imshow(img_OpenCV)
plt.subplot(122)
plt.imshow(img_matplotlib)
# plt.show()

# 使用opencv可视化图片
cv2.imshow('BGR image', img_OpenCV)
cv2.imshow('RGB image', img_matplotlib)
# 拼接图片
img_connect = np.concatenate((img_OpenCV, img_matplotlib), axis=1)
cv2.imshow('BGR image-RGB image', img_connect)

cv2.waitKey(0)  # 等待键盘动作
cv2.destroyAllWindows()  # 关闭窗口

2 灰度图像

  • 使用cv2.imread()函数加载灰度图像;
  • 图像切片。
python 复制代码
import cv2

# 加载灰度图片
img_OpenCV = cv2.imread(r"demo.png", cv2.IMREAD_GRAYSCALE)
print(type(img_OpenCV), img_OpenCV.shape)  # <class 'numpy.ndarray'> (484, 515)
cv2.imshow('BGR image', img_OpenCV)

# 图片切片
img_split = img_OpenCV[100:200, 250:350]
cv2.imshow("BGR image", img_split)

cv2.waitKey(0)  # 等待键盘动作
cv2.destroyAllWindows()  # 关闭窗口

3 黑白图像

  • 将灰度图像转换成黑白图像;
  • 使用cv2.threshold()函数二值化图像;
  • 使用cv2.adaptiveThreshold()函数二值化图像。
python 复制代码
"""
@Title: Black And White
@Time: 2024/3/13
@Author: Michael Jie
"""

import cv2

# 加载灰度图片
img_OpenCV = cv2.imread(r"demo.png", cv2.IMREAD_GRAYSCALE)
print(type(img_OpenCV), img_OpenCV.shape)  # <class 'numpy.ndarray'> (484, 515)
print(img_OpenCV)

# 二值化图像
ret, img_binary = cv2.threshold(
    # 图像
    img_OpenCV,
    # 阈值
    thresh=200,
    # 填充色
    maxval=255,
    # 填充类型
    type=cv2.THRESH_BINARY  # 小于阈值置0,大于阈值置填充色
)
cv2.imshow('BlackAndWhite image', img_binary)

# 自适应阈值
img_binary1 = cv2.adaptiveThreshold(
    # 图像
    img_OpenCV,
    # 填充色
    maxValue=255,
    # 填充类型
    adaptiveMethod=cv2.ADAPTIVE_THRESH_MEAN_C,
    # 二值化方法,计算阈值
    thresholdType=cv2.THRESH_BINARY,
    # 邻域大小,奇数
    blockSize=11,
    # 从计算的平均值或加权平均值中减去常数
    C=8
)
cv2.imshow('BlackAndWhite image1', img_binary1)

cv2.waitKey(0)  # 等待键盘动作
cv2.destroyAllWindows()  # 关闭窗口

"""
阈值类型	            小于等于阈值	大于阈值
THRESH_BINARY	    0	        maxval
THRESH_BINARY_INV	maxval	    0
THRESH_TRUNC	    保持原样	    thresh
THRESH_TOZERO	    0	        保持原样
THRESH_TOZERO_INV	保持原样	    0
"""

"""
二值化方法
ADAPTIVE_THRESH_MEAN_C      邻域的平均值
ADAPTIVE_THRESH_GAUSSIAN_C  邻域值的加权和
"""
相关推荐
happylifetree17 小时前
Python67:核心语法-函数进阶-参数类型(函数作为参数)
python
小羊没烦恼!17 小时前
铁道部新客票系统设计(二)
开发语言·前端·数据库·python·oracle·c#·php
quantdash_cc18 小时前
从实时行情到价格观察表:用 Python 和 Pandas 构建策略输入数据
开发语言·python·数据分析·pandas·量化交易·股票数据·quantdash
镜子AI18 小时前
镜子AI工程实践:用 Python 做 GEO 实体一致性审计,并自动生成 JSON-LD 结构化数据
人工智能·python·json
2601_962885721 天前
如何用 Python 计算 VaR 和 CVaR(在险价值)?
开发语言·python
李航19831 天前
DeepSeekAI建模三 - 实战建模一组柜子
人工智能·python·计算机视觉·ai·ai编程
严谨的麻辣烫1 天前
静态代理 IP 的出口一致性如何验证?Python 请求检测与异常排查
开发语言·python·http·网络安全
我命由我123451 天前
5-1.Python 编码进阶 - 函数进阶(参数、返回值、递归、函数作为参数传递、lambda 匿名函数)
java·服务器·数据库·后端·python·java-ee·python3.11
FITA阿泽要努力1 天前
Fork Agent:共享上下文的艺术
开发语言·python·agent
DeeGLMath1 天前
optimtool最新版研究梯度优化
python·optimtool·梯度优化