立体匹配算法(Stereo correspondence)

news2024/10/6 12:21:05

SGM(Semi-Global Matching)原理:

SGM的原理在wiki百科和matlab官网上有比较详细的解释:
wiki matlab
如果想完全了解原理还是建议看原论文 paper(我就不看了,懒癌犯了。)
优质论文解读和代码实现
一位大神自己用c++实现的SGM算法github
先介绍两个重要的参数:
注:这一部分参考的是matlab的解释,后面的部分是参考的opencv的实现,细节可能有些出入,大体上是一致的。
Disparity Levels and Number of Directions

Disparity Levels

Disparity Levels: Disparity levels is a parameter used to define the search space for matching. As shown in figure below, the algorithm searches for each pixel in the Left Image from among D pixels in the Right Image. The D values generated are D disparity levels for a pixel in Left Image. The first D columns of Left Image are unused because the corresponding pixels in Right Image are not available for comparison. In the figure, w represents the width of the image and h is the height of the image. For a given image resolution, increasing the disparity level reduces the minimum distance to detect depth. Increasing the disparity level also increases the computation load of the algorithm. At a given disparity level, increasing the image resolution increases the minimum distance to detect depth. Increasing the image resolution also increases the accuracy of depth estimation. The number of disparity levels are proportional to the input image resolution for detection of objects at the same depth. This example supports disparity levels from 8 to 128 (both values inclusive). The explanation of the algorithm refers to 64 disparity levels. The models provided in this example can accept input images of any resolution.——matlab

字太多,看不懂,让gpt解释了一下:

# gpt生成,仅供本人理解SSD原理
import numpy as np

