最近在做全景拼接项目,有有六张来自于六目摄像头的图片,分别来自不同方向上的图片,摄像头示意图如下:
                                                                   

各方向上图片的位置示意图如下:
                                                      

假设相对应位置的图片为拼接起来后(此处我们用代码进行了拼接,而且需要这样结构拼接起来作为输入):

球面变换和拼接的代码如下:

from PIL import Image
import math
import numpy as np
import cv2def spherical_coordinates(i, j, w, h):""" Returns spherical coordinates of the pixel from the output image. """theta = 2*float(i)/float(w)-1phi = 2*float(j)/float(h)-1# phi = lat, theta = longreturn phi*(math.pi/2), theta*math.pidef vector_coordinates(phi, theta):""" Returns 3D vector which points to the pixel location inside a sphere. """return (math.cos(phi) * math.cos(theta),  # Xmath.sin(phi),                    # Ymath.cos(phi) * math.sin(theta))  # Z# Assign identifiers to the faces of the cube
FACE_Z_POS = 1  # Left
FACE_Z_NEG = 2  # Right
FACE_Y_POS = 3  # Top
FACE_Y_NEG = 4  # Bottom
FACE_X_NEG = 5  # Front
FACE_X_POS = 6  # Backdef get_face(x, y, z):""" Uses 3D vector to find which cube face the pixel lies on. """largest_magnitude = max(abs(x), abs(y), abs(z))if largest_magnitude - abs(x) < 0.00001:return FACE_X_POS if x < 0 else FACE_X_NEGelif largest_magnitude - abs(y) < 0.00001:return FACE_Y_POS if y < 0 else FACE_Y_NEGelif largest_magnitude - abs(z) < 0.00001:return FACE_Z_POS if z < 0 else FACE_Z_NEGdef raw_face_coordinates(face, x, y, z):"""Return coordinates with necessary sign (- or +) depending on which face they lie on.From Open-GL specification (chapter 3.8.10) https://www.opengl.org/registry/doc/glspec41.core.20100725.pdf"""if face == FACE_X_NEG:xc = zyc = yma = xreturn xc, yc, maelif face == FACE_X_POS:xc = -zyc = yma = xreturn xc, yc, maelif face == FACE_Y_NEG:xc = zyc = -xma = yreturn xc, yc, maelif face == FACE_Y_POS:xc = zyc = xma = yreturn xc, yc, maelif face == FACE_Z_POS:xc = xyc = yma = zreturn xc, yc, maelif face == FACE_Z_NEG:xc = -xyc = yma = zreturn xc, yc, madef raw_coordinates(xc, yc, ma):""" Return 2D coordinates on the specified face relative to the bottom-left corner of the face. Also from Open-GL spec."""return (float(xc)/abs(float(ma)) + 1) / 2, (float(yc)/abs(float(ma)) + 1) / 2def face_origin_coordinates(face, n):""" Return bottom-left coordinate of specified face in the input image. """if face == FACE_X_NEG:return n, nelif face == FACE_X_POS:return 3*n, nelif face == FACE_Z_NEG:return 2*n, nelif face == FACE_Z_POS:return 0, nelif face == FACE_Y_POS:return n, 0elif face == FACE_Y_NEG:return n, 2*ndef normalized_coordinates(face, x, y, n):""" Return pixel coordinates in the input image where the specified pixel lies. """face_coords = face_origin_coordinates(face, n)normalized_x = math.floor(x*n)normalized_y = math.floor(y*n)# Stop out of bound behaviourif normalized_x < 0:normalized_x = 0elif normalized_x >= n:normalized_x = n-1if normalized_y < 0:normalized_x = 0elif normalized_y >= n:normalized_y = n-1return face_coords[0] + normalized_x, face_coords[1] + normalized_ydef find_corresponding_pixel(i, j, w, h, n):""":param i: X coordinate of output image pixel:param j: Y coordinate of output image pixel:param w: Width of output image:param h: Height of output image:param n: Height/Width of each square face:return: Pixel coordinates for the input image that a specified pixel in the output image maps to."""spherical = spherical_coordinates(i, j, w, h)vector_coords = vector_coordinates(spherical[0], spherical[1])face = get_face(vector_coords[0], vector_coords[1], vector_coords[2])raw_face_coords = raw_face_coordinates(face, vector_coords[0], vector_coords[1], vector_coords[2])cube_coords = raw_coordinates(raw_face_coords[0], raw_face_coords[1], raw_face_coords[2])return normalized_coordinates(face, cube_coords[0], cube_coords[1], n)def get_six_pics_stitching(val_resize):size_t = val_resize# generate a (512,512) black imagetarget = np.zeros((size_t, size_t), dtype=np.uint8) ret = cv2.cvtColor(target, cv2.COLOR_GRAY2BGR)imgs_index = ["top", "left", "front", "right", "back", "down"]imgs_set = []# read all the images from target filefor i in range(6):img = cv2.imread("./pics/{}.jpg".format(imgs_index[i]))img_res = cv2.resize(img, (size_t, size_t))imgs_set.append(img_res)# first row stitchingret_pano1 = np.concatenate((ret, imgs_set[0]), axis=1)for i in range(2):ret_pano1 = np.concatenate((ret_pano1, ret), axis=1)# Second row stitchingimgs_mid_four = imgs_set[1:5]ret_pano2 = imgs_mid_four[0]for i in range(3):ret_pano2 = np.concatenate((ret_pano2, imgs_mid_four[i + 1]), axis=1)# Third row stitchingret_pano3 = np.concatenate((ret, imgs_set[5]), axis=1)for i in range(2):ret_pano3 = np.concatenate((ret_pano3, ret), axis=1)# whole image sitichingret_pano = np.concatenate((ret_pano1, ret_pano2), axis=0)ret_pano = np.concatenate((ret_pano, ret_pano3), axis=0)cv2.imwrite('six_pics_stitching.jpg',ret_pano)def convert_img(infile, outfile):inimg = Image.open(infile)wo, ho = inimg.size# Calculate height and width of output image, and size of each square faceh = int(wo/3)w = 2*hn = int(ho/3)# Create new image with width w, and height houtimg = Image.new('RGB', (w, h))# For each pixel in output image find colour value from input imagefor ycoord in range(0, h):for xcoord in range(0, w):corrx, corry = find_corresponding_pixel(xcoord, ycoord, w, h, n)outimg.putpixel((xcoord, ycoord), inimg.getpixel((corrx, corry)))# Print progress percentageprint(str(round((float(ycoord)/float(h))*100, 2)) + '%')outimg.save(outfile, 'PNG')if __name__ == '__main__':val_resize = 512# you need put your images in the file"./pics/" or you can define by yourself.get_six_pics_stitching(val_resize)convert_img('six_pics_stitching.jpg', 'output.jpg')# out_img = cv2.imread('output.jpg')# cv2.imshow('out_img', out_img)# cv2.waitKey()

