【问题标题】:OpenCV - Adjusting photo with skew angle (tilt)OpenCV - 用倾斜角度调整照片(倾斜)
【发布时间】:2016-02-03 12:34:21
【问题描述】:

我有一个相机从上面指向一个禅宗花园。但是,相机固定在侧面而不是直接在板上方。结果,图像看起来像这样(注意矩形的倾斜形状):

有没有办法处理图像,使沙区看起来或多或少像一个完美的正方形?

cap = cv2.VideoCapture(0)
while True:
    ret, img = cap.read()
    img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) 
    img = cv2.flip(img,0)
    cv2.imshow('Cropping', img)
    if cv2.waitKey(500) & 0xff == 27:
        cv2.destroyAllWindows()
        break

非常感谢。

【问题讨论】:

    标签: python opencv image-processing


    【解决方案1】:

    您可以对图像进行透视变换。这是让您进入球场的第一枪:

    import cv2
    import numpy as np
    
    img = cv2.imread('zen.jpg')
    rows, cols, ch = img.shape    
    pts1 = np.float32(
        [[cols*.25, rows*.95],
         [cols*.90, rows*.95],
         [cols*.10, 0],
         [cols,     0]]
    )
    pts2 = np.float32(
        [[cols*0.1, rows],
         [cols,     rows],
         [0,        0],
         [cols,     0]]
    )    
    M = cv2.getPerspectiveTransform(pts1,pts2)
    dst = cv2.warpPerspective(img, M, (cols, rows))
    cv2.imshow('My Zen Garden', dst)
    cv2.imwrite('zen.jpg', dst)
    cv2.waitKey()
    

    你可以更多地摆弄数字,但你明白了。

    这里有一些在线示例。第一个链接有一个有用的与变换矩阵相关的定位点图:

    【讨论】:

      猜你喜欢
      • 2023-03-23
      • 2019-12-15
      • 2017-05-18
      • 2013-03-16
      • 1970-01-01
      • 2016-05-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多