def compute_disparity(left_img, right_img, block_size=5, num_disparities=64):
    # 图像尺寸
    height, width = left_img.shape

    # 初始化视差图
    disparity_map = np.zeros_like(left_img)

    # 遍历每个像素
    for y in range(height):
        for x in range(width):
            # 定义搜索范围
            min_x = max(0, x - num_disparities // 2)
            max_x = min(width, x + num_disparities // 2)

            # 提取左图像块
            left_block = left_img[y:y+block_size, x:x+block_size]

            # 初始化最小 SSD 和对应的视差
            min_ssd = float('inf')
            best_disparity = 0

            # 在搜索范围内寻找最佳视差
            for d in range(min_x, max_x):
                # 提取右图像块
                right_block = right_img[y:y+block_size, d:d+block_size]

                # 计算 SSD
                ssd = np.sum((left_block - right_block)**2)

                # 更新最小 SSD 和对应的视差
                if ssd < min_ssd:
                    min_ssd = ssd
                    best_disparity = abs(x - d)

            # 将最佳视差保存到视差图中
            disparity_map[y, x] = best_disparity

    return disparity_map

# 示例用法
left_img = np.random.randint(0, 255, size=(100, 100), dtype=np.uint8)
right_img = np.roll(left_img, shift=5, axis=1)  # 创建右图,右移了5个像素

disparity_map = compute_disparity(left_img, right_img, block_size=5, num_disparities=64)

# 可视化结果(这里简化为将视差图缩放以便可视化)
import matplotlib.pyplot as plt
plt.imshow(disparity_map, cmap='gray')
plt.title('Disparity Map')
plt.show()

这样就明白了,Disparity Levels就是计算视差的范围(视差搜索范围)。

Number of Directions

Number of Directions:

Number of Directions: In the SGBM algorithm, to optimize the cost function, the input image is considered from multiple directions. In general, accuracy of disparity result improves with increase in number of directions. This example analyzes five directions: left-to-right (A1), top-left-to-bottom-right (A2), top-to-bottom (A3), top-right-to-bottom-left (A4), and right-to-left (A5).
在这里插入图片描述

按照单一路径匹配像素不够稳健,按照图像进行二维最优的全局匹配时间复杂度太高(NP完全问题),所以SGM的作者使用一维路径聚合的方式来近似二维最优。
在这里插入图片描述
pic 参考

SAD和SSD

用SAD 或者 SSD计算图像相似度,来做匹配。
公式:
> 这里是引用
公式和代码虽然是gpt生成的,但是公式看起来没错,代码可以帮助理解,仅供参考。
代码里面的 num_disparities 就是 Disparity Levels

SGBM in opencv

本人用opencv较多,这里仅关注代码在opencv的实现。

opencv StereoSGBM_create示例:

# gpt生成,仅作为参考,具体请查看opencv官方文档https://docs.opencv.org/4.x/d2/d85/classcv_1_1StereoSGBM.html
import cv2
import numpy as np

# 读取左右视图
left_image = cv2.imread('left_image.png', cv2.IMREAD_GRAYSCALE)
right_image = cv2.imread('right_image.png', cv2.IMREAD_GRAYSCALE)

# 创建SGBM对象
sgbm = cv2.StereoSGBM_create(
    minDisparity=0,
    numDisparities=16,  # 视差范围,一般为16的整数倍
    blockSize=5,        # 匹配块的大小,一般为奇数
    P1=8 * 3 * 5 ** 2,   # SGBM算法参数
    P2=32 * 3 * 5 ** 2,  # SGBM算法参数
    disp12MaxDiff=1,    # 左右视差图的最大差异
    uniquenessRatio=10,  # 匹配唯一性百分比
    speckleWindowSize=100,  # 过滤小连通区域的窗口大小
    speckleRange=32      # 连通区域内的差异阈值
)

# 计算视差图
disparity_map = sgbm.compute(left_image, right_image)

# 将视差图进行归一化处理
disparity_map = cv2.normalize(disparity_map, None, 0, 255, cv2.NORM_MINMAX)

# 显示左图、右图和视差图
cv2.imshow('Left Image', left_image)
cv2.imshow('Right Image', right_image)
cv2.imshow('Disparity Map', disparity_map.astype(np.uint8))

cv2.waitKey(0)
cv2.destroyAllWindows()

Difference between SGBM and SGM

what is the difference between opencv sgbm and sgm
opencv官方的解释:
The class implements the modified H. Hirschmuller algorithm [82] that differs from the original one as follows:

  1. By default, the algorithm is single-pass, which means that you consider only 5 directions instead of 8. Set mode=StereoSGBM::MODE_HH in createStereoSGBM to run the full variant of the algorithm but beware that it may consume a lot of memory.
  2. The algorithm matches blocks, not individual pixels. Though, setting blockSize=1 reduces the blocks to single pixels.
  3. Mutual information cost function is not implemented. Instead, a simpler Birchfield-Tomasi sub-pixel metric from [15] is used. Though, the color images are supported as well.
    Some pre- and post- processing steps from K. Konolige algorithm StereoBM are included, for example: pre-filtering (StereoBM::PREFILTER_XSOBEL type) and post-filtering (uniqueness check, quadratic interpolation and speckle filtering).

大概的意思就是,与SGM不同之处在于,SGBM算法匹配的时候最小单位是blocks,而不是像素,不过设置blockSize=1的时候,就变成SGM了。没有实现互信息,而是用了更简单的Birchfield-Tomasi sub-pixel metric。除此之外还有一些预处理和后处理操作。
在这里插入图片描述
大概是这样,不知道对不对。

深度的立体匹配算法

先开个坑

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1352267.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

系列九、Feign

一、Feign 1.1、Java中如何实现跨接口调用 &#xff08;1&#xff09; Httpclient Httpclient是Apache Jakarta Comon下的子项目&#xff0c;用来提供高效的、最新的、功能丰富的支持HTTP协议的客户端编程工具包&#xff0c;并且它支持HTTP协议的最新版本和建议。HttpC…

鸿蒙原生应用再添新丁!中国移动 入局鸿蒙

鸿蒙原生应用再添新丁&#xff01;中国移动 入局鸿蒙 来自 HarmonyOS 微博1月2日消息&#xff0c;#中国移动APP启动鸿蒙原生应用开发#&#xff0c;拥有超3亿用户的中国移动APP宣布&#xff0c;正式基于HarmonyOS NEXT启动#鸿蒙原生应用#及元服务开发。#HarmonyOS#系统的分布式…

openGauss学习笔记-183 openGauss 数据库运维-升级-升级操作

文章目录 openGauss学习笔记-183 openGauss 数据库运维-升级-升级操作183.1 就地升级和灰度升级操作步骤 openGauss学习笔记-183 openGauss 数据库运维-升级-升级操作 介绍就地升级、灰度升级和滚动升级的详细操作。 183.1 就地升级和灰度升级操作步骤 以root身份登录节点。 …

突发!博世「裁员」

对于未来几年的汽车行业需求变化&#xff0c;一级零部件供应商正在加快「降本增效」举措&#xff0c;犹如下游客户更加倾向于降本&#xff0c;而不是无休止的提升整车性能&#xff0c;比如&#xff0c;续航里程、智能化。 本周&#xff0c;全球汽车零部件龙头供应商博世宣布&am…

每日一题 2487. 从链表中移除节点(中等,回溯)

显然只要从后往前遍历链表&#xff0c;设 t 为当前的最大值&#xff0c;只要在遍历过程中比 t 小的节点都删除&#xff0c;大于等于 t 的则更新 t 为新的节点 通过递归回溯的方法可以很简单地实现从后往前遍历链表 # Definition for singly-linked list. # class ListNode: # …

SpringBoot实现Websocket聊天交友微信小程序(一)

记录一下我开发一个交友微信小程序并且上线运营的心得体会。 2022年10月1日上线的&#xff0c;到目前终于实现每天收益300左右。 界面比较简洁&#xff0c;功能有动态&#xff0c;动态可以选择话题&#xff0c;相册&#xff0c;相册可以设置看广告解锁&#xff0c;私信&#…

CCF模拟题 202312-2 因子化简

CCF模拟题 202312-2 因子化简 问题描述 试题编号&#xff1a; 202312-2 试题名称&#xff1a; 因子化简 时间限制&#xff1a; 2.0s 内存限制&#xff1a; 512.0MB 题目背景 质数&#xff08;又称“素数”&#xff09;是指在大于 1 的自然数中&#xff0c;除了 1 和它本身以外…

Nginx多ip部署多站点

目录 1.修改网卡配置信息 2.修改主要配置文件nginx.conf 1.修改网卡配置信息 1)来到网卡配置文件存放目录下 cd /etc/sysconfig/network-scripts/ 2)对 ifcfg-ens33 文件进行配置修改前先进行备份 cp ifcfg-ens33 ifcfg-ens33.default 3)先修改成最小配置&#xff0c;使用 d…

