OPENCV Computer Vision Learning notes Three

Source: Internet
Author: User

Fourth chapter depth Estimation and segmentation

1 Capture Depth camera frame

Depth graph grayscale Each pixel is a camera to the surface of the object from mm

Point cloud color Each color corresponds to one dimension space m

Disparity Map gray-scale each pixel represents the stereo parallax of the surface of the object near large and far small

Effective depth mask The depth information for a given pixel is valid

2 Mask from disparity map

#!/usr/bin/env python #-*-coding:utf-8-*-# @Time: 2016/12/1 10:16 # @Author: RETACN # @Site: Processing of depth camera data # @ File:depth.py # @Software: Pycharm __author__ = "RETACN" __copyright__ = "property of mankind." __license__ = "CN" __ version__ = "0.0.1" __maintainer__ = "RETACN" __email__ = "zhenhuayue@sina.com" __status__ = "Development"ImportNumPy asNP #设备 cap_openni=900 cap_openni_asus=910 #通道 (distance based on floating-point numbers) Cap_openni_depth_map=0 #会得到bgr图像 cap_openni_point_cloud_map=1 #XYZ cap_openni_disparity_map=2 # cap_openni_disparity_map_32f=3 cap_openni_valid_depth_mask=4 CAP_OPENNI_BGR_IMAGE =5 cap_openni_gray_image=6 #生成掩模def Createmedianmask(Disparitymap, #视差图 validdepthmask, #有效深度掩模 rect=None): #矩形ifRectAre not None: X,y,w,h=rect Disparitymap=disparitymap[y:y+h,x:x+w] validdepthmask=validdepthmask[y:y+h,x:x+w] #得到中值 Median=np.median (Disparitymap) #生成掩模, Boolean operations by Pixel returnNp.where ((validdepthmask==0) |
                    (ABS (Disparitymap-median) <12), #值为真假的数组, when effective parallax and mean Parallax >=12, as Noise 1.0, #为真时, the corresponding element of the array is the value 0.0) #为假时, for this value

3 Mask the copy operation

#!/usr/bin/env python #-*-coding:utf-8-*-# @Time: 2016/12/1 10:22 # @Author: RETACN # @Site: Rectangular area Copy # @File : rects.py # @Software: Pycharm __author__ = "RETACN" __copyright__ = "property of mankind." __license__ = "CN" __vers ion__ = "0.0.1" __maintainer__ = "RETACN" __email__ = "zhenhuayue@sina.com" __status__ = "Development"ImportCv2ImportNumPy asNp fromThreeImportUtils #对复制操作执行掩模def Copyrect(SRC, DST, srcrect, Dstrect, mask=None, #掩模参数, the mask wants to have the same channel number INTERPOLATION=CV2 as the image. Inter_linear): #插值方法为线性插值 x0,y0,w0,h0=srcrect x1,y1,w1,h1=dstrect #如果掩模为空, perform the copy operationifMaskis None: Dst[y1:y1+h1,x1:x1+w1]=cv2.resize (Src[y0:y0+h0,y0:y0+h0], #源图像 (W1,H1), #目标图像 interpolation=interpolation) #插值方法Else: #如果掩模为单通道, the Copy channelif notUtils.isgray (SRC):             mask=mask.repeat (3). Reshape (h0,w0,3)         Dst[y1:y1+h1,x1:x1+w1]=np.where (Cv2.resize (Mask, W1, H1), Interpolation=cv2. Inter_nearest),                                   
       cv2.resize (Src[y0:y0+h0,x0:x0+w0], (W1,H1), interpolation=interpolation),                                           dst[y1:y1 + h1, x1:x1 + W1]                                         ) #一组矩形的循环交换
 def swqprects(src,dst,rects,masks=None, Interpolation=cv2. Inter_linear):ifDst is notSRC:DST[:]=SRC Numrects=len (rects)ifNUMRECTS&LT;2:return ifMasksis None: masks=[None]*numrects X,y,w,h=rects[numrects-1] Temp=src[y:y+h,x:x+w].copy () i=numrects-2 whileI>=0:copyrect (src,dst,rects[i],rects[i+1],masks[i],interpolation) i-=1 copyrect (TEMP,DST, 0,0,w,h) , rects[0],masks[numrects-1],interpolation)

