OpenCV - 使用倾斜角度(倾斜)调整照片 [英] OpenCV - Adjusting photo with skew angle (tilt)

查看:2187
本文介绍了OpenCV - 使用倾斜角度(倾斜)调整照片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a camera pointing at a Zen Garden from above. However, the camera is fixed on the side rather than directly above the plate. As a result, the image looks like this (note the skewed shape of the rectangle):

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

Is there a way to process the image so that the sand area can look more or less like a perfect square?

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

非常感谢。

推荐答案

您可以对图像进行透视转换。这是第一个让你进入球场的镜头:

You can do perspective transformation on your image. Here's a first shot that gets you in the ballpark:

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()

您可以更多地使用这些数字,但是你得到了想法。

You can fiddle more with the numbers, but you get the idea.

以下是一些在线示例。第一个链接有一个与转换矩阵相关的有用的挂钩点图:

Here's some online examples. The first link has a useful plot of peg points that correlate to the transformation matrix:

  • Geometric Transformations of Images
  • 4 Point OpenCV getPerspective Transform Example
  • Learn OpenCV by Examples: Perspective Transform

这篇关于OpenCV - 使用倾斜角度(倾斜)调整照片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