python opencv仿射变化--缩放--平移--旋转

python opencv仿射变化--缩放--平移--旋转

# This is a sample Python script.

# Press Shift+F10 to execute it or replace it with your code.
# Press Double Shift to search everywhere for classes, files, tool windows, actions, and settings.


def print_hi(name):
    # Use a breakpoint in the code line below to debug your script.
    print(f'Hi, {name}')  # Press Ctrl+F8 to toggle the breakpoint.

import sys
import cv2
from numpy import *
import numpy as np
# Press the green button in the gutter to run the script.
def zh_cn(string):
    return str(string).encode('gb2312').decode(errors='ignore')

if __name__ == '__main__':
    print_hi('nice day')
    # if(len(sys.argv)>1):
    #   #  img = cv2.imread(sys.argv[1], cv2.CV_LOAD_IMAGE_GRAYSCALE)
    #   #img = cv2.imread(sys.argv[1], cv2.IMREAD_GRAYSCALE)
    #   img = cv2.imread(sys.argv[1], cv2.IMREAD_COLOR)
    # else:
    #     print('img read error')
    # blue = img[:, :, 0]
    # green = img[:, :, 1]
    # red = img[:, :, 2]

    # cv2.imshow('blue', blue)
    # cv2.imshow('green', green)
    # cv2.imshow('red', red)
    # cv2.waitKey(0)
    # cv2.destroyAllWindows()
    #如何通过已知坐标及其对应的经过某种仿射变换后的坐标,而计算出它们之间的仿射变换矩阵
    src = np.array([
        [0,0],
        [200,0],
        [0,200]],np.float32)
    dst = np.array([
        [0,0],
        [100,0],
        [0,100]],np.float32)
    A = cv2.getAffineTransform(src,dst)
    print('\n', A)
    print('对空间坐标先等比例缩放2倍,然后在水平方向上平移100,在垂直方向上平移200,'
          '计算该仿射变换矩阵\n')
    #先缩放,再平移
    s = np.array([
        [0.5,0,0],
        [0,0.5,0],
        [0,0,1]])
    t = np.array([
        [1,0,100],
        [0,1,200],
        [0,0,1]])
    A_s_t = np.dot(t,s) #右乘,所以s放后面
    print('\n',A_s_t)
    '''
    [[  0.5   0.  100. ]
    [  0.    0.5 200. ]
    [  0.    0.    1. ]]
    '''
    #以一个坐标点(40,50),旋转30度,等比缩放0.5
    dengbi_suofang = cv2.getRotationMatrix2D((40,50),30,0.5)
    print('\n', dengbi_suofang)
    '''
    [[ 0.4330127   0.25       10.17949192]
    [-0.25        0.4330127  38.34936491]]
    '''
    print('矩阵数据类型\n',dengbi_suofang.dtype)
    #D:\deep_learning\qiqiu\qiqiu.jpg


    #图像的仿射变换
    #if len(sys.argv) >1:
    if 2 > 1:
        #image = cv2.imread(sys.argv[1],cv2.IMREAD_GRAYSCALE)
        image = cv2.imread(r'D:\deep_learning\qiqiu\qiqiu.jpg', cv2.IMREAD_GRAYSCALE)
    else:
        print('image error')
    cv2.imwrite('img0206.jpg',image)
    #读取原图高和宽
    h_y, w_x = image.shape[:2]
    print('h,w',h_y, w_x)
    suoxiao2x=np.array([[0.5,0,0],
                       [0,0.5,0]],np.float32) #仿射变化矩阵,缩小2倍
    # 图像的仿射变换
    img_bianhuan = cv2.warpAffine(image,suoxiao2x,(w_x,h_y),borderValue=125)

    #先缩小2倍,再平移
    sx_2x = np.array([
        [0.5,0,w_x/4],
        [0,0.5,h_y/4]],float32)
    img_bianhuan2 = cv2.warpAffine(image,sx_2x,(w_x,h_y),borderValue=125)

    #在img_bianhuan2基础上饶图像中心点旋转
    a3_mat = cv2.getRotationMatrix2D((w_x/2.0,h_y/2.0),30,1)
    img_bianhuan3 = cv2.warpAffine(img_bianhuan2,a3_mat,(w_x,h_y),borderValue=125)
    cv2.imshow('image',image)
    #在图像上绘制文字
    from new_font import cv2_chinese_text
    img2a = cv2_chinese_text(img_bianhuan,r'缩放', w_x/2-50, h_y/2-50)
    cv2.imshow('suofang',img2a)
    #cv2.imshow(r'平移', img_bianhuan2)
    #cv2.imshow(r'旋转', img_bianhuan3)
    cv2.waitKey(0)
    cv2.destroyAllWindows()

 

posted @ 2024-02-07 16:58  txwtech  阅读(88)  评论(0编辑  收藏  举报