1、读取并显示
1.1、代码
import cv2 as cv
img = cv.imread("dota.jpg")
cv.imshow("image_show",img)
cv.waitKey(0)
cv.destroyAllWindows()
1.2、函数解析
(1)imread
imread(filename[, flags]) -> retval
@param filename Name of file to be loaded. filename:欲显示图片的路径名字
@param flags Flag that can take values of cv::ImreadMode flags :可选,以何种方式打开
总结:该函数的功能是找到相应的图片,然后转换为计算机能处理的格式(Mat),并返回。
The function imread loads an image from the specified file and returns it. If the image cannot be
read (because of missing file, improper permissions, unsupported or invalid format), the function
returns an empty matrix ( Mat::data==NULL ).
打开一张图片并返回,若图片不能读,返回null。
(2)imshow
imshow(winname, mat) -> None
@param winname Name of the window. winname:显示图片 框 的名字 @param mat Image to be shown. mat: 将要显示的图片
总结:imshow的功能是告诉电脑,在哪里(winname)显示什么(Mat)
(3) waitKey()
waitKey([, delay]) -> retval
@param delay Delay in milliseconds. 0 is the special value that means "forever".
若参数不为n(n≠0),则阻塞在这里n毫秒,为零,则一直阻塞有按键触发才终止。
(4)destroyAllWindows()
destroyAllWindows() -> None
销毁所有由该程序创建的窗口。
2、读取并重写
import cv2 as cv
src = cv.imread("dota.jpg")
cv.imshow("image_orgine",src)
cv.imwrite("write_pic.png",src)
dst = cv.imread("write_pic.png")
cv.imshow("imgage_writed",dst)
cv.waitKey(0)
cv.destroyAllWindows()
2.1 imwrite
imwrite(filename, img[, params]) -> retval
@param filename Name of the file. 将要写入文件的名字(目标文件的名字) @param img Image to be saved. 源文件的名字 @param params Format-specific parameters encoded as pairs (paramId_1, paramValue_1, paramId_2, paramValue_2, ... .) see cv::ImwriteFlags 编码方式