图像的几何变换
demo1
# dsize实现缩放
import cv2
img = cv2.imread("./cat.jpg")
dst1 = cv2.resize(img, (100, 100))
dst2 = cv2.resize(img, (400, 400))
# cv2.imshow("img", img)
# cv2.imshow("dst1", dst1)
# cv2.imshow("dst2", dst2)
cv2.imwrite("./cat01.png", dst1)
cv2.waitKey()
cv2.destroyAllWindows()
demo2
# fx 和 fy 参数实现缩放
import cv2
img = cv2.imread("./cat.jpg")
dst3 = cv2.resize(img, None, fx=1/3, fy=1/2)
dst4 = cv2.resize(img, None, fx=2, fy=2)
cv2.imshow("img", img)
cv2.imshow("dst3", dst3)
cv2.imshow("dst4", dst4)
cv2.waitKey()
cv2.destroyAllWindows()
demo3
# 翻转
import cv2
img = cv2.imread("./cat01.png")
dst1 = cv2.flip(img, 0)
dst2 = cv2.flip(img, 1)
dst3 = cv2.flip(img, -1)
cv2.imshow("dst1", dst1)
cv2.imshow("dst2", dst2)
cv2.imshow("dst3", dst3)
cv2.waitKey()
cv2.destroyAllWindows()
demo4
# 图像平移
import cv2
import numpy as np
img = cv2.imread("./cat01.png")
# 读取像素行数
rows = len(img)
# 读取像素列数
cols = len(img[0])
M = np.float32([[1, 0, 0], [0, 1, -50]])
dst = cv2.warpAffine(img, M, (cols, rows))
cv2.imshow("img", img)
cv2.imshow("dst", dst)
cv2.waitKey()
cv2.destroyAllWindows()
demo5
import cv2
img = cv2.imread("./cat01.png")
# 读取像素行数
rows = len(img)
# 读取像素列数
cols = len(img[0])
center = (rows/2, cols/2)
M = cv2.getRotationMatrix2D(center, 30, 0.8)
dst = cv2.warpAffine(img, M, (cols, rows))
cv2.imshow("img", img)
cv2.imshow("dst", dst)
cv2.waitKey()
cv2.destroyAllWindows()
demo6
import cv2
import numpy as np
img = cv2.imread("./cat01.png")
# 读取像素行数
rows = len(img)
# 读取像素列数
cols = len(img[0])
p1 = np.zeros((3, 2), np.float32)
# 左上角坐标
p1[0] = [0, 0]
# 右上角坐标
p1[1] = [cols - 1, 0]
# 左下角坐标
p1[2] = [0, rows - 1]
p2 = np.zeros((3, 2), np.float32)
p2[0] = [50, 0]
p2[1] = [cols - 1, 0]
p2[2] = [0, rows - 1]
M = cv2.getAffineTransform(p1, p2)
dst = cv2.warpAffine(img, M, (cols, rows))
cv2.imshow("img", img)
cv2.imshow("dst", dst)
cv2.waitKey()
cv2.destroyAllWindows()
demo7
import cv2
import numpy as np
img = cv2.imread("./cat01.png")
rows = len(img)
cols = len(img[0])
p1 = np.zeros((4, 2), np.float32)
p1[0] = [0, 0]
p1[1] = [cols - 1, 0]
p1[2] = [0, rows - 1]
p1[3] = [cols - 1, rows - 1]
p2 = np.zeros((4, 2), np.float32)
M = cv2.getPerspectiveTransform(p1, p2)
dst = cv2.warpPerspective(img, M, (cols, rows))
cv2.imshow("img", img)
cv2.imshow("dst", dst)
cv2.waitKey()
cv2.destroyAllWindows()
文章来源地址https://www.toymoban.com/news/detail-657783.html
文章来源:https://www.toymoban.com/news/detail-657783.html
到了这里,关于open cv学习 (四)图像的几何变换的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!