双目视觉07_立体匹配Census算法
发布时间
阅读量:
阅读量
以下实验为左图

以下为实验右图

实验代码
import os
import time
import cv2 as cv
import numpy as np
import matplotlib.pyplot as plt
from numba import jit
# lres = census(limg[:, maxDisparity:w])
# maxDisparity = 12 # 最大视差,也就是搜索范围,搜索范围越大耗时越长
# window_size = 5 # 滑动窗口大小,窗口越大匹配成功难度越高
# same = np.int64(window_size / 2) # 窗口大小的一半向下取整,比如用来衡量窗口左上角与窗口中点的距离的x与y坐标
@jit
def census(img):
size = np.shape(img)[0:2]
h = size[0]
w = size[1]
census = np.zeros((h - 2 * same, w - 2 * same), dtype=np.int64)
# census就是经过变换后的图像
cp = img[same:h - same, same:w - same]
# cp是变换中心点的坐标,0,0点作为左上角的窗口中,中心点就是(same,same)
offsets = [(u, v) for v in range(window_size) for u in range(window_size) if not u == same == v]
# offset是分别衡量某个点作为窗口左上角是否匹配成功,作为窗口左上角向左平移一个匹配是否成功,一直到作为窗口右下角匹配是否成功,都有衡量
for u, v in offsets:
census = (census << 1) | (img[v:v + h - 2 * same,
u:u + w - 2 * same] >= cp) # 这里是最为难懂的,请参考一下[我的另一篇博文](),如果还有不懂请私信我或给我留言
return census
def hanming(limg, rimg, dispmap, Diffmap):
size = np.shape(limg)[0:2]
h = size[0]
w = size[1]
lres = census(limg[:, maxDisparity:w])
rres = census(rimg[:, 0:w - maxDisparity])
for x in range(0, np.shape(lres)[0]):
for y in range(maxDisparity, np.shape(lres)[1]):
for i in range(0, maxDisparity):
dispmap[x, y, i] = '{:025b}'.format(((rres[x, y - i]) ^ (lres[x, y]))).count(
'0') # 从当前位置开始,在右图中寻找和左图最接近的二进制串,从而完成匹配
for x in range(0, np.shape(lres)[0]):
for y in range(0, np.shape(lres)[1]):
val = np.sort(dispmap[x, y, :]) # 排名
val_id = np.argsort(dispmap[x, y, :]) # 找出最接近的
Diffmap[x, y] = dispmap[x, y, :].argsort()[-1]
# os.chdir(r'')
limg = np.asanyarray(cv.imread('pic/left/left.png', cv.IMREAD_GRAYSCALE), dtype=np.int64)
rimg = np.asanyarray(cv.imread('pic/right/right.png', cv.IMREAD_GRAYSCALE), dtype=np.int64)
# img_size=np.shape(limg)
size = np.shape(limg)[0:2]
h = size[0]
w = size[1]
maxDisparity = 12 # 最大视差,也就是搜索范围,搜索范围越大耗时越长
window_size = 5 # 滑动窗口大小,窗口越大匹配成功难度越高
same = np.int64(window_size / 2) # 窗口大小的一半向下取整,比如用来衡量窗口左上角与窗口中点的距离的x与y坐标
lres = census(limg[:, maxDisparity:w])
rres = np.zeros((np.shape(lres)[0], np.shape(lres)[1], maxDisparity), dtype=np.int64)
dispmap = np.zeros((np.shape(lres)[0], np.shape(lres)[1], maxDisparity), dtype=np.int64)
Diffmap = np.zeros((np.shape(lres)[0], np.shape(lres)[1]), dtype=np.int64)
tic1 = time.time()
hanming(limg, rimg, dispmap, Diffmap)
plt.imshow(Diffmap[:, maxDisparity:])
plt.show()
print('用时:', time.time() - tic1)
实验结果:

视差图

全部评论 (0)
还没有任何评论哟~
