You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
workspace/geometric_trans.py

54 lines
1.6 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

import cv2
import numpy as np
# 扩展
def extend(img):
l, w, h = img.shape
# 放大图像至原来的两倍,使用双线性插值法
img=cv2.resize(img,(0,0),fx=2,fy=2,interpolation=cv2.INTER_LINEAR)
cv2.imwrite("image/result.jpg", img)
#平移
def move():
img=cv2.imread('test.png')
height, width, channel = img.shape
# 构建移动矩阵,x轴左移 10 个像素y轴下移 30 个
M=np.float32([[1,0,30],[0,1,60]])
img = cv2.warpAffine(img, M, (width, height))
cv2.imwrite("image/result.jpg", img)
#旋转
def rotate():
img=cv2.imread('test.png')
# 构建矩阵旋转中心坐标为处理后图片长宽的一半旋转角度为45度缩放因子为1
height, width, channel = img.shape
rows,cols,depth=img.shape
M=cv2.getRotationMatrix2D((cols/2,rows/2),45,1)
img = cv2.warpAffine(img, M, (width, height))
cv2.imwrite('image/result.jpg', img)
# 翻转
def Flip(src):
########Begin########
#图像放缩
src=cv2.resize(src,(256,256))
#水平镜像
horizontal=cv2.flip(src,1)
#垂直镜像
vertical=cv2.flip(src,0)
#对角镜像 ,并保存
cross=cv2.flip(src,-1)
cv2.imwrite("image/result.jpg", cross)
# 仿射变换
def affine_trans(src):
src = cv2.resize(src, (256, 256))
# 获取图像shape
rows, cols = src.shape[: 2]
post1=np.float32([[50,50],[200,50],[50,200]])
post2=np.float32([[10,100],[200,50],[100,250]])
M=cv2.getAffineTransform(post1,post2)
result = cv2.warpAffine(src,M,(rows,cols))
cv2.imwrite("image/try.jpg", result)