【OpenCV】(一)图像基础处理

目录

 图像的IO操作

 在图像上绘制图形

 获取并修改图像中的像素点

 获取图像的属性

 图像通道的拆分与合并

 色彩空间的改变


 图像的IO操作

import cv2 as cv
import matplotlib.pyplot as plt

# 以彩色模式(默认,可不加1)读入图像
src1 = cv.imread('picture/apple.jpg', 1)
# 以灰度模式读入图像
src2 = cv.imread('picture/apple.jpg', 0)
# 以解码的方式读入图像
src3 = cv.imread('picture/apple.jpg', -1)

# 显示图像,:显示图像窗口的名称,要加载的图像
cv.imshow('1', src1)
cv.imshow('2', src2)
cv.imshow('3', src3)

# 在matplotlib中展示
plt.imshow(src1[:, :, ::-1])
# 灰度图
plt.imshow(src1, cmap=plt.cm.gray)
plt.show()

# 为图像绘制留下时间
cv.waitKey(0)

# 保存图像:文件名,要保存的图像
cv.imwrite('picture/apple1.jpg', src1)
cv.imwrite('picture/apple2.jpg', src2)
cv.imwrite('picture/apple3.jpg', src3)

运行结果 

 在图像上绘制图形

import numpy as np
import cv2 as cv
import matplotlib.pyplot as plt

# 创建一个空白的图像
img = np.zeros((512, 512, 3), np.uint8)

# 绘制直线
# cv.line(img, start, end, color, thickness)
# img: 要绘制直线的图像
# start, end: 直线的起点和终点
# color: 线条的颜色
# thickness: 线条宽度
cv.line(img, (0,0), (511, 511), (255, 0, 0), 5)

# 绘制圆形
# cv.circle(img, centerpoint, r, color, thickness)
# img: 要绘制圆形的图像
# centerpoint, r: 圆形和半径
# thickness: 线条宽度,为-1时生成闭合团并填充颜色
cv.circle(img, (447, 63), 63, (0, 0, 255), -1)

# 绘制矩形
# cv.rectangle(img, leftupper, rightdown, color, thickness)
# img: 要绘制矩形的图像
# leftupper, rightupper: 矩形的左上角和右下角坐标
# thickness: 线条宽度,为-1时生成闭合团并填充颜色
cv.rectangle(img, (384, 0), (510, 128), (0, 255, 0), 3)

# 向图像中添加文字
# cv.putText(img, text, station, font, fontsize, color, thickness, cv.LINE_AA)
# img: 要添加文字的图像
# text: 要添加的文本数据
# station: 文本的放置位置
# font: 字体
# fontsize: 文字大小
# color: 文字颜色
# thickness: 文字宽度
font = cv.FONT_HERSHEY_SIMPLEX
cv.putText(img, 'OpenCV', (10, 500), font, 4, (255, 255, 255), 2, cv.LINE_AA)

# 图像展示
plt.imshow(img[:, :, ::-1])
plt.title('img'), plt.xticks([]), plt.yticks([])
plt.show()

运行结果

 获取并修改图像中的像素点

import numpy as np
import matplotlib.pyplot as plt

# 获取并修改图像中的像素点
# 通过行和列的坐标值获取该像素点的像素值
# 对于RGB图像,返回一个蓝,绿,红值的数组
# 对于灰度图像,仅返回相应的强度值

img = np.zeros((256, 256, 3), np.uint8)
plt.imshow(img[:, :, ::-1])

# 获取某个像素点的值
print(img[100, 100])

# 仅获取蓝色通道的强度值
print(img[100, 100, 0])

# 修改某个位置的像素值为红色
img[100, 100] = [0, 0, 255]

# 再次获取该像素点的值
print(img[100, 100])

plt.show()

运行结果 

 获取图像的属性

import cv2 as cv

img = cv.imread('picture/apple.jpg')

# 获取形状
print(img.shape)

# 获取图像大小(像素数)
print(img.size)

# 获取数据类型
print(img.dtype)

运行结果

 图像通道的拆分与合并

import cv2 as cv
import matplotlib.pyplot as plt

img = cv.imread('picture/apple.jpg')

# 通道拆分
b, g, r = cv.split(img)
print(b)
print(g)
print(r)
plt.imshow(b, cmap=plt.cm.gray)
plt.show()
plt.imshow(g, cmap=plt.cm.gray)
plt.show()
plt.imshow(r, cmap=plt.cm.gray)
plt.show()

# 通道合并
img = cv.merge((b, g, r))
plt.imshow(img[:, :, ::-1])
plt.show()

 运行结果

[r, g, b]:

[[252 253 253 ... 240 240 242]
 [255 255 255 ... 238 238 238]
 [246 249 249 ... 232 232 234]
 ...
 [254 254 254 ... 159 159 155]
 [254 254 254 ... 157 157 154]
 [255 254 254 ... 155 160 161]]
[[254 255 255 ... 245 245 247]
 [255 255 255 ... 244 244 244]
 [254 255 255 ... 240 240 242]
 ...
 [254 254 254 ... 214 212 208]
 [254 254 254 ... 212 212 207]
 [255 254 254 ... 211 210 209]]
[[254 255 255 ... 244 244 246]
 [255 255 255 ... 243 243 243]
 [254 254 254 ... 239 239 241]
 ...
 [254 254 254 ... 235 233 229]
 [254 254 254 ... 233 233 228]
 [255 254 254 ... 236 233 233]]

b通道拆分:g通道拆分:r通道拆分:

通道合并: 

 色彩空间的改变

import cv2 as cv
import matplotlib.pyplot as plt

# 色彩空间改变
# cv.cvtColor(input_image, flag)
# input_image: 进行颜色空间转换的图像
# flag: 转换类型

img = cv.imread('picture/apple.jpg')
# cv.COLOR_BGR2GRAY:BGR与GRAY转换
gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
plt.imshow(gray, cmap=plt.cm.gray)
plt.show()

# cv.COLOR_BGR2HSV: BGR与HSV转换
hsv = cv.cvtColor(img, cv.COLOR_BGR2HSV)
plt.imshow(hsv)
plt.show()

运行结果

BGR与GRAY转换:BGR与HSV转换:

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值