利用OpenCV建立视差图像
点击上方“小白学视觉”,选择加"星标"或“置顶”
重磅干货,第一时间送达

推荐阅读

视差
让我们分解一下这个过程

深度图
让我们看看如何编写此工具的代码
import os, sysimport numpy as npimport pygame as pgimport cv2img = cv2.imread('moon.jpg', flags=cv2.CV_8UC4)depth_map = cv2.imread('moon_depth_map.png')depth_map = cv2.cvtColor(depth_map,cv2.COLOR_RGB2GRAY)img = cv2.resize(img, depth_map.shape[:2])layers = [] prev_thres = 255div=30 for thres in range(255 - div, 0, -div): ret, mask = cv2.threshold(depth_map, thres, 255, cv2.THRESH_BINARY) ret, prev_mask = cv2.threshold(depth_map, prev_thres, 255, cv2.THRESH_BINARY) prev_thres = thres inpaint_img = cv2.inpaint(img, prev_mask, 10, cv2.INPAINT_NS) layer = cv2.bitwise_and(inpaint_img, inpaint_img, mask = mask) layers.append(conv_cv_alpha(layer, mask)) # adding last layer mask = np.zeros(depth_map.shape, np.uint8) mask[:,:] = 255 ret, prev_mask = cv2.threshold(depth_map, prev_thres, 255, cv2.THRESH_BINARY) inpaint_img = cv2.inpaint(img, prev_mask, 10, cv2.INPAINT_NS) layer = cv2.bitwise_and(inpaint_img, inpaint_img, mask = mask)layers.append(conv_cv_alpha(layer, mask)) layers = layers[::-1]def conv_cv_alpha(cv_image, mask): b, g, r = cv2.split(cv_image) rgba = [r, g, b, mask] cv_image = cv2.merge(rgba,4) return cv_imageface_cascade = cv2.CascadeClassifier( 'haarcascade_frontalface_default.xml')
def get_face_rect(img): gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) face_rects = face_cascade.detectMultiScale(gray_img, 1.3, 5) if len(face_rects) == 0: return () return face_rects[0]scale = 1off_set = 20width, height = layers[0].get_width(), layers[0].get_height() win = pg.display.set_mode((int((width - off_set)*scale), int((height - off_set)*scale))) pg.display.set_caption('Parallax_image')scaled_layers = [] for layer in layers: scaled_layers.append(pg.transform.scale(layer, (int(width*scale), int(height*scale))))cap = cv2.VideoCapture(0, cv2.CAP_DSHOW)x_transform = True # allow shift in x-axisy_transform = False # allow shift in y-axissens = 50 # the amount of scale down of shift valueshow_cam = False # show your face camshift_x = 0 shift_y = 0 run = Truewhile run: for event in pg.event.get(): if event.type==pg.QUIT: run = False ret, frame = cap.read() frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) initial_pos = (frame.shape[0]/2, frame.shape[1]/2) face_rect = get_face_rect(frame) if len(face_rect) != 0: x,y,w,h, = face_rect face_rect_frame = cv2.rectangle(frame, (x, y), (x + w, y + h), (255,255,0), 3) shift_x = (initial_pos[0] - (x + w/2))/(sens*scale) shift_y = (initial_pos[1] - (y + h/2))/(sens*scale) win.fill((255, 255, 255)) for i, layer in enumerate(scaled_layers): new_x = -off_set/2 new_y = -off_set/2 if x_transform: new_x = 0 + shift_x*i if y_transform: new_y = 0 + shift_y*i win.blit(layer, (new_x, new_y)) face_rect_frame = cv2.resize(face_rect_frame, (100, 100)) if show_cam: win.blit(conv_cv_pygame(face_rect_frame), (0, 0)) pg.display.update()cap.release()cv2.destroyAllWindows()pg.quit()
最终结果

不同图像的演示
赞 (0)
