image-perspective-transformation-图片透视变换(投影变换)

17 篇文章 6 订阅
11 篇文章 1 订阅
本文介绍了如何使用Python处理图片,特别是实现透视变换。通过选取图片的四个关键点,生成新的透视图。提供了核心代码实现,并包含鼠标交互功能,允许用户自定义变换点。此外,还分享了参考代码链接和个人项目代码库。
摘要由CSDN通过智能技术生成

image-perspective-transformation-图片透视变换(投影变换)

前言:

最近可能会用到透视变换,做一个数据预处理,因此想起来我之前写过一个透视变换的脚本,今天跑起来,还不够完善,因此将之前夹杂在论文阅读的博客中单独抽取出来,供大家参考~
话不多说,上示例图!
在这里插入图片描述
快来一起学习吧!

主要功能:

python处理图片,包括图片平移、图片旋转、图片缩放、图片翻转、透视变换。选择图片中的四个关键点和将要变换的点,用来生成新的透视图

Use translate,scale,flip,rotation to turn one picture into multiple pictures.

参考链接:

https://github.com/dapsjj/dealAllPicture 主框架用的这位大佬的代码,我加了透视变换和鼠标交互的功能。

我的代码链接:

https://github.com/kaixindelele/image-perspective-transformation

核心代码:

透视变换函数,首先要获取源区域和目标区域的四个点

    def pic_perspective(self,):    
	    ...        
        pts1 = []
        pts2 = []
        cv2.setMouseCallback("image", self.mouse, param=(image, pts1, pts2))        
        cv2.waitKey(0)
        cv2.destroyAllWindows()
        print("pts1:", pts1)
        pts1 = np.float32(pts1[:4])
        print("pts2:", pts2)
        pts2 = np.float32(pts2[:4])

        assert len(pts1)==4, "每个只允许四个点"    
    
        # 生成透视变换矩阵
        M = cv2.getPerspectiveTransform(pts1, pts2)
        # 进行透视变换
        dst = cv2.warpPerspective(image, M, (image.shape[1], image.shape[0]))
        cv2.imwrite('dst.jpg', dst)
        # matplotlib默认以RGB通道显示,所以需要用[:, :, ::-1]翻转一下
        plt.subplot(121), plt.imshow(image[:, :, ::-1]), plt.title('input')
        plt.subplot(122), plt.imshow(dst[:, :, ::-1]), plt.title('output')
        plt.show()

        # tkinter.messagebox.showinfo('提示', '透视变换的图片处理完毕!')
        return dst

对鼠标的坐标点获取:

    def mouse(self, event, x, y, flags, param):
        image = param[0]
        pts1 = param[1]
        pts2 = param[2]
        # 如果摁住左键,将坐标赋值给pts1
        if event == cv2.EVENT_LBUTTONDOWN:
            pts1.append([x, y])
            xy = "%d,%d" % (x, y)
            cv2.circle(image, (x, y), 4, (0, 255, 255), thickness = -1)
            cv2.putText(image, xy, (x, y), cv2.FONT_HERSHEY_PLAIN,
                        1.0, (0, 255, 255), thickness = 2)
            cv2.imshow("image", image)  
        # 如果摁住右键,将坐标赋值给pts2
        if event == cv2.EVENT_RBUTTONDOWN:
            pts2.append([x, y])
            xy = "%d,%d" % (x, y)
            cv2.circle(image, (x, y), 4, (255, 0, 255), thickness = -1)
            cv2.putText(image, xy, (x, y), cv2.FONT_HERSHEY_PLAIN,
                        1.0, (255, 0, 255), thickness = 2)
            cv2.imshow("image", image)    

联系方式:

ps: 欢迎做强化的同学加群一起学习:

深度强化学习-DRL:799378128

欢迎关注知乎帐号:未入门的炼丹学徒

CSDN帐号:https://blog.csdn.net/hehedadaq

极简spinup+HER+PER代码实现:https://github.com/kaixindelele/DRLib

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

hehedadaq

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值