机器视觉OpenCv(三)-图片几何变换(一)

机器视觉OpenCv+Python 学习(三)-图片几何变换(一)

图片几何变换包括图片剪切、缩放、位移、镜像、旋转等,可以归结为仿射变换。以下为代码实现:

图片位移

import cv2 as cv
import numpy as np
img =cv.imread ('E:/pythonProject/picture/CSet12/monarch.bmp',1)
imgInfo = img.shape
height = imgInfo[0]
width  = imgInfo[1]
print(imgInfo)
matShift =np.float32([[1,0,100],
                    [0,1,100]])                  #仿射变换矩阵
dst =cv.warpAffine(img,matShift,(imgInfo[0],imgInfo[1]))  #向下,向右平移100
print(dst.shape)
#源码
dst2 =np.zeros(imgInfo,np.uint8)
for i in range(0,height-100):
    for j in range(0,width-100):
        dst2[i+100,j+100]=img[i,j]

cv.imshow('img',img)
cv.imshow('dst',dst)
cv.imshow('dst2',dst2)
cv.waitKey(0)

图片剪切

import cv2 as cv

img =cv.imread ('E:/pythonProject/picture/CSet12/lena.png',1)
imgInfo = img.shape
dst =img[100:200,100:300]  # 需要剪切的坐标位置
cv.imshow('dst',dst)
cv.waitKey(0)

图片旋转

import cv2 as cv
import numpy as np
img =cv.imread ('E:/pythonProject/picture/CSet12/monarch.bmp',1)
cv.imshow('img',img)
imgInfo = img.shape
height = imgInfo[0]
width  = imgInfo[1]

matRotate =cv.getRotationMatrix2D((height*0.5,width*0.5),45,0.5)
'''
函数调用形式:
getRotationMatrix2D(Point2f center, double angle, double scale)

参数详解:
Point2f center:表示旋转的中心点
double angle:表示旋转的角度
double scale:图像缩放因子
'''
dst = cv.warpAffine(img,matRotate,(width,height))

cv.imshow('dst',dst)
cv.waitKey(0)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值