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()

平移效果
opencv学习(五)缩放平移旋转_第1张图片
旋转效果
opencv学习(五)缩放平移旋转_第2张图片

你可能感兴趣的:(opencv)