計算機內的圖像是用數據來存儲的
opencv3能支持基本上所有的圖片格式
import cv2 as cv
import numpy as np
def video_demo(): # 無參函數,調用攝像頭
capture = cv.VideoCapture(0)
while True: # python對大小寫敏感
ret, frame = capture.read() # frame是視頻當中的每一幀
frame = cv.flip(frame, 1) # 鏡像
cv.imshow("video", frame)
c = cv.waitKey(50)
if c == 27: # 當點ESC退出時停止
break
def get_image_info(image):
print(type(image)) # 圖片類型 輸出:<class 'numpy.ndarry'>
print(image.shape) # 形狀 輸出(483,475,3)
print(image.size) # 圖片大小=長*寬*通道數 輸出=483*475*3*1B=688275
print(image.dtype) # 輸出unit8
pixel_data = np.array(image) # 像素數據
print(pixel_data)
src = cv.imread("D:/demo.png")
cv.namedWindow("input image", cv.WINDOW_AUTOSIZE)
cv.imshow("input image", src)
get_image_info(src)
video_demo()
gray = cv.cvtColor(src, cv.COLOR_BGR2GRAY) # 灰度圖像
cv.imwrite("D:/result.png", gray) # 保存圖片的語法
cv.waitKey(0)
cv.destroyAllWindows()