打开CV Python-图像变形径向和透视 -

时间:2017-03-14 17:23:03

标签: python opencv perspective distortion

我有一张扭曲的图片,在没有扭曲的情况下,A,B C和D点形成一个1 cm * 1 cm的正方形。

enter image description here

我尝试使用单应性来纠正它,但它会扭曲AD和BC线,如图所示。

enter image description here

你知道我怎么能纠正这个?

非常感谢!

Marie-coder初学者

PS:有关信息,图像是在带有内窥镜摄像头的管子中拍摄的,该摄像头具有大视野,可以拍摄几乎在摄像机周围的管子。我将使用1 * 1平方厘米估计根系生长,随着时间的推移拍摄几张照片。

这是我的代码:

import cv2
import numpy as np
import matplotlib.pyplot as plt

if __name__ == '__main__' :

# Read source image.
im_src = cv2.imread('points2.jpg', cv2.IMREAD_COLOR)
# Four points of the miniR image
pts_src = np.array([[742,223],[806,255],[818,507],[753,517]], dtype=float)



# Read destination image.
im_dst = cv2.imread('rectangle.jpg', cv2.IMREAD_COLOR) 
# Four points of the square
pts_dst = np.array([[200,200],[1000,200],[1000,1000],[200,1000]], dtype=float)


# Calculate Homography
h, status = cv2.findHomography(pts_src, pts_dst)

# Warp source image to destination based on homography
im_out = cv2.warpPerspective(im_src, h, (im_dst.shape[1],im_dst.shape[0]))

cv2.imwrite('corrected2.jpg', im_out)



# Display images
cv2.imshow("Source Image", im_src)
cv2.imshow("Destination Image", im_dst)
cv2.imshow("Warped Source Image", im_out)

cv2.waitKey(0)

2 个答案:

答案 0 :(得分:1)

单应性是一种投射变换。因此,它只能将直线映射到直线。您输入的曲线四边形的直边被正确校正,但是您无法使用投影变换来矫正弯曲的边。

在您发布的照片​​中,可以合理地假设整体几何形状大致为圆柱形,并且垂直方向为"线平行于圆柱轴。所以它们近似笔直,投影变换(相机投影)将它们映射成直线。 "水平"线条是圆圈的图像,如果圆柱体被压扁则为椭圆形。投影变换将椭圆(特别是圆圈)映射为椭圆。所以你可以通过拟合椭圆来继续。有关提示,请参阅this other answer

答案 1 :(得分:0)

我找到了使用GDAL的解决方案。我们可以使用两个棋盘图像。使用该设备对一张图像成像会产生变形,并且不会发生变化-因此不会变形。使用QGIS帮助,您可以创建一个文件,将失真点与未失真点相关联。为此,您可以使用定义的网格间隔(例如100px)在每个交叉点处添加一个地面控制点,并将导出的GCP导出为pointsfile.points

此后,您可以使用协作者创建的here批处理文件。它正在使用GDAL对图像进行地理校正。

您只需要将要转换的图像(jpg格式)放入存储库的根目录中,然后运行bashwarp.sh。这会将重新转换的图像输出到out /目录。

相关问题