使用python-opencv读取图片,利用opencv或matplotlib显示图片。
# -*- coding: utf-8 -*-
import numpy as np
from matplotlib import pyplot as plt
#import urllib
import cv2
def load_image1(file):
# Load an color image in grayscale
img = cv2.imread(file,0)
cv2.imshow('image',img)
cv2.waitKey(0)
cv2.destroyAllWindows()
def load_image2(file):
# Load an color image in RGB color scale
img = cv2.imread(file,1)
cv2.namedWindow('image', cv2.WINDOW_NORMAL)
cv2.imshow('image',img)
k = cv2.waitKey(0)
if k == ord('s') or k == ord('S'):
# save image
cv2.imwrite('messigray.png',img)
print('Save image ', file, ' as png format.')
#cv2.imwrite('messigray1.png',img)
cv2.destroyAllWindows()
def matplotlibShowImage(picture):
img = cv2.imread(picture,0)
plt.imshow(img, cmap = 'gray', interpolation = 'bicubic')
plt.xticks([]), plt.yticks([]) # to hide tick values on X and Y axis
plt.show()
def main():
file = "F:\\dataSet\\picture\\000006.jpg"
#load_image1(file)
load_image2(file)
#matplotlibShowImage(file)
if __name__ == '__main__':
main()

本文介绍如何使用Python结合OpenCV库读取图片,并通过OpenCV和matplotlib库进行图片显示的方法。文中提供了两种不同的图片读取方式:灰度读取和RGB颜色读取,以及展示图片的具体实现代码。
550

被折叠的 条评论
为什么被折叠?



