赞
踩
包括图像的读入、显示、保存,获取图片大小、通道数等属性,求取图像均值、标准差,遍历图像各个像素,图像取反,图像色彩空间转换,提取某种颜色,通道分离与合并等
# 默认的读入格式是BGR
img1 = cv2.imread('E:/PycharmProjects/one.jpg')
cv2.imshow(‘winname’, img)
cv2.waitKey(0)
cv2.destroyAllWindows()
cv2.imshow(winname, mat) 有窗口名和图片两个参数,不要忘记第一个参数
cv2.waitKey(delay: Any = None) 等待键盘输入。参数为延迟时间,单位为ms。参数为0时表示一直等到有键盘任意输入再进行下一操作。
cv2.imwrite('E:/PycharmProjects/one.png', img1)
cv2.imwrite(filename, img) 有文件名和图片两个参数,通过设定文件名的后缀可以转换图片格式
def img_info(img):
print('type:', type(img))
print('img.size:', img.size)
print('img.dtype:', img.dtype)
print('img.shape', img.shape)
img_info(img1)
type( ): 获取对象类型,图像类型为numpy.ndarray
.shape: h, w, c 高度、宽度、通道数,最常用
.size: 图像像素总数
.dtype: 数据类型,默认为uint8
mean_img = cv2.mean(img)
m, stddev = cv2.meanStdDev(img)
图像有三个通道时,结果为三个通道分别求均值和标准差
import cv2 import numpy as np img1 = cv2.imread('E:/PycharmProjects/one.jpg') def reverse_img(img): h, w, c = img.shape r_img = np.ones([h, w, c], dtype=np.uint8)#要注意dtype为np.uint8,否则图像无法正常显示 print(r_img.dtype) print(r_img) print(r_img.shape) for row in range(h): for col in range(w): for ch in range(c): r_img[row, col, ch] = 255-img[row, col, ch] print(r_img.dtype) show('reverse_img', r_img) reverse_img(img1)
ot_img1 = cv2.bitwise_not(img1)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
HSV(Hue, Saturation, Value)色彩空间,H(色调),S(饱和度),V(明度)
low = np.array([0, 0, 0])
up = np.array([255, 100, 100])
blue_img1 = cv2.inRange(img1, low, up)
cv2.inRange(src: Any, lowerb: Any, upperb: Any, dst: Any = None)
lowerb, upperb: numpy.ndarray类型
HSV基本颜色分量范围:
b, g, r = cv2.split(img)
merge_img = cv2.merge([b, g, r])
import cv2 import numpy as np # 1.读入图片和保存图片 img1 = cv2.imread('E:/PycharmProjects/one.jpg') # 2.显示图片 def show(name, img): cv2.imshow(name, img) cv2.waitKey(0) show('img1', img1) # 3.保存图像 cv2.imwrite('E:/PycharmProjects/one.png', img1) # 4.获取图片属性 def img_info(img): print('type:', type(img)) print('img.size:', img.size) print('img.dtype:', img.dtype) print('img.shape', img.shape) img_info(img1) # 5.图像求均值、标准差 def mean_dev(img): mean_img = cv2.mean(img) print('mean', mean_img) m, stddev = cv2.meanStdDev(img) print('m', m, '\n', 'StdDev', stddev) mean_dev(img1) # 6.遍历图像各个像素且取反(有问题) def reverse_img(img): h, w, c = img.shape r_img = np.zeros([h, w, c]) for row in range(h): for col in range(w): for ch in range(c): r_img[row, col, ch] = 255-img[row, col, ch] show('reverse_img', r_img) # reverse_img(img1) # 7.openCV取反函数 def not_img(img): not_img = cv2.bitwise_not(img) show('not_img', not_img) not_img(img1) # 8.图像色彩空间转换 def color_cvt_img(img): gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV) show('gray', gray) show('hsv', hsv) color_cvt_img(img1) # 9.对图片的某种颜色进行提取 def color_inrange(img): low = np.array([0, 0, 0]) up = np.array([255, 100, 100]) blue_img1 = cv2.inRange(img1, low, up) show('blue_img1', blue_img1) color_inrange(img1) # 10.通道分离、合并 def img_channel(img): b, g, r = cv2.split(img) show('blue', b) print('blue.shape', b.shape) merge_img = cv2.merge([b, g, r]) show('merge_img', merge_img) img_channel(img1)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。