初步使用python cv2,简单使用操作笔记
读入图像保存图像
import numpy as np
import cv2
img = cv2.imread("P1.jpg", 0)
'''
# 还可先创建窗口
cv2.namedWindow('image', cv2.WINDOW_NORMAL)
'''
cv2.imshow('image', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
cv2.imwrite('P2.jpg', img)
使用matplotlib展示
import cv2
import numpy as np
from matplotlib import pyplot as plt
img = cv2.imread('./P1.jpg', 0)
plt.imshow(img, cmap='gray', interpolation='bicubic')
plt.xticks([])
plt.yticks([])
plt.show()
绘制图形
import cv2
import numpy as np
img = np.zeros((512, 512, 3), np.uint8)
cv2.line(img, (0, 0), (511, 511), (255, 0, 0), 5)
cv2.rectangle(img, (0, 0), (100, 100), (0, 255, 0), 3)
cv2.circle(img, (255, 255), 20, (0, 0, 255), -1)
cv2.ellipse(img, (400, 400), (50, 25), 90, 0, 180, (0, 255, 255), -1)
cv2.putText(img, 'cv2', (10, 300),cv2.FONT_HERSHEY_SIMPLEX,4,(255,255,255),2)
cv2.imshow('image', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
查看图像性质
import cv2
import numpy as np
img = cv2.imread('./P1.jpg')
print("图像维度(灰度图的话没有第三维):" ,img.shape)
print("图像像素:", img.size)
print("图像数据类型:", img.dtype)
输出->
图像维度(灰度图的话没有第三维): (1600, 2560, 3)
图像像素: 12288000
图像数据类型: uint8