Open CV系列学习笔记(一)图像的加载与保存
一、视频读取
调用电脑摄像头
import cv2 as cv
def video_demo(): # 视屏读取
capture = cv.VideoCapture(0)
while (True):
ret, frame = capture.read()
frame = cv.flip(frame, 1)
cv.imshow("video", frame)
c = cv.waitKey(20)
if c == 27:
break
video_demo()
cv.waitKey(0)
二、图片读取
读取图片,输出图片的大小等信息:
import cv2 as cv
import numpy as np
def get_image_info(image):#图片读取
print(type(image))#数据类型
print(image.shape)#像素大小和通道
print(image.size)#总大小
print(image.dtype)
pixel_date = np.array(image)
print(pixel_date)
print("--------HEllow Python-------")
src = cv.imread("E:/picture/10.bmp")
cv.namedWindow("input image",cv.WINDOW_AUTOSIZE)
cv.imshow("input image",src)
cv.waitKey(0)
以此为例:
输出结果:
--------HEllow Python-------
<class ‘numpy.ndarray’>
(659, 615, 3)
1215855
uint8
[[[235 185 132]
[235 185 132]
[235 185 132]
…
[231 177 116]
[231 177 116]
[231 177 116]]
[[234 184 131]
[234 184 131]
[234 184 131]
…
[232 178 117]
[232 178 117]
[232 178 117]]
[[234 184 131]
[234 184 131]
[234 184 131]
…
[232 178 117]
[232 178 117]
[232 178 117]]
…
[[193 224 239]
[194 225 240]
[196 225 239]
…
[230 217 139]
[223 210 132]
[220 207 129]]
[[193 223 240]
[192 225 241]
[196 225 240]
…
[229 216 138]
[223 210 132]
[219 206 128]]
[[193 223 240]
[192 225 241]
[196 225 240]
…
[229 216 138]
[223 210 132]
[219 206 128]]]
三、图片的保存
import cv2 as cv
import numpy as np
def get_image_info(image):#图片读取
print(type(image))#数据类型
print(image.shape)#像素大小和通道
print(image.size)#总大小
print(image.dtype)#
pixel_date = np.array(image)
print(pixel_date)
print("--------HEllow Python-------")
src = cv.imread("E:/picture/10.bmp")
cv.namedWindow("input image",cv.WINDOW_AUTOSIZE)
cv.imshow("input image",src)
get_image_info(src)
v.imwrite("./result.png",src)#保存图片
cv.waitKey(0)
将读取的图片,保存 或者将电脑摄像头读取的到视频截图。