opencv学习(五)缩放平移旋转

import cv2
import numpy as np

img = cv2.imread("logo.jpg")
cv2.imshow("org", img)

#####################缩放############################

# 方式1,通过设置缩放因子来缩放
res1 = cv2.resize(img, None, fx=2, fy=2, interpolation=cv2.INTER_LINEAR)
cv2.imshow("big", res1)

# 方式2,直接通过宽高进行缩放
height, width = img.shape[:2]
res2 = cv2.resize(img, (2*width, 2*height), interpolation=cv2.INTER_LINEAR)
cv2.imshow("big2", res2)

#####################平移############################

# 转换到hsv
hsv = cv2.cvtColor(res2, cv2.COLOR_BGR2HSV)

# 设定白色的阈值
lower_white = np.array([0,0,221])
uper_white = np.array([180,30,255])

# 根据阈值构建掩模
mask = cv2.inRange(hsv, lower_white, uper_white)
cv2.imshow("mask", mask)

# 平移
res3 = cv2.bitwise_and(res2, res2, mask=mask)
direction = np.float32([[1, 0, 50], [0, 1, 90]])
move_img = cv2.warpAffine(res3, direction, (res3.shape[1], res3.shape[0]))
cv2.imshow("move", move_img)

#####################旋转############################
M = cv2.getRotationMatrix2D((width/2, height/2), 45, 0.6)
rotation = cv2.warpAffine(mask, M, (width*2, height*2))
cv2.imshow("rotation", rotation)

cv2.waitKey(0)
cv2.destroyAllWindows()

平移效果
在这里插入图片描述
旋转效果
在这里插入图片描述

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值