使用工具Python3.5
使用库numpy ; opencv,
从图片导入到另存为
1.cv2.imread(文件名,标记) 读入图像
cv2.IMREAD_COLOR() :读入彩色图像
cv2.IMREAD_GRAYSCALE() :以灰度模式读入图像
2.cv2.imshow() 显示图像
cv2.waitKey()等待键盘输入,为毫秒级
cv2.destroyAllWindows() 可以删除我们建立的任何窗口
代码示例:
cv2.namedwindows('image',cv2.WINDOWS_NARMAL)
cv2.imshow('image',img)
cv2.waitKey(0)
cv2.destroyAllWindows()
3.cv2.imwrite(文件名,img) 保存图像
cv2.imwrite('messigray.png',img)
4.练习加载一个灰度图像,显示图片,按下“S”键,保存然后退出,或按下ESC键退出不保存
import cv2 as cv
import numpy as np
img = cv.imread('111.jpg',0)
cv.imshow('input_image',img)
k = cv.waitKey(0)
if(k == 27){
cv.destroyAllWindows() #wait for ESC key to exit
}else if( k == ord('s')){
cv.imwrite('22.jpg',img) #wait for s key to save and exit
cv.destroyAllWindows()
}
注释:如果是64位系统,需要将k = cv2.waitKey(0)改为k = cv2.waitKey(0)&0xFF@!!
5.Matplotlib 绘图介绍
import numpy as np
import cv2 as cv
from matpoltlib import pyplot as plt
img = cv.imread('11.jpg',0)
plt.imshow(img,cmap = 'gray',interpolation = 'bicubic')
plt.xticks([])
plt.yticks([]) #to hide tick values on X and Y axis
plt.show()