图片的读写:
import cv2 as cv src=cv.imread("C:/Users/a/Desktop/csdn/1.jpg") #read a picture cv.namedWindow("input image",cv.WINDOW_AUTOSIZE) #create a GUI,named input image cv.imshow("input image",src) #put picture into GUI,parameter:(GUI NAME,PICTURE) cv.imwrite("D:/2.jpg",src) #写一张图片到指定路径 cv.waitKey(0) #wait next operation cv.destroyAllWindows() #free memory print("hello")将图片转化为灰度图,并输出到指定路径
import cv2 as cv src=cv.imread("C:/Users/a/Desktop/csdn/1.jpg") #read a picture cv.namedWindow("input image",cv.WINDOW_AUTOSIZE) #create a GUI,named input imagegray=cv.cvtColor(src,cv.COLOR_BGR2GRAY) #将图像转化为灰度图像 cv.imshow("input image",src) #put picture into GUI,parameter:(GUI NAME,PICTURE) cv.imwrite("D:/2.jpg",gray) #写一张图片到指定路径 cv.waitKey(0) #wait next operation cv.destroyAllWindows() #free memory print("hello")获取图片信息:通道数目,高与宽,像素数据,位图深度
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_data=np.array(image) #获取图片矩阵数据 print(pixel_data) #打印图像数据类型 src=cv.imread("C:/Users/a/Desktop/csdn/1.jpg") #read a picture cv.namedWindow("input image",cv.WINDOW_AUTOSIZE) #create a GUI,named input image cv.imshow("input image",src) #put picture into GUI,parameter:(GUI NAME,PICTURE) get_image_info(src)opencv打开摄像头操作
import cv2 as cv import numpy as np 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(50) if c==27: break video_demo() #打开摄像头操作