1、原理
可以推出不同物体的高度比可以通过如下公式得到:
是左边指定物体的高度(图片中尺寸)
是右边指定物体的高度(图片中尺寸)
是左边指定物体的平均视差值
是右边指定物体的平均视差值
2、代码逻辑框图
3、代码详解
import multiprocessing as mp
import numpy as np
import cv2
import time#读取图像
img5_L = cv2.imread('relative_height/3_a.jpg')
img5_R = cv2.imread('relative_height/3_b.jpg')
img_L=img5_L
img_R=img5_R#功能:获取视差图
#输入:两张图片
#输出:视差图
def disparity(img_L,img_R):stereo = cv2.StereoSGBM_create(numDisparities=48,blockSize=3)disparity = stereo.compute(img_L, img_R)return disparityrect_L = (310,36,521,759) #起点x,y, 增加至为w,h
rect_R = (835,70,590,941)#功能:截图
#输入:图片,截图范围
#输出:截图
def segment(img,rect):mask = np.zeros(img.shape[:2],np.uint8)bgdModel = np.zeros((1,65),np.float64)fgdModel = np.zeros((1,65),np.float64)cv2.grabCut(img,mask,rect,bgdModel,fgdModel,20,cv2.GC_INIT_WITH_RECT)mask2 = np.where((mask==2)|(mask==0),0,1).astype('uint8')img = img*mask2[:,:,np.newaxis]return img#功能:构建高斯核sieze和sigma
#输入:size,sigma
#输出:二维数组
def gaussian_kernel_2d(kernel_size, sigma):kx = cv2.getGaussianKernel(kernel_size, sigma)ky = cv2.getGaussianKernel(kernel_size, sigma)return np.dot(kx, np.transpose(ky))#功能:彩色图转成灰色图
#输入:彩色图img
#输出:灰色图gray_img
def color_to_gray(img):height = img.shape[0]width = img.shape[1]channels = img.shape[2]value = [0] * 3gray_img = np.zeros([height, width], np.uint8)for row in range(height):for column in range(width):for chan in range(channels):value[chan] = img[row, column, chan]R = value[2]G = value[1]B = value[0]# new_value = 0.2989 * R + 0.5870 * G + 0.1140 * Bnew_value = 0.2989 * R + 0.5870 * G + 0.1140 * B # 转为灰度像素gray_img[row, column] = new_valuereturn gray_img#功能:获取截图的高度(像素之差)
#输入:img
#输出:高度
def object_height(img):point1=(0,0)point2 = (0, 0)height1=0height2=0for row in range(len(img)):for col in range(len(img[row])):if img[row][col] != 0:height1=rowpoint1=(row,col)breakfor row in range(len(img)-1,-1,-1):for col in range(len(img[row])):if img[row][col] != 0:height2=rowpoint2 = (row, col)breakheight=abs(height2-height1)return point1,point2,height# 功能:求平均视差值
# 输入:抠图img,视差图disparity
# 输出:平均视差值
def average_disparity_value(img,disparity):n=0value=0for row in range(len(img)):for col in range(len(img[row])):if img[row][col] != 0:n +=1value+=disparity[row][col]return value/n#功能:估计图片不同物体的实际高度比
#输入:图片img_L和img_R
#输出:图片中不同物体的实际高度比
def height_ratio(img_L,img_R):t_start = time.clock()print('compute disparity....')disparity1=disparity(img_L,img_R)disparity1 = cv2.filter2D(disparity1, -1, gaussian_kernel_2d(16, 60))print('object segment...')pool = mp.Pool(processes=mp.cpu_count() - 1) #并行处理设置res1=pool.apply_async(segment,(img_L, rect_L))#并行处理seg_L=res1.get()res2=pool.apply_async(segment, (img_R, rect_R))#并行处理seg_R = res2.get()print('color_to_gray...')seg_L=color_to_gray(seg_L)seg_R = color_to_gray(seg_R)print('computer object height...')point1_L,point2_L,height_L=object_height(seg_L)point1_R, point2_R, height_R = object_height(seg_R)d_L=average_disparity_value(seg_L, disparity1)d_R = average_disparity_value(seg_R, disparity1)t_end = time.clock()print('数据处理用时', t_end - t_start)ratio= height_L*d_R/(height_R*d_L)return ratioif __name__ == '__main__':hei_ratio5=height_ratio(img_L,img_R)print(hei_ratio5)