python-opencv基础入门(一)

最近想学习opencv,  先总结一下基础的操作。贴出来分享一下!

一、图片读写

img1 = cv2.imread('01.jpg')
cv2.imshow('', img1)
cv2.imwrite('res.jpg', img1)
cv2.waitKey()

二、显示彩色图像

img1 = cv2.imread('01.jpg')
cv2.namedWindow('my_win', cv2.WINDOW_AUTOSIZE)
cv2.imshow('my_win', img1)
cv2.waitKey()
print(img1.shape)  # 显示像素矩阵大小

三、显示黑白图像

img2 = cv2.imread('res.jpg', 0)
cv2.namedWindow('my_win1', cv2.WINDOW_NORMAL)
cv2.imshow('my_win1', img2)
cv2.waitKey()
print(img2.shape)
print(img2.size)  # 显示总像素个数
print(img2.dtype)  # 显示图像数据类型

四、生成空图像

img3 = np.zeros(img2.shape,np.uint8)
img4 = np.zeros((300, 500, 3), np.uint8)
cv2.namedWindow('test_win1', cv2.WINDOW_AUTOSIZE)
cv2.namedWindow('test_win2', cv2.WINDOW_AUTOSIZE)
cv2.imshow('test_win1', img3)
cv2.imshow('test_win2', img4)
cv2.waitKey()

五、通过坐标访问图像

img1 = cv2.imread('res.jpg')
temp = img1[50, 100]
print(temp)
cv2.namedWindow('my_win1', cv2.WINDOW_AUTOSIZE)
print(type(img1[50:100, 100:300]))
img1[50:100, 100:300] = [0, 0, 255]  # Blue, Green, Red = [0, 0, 255]
cv2.imshow('my_win1', img1)
cv2.waitKey()
img2 = cv2.imread('res.jpg')
cv2.namedWindow('my_win2', cv2.WINDOW_AUTOSIZE)
img2[100:200, 300:500, 0] = 100
img2[100:200, 300:500, 1] = 100
img2[100:200, 300:500, 2] = 100
cv2.imshow('my_win2', img2)
cv2.waitKey()
img3 = cv2.imread('res.jpg')
img3[50:100, 100:300] = 255  # Python会认为Blue, Green, Red = [255, 255, 255]
cv2.namedWindow('my_win3', cv2.WINDOW_AUTOSIZE)
cv2.imshow('my_win3', img3)
cv2.waitKey()

六、彩色图像通道分离与合并

img1 = cv2.imread('res.jpg')
cv2.imshow('ori', img1)
blue, green, red = cv2.split(img1)
cv2.imshow('blue', blue)
cv2.imshow('green', green)
cv2.imshow('red', red)
img2 = cv2.merge([blue, green, red])
cv2.imshow('merge', img2)
cv2.waitKey()

七、在图像上添加文本并旋转

# 函数原型:putText(img, text, org, fontFace, fontScale, color, thickness=None, lineType=None, bottomLeftOrigin=None)
img1 = cv2.imread('res.jpg')
txt = 'call me iron man'
position = (250, 80)
cv2.putText(img1, text=txt, org=position, fontFace=cv2.FONT_HERSHEY_SIMPLEX, fontScale=1, color=(0, 255, 0),
            thickness=2)
cv2.namedWindow('my_win1', cv2.WINDOW_AUTOSIZE)
h, w = img1.shape[:2]
center = (h/2, w/2)
matrix = cv2.getRotationMatrix2D(center, 45, 1)  # 生成图像旋转所需要的矩阵
rotated = cv2.warpAffine(img1, matrix, (w, h))
cv2.imshow('my_win1', rotated)
cv2.waitKey()

八、改变图像大小

img1= cv2.imread('res.jpg')
img2 = cv2.resize(img1, (222, 333))
cv2.imshow('', img2)
cv2.waitKey()

九、图像平移

img1= cv2.imread('res.jpg')
h, w = img1.shape[:2]
p = np.float32([[1, 0, 50], [0, 1, 100]])
moved_img = cv2.warpAffine(img1, p, (w, h))
cv2.namedWindow('my_win', cv2.WINDOW_AUTOSIZE)
cv2.imshow('my_win', moved_img)
cv2.waitKey(0)

十、仿射变换

img1 = cv2.imread('res.jpg')
h, w = img1.shape[:2]
p1 = np.float32([[0, 0], [50, 50], [25, 200]])  # 变换前的三个点
p2 = np.float32([[50, 20], [80, 80], [60, 250]])
M = cv2.getAffineTransform(p1, p2)
affined_img = cv2.warpAffine(img1, M, (w, h))
my_win = cv2.namedWindow('my_win', cv2.WINDOW_AUTOSIZE)
cv2.imshow('my_win', affined_img)
cv2.waitKey(0)

十一、图片添加边框

img1 = cv2.imread('res.jpg')

replicate = cv2.copyMakeBorder(img1, 10, 10, 10, 10, cv2.BORDER_REPLICATE)
reflect = cv2.copyMakeBorder(img1, 10, 10, 10, 10, cv2.BORDER_REFLECT)
reflect101 = cv2.copyMakeBorder(img1, 10, 10, 10, 10, cv2.BORDER_REFLECT_101)
wrap = cv2.copyMakeBorder(img1, 50, 50, 30, 30, cv2.BORDER_WRAP)
cv2.imshow('replicate', replicate)
cv2.imshow('reflect', reflect)
cv2.imshow('reflect101', reflect101)
cv2.imshow('wrap', wrap)
constant = cv2.copyMakeBorder(img1, 10, 10, 10, 10, cv2.BORDER_CONSTANT, value=(0, 255, 0))
cv2.imshow('constant', constant)
cv2.waitKey()

十二、透视变换

import matplotlib.pyplot as plt
img = cv2.imread('res.jpg')
rows,cols,ch = img.shape

pts1 = np.float32([[56,65],[368,52],[28,387],[389,390]])
pts2 = np.float32([[0,0],[300,0],[0,300],[300,300]])

M = cv2.getPerspectiveTransform(pts1,pts2)

dst = cv2.warpPerspective(img,M,(cols, rows))

plt.subplot(121),plt.imshow(img),plt.title('Input')
plt.subplot(122),plt.imshow(dst),plt.title('Output')
plt.show()

你可能感兴趣的:(opencv)