http://www.woshicver.com/ OpenCV-Python 中文文档
import cv2 as cv img = cv.imread("d:/test/1.jpg",cv.IMREAD_COLOR) # 读取 cv.imshow("hello OpenCV",img) # 显示 cv.imwrite("d:/test/1.png",img) # 保存 cv.waitKey(0) cv.destroyAllWindows()
import cv2 as cv img = cv.imread("d:/test/1.jpg",cv.IMREAD_COLOR) # 读取 cv.imshow("hello OpenCV",img) # 显示 cv.imwrite("d:/test/1.png",img) # 保存 retKey = cv.waitKey(0) if retKey == 27: # 等待ESC退出 ESC的ASCII值为27 cv.destroyAllWindows() elif retKey == ord(‘s‘): # 如果用户输入的是 s ,保存和退出 cv.imwrite(‘mysave.png‘,img) cv.destroyAllWindows()
Matplotlib是Python的绘图库,可为你提供多种绘图方法。你将在接下来的文章中看到它们。在这里,你将学习如何使用Matplotlib显示图像。你可以使用Matplotlib缩放图像,保存图像等。
import cv2 as cv import matplotlib.pyplot as plt img = cv.imread("d:/test/4.jpg",cv.IMREAD_COLOR) # 读取 plt.imshow(img,cmap="gray") plt.xticks([]) # 隐藏 x 轴和 y 轴上的刻度值 plt.yticks([]) plt.show()
注:
OpenCV加载的彩色图像处于BGR模式。但是Matplotlib以RGB模式显示。因此,如果使用OpenCV读取彩色图像,则Matplotlib中将无法正确显示彩色图像,解决方法见下面。
import cv2 as cv import matplotlib.pyplot as plt img = cv.imread("d:/test/4.jpg",cv.IMREAD_COLOR) # 读取 # print(img.shape) (1080, 1920, 3) H W C img = img[:,:,::-1] # 将OpenCV的bgr 转为 rgb plt.imshow(img) plt.xticks([]) # 隐藏 x 轴和 y 轴上的刻度值 plt.yticks([]) plt.show()
此时,彩色图片就显示正常了 ,
或者使用 cv.CvtColor() 方法:
import cv2 as cv import matplotlib.pyplot as plt img = cv.imread("d:/test/4.jpg",cv.IMREAD_COLOR) # 读取 # img = img[:,:,::-1] # 将OpenCV的bgr 转为 rgb img = cv.cvtColor(img,cv.COLOR_BGR2RGB) plt.imshow(img) plt.xticks([]) # 隐藏 x 轴和 y 轴上的刻度值 plt.yticks([]) plt.show()
原文:https://www.cnblogs.com/zach0812/p/13288556.html