2016年5月30日 星期一

3D scan code study step 1 and step 2

SL3DS1.projcapt.py
=================================================
import cv2
import sys

CV_CAP_PROP_BRIGHTNESS=10
CV_CAP_PROP_CONTRAST=11
CV_CAP_PROP_SATURATION=12
CV_CAP_PROP_EXPOSURE=15
CV_CAP_PROP_WHITE_BALANCE=17
## video capture right camera
video_capture0 = cv2.VideoCapture(0)
video_capture0.set(CV_CAP_PROP_BRIGHTNESS,30.0)
video_capture0.set(CV_CAP_PROP_CONTRAST,5.0)
video_capture0.set(CV_CAP_PROP_SATURATION,100.0)
video_capture0.set(CV_CAP_PROP_EXPOSURE,-8.0)
video_capture0.set(CV_CAP_PROP_WHITE_BALANCE,10000.0)

print video_capture0.get(4)
video_capture0.set(3,1920.0)   ## horizontal pixels
video_capture0.set(4,1080.0)  ##  vertical pixels
print video_capture0.get(4)    ##  check vertical pixel

##video capture left camera
video_capture1 = cv2.VideoCapture(1)
print video_capture1.get(4)
video_capture1.set(3,1920.0)
video_capture1.set(4,1080.0)
print video_capture1.get(4)
print video_capture1.get(9)

video_capture1.set(CV_CAP_PROP_BRIGHTNESS,30.0)
video_capture1.set(CV_CAP_PROP_CONTRAST,5.0)
video_capture1.set(CV_CAP_PROP_SATURATION,100.0)
video_capture1.set(CV_CAP_PROP_EXPOSURE,-8.0)
video_capture1.set(CV_CAP_PROP_WHITE_BALANCE,10000.0)

## camera properties in opencv
##CV_CAP_PROP_FRAME_WIDTH Width of the frames in the video stream.
##CV_CAP_PROP_FRAME_HEIGHT Height of the frames in the video stream.
##CV_CAP_PROP_FPS Frame rate.
##CV_CAP_PROP_FORMAT Format of the Mat objects returned by retrieve() .
##CV_CAP_PROP_MODE Backend-specific value indicating the current capture mode.
##CV_CAP_PROP_BRIGHTNESS Brightness of the image (only for cameras).
##CV_CAP_PROP_CONTRAST Contrast of the image (only for cameras).
##CV_CAP_PROP_SATURATION Saturation of the image (only for cameras).
##CV_CAP_PROP_HUE Hue of the image (only for cameras).
##CV_CAP_PROP_GAIN Gain of the image (only for cameras).
##CV_CAP_PROP_EXPOSURE Exposure (only for cameras).
##CV_CAP_PROP_CONVERT_RGB Boolean flags indicating whether images should be converted to RGB.
##CV_CAP_PROP_WHITE_BALANCE Currently unsupported
##CV_CAP_PROP_RECTIFICATION Rectificat


#show frames from cameras
ret, frame0 = video_capture0.read()
cv2.imshow("cam0", frame0)
ret, frame1 = video_capture1.read()
cv2.imshow("cam1", frame1)
cv2.waitKey(3000)

ret, frame0 = video_capture0.read()
cv2.waitKey(100)
##    cv2.imshow("cam0", frame0)
ret, frame1 = video_capture1.read()
cv2.waitKey(100)

cv2.destroyAllWindows()
horzlino=1280
vertlino=720
for ii in range(0,19):
    print ii, video_capture1.get(ii)
## open a borderless window for showing projector images as a second display  
cv2.namedWindow("Projector Window",cv2.WND_PROP_FULLSCREEN )
cv2.setWindowProperty("Projector Window", 0,1)
cv2.resizeWindow("Projector Window", 1024,768)
cv2.moveWindow("Projector Window", 1025, -2)

## folders for saving images of left and right cameras
import os
try:
    os.makedirs(textvalcap+"/"+'CAMR')
except OSError:
    pass

try:
    os.makedirs(textvalcap+"/"+'CAML')
except OSError:
    pass

imggray=np.load('proj.npy')
# Capture images from left and right cameras after showing pattern in projector
for x in range(1, 43):  # processing 42 to left and right 
    cv2.imshow("Projector Window",imggray[:,:,x-1])
    filename0 = 'CAMR/CAM0%02d.png'%(x,)
    filename1 = 'CAML/CAM1%02d.png'%(x,)
    print filename0
    ret, frame0 = video_capture0.read()
    cv2.waitKey(100)
    ret, frame1 = video_capture1.read()
    cv2.waitKey(100)
    cv2.imwrite(filename0,frame0)
    cv2.imwrite(filename1,frame1)

video_capture0.release()
video_capture1.release()
# When everything is done, release the capture
cv2.destroyAllWindows()
print 'pcapture Done!'


