使用python opencv对畸变图像进行矫正的实现

作者:燕阳天 时间:2021-08-18 01:09:44 

代码:

__Author__ = "Shliang"
__Email__ = "shliang0603@gmail.com"

import os
import cv2
import numpy as np
from tqdm import tqdm

def undistort(frame):
   fx = 685.646752
   cx = 649.107905
   fy = 676.658033
   cy = 338.054431
   k1, k2, p1, p2, k3 = -0.363219, 0.093818, 0.006178, -0.003714, 0.0

# 相机坐标系到像素坐标系的转换矩阵
   k = np.array([
       [fx, 0, cx],
       [0, fy, cy],
       [0, 0, 1]
   ])
   # 畸变系数
   d = np.array([
       k1, k2, p1, p2, k3
   ])
   h, w = frame.shape[:2]
   mapx, mapy = cv2.initUndistortRectifyMap(k, d, None, k, (w, h), 5)
   return cv2.remap(frame, mapx, mapy, cv2.INTER_LINEAR)

# 对摄像头实时视频流做畸变矫正
def distortion_correction_cam():
   cap = cv2.VideoCapture(0)
   while (cap.isOpened()):
       ret, frame = cap.read()
       undistort_frame = undistort(frame)
       compare = np.hstack((frame, undistort_frame))
       cv2.imshow('frame', compare)

if cv2.waitKey(1) & 0xFF == ord('q'):
           break
   cap.release()
   cv2.destroyAllWindows()

# 对目录下的所有图片做畸变矫正,并把畸变矫正后的图片保存下来
def distortion_correction_imgs(input_dir, output_dir):
   in_imgs = os.listdir(input_dir)

for img_name in tqdm(in_imgs):
       image = cv2.imread(os.path.join(input_dir, img_name))
       distroted_img = undistort(image)
       cv2.imwrite(os.path.join(output_dir, img_name), distroted_img)

if __name__ == '__main__':
   input_dir = "/home/shl/extract_rosbag_data/0324_bags/plycal_calib/root/images"
   output_dir = "/home/shl/extract_rosbag_data/0324_bags/plycal_calib/root/distro_imgs"
   # distortion_correction_imgs(input_dir, output_dir)

distortion_correction_cam()

对图片进行矫正效果:

原图:

使用python opencv对畸变图像进行矫正的实现

矫正后的图片:

使用python opencv对畸变图像进行矫正的实现

采集的摄像头画面矫正效果:

使用python opencv对畸变图像进行矫正的实现

从上面的换面可以看到,左边是未矫正的画面,右边是矫正后的画面

  • 矫正后的画面会被裁切,明显可以看到画面中的信息是有裁切的,例如左边的椅子已经被裁切掉了

  • 矫正后的画面和原画面是保持相同的分辨率:640x480,但是,为什么会看到画面会出现横向的拉伸,这是因为标定相机内参的时候画面的分辨率设置为1280x720=16:9,但是opencv读取摄像头默认的分辨率却是640x480=4:3,两者的比例都不一样,所以肯定会出现拉伸

解决拉伸的方式,就是把读取摄像头的时候,把摄像头的分辨率设置成和标定的时候一样的分辨率,设置为1280x720,下面是如何在opencv读取摄像头的时候设置摄像头分辨率:

# 对摄像头实时视频流做畸变矫正
def distortion_correction_cam():
   cap = cv2.VideoCapture(0)

# 获取摄像头读取画面的宽和高
   width = cap.get(3)
   height = cap.get(4)
   fps = cap.get(5)
   print(width, height, fps)  # 640.0 480.0 30.0

# 在这里把摄像头的分辨率修改为和我们标定时使用的一样的分辨率 1280x720
   cap.set(3, 1280)
   cap.set(4, 720)
   width = cap.get(3)
   height = cap.get(4)
   print(width, height, fps)  # 1280.0 720.0 30.0

while (cap.isOpened()):
       ret, frame = cap.read()
       print(frame.shape)
       undistort_frame = undistort(frame)
       compare = np.hstack((frame, undistort_frame))
       cv2.imshow('frame', compare)

if cv2.waitKey(1) & 0xFF == ord('q'):
           break
   cap.release()
   cv2.destroyAllWindows()

重新设置分辨率后,矫正前后画面对比,可以看到几乎是没有横向或纵向拉伸的!

使用python opencv对畸变图像进行矫正的实现

参考:

https://blog.csdn.net/weixin_40516558/article/details/103494029

https://blog.csdn.net/guaiderzhu1314/article/details/96306509

https://www.codenong.com/cs110623399/

来源:https://juejin.cn/post/7094064438243753991

标签:python,opencv,图像矫正
0
投稿

猜你喜欢

  • JavaScript运动框架 多值运动(四)

    2023-09-08 01:44:51
  • python队列queue模块详解

    2023-03-28 17:26:02
  • 基于Keras的格式化输出Loss实现方式

    2021-10-20 20:44:00
  • OpenCV实现直线检测

    2023-08-14 01:37:35
  • 一文秒懂python中的 \\r 与 end=‘’ 巧妙用法

    2022-10-06 08:33:28
  • Server_Name与Http_Host的区别

    2008-06-19 13:39:00
  • python tensorflow学习之识别单张图片的实现的示例

    2023-06-30 21:11:32
  • asp从Excel中筛选符合条件的记录保存至新的Excel中

    2007-09-06 19:20:00
  • python模拟哔哩哔哩滑块登入验证的实现

    2021-05-01 22:23:40
  • python单测框架之pytest常见用法

    2021-05-22 04:13:02
  • WEB页面工具语言XML应用分类之运用

    2008-05-29 10:58:00
  • Linux操作系统中如何安装MySQL数据库

    2007-10-26 15:58:00
  • “)”引起PNG透明滤镜失效

    2008-08-11 13:10:00
  • pyppeteer执行js绕过webdriver监测方法上

    2021-12-13 12:04:40
  • Python Selenium XPath根据文本内容查找元素的方法

    2022-02-18 12:55:16
  • Python利用物理引擎Pymunk编写一个解压小游戏

    2022-03-08 16:15:45
  • python-pandas创建Series数据类型的操作

    2022-01-29 14:16:24
  • 利用Python删除电脑中重复文件的方法

    2023-06-19 21:58:20
  • Python处理mysql数据库

    2010-12-03 16:23:00
  • 在pytorch中如何查看模型model参数parameters

    2021-12-04 22:43:29
  • asp之家 网络编程 m.aspxhome.com