opencv 旋转

 

这个旋转是ok的

import cv2
 
# 顺时针旋转90度
def Rotate90(img):
    trans_img = cv2.transpose(img)
    new_img = cv2.flip(trans_img, 1)
    return new_img
 
 
# 逆时针旋转90度
def Rotate_90(img):
    trans_img = cv2.transpose( img )
    new_img = cv2.flip( trans_img, 0 )
    return new_img
 
def test(img_path):
    img = cv2.imread(img_path)
    cv2.imshow('raw', img)
 
    clock90_img = Rotate_90(img)
    cv2.imshow( 'Rotate90', clock90_img )
 
    clock_90_img = Rotate90(img)
    cv2.imshow('Rotate_90', clock_90_img)
 
    resize_img = cv2.resize(img,(int(img.shape[1]*.5),int(img.shape[0]*.5)))
    cv2.imshow('resize_img', resize_img)
 
 
 
if __name__ == '__main__':
    test('mi.jpg')
 
    cv2.waitKey(0)
    cv2.destroyAllWindows()


 

这个旋转矩形,会自动裁剪头尾,不能用

 

import numpy as np #1
import argparse #2
import imutils #3
import cv2 #4
 
ap = argparse.ArgumentParser() #5
ap.add_argument("-i", "--image", required = True,
    help = "Path to the image") #6
args = vars(ap.parse_args()) #7
 
image = cv2.imread(args["image"]) #8
cv2.imshow("Original", image) #9
 
(h, w) = image.shape[:2] #10
center = (w // 2, h // 2) #11
 
M = cv2.getRotationMatrix2D(center, 45, 1.0) #12
rotated = cv2.warpAffine(image, M, (w, h)) #13
cv2.imshow("Rotated by 45 Degrees", rotated) #14
 
M = cv2.getRotationMatrix2D(center, -90, 1.0) #15
rotated = cv2.warpAffine(image, M, (w, h)) #16
cv2.imshow("Rotated by -90 Degrees", rotated) #17
 
rotated = imutils.rotate(image, 180) #18
cv2.imshow("Rotated by 180 Degrees", rotated) #19
cv2.waitKey(0) #20


————————————————
版权声明:本文为CSDN博主「ShellCollector」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/jacke121/article/details/106278894

你可能感兴趣的:(opencv)