========================================================
step 2 : Processing the 42 images of each camera and capture points codes
========================================================
import numpy as np
import cv2
import os
import glob

def imgdesig(img1,img2): #define a function for getting dark and light pattern
   old_settings = np.seterr(all='ignore')
   img1=cv2.imread(img1,cv2.IMREAD_GRAYSCALE)  # 轉成  灰階
   ret,img1 = cv2.threshold(img1,10,255,cv2.THRESH_TOZERO) # thr -> 10,  max_val -> 255
   img2=cv2.imread(img2,cv2.IMREAD_GRAYSCALE)  # 轉成  灰階
   ret,img2 = cv2.threshold(img2,10,255,cv2.THRESH_TOZERO)  # thr -> 10,  max_val -> 255
   img12=(((img1//2)+(img2//2)))  #  sum of img1/2 + img2/2
   img123=(np.divide(img1,img12))
   img123=(np.divide(img123,img123))
   return img123     #  會得到  img1 or (img2 + img1)/2 的影像, 而且只有 0和1的影像

#   np.divide ->   The quotient x1/x2, element-wise. Returns a scalar if both x1 andx2 are scalars
horzlino=1920
vertlino=1080
Direct=captdirect+"/"+"CAMR/"
img_names = glob.glob(Direct+"*.png")
print captdirect
#call for processing the right camera images
execfile("camlcoloc.py")
np.save(Direct+"coloccod" , rightcamcode)
cv2.waitKey(200)
Direct=captdirect+"/"+"CAML/"
img_names = glob.glob(Direct+"*.png")
#call for processing the left camera images
execfile("camlcoloc.py")
np.save(Direct+"coloccod" , rightcamcode)

cv2.destroyAllWindows()
print 'Procimg Done!'

============

camlcoloc.py
============

#  horzlino = 1920,   vertlino = 1080
grayimg=np.zeros((vertlino, horzlino), dtype=np.int16)
rightcamcode=np.zeros((vertlino, horzlino,2), dtype=np.int16)
##=======================================================
#Horizontal gray code
for ii in range(3,22,2):

#range([start], stop[, step])
  • start: Starting number of the sequence.
  • stop: Generate numbers up to, but not including this number.
  • step: Difference between each number in the sequence.

    xx=ii-3
    xx=xx//2
    filename1 =  img_names[ii]
    filename2 =  img_names[ii-1]
    ff=imgdesig(filename1,filename2)  # ff 為 img
    print 'processing %s...' % filename1, (2**xx)
    grayimg=grayimg+(2**xx)*ff   

#  當  xx -> 0    (2**xx) ->  1       3
#  當  xx -> 1    (2**xx) ->  2       5
#  當  xx -> 2    (2**xx) ->  4        7
#  當  xx -> 3    (2**xx) ->  8         9
#  當  xx -> 4    (2**xx) ->  16       11
#  當  xx -> 5    (2**xx) ->  32       13
#  當  xx -> 6    (2**xx) ->  64       15
#  當  xx -> 7    (2**xx) ->  128     17
#  當  xx -> 8    (2**xx) ->  256      19
#  當  xx -> 9    (2**xx) ->  512      21

    

imgbin3=np.zeros((vertlino, horzlino,3), dtype=np.uint8)
for ii in range(0, horzlino):
    for jj in range(0, vertlino):
        rightcamcode[jj][ii][0]=grayimg[jj][ii]
        imgbin3[jj][ii][1]= grayimg[jj][ii]%256
        imgbin3[jj][ii][2]= 40*grayimg[jj][ii]//256
        imgbin3[jj][ii][0]= 4
img1=(grayimg%255)
cv2.imshow("PWindow2",imgbin3)
cv2.waitKey(100)

##=======================================================
#Vertical gray code

img1=cv2.imread(img_names[0],cv2.IMREAD_GRAYSCALE)
grayimg=(img1*0)+1023
grayimg=grayimg*0    #  把  grayimg 清除為0
for ii in range(23,42,2):
    xx=ii-22
    xx=xx//2
    filename1 =  img_names[ii]
    filename2 =  img_names[ii-1]
    ff=imgdesig(filename1,filename2)
    print 'processing %s...' % filename1, (2**xx)
    grayimg=grayimg+(2**xx)*ff

for ii in range(0, horzlino):
    for jj in range(0, vertlino):
        rightcamcode[jj][ii][1]=grayimg[jj][ii]
        imgbin3[jj][ii][0]= (imgbin3[jj][ii][0]+grayimg[jj][ii]%256)%256
        imgbin3[jj][ii][2]= 40*(imgbin3[jj][ii][2]+grayimg[jj][ii]%256)//80
        imgbin3[jj][ii][1]= 4
img1=(grayimg%255)
cv2.imshow("PWindow2",imgbin3)
cv2.waitKey(2000)



沒有留言:

張貼留言