1然走、讀取圖片
import cv2 as cv
src = cv.imread('./image.png')
cv.namedWindow('Jun',cv.WINDOW_AUTOSIZE) #以圖片的大小自動拉伸
cv.imshow('Jun ', src ) # 必須和 nameWindow 同名才行
cv.waitKey(0) #靜止的意思
cv.destroyAllWindows()
2、獲取圖片參數(shù)
def get_image_info(image):
print(type(image))
print(image.shape) #高度戏挡,寬度芍瑞,通道
print(image.size)
print(image.dtype) #矩陣里的數(shù)據(jù)類型
get_image_info(src)
3、保存圖片
cv.imwrite('./opencv_2.jpg', img)
4褐墅、圖片像素取反
def inverse(image): #opencv自帶像素取反函數(shù)
des = cv.bitwise_not(image)
cv.imshow("inverse demo", des)
恐怖吧2鹈省!妥凳!
5竟贯、創(chuàng)建一個圖片
其實就是創(chuàng)建一個矩陣,然后顯示出來而已
def create_image():
img = np.zeros([300,300,3], np.uint8) #三通道
cv.imshow("black", img)
for i in range(3):
img[:, :, i] = np.ones([300,300])*255 #所有通道全為 255
cv.imshow("white", img)
img =np.zeros([300,300,1],np.uint8) #單通道
img[ :, :, 0] = np.ones([300,300])*127 #取值范圍 0~255逝钥,超過也是255
cv.imshow("gray", img)
image.png