使用深度摄像头的数据来识别前景区域和背景区域,首先要有一个深度摄像头,比如微软的Kinect,英特尔的realsense。
RealSense不支持在OSX上运行
https://communities.intel.com/thread/109986
mac上配置解决方案
https://github.com/IntelRealSense/librealsense/blob/master/doc/installation_osx.md
深度摄像头RealSense 的SDK
GitHub地址:https://github.com/IntelRealSense/librealsense(建议Window或Ubuntu)
使用普通摄像头进行深度估算
深度摄像头是极少在捕获图像时能估计物体与摄像头之间距离的设备,深度摄像头是如何得到深度信息的呢? 深度摄像头(比如微软的Kinect)将传统摄像头和一个红外传感器相结合来帮助摄像头区别相似物体并计算他们与摄像头之间的距离。
如何用realsensesdk,如何用Kinect
普通摄像头完成物体到摄像头之间的距离,极几何。极几何是如何工作的呢?它跟踪从摄像头到图像上每个物体的虚线,然后在第二张图片做同样的操作,并根据同一个物体对应的线交叉来计算距离。
OpenCV如何使用极几何来计算所谓的视差图?
视差图计算StereoSGBM
使用GrabCut进行前景检测
计算视差图对检测图像的前景很有用,(OpenCV)StereoSGBM主要是从二维图片中得到三维信息。
GrabCut算法的实现步骤为:
import numpy as np
import cv2
from matplotlib import pyplot as plt
#使用分水岭和GrabCut算法进行物体分割
img = cv2.imread('images/statue_small.jpg')
mask = np.zeros(img.shape[:2],np.uint8)
bgdModel = np.zeros((1,65),np.float64)
fgdModel = np.zeros((1,65),np.float64)
rect = (100,1,421,378)
cv2.grabCut(img,mask,rect,bgdModel,fgdModel,5,cv2.GC_INIT_WITH_RECT)
mask2 = np.where((mask==2)|(mask==0),0,1).astype('uint8')
img = img*mask2[:,:,np.newaxis]
plt.subplot(121), plt.imshow(img)
plt.title("grabcut"), plt.xticks([]), plt.yticks([])
plt.subplot(122), plt.imshow(cv2.cvtColor(cv2.imread('images/statue_small.jpg'), cv2.COLOR_BGR2RGB))
plt.title("original"), plt.xticks([]), plt.yticks([])
plt.show()
使用分水岭算法进行图像分割
#-*- coding=utf-8 -*-
import numpy as np
import cv2
from matplotlib import pyplot as plt
#使用分水岭算法进行图像分割
img = cv2.imread('images/basil.jpg')
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
#将颜色转为灰度后,可为图像设一个阈值,将图像分为两部分:黑色部分和白色部分
ret, thresh = cv2.threshold(gray,0,255,cv2.THRESH_BINARY_INV+cv2.THRESH_OTSU)
# noise removal 噪声去除,morphologyEx是一种对图像进行膨胀之后再进行腐蚀的操作
kernel = np.ones((3,3),np.uint8)
opening = cv2.morphologyEx(thresh,cv2.MORPH_OPEN,kernel, iterations = 2)
# sure background area 确定背景区域,图像进行膨胀操作
sure_bg = cv2.dilate(opening,kernel,iterations=3)
# Finding sure foreground area,通过distanceTransform来获取确定的前景区域
dist_transform = cv2.distanceTransform(opening,cv2.DIST_L2,5)
ret, sure_fg = cv2.threshold(dist_transform,0.7*dist_transform.max(),255,0)
# Finding unknown region
sure_fg = np.uint8(sure_fg)
unknown = cv2.subtract(sure_bg,sure_fg)
# Marker labelling
ret, markers = cv2.connectedComponents(sure_fg)
# Add one to all labels so that sure background is not 0, but 1
markers = markers+1
# Now, mark the region of unknown with zero
markers[unknown==255] = 0
markers = cv2.watershed(img,markers)
img[markers == -1] = [255,0,0]
plt.imshow(img)
plt.show()