图片放大、平移、旋转、透视操作
import cv2 as cvimport numpy as npfrom matplotlib import pyplot as plotdef resize():img = cv.imread("images/ly.jpg")# 缩放# cv.INTER_CUBIC放大# small = cv.resize(img, None, fx=2, fy=2, interpolation=cv.INTER_LINEAR)# big = cv.resize(img, None, fx=2, fy=2, interpolation=cv.INTER_CUBIC)# 等价height, width = img.shape[:2]size = (int(width * 0.5), int(height * 0.4))# 缩小small = cv.resize(img, size, interpolation=cv.INTER_AREA)# 放大big = cv.resize(img, (2 * width, 2 * height), interpolation=cv.INTER_CUBIC)cv.imshow("small", small)cv.imshow("big", big)cv.waitKey(0)cv.destroyAllWindows()def warpFfile():# 灰色读取img = cv.imread("images/ly.jpg", cv.IMREAD_GRAYSCALE)# 获取行跟列plot.subplot(221)plot.imshow(img)width, height = img.shape# 第一个参数是100 100是向x偏移 100 50是向y偏移50M = np.float32([[1, 0, 100], [0, 1, 50]])dst = cv.warpAffine(img, M, (width, height))plot.subplot(222)plot.imshow(img)plot.subplot(223)plot.imshow(dst)plot.show()# 旋转def RotationMatrix():img = cv.imread("images/ly.jpg", 0)width, height = img.shapeM = cv.getRotationMatrix2D(((width - 1) / 2.0, (height - 1) / 2.0), 90, 1)dst = cv.warpAffine(img, M, (width, height))cv.imshow("test", dst)cv.waitKey(0)cv.destroyAllWindows()# 透视def toushi():# 读取图片img = cv.imread("images/ly.jpg")image2 = cv.cvtColor(img, cv.COLOR_RGB2BGR)plot.imshow(image2)# cv.waitKey(0)# cv.destroyAllWindows()plot.show()if __name__ == '__main__':# warpFfile()# RotationMatrix()toushi()import cv2 as cvimport numpy as npfrom matplotlib import pyplot as plotdef resize():img = cv.imread("images/ly.jpg")# 缩放# cv.INTER_CUBIC放大# small = cv.resize(img, None, fx=2, fy=2, interpolation=cv.INTER_LINEAR)# big = cv.resize(img, None, fx=2, fy=2, interpolation=cv.INTER_CUBIC)# 等价height, width = img.shape[:2]size = (int(width * 0.5), int(height * 0.4))# 缩小small = cv.resize(img, size, interpolation=cv.INTER_AREA)# 放大big = cv.resize(img, (2 * width, 2 * height), interpolation=cv.INTER_CUBIC)cv.imshow("small", small)cv.imshow("big", big)cv.waitKey(0)cv.destroyAllWindows()def warpFfile():# 灰色读取img = cv.imread("images/ly.jpg", cv.IMREAD_GRAYSCALE)# 获取行跟列plot.subplot(221)plot.imshow(img)width, height = img.shape# 第一个参数是100 100是向x偏移 100 50是向y偏移50M = np.float32([[1, 0, 100], [0, 1, 50]])dst = cv.warpAffine(img, M, (width, height))plot.subplot(222)plot.imshow(img)plot.subplot(223)plot.imshow(dst)plot.show()# 旋转def RotationMatrix():img = cv.imread("images/ly.jpg", 0)width, height = img.shapeM = cv.getRotationMatrix2D(((width - 1) / 2.0, (height - 1) / 2.0), 90, 1)dst = cv.warpAffine(img, M, (width, height))cv.imshow("test", dst)cv.waitKey(0)cv.destroyAllWindows()# 透视def toushi():img = cv.imread('images/ly.jpg', 0)image2 = cv.cvtColor(img, cv.COLOR_BGR2RGB)# 左上 右上 左下 右下pts1 = np.float32([[150, 150], [450, 150], [150, 800], [450, 800]])# 第一个参数是值 第一个点的展示位置0,0表示左上角 300,0表示右上角依次类推pts2 = np.float32([[0, 0], [300, 0], [0, 300], [300, 300]])M = cv.getPerspectiveTransform(pts1, pts2)dst = cv.warpPerspective(image2, M, (300, 300))plot.subplot(121), plot.imshow(image2), plot.title('Input')plot.subplot(122), plot.imshow(dst), plot.title('Output')plot.show()if __name__ == '__main__':# warpFfile()# RotationMatrix()toushi()
