[cv2] python cv2

1.cv2.imread(文件名,标记)读入图像,

cv2.IMREAD_COLOR():读入彩色图像
cv2.IMREAD_GRAYSCALE():以灰度模式读入图像
import numpy as np import cv2 img = cv2.imread(‘45.jpg’,0)

2.cv2.imshow()显示图像

cv2.waitKey()等待键盘输入,为毫秒级
cv2.destroyAllWindows()可以轻易删除任何我们建立的窗口,括号内输入想删除的窗口名

cv2.imshow('image',img)
cv2.waitKey(0)
cv2.destroyAllWindows()

3.cv2.imwrite(文件名,img)保存图像,

cv2.imwrite('messigray.png',img)

4.

练习加载一个灰度图,显示图片,按下‘s’键保存后退出,或者按下ESC键退出不保存

import cv2

img = cv2.imread('45.jpg',0)
cv2.imshow('image',img)
k = cv2.waitKey(0)
if k==27:
		cv2.destroyAllWindows()  #wait for ESC key to exit
elif k == ord('s'):
		cv2.imwrite('46.png',img)  #wait for 's' key to save and exit
        cv2.destoryAllWindows()
如果用的是64位系统,需将key=cv2.waitKey(0)改为k=cv2.waitKey(0)&0xFF @!!

5.Matplotlib

是牛X的绘图库,先简单介绍显示图像

import cv2
from matplotlib import pyplot as plt
img =cv2.imread('45.jpg',0)
plt.imshow(img,cmap='gray',interpolation = 'bicubic')
plt.xticks([]),plt.yticks([])  #to hide tick values on X and Y axis
plt.show()

6.resize

cv2.resize(src, dsize[, dst[, fx[, fy[, interpolation]]]]) → dst
pic = cv2.resize(pic, (400, 400), interpolation=cv2.INTER_CUBIC)
interpolation 选项 所用的插值方法
INTER_NEAREST 最近邻插值
INTER_LINEAR 双线性插值(默认设置)
INTER_AREA 使用像素区域关系进行重采样。 它可能是图像抽取的首选方法,因为它会产生无云纹理的结果。 但是当图像缩放时,它类似于INTER_NEAREST方法。
INTER_CUBIC 4x4像素邻域的双三次插值
INTER_LANCZOS4 8x8像素邻域的Lanczos插值

im1 = cv2.resize(img1, (200, 200))
im2 = cv2.resize(img2, (200, 200))
hmerge = np.hstack((im1, im2)) #水平拼接
vmerge = np.vstack((im1, im2)) #垂直拼接

opencv python tutorial
https://blog.csdn.net/liangjiubujiu/article/details/80437481

猜你喜欢

转载自blog.csdn.net/u013608336/article/details/82808075