opencv python投影变换效果

变换原理:

https://www.cnblogs.com/txwtech/p/18024547

python示范代码:

src2原图,4个坐标点

dst2转换后,4个坐标点

复制代码
p_touyin = cv2.getPerspectiveTransform(src2,dst2) #计算投影变换矩阵
复制代码
#利用矩阵值进行图像投影变换
复制代码
r_bianhuan = cv2.warpPerspective(img_touyin,p_touyin,(w2,h2),borderValue = 125)
# 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.


if __name__ == '__main__':
    print_hi('nice day')

    #图像的仿射变换
    #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)


    #python opencv resize实现缩放,缩放到原来的一般
    dst2 = cv2.resize(image,(int(h_y/2),int(w_x/2)))
    #cv2.imshow('0.5',dst2)
    '''
    dsize:是一个整体包好了(宽和高)(int(h_y/2),int(w_x/2))
    Traceback (most recent call last):
  File "d:/PycharmProjects/pythonProject2/main.py", line 35, in <module>
    dst2 = cv2.resize(image,int(h_y/2),int(w_x/2))
cv2.error: OpenCV(4.9.0) :-1: error: (-5:Bad argument) in function 'resize'
> Overload resolution failed:
>  - Can't parse 'dsize'. Input argument doesn't provide sequence protocol
>  - Can't parse 'dsize'. Input argument doesn't provide sequence protocol
    '''
    #缩放0.25倍,四分之一
    dst2a = cv2.resize(image,(0,0),fx=0.25,fy=0.25,interpolation=cv2.INTER_LINEAR)
    # cv2.imshow('0.25x', dst2a)
    # cv2.waitKey(0)
    # cv2.destroyAllWindows()
    #rotate实现图像旋转函数翻转
    rImg = cv2.rotate(image, cv2.ROTATE_90_CLOCKWISE)
   # cv2.imshow('rotate', rImg)
   # cv2.waitKey(0)
   #cv2.destroyAllWindows()
    img_touyin = cv2.imread(r'D:\deep_learning\hub2.jpg', cv2.IMREAD_GRAYSCALE)
    h2, w2 = img_touyin.shape
    #src2 = np.array([[343,1140],[715,1136],[295,1644],[889,1656]],np.float32)
    #dst2 = np.array([[295,1140],[889,1140],[295,1844],[889,1856]],np.float32)

    src2 = np.array([[525, 187], [735,337], [503, 1299], [741, 1313]], np.float32)
    dst2 = np.array([[280, 337], [735,337], [280, 1313], [741, 1313]], np.float32)
    p_touyin = cv2.getPerspectiveTransform(src2,dst2) #计算投影变换矩阵
    #利用矩阵值进行图像投影变换
    r_bianhuan = cv2.warpPerspective(img_touyin,p_touyin,(w2,h2),borderValue = 125)
    cv2.imshow('yuantu',img_touyin)
    cv2.imwrite(r'D:\deep_learning\bianhuan2a.jpg', r_bianhuan)
    cv2.imshow('warpPerspective',r_bianhuan)
    cv2.waitKey(0)
    cv2.destroyAllWindows()
相关推荐
Chef_Chen几秒前
从0开始机器学习--Day17--神经网络反向传播作业
python·神经网络·机器学习
百事老饼干4 分钟前
Java[面试题]-真实面试
java·开发语言·面试
千澜空20 分钟前
celery在django项目中实现并发任务和定时任务
python·django·celery·定时任务·异步任务
斯凯利.瑞恩27 分钟前
Python决策树、随机森林、朴素贝叶斯、KNN(K-最近邻居)分类分析银行拉新活动挖掘潜在贷款客户附数据代码
python·决策树·随机森林
杨荧42 分钟前
【JAVA毕业设计】基于Vue和SpringBoot的服装商城系统学科竞赛管理系统
java·开发语言·vue.js·spring boot·spring cloud·java-ee·kafka
白子寰1 小时前
【C++打怪之路Lv14】- “多态“篇
开发语言·c++
yannan201903131 小时前
【算法】(Python)动态规划
python·算法·动态规划
蒙娜丽宁1 小时前
《Python OpenCV从菜鸟到高手》——零基础进阶,开启图像处理与计算机视觉的大门!
python·opencv·计算机视觉
光芒再现dev1 小时前
已解决,部署GPTSoVITS报错‘AsyncRequest‘ object has no attribute ‘_json_response_data‘
运维·python·gpt·语言模型·自然语言处理
王俊山IT1 小时前
C++学习笔记----10、模块、头文件及各种主题(一)---- 模块(5)
开发语言·c++·笔记·学习