阿里云服务器Alibaba Cloud Linux镜像系统详细介绍

Alibaba Cloud Linux是基于龙蜥社区OpenAnolis龙蜥操作系统Anolis OS的阿里云发行版&#xff0c;针对阿里云服务器ECS做了大量深度优化&#xff0c;Alibaba Cloud Linux由阿里云官方免费提供长期支持和维护LTS&#xff0c;Alibaba Cloud Linux完全兼容CentOS/RHEL生态和操作方式…

网络安全有哪些细分方向?零基础学Web安全需要掌握的知识(附系统路线+工具笔记)

前言 “没有网络安全就没有国家安全”。当前&#xff0c;网络安全已被提升到国家战略的高度&#xff0c;成为影响国家安全、社会稳定至关重要的因素之一。 一、网络安全行业特点 行业发展空间大&#xff0c;岗位非常多 网络安全行业产业以来&#xff0c;随即新增加了几十个…

0基础学java-day23(反射)

一、反射机制 1、一个需求引出反射 package com.hspedu.reflection.question;import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; …

努力打工的你存到钱了?2024新蓝海创业项目/适合普通人创业项目

为什么有钱人那么有钱&#xff1f;是他们够努力吗&#xff1f;有一位网友回答是这样回答的&#xff1a; “从小到大我所接触到的一切成功&#xff0c;他的基础都是努力&#xff0c;甚至于奉承时的吃得苦上苦方为人上人。但是那天我的三观出现了认知错误&#xff0c;光靠努力赚…

python c语言 代码动态检查,python c语言语法分析

大家好&#xff0c;小编来为大家解答以下问题&#xff0c;python c语言 代码动态检查&#xff0c;python c语言语法分析&#xff0c;今天让我们一起来看看吧&#xff01; Source code download: 本文相关源码 初学编程&#xff0c;应该学习哪一门编程语言&#xff0c;有不少人感…

02--数据定义语言DDL

1、数据定义语言DDL 1.1 操作数据库-DDL 创建数据库 create database 数据库名称; 创建数据库&#xff0c;并指定字符集 create database 数据库名称 character set 字符集名; 查询所有数据库的名称 show databases; 查询某个数据库的字符集:查询某个数据库的创建语句及字…

二、Redis的特性与应用场景

Redis是一个在内存中存储数据的中间件&#xff0c;主要用于作为数据库、数据缓存&#xff0c;在分布式系统中有着非常重要的地位。面试中可以围绕Redis的特性进行介绍。 一、Redis特性 1、在内存中存储数据 MySQL主要是“表”的方式来存储组织数据的&#xff0c;是“关系型数…

聚醚胺市场分析:预计到2025年将达到10亿美元

聚醚胺是一种有机化合物&#xff0c;在涂料、胶粘剂、树脂等多种行业中用作固化剂、缓蚀剂和燃料添加剂。由于对广泛用于建筑和汽车行业的聚脲涂料的需求不断增加&#xff0c;全球聚醚胺市场一直在经历显着增长。 全球市场分析&#xff1a; 2020 年全球聚醚胺市场价值为 6.2 亿…

【竞技宝】LOL:S14新赛季改动 将trueskill2隐藏分算法

北京时间2024年1月3日,随着英雄联盟德玛西亚杯的进行,英雄联盟赛事已经进入新赛季的征途。每个赛季的春季赛之前,都会进行一次大的版本更新。据爆料,今年的S14版本大更新中,除了游戏内的英雄、道具、地图的更新之外,排位的隐藏分算法也将进行重大改变。 昨日,英雄联盟设计总监…

c++ 静态联编+动态联编 (多态)

静态多态 动态多态 1&#xff09;静态多态和动态多态的区别就是函数地址是早绑定(静态联编)还是晚绑定(动态联编)。 如果函数的调用&#xff0c;在编译阶段就可以确定函数的调用地址&#xff0c;并产生代码&#xff0c;就是静态多态(编译时多态)&#xff0c;就是说地址是早绑定…

Android Studio 报错AAPT: error: resource android:attr/lStar not found.解决方法!

目录 前言 一、报错信息 二、解决方法 三、常见处理方法总结 四、更多资源 前言 在快速发展的科技领域中&#xff0c;移动应用开发已经成为了一个非常热门的领域。而作为开发Android应用的主要工具之一&#xff0c;Android Studio 提供了丰富的功能和工具来帮助开发者构建…

c语言和python区别哪个难,c语言和python区别大不大

大家好&#xff0c;给大家分享一下c语言和python区别主要用来写什么&#xff0c;很多人还不知道这一点。下面详细解释一下。现在让我们来看看&#xff01; Python可以说是目前最火的语言之一了&#xff0c;人工智能的兴起让Python一夜之间变得家喻户晓&#xff0c;Python号称目…