Python+OpenCV:仿射变换和透射变换
仿射变换
####################################################################################################
# 仿射图像
def lmc_cv_affining():
"""
函数功能: 仿射图像.
"""
# 读取图像
image = lmc_cv.imread('D:/99-Research/Python/Image/CheckerBoard.jpg')
# 仿射图像
rows, cols, channel = image.shape
pts1 = np.float32([[50, 50], [200, 50], [50, 200]])
pts2 = np.float32([[10, 100], [200, 50], [100, 250]])
matrix = lmc_cv.getAffineTransform(pts1, pts2)
affining_image = lmc_cv.warpAffine(image, matrix, (cols, rows))
# 显示图像
pyplot.figure('Image Display')
pyplot.subplot(121)
pyplot.imshow(image)
pyplot.title('Input')
pyplot.subplot(122)
pyplot.imshow(affining_image)
pyplot.title('Output')
pyplot.show()
# 根据用户输入保存图像
if ord("q") == (lmc_cv.waitKey(0) & 0xFF):
# 销毁窗口
pyplot.close()
return
透射变换
image = lmc_cv.imread('D:/99-Research/Python/Image/Sudoku.jpg')
# 透视图像
rows, cols, channel = image.shape
pts1 = np.float32([[33, 47], [424, 43], [2, 498], [441, 511]])
pts2 = np.float32([[0, 0], [300, 0], [0, 300], [300, 300]])
matrix = lmc_cv.getPerspectiveTransform(pts1, pts2)
perspectiving_image = lmc_cv.warpPerspective(image, matrix, (300, 300))
# 显示图像
pyplot.figure('Image Display')
pyplot.subplot(121)
pyplot.imshow(image)
pyplot.title('Input')
pyplot.subplot(122)
pyplot.imshow(perspectiving_image)
pyplot.title('Output')
pyplot.show()
# 根据用户输入保存图像
if ord("q") == (lmc_cv.waitKey(0) & 0xFF):
# 销毁窗口
pyplot.close()
return