python opencv 图像透视变换,图像平移,图像旋转,图像翻转

1, 缩放
缩放,使用cv2.INTER_AREA
扩展,使用cv2.INTER_CUBIC和cv2.INTER_LINEAR

img = cv.imread('./data/messi5.jpg')
result = cv.resize(img, None, fx=2, fy=2, interpolation=cv.INTER_CUBIC) # w,h都放大两倍

# height, width = img.shape[:2]
# result = cv2.resize(img,(2*width, 2*height), interpolation = cv2.INTER_CUBIC)

cv.imshow('img', img)
cv.imshow('res', res)
cv.waitKey(0)
cv.destroyAllWindows()

2,平移

img = cv.imread('./data/messi5.jpg')
rows, cols = img.shape[:2]
M = np.float32([[1,0,100],[0,1,50]]) # (x,y) -> (100,50)
dst = cv.warpAffine(img, M, (cols, rows))

3,旋转

img = cv.imread('./data/messi5.jpg')
rows,cols = img.shape[:2]

# 这里的第一个参数为旋转中心,第二个参数为旋转角度,第三个参数为旋转后的缩放因子
# 可以设置旋转中心,缩放因子,以及窗口大小来防止旋转后超出边界的问题
M = cv.getRotationMatrix2D((cols/2, rows/2), 90, 1.0)

# 第三个参数是输出图像的尺寸中心
dst = cv.warpAffine(img, M, (cols, rows))

4,仿射变换

img = cv2.imread('drawing.png')
rows, cols, ch = img.shape
print(img.shape)

pts1 = np.float32([[50, 50], [200, 50], [50, 200]])
pts2 = np.float32([[10, 100], [200, 50], [100, 250]])

M = cv2.getAffineTransform(pts1, pts2)
dst = cv2.warpAffine(img, M, (cols, rows))

5,透视变换

img = cv2.imread('../data/sudoku.jpg')
rows, cols, ch = img.shape

pts1 = np.float32([[56, 65], [368, 52], [28, 387], [389, 390]])
pts2 = np.float32([[0, 0], [300, 0], [0, 300], [300, 300]])

M = cv2.getPerspectiveTransform(pts1, pts2)
dst = cv2.warpPerspective(img, M, (300, 300))

6, 图像翻转

import cv2 as cv

src = cv.imread('./img/lena.jpg')
cv.imshow('Original', src)

# 图像水平翻转
flipped1 = cv.flip(src, 1)
cv.imshow('Flipped Horizontally', flipped1)

# 图像垂直翻转
flipped2 = cv.flip(src, 0)
cv.imshow('Flipped Vectically', flipped2)

# 图像水平垂直翻转
flipped3 = cv.flip(src, -1)
cv.imshow('Flipped Horizontally & Vertically', flipped3)

7, 读取json文件,透视变换保存图片

# -*- coding: utf-8 -*-  
import json, os
import numpy as np
import cv2 as cv

label_path = r"C:\Users\RoseC\Desktop\blured"

save_path = r"C:\Users\RoseC\Desktop\blured_img"

for filename in os.listdir(label_path):
    # 定义特征点
    mark_x1, mark_y1 = 0, 0    # 左上
    mark_x2, mark_y2 = 0, 0    # 右上
    mark_x3, mark_y3 = 0, 0    # 左下
    mark_x4, mark_y4 = 0, 0    # 右下
    if filename.endswith(".json"):
        json_path = os.path.join(label_path, filename)
        data = json.load(open(json_path, 'r'))

        img_path = os.path.join(label_path, data['imagePath'])     # 图像路径
        save_img_path = os.path.join(save_path, data['imagePath']) # 保存路径

        # 拿到坐标
        for obj in data['shapes']:
            if obj['label'] == "left_up":   # 左上点
                mark_x1 = int(obj['points'][0][0])
                mark_y1 = int(obj['points'][0][1])
            if obj['label'] == "right_up":   # 右上点
                mark_x2 = int(obj['points'][0][0])
                mark_y2 = int(obj['points'][0][1])
            if obj['label'] == "left_down":   # 左下点
                mark_x3 = int(obj['points'][0][0])
                mark_y3 = int(obj['points'][0][1])
            if obj['label'] == "right_down":  # 右下点
                mark_x4 = int(obj['points'][0][0])
                mark_y4 = int(obj['points'][0][1])

        # 显示图片
        src = cv.imread(img_path)

        width = mark_x4 - mark_x1
        height = mark_y4 - mark_y1
        pts1 = np.float32([[mark_x1, mark_y1], [mark_x2, mark_y2], [mark_x3, mark_y3], [mark_x4, mark_y4]])  # 坐标分别为,左上,右上,左下,右下
        pts2 = np.float32([[0, 0], [width, 0], [0, height], [width, height]])

        M = cv.getPerspectiveTransform(pts1, pts2)
        dst = cv.warpPerspective(src, M, (width, height))


        cv.imwrite(save_img_path, dst, [int(cv.IMWRITE_JPEG_QUALITY), 95])  # 默认95

8,自定义图片旋转

import cv2
import numpy as np

def rotate_bound(image, angle):
    # grab the dimensions of the image and then determine the
    # center
    (h, w) = image.shape[:2]
    (cX, cY) = (w // 2, h // 2)
 
    # grab the rotation matrix (applying the negative of the
    # angle to rotate clockwise), then grab the sine and cosine
    # (i.e., the rotation components of the matrix)
    M = cv2.getRotationMatrix2D((cX, cY), -angle, 1.0)
    cos = np.abs(M[0, 0])
    sin = np.abs(M[0, 1])
 
    # compute the new bounding dimensions of the image
    nW = int((h * sin) + (w * cos))
    nH = int((h * cos) + (w * sin))
 
    # adjust the rotation matrix to take into account translation
    M[0, 2] += (nW / 2) - cX
    M[1, 2] += (nH / 2) - cY
 
    # perform the actual rotation and return the image
    return cv2.warpAffine(image, M, (nW, nH))
 
image=cv2.imread('./img/fin.png')
angle=90
imag=rotate_bound(image,angle)
cv2.imshow('ww',imag)
cv2.waitKey()
cv.destroyAllWindows()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值