# -*- coding: utf-8 -*- from common import get_data_set, show_image, save_image import numpy as np data_set = get_data_set(0) # get data left = data_set['left'] right = data_set['right'] result = data_set['result'] my_result = np.zeros(left.shape) def calculate_diff_naive(pixel_value, row_right, pixel_pos, d_max=10): """ :param pixel_value:左视图像素点 :param row_right:右视图的这一行 显而易见 右视图中的点在左视图左侧 :param pixel_pos:左视图像素点位置 :param d_max:最大深度 :return:视差值 """ start_pos = (pixel_pos - d_max) if (pixel_pos - d_max) > 0 else 0 row_right = row_right[start_pos:pixel_pos] diff = map(lambda value: abs(value - pixel_value), row_right) diff = diff[::-1] # 逆序 data_min = 0 for depth in range(len(diff)): if diff[data_min] == 0: break if diff[depth] < diff[data_min]:
# 其他区域赋值 for row_pos in np.arange(color_window.shape[0]): # 记录行差异 diff_row = np.absolute(row_pos - row_start) for column_pos in np.arange(color_window.shape[1]): if result_window[row_pos][column_pos] != 0: continue diff_column = np.absolute(column_pos - column_start) result_window[row_pos][column_pos] = (window_size - 1) / ( diff_column + diff_row) return result_window if __name__ == '__main__': data_set = get_data_set(9) # get data left = data_set['left'] right = data_set['right'] result = data_set['result'] import time window_size = 19 d_max = 64 tt = time.time() stereo = StereoVisionBM2(left, right, window_size, d_max) (low_texture_row, low_texture_column) = stereo.low_texture_detection() data_set['low_texture_row'] = low_texture_row data_set['low_texture_column'] = low_texture_column
self.right_low_cost_extend = self.make_border(self.low_texture_cost_lr, self.d_max) for d in range(self.d_max): # print 'compute_cost_d:', d self.left_diff[d] = self.gaussian_filter(self.compute_cost_d(d)) return self.left_diff.copy() def compute_cost_d(self, d): return compute_cost_d_cpp(self.compute_cost_d_cpp_func, self.low_texture_cost_ll, self.right_low_cost_extend[:, self.d_max - d:self.column_length + self.d_max - d].copy())+StereoVisionBM2.compute_cost_d(self,d) #return StereoVisionBM2.compute_cost_d(self,d) if __name__ == '__main__': data_set = get_data_set(8) # get data left = data_set['left'] right = data_set['right'] result = data_set['result'] import time window_size = 21 d_max = 64 tt = time.time() stereo = StereoVisionLowTexture_BT(left, right, window_size, d_max) low_texture = stereo.low_texture_detection(0.2)[0] (left_c, right_c) = stereo.get_low_cost_l() stereo.compute_cost()
for d in np.arange(1, self.d_max): if self.sad_result[min_sad][row][column] > self.sad_result[d][row][column]: min_sad = d self.my_result[row][column] = min_sad return self.my_result @staticmethod def default_window_compare_method(window_1, window_2): return np.sum(np.absolute(window_1 - window_2)) def set_window_compare_method(self, method): self.used_window_compare_method = method if __name__ == '__main__': data_set = get_data_set(0) # get data left = data_set['left'] right = data_set['right'] result = data_set['result'] import time window_size = 5 d_max = 15 tt = time.time() stereo = StereoVisionBM1(left, right, window_size, d_max) ''' stereo.get_sad_all() my_result = stereo.get_result() my_result = my_result * 255 / d_max data_set['my_result_6'] = my_result
if self.sad_result[min_sad][row][column] > self.sad_result[ d][row][column]: min_sad = d self.my_result[row][column] = min_sad return self.my_result @staticmethod def default_window_compare_method(window_1, window_2): return np.sum(np.absolute(window_1 - window_2)) def set_window_compare_method(self, method): self.used_window_compare_method = method if __name__ == '__main__': data_set = get_data_set(0) # get data left = data_set['left'] right = data_set['right'] result = data_set['result'] import time window_size = 5 d_max = 15 tt = time.time() stereo = StereoVisionBM1(left, right, window_size, d_max) ''' stereo.get_sad_all() my_result = stereo.get_result() my_result = my_result * 255 / d_max data_set['my_result_6'] = my_result
from common import get_data_set, show_image, save_image, get_compute_cost_bt_d_cpp_func from stereo_test_dp import StereoVisionBM_DP from stereo_test_bm import StereoVisionBM2 import numpy as np class StereoVisionBM_BT(StereoVisionBM2): def __init__(self, left, right, window_size=13, d_max=10, is_color=False): StereoVisionBM2.__init__(self, left, right, window_size, d_max, is_color) self.compute_cost_d_cpp_func = get_compute_cost_bt_d_cpp_func(self.dll) if __name__ == '__main__': data_set = get_data_set(1) # get data left = data_set['left'] right = data_set['right'] result = data_set['result'] import time window_size = 11 d_max = 20 tt = time.time() stereo = StereoVisionBM_BT(left, right, window_size, d_max) stereo.low_texture_detection() stereo.compute_cost() stereo.aggregate_cost() my_result = stereo.get_result() my_result = my_result * (255.0 / d_max / 16)
break # 其他区域赋值 for row_pos in np.arange(color_window.shape[0]): # 记录行差异 diff_row = np.absolute(row_pos - row_start) for column_pos in np.arange(color_window.shape[1]): if result_window[row_pos][column_pos] != 0: continue diff_column = np.absolute(column_pos - column_start) result_window[row_pos][column_pos] = (window_size - 1) / (diff_column + diff_row) return result_window if __name__ == '__main__': data_set = get_data_set(9) # get data left = data_set['left'] right = data_set['right'] result = data_set['result'] import time window_size = 19 d_max = 64 tt = time.time() stereo = StereoVisionBM2(left, right, window_size, d_max) (low_texture_row, low_texture_column) = stereo.low_texture_detection() data_set['low_texture_row'] = low_texture_row data_set['low_texture_column'] = low_texture_column