4 using a normal camera for deep test evaluation

#!/usr/bin/env python #-*-coding:utf-8-*-# @Time: 2016/12/1 11:40 # @Author: RETACN # @Site: Deep-Test estimation using a common camera # @File: commoncamera2depth.py # @Software: Pycharm __author__ = "RETACN" __copyright__ = "property of mankind." __lic ense__ = "CN" __version__ = "0.0.1" __maintainer__ = "RETACN" __email__ = "zhenhuayue@sina.com" __status__ = "Development"ImportCv2ImportNumPy asNpdef update(val=0): Stereo.setblocksize (Cv2.gettrackbarpos (' window_size ', ' disparity ')) Stereo.setuniquenessratio ( Cv2.gettrackbarpos (' uniquenessratio ', ' disparity ')) stereo.setspecklewindowsize (Cv2.gettrackbarpos ('
    Specklewindowsize ', ' disparity ') stereo.setspecklerange (Cv2.gettrackbarpos (' specklerange ', ' disparity ')) Stereo.setdisp12maxdiff (Cv2.gettrackbarpos (' Disp12maxdiff ', ' disparity ')) print (' Computing disparity ... ') disp = St Ereo.compute (IMGL, Imgr). Astype (Np.float32)/16.0 cv2.imshow (' left ', IMGL) cv2.imshow (' disparity ', disp-min_di SP)/Num_disp)if__name__== ' __main__ ':     windows_size=5 #一个匹配块的大小, more than 1 odd     min_disp=16 #最小视差值      num_disp=192-min_disp #最大视差值和最小视差值的差     blocksize=windows_size     Uniquenessratio=1     specklerange=3 #每个已连接部分的最大视差变化     specklewindowsize=3 # Maximum window size for smooth parallax area     disp12maxdiff=200     p1=600 #控制视差图平滑度有第一个参数     p2= 2400# the second parameter, the greater the value the Parallax graph smoother     #读入图像     imgl=cv2.imread ('. /imgl.jpg ')     imgr=cv2.imread ('.. /imgr.jpg ')     cv2.namedwindow (' disparity ')     cv2.createtrackbar (' Specklerange ', ' Disparity ', specklerange,50,update)     cv2.createtrackbar (' window_size ', ' disparity ', windows_size,
21,update)     cv2.createtrackbar (' specklewindowsize ', ' disparity ', specklewindowsize,200,update)     cv2.createtrackbar (' uniquenessratio ', ' disparity ', uniquenessratio,50,update)     cv2.createtrackbar (' Disp12maxdiff ', ' disparity ', disp12maxdiff,250,update)     Stereo=cv2. Stereosgbm_create (Mindisparity=min_disp,                                    Numdisparities=num_disp,                                    Blocksize=blocksize,                                    Uniquenessratio=uniquenessratio,                                   Specklerange=specklerange,                                    specklewindowsize=specklewindowsize,                                    Disp12maxdiff=disp12maxdiff,                                    P1=p1,                                    p2=p2)     update ()     Cv2.waitkey ()

5 using watershed and Grabcut algorithms for object segmentation

A use brabcut for foreground detection

#!/usr/bin/env python
#-*-coding:utf-8-*-
# @Time: 2016/12/2 11:17 #
@Author: RETACN
# @Site : Use grubcut for foreground detection
# @File: grabcutnew.py
# @Software: pycharm
__author__ = "RETACN"
__copyright__ = "P Roperty of mankind. "
__license__ = "CN"
__version__ = "0.0.1"
__maintainer__ = "RETACN"
__email__ = "zhenhuayue@sina.com"
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.