拼接后的效果如图:

6目摄像头照片球面投影变换拼接全景图片相关推荐

  1. python opencv 摄像头标定_(五)单目摄像头标定与畸变矫正(C++,opencv)

    本文将梳理一种单目摄像头标定和矫正的方法,在梳理的过程中,首先使用网上离线的图片数据跑通流程,然后接入自己的camera,手动采集标定图像,实时矫正相机的畸变,然后输出矫正后的图像.全文基于Openc ...

  2. OpenCV实现单目摄像头对图像目标测距

    使用opencv对单目摄像头中的目标实现测量距离(python实现) 1.方法介绍: 根据相似三角形的方法: F = P×D / W , 其中W为物体的实际宽度,D为物体平面与相机平面的距离,照片上物 ...

  3. 单目摄像头标定与测距

    单目摄像头标定与测距 一. 标定 首先要对摄像头做标定,具体的公式推导在learning opencv中有详细的解释,这里顺带提一句,这本书虽然确实老,但有些理论.算法类的东西里面还是讲的很不错的,必 ...

  4. 单目摄像头检测6D姿态

    单目摄像头检测6D姿态 CVPR2019: ROI-10D: Monocular Lifting of 2D Detection to 6D Pose and Metric Shape 论文链接: h ...

  5. 引人遐想,用 Python 获取你想要的 “某个人” 摄像头照片

    仅用来学习,希望给你们有提供到学习上的作用. 1.安装库 需要安装python3.5以上版本,在官网下载即可.然后安装库opencv-python,安装方式为打开终端输入命令行. 2.更改收件人和发件 ...

  6. 基于单目摄像头的BEV实例预测(ICCV 2021)

    作者丨黄浴@知乎 来源丨https://zhuanlan.zhihu.com/p/422992592 编辑丨3D视觉工坊 ICCV'21论文 "FIERY: Future Instance ...

  7. Mobileye采用单目摄像头做ADAS太不精确

    Mobileye采用单目摄像头做ADAS太不精确?用双目摄像头的"中科慧眼"认为双目测距算法才是最精确的 Nicholas • 2016-01-05 18:53 ADAS(Adva ...

  8. 单目摄像头光学图像测距_摄像头与毫米波雷达(Radar)融合解析

    01 摄像头和雷达的融合是很多做ADAS当前所关注的关键问题之一.因为单纯摄像头和雷达都无法解决测距问题.不仅在测距,今后可能所应用到的高精度地图也都是需要使用摄像头和雷达的融合才能够实现.当前寻求到 ...

  9. OPENCV3.0 单目摄像头标定(使用官方自带的标定图片)

    OPENCV3.0 单目摄像头标定(使用官方自带的标定图片) 转载于:https://blog.csdn.net/zc850463390zc/article/details/48946855 // o ...

  10. 双目摄像头和单目摄像头_挑战激光雷达,MAXIEYE要重新定义单目摄像头?

    周圣砚认为:以往业内对单目摄像头的顾虑更像是一种偏见,并非不能逾越的鸿沟,而偏见正是用来打破的. 作者 | 安琪 自动驾驶的"唯激光雷达论"正在受到冲击. 前不久恩智浦的全球CTO ...

最新文章

  1. stm32的语音识别_免费开源基于STM32的智能垃圾桶之舵机控制(HAL库)
  2. 使用JSP的标准标签库JSTL处理XML格式的数据
  3. 微软开源P语言,实现安全的异步事件驱动编程
  4. 感知器 机器学习_机器学习感知器实现
  5. mysql报错级别_MySQL启动出现几个警告级别错误
  6. centos7安装MySQL 5.7
  7. php laravel 空白,php – Laravel View Make返回空白页面
  8. for循环,while循环,break跳出循环,continue结束本次循环,exit退出整个脚本
  9. vue-版的老虎机抽奖活动效果折腾小记
  10. kubernetes pod NodeLost 分析
  11. linux vim下自动补全,linux-python在vim下的自动补全功能
  12. 64位服务器IIS7.5运行ASP网站连接数据库错误解决方案
  13. 吴恩达深度学习教程中文笔记目录
  14. Java练习习题,百钱买百鸡问题,用100文钱买鸡,公鸡5文钱一只,母鸡3文钱一只,小鸡3只1文钱
  15. 力扣88 - 合并两个有序数组【归并排序思维】
  16. 阿里测试开发python面试题_[阿里面试]测试开发工程师面试
  17. Java SE,Java EE
  18. python爬取南京市房价_Python的scrapy之爬取链家网房价信息并保存到本地
  19. 细分市场或成为OA产品同质化的救星|企服三会系列报道
  20. windows2003中未分页内存泄漏导致服务器不稳定的解决方法

热门文章

  1. 前端通信实现l聊天室
  2. 牛顿迭代法求解开根号、泰勒展开式求解arctan及cordic的原理
  3. 山西省太原市、运城市、大同等市全面推行建筑工地劳务实名制
  4. 克隆虚拟机后开机出现device eth0 does not seem to be present delaying initializa错误
  5. JavaScript中的this指向以及bind()函数
  6. VIO与全局快门及轮速计的一些应用小技巧
  7. mysql全量备份命令_mysql全量备份与增量备份
  8. matlab中单对数函数,matlab对数函数-对数函数,MATLAB
  9. 餐厅经营模拟游戏实战项目
  10. Nacos 服务注册中心