微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何使用open cv python从倾斜图像中获取拉直图像

如何解决如何使用open cv python从倾斜图像中获取拉直图像

我有一个倾斜的图像。我想从倾斜的图像中拉直图像。为此,我在这函数中使用了 fout 点变换函数,我使用了 getpersptive 变换方法代码在这里

   def order_points(pts):
     
        rect = np.zeros((4,2),dtype = "float32")
        s = pts.sum(axis = 1)
       
        rect[0] = pts[np.argmin(s)]
        rect[2] = pts[np.argmax(s)]

        diff = np.diff(pts,axis = 1)
        rect[1] = pts[np.argmin(diff)]
        rect[3] = pts[np.argmax(diff)]
        return rect

   def four_point_transform(image,pts):
 
      rect = order_points(pts)
      (tl,tr,br,bl) = rect
 
      widthA = np.sqrt(((br[0] - bl[0]) ** 2) + ((br[1] - bl[1]) ** 2))
      widthB = np.sqrt(((tr[0] - tl[0]) ** 2) + ((tr[1] - tl[1]) ** 2))
      maxWidth = max(int(widthA),int(widthB))

      heightA = np.sqrt(((tr[0] - br[0]) ** 2) + ((tr[1] - br[1]) ** 2))
      heightB = np.sqrt(((tl[0] - bl[0]) ** 2) + ((tl[1] - bl[1]) ** 2))
      maxHeight = max(int(heightA),int(heightB))

      dst = np.array([ [0,0],[maxWidth - 1,maxHeight - 1],[0,maxHeight - 1]],dtype = "float32")
      # compute the perspective transform matrix and then apply it
      M = cv2.getPerspectiveTransform(rect,dst)
      warped = cv2.warpPerspective(image,M,(maxWidth,maxHeight))

      return warped

 contours,hierarchy=cv2.findContours(canny_img,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
 contours=sorted(contours,key=cv2.contourArea,reverse=True)
 for c in contours:
    p=cv2.arcLength(c,True)
    approx=cv2.approxpolyDP(c,0.02*p,True)
    if len(approx)==4:
        target=approx.reshape(4,2)
        break
        
 target_image=four_point_transform(canny_img,target)
 plt.imshow(target_image)

我的代码或其他方法中缺少什么。请告诉我

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?