示例#1
0
def calibrationExample():
    camNum =0           # The number of the camera to calibrate
    nPoints = 5         # number of images used for the calibration (space presses)
    patternSize=(9,6)   #size of the calibration pattern
    saveImage = False

    calibrated, camera_matrix,dist_coefs,rms = SIGBTools.calibrateCamera(camNum,nPoints,patternSize,saveImage)
    K = camera_matrix
    cam1 =Camera( np.hstack((K,np.dot(K,np.array([[0],[0],[-1]])) )) )
    cam1.factor()
    #Factor projection matrix into intrinsic and extrinsic parameters
    print "K=", cam1.K
    print "R=", cam1.R
    print "t", cam1.t
    
    if (calibrated):
        capture = cv2.VideoCapture(camNum)
        running = True
        while running:
            running, img =capture.read()
            imgGray=cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
            
            ch = cv2.waitKey(1)
            if(ch==27) or (ch==ord('q')): #ESC
                running = False
            img=cv2.undistort(img, camera_matrix, dist_coefs )
            found,corners=cv2.findChessboardCorners(imgGray, patternSize  )
            if (found!=0):
                cv2.drawChessboardCorners(img, patternSize, corners,found)
            cv2.imshow("Calibrated",img)
示例#2
0
def calibrationExample():
    camNum = 0  # The number of the camera to calibrate
    nPoints = 5  # number of images used for the calibration (space presses)
    patternSize = (9, 6)  #size of the calibration pattern
    saveImage = 'calibrationShoots'

    calibrated, camera_matrix, dist_coefs, rms = SIGBTools.calibrateCamera(
        camNum, nPoints, patternSize, saveImage)
    K = camera_matrix
    cam1 = Camera(np.hstack((K, np.dot(K, np.array([[0], [0], [-1]])))))
    cam1.factor()
    #Factor projection matrix into intrinsic and extrinsic parameters
    print "K=", cam1.K
    print "R=", cam1.R
    print "t", cam1.t

    if (calibrated):
        capture = cv2.VideoCapture(camNum)
        running = True
        while running:
            running, img = capture.read()
            imgGray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

            ch = cv2.waitKey(1)
            if (ch == 27) or (ch == ord('q')):  #ESC
                running = False
            img = cv2.undistort(img, camera_matrix, dist_coefs)
            found, corners = cv2.findChessboardCorners(imgGray, patternSize)
            if (found != 0):
                cv2.drawChessboardCorners(img, patternSize, corners, found)
            cv2.imshow("Calibrated", img)
示例#3
0
def augmentImage(imagesNr):
    patternSize=(9,6)   #size of the calibration pattern
    camNum =0           # The number of the camera to calibrate
    camera_matrix = np.load('PMatrix.npy')
    dist_coefs = np.load('distCoef.npy')
    #loads the video
    capture = cv2.VideoCapture(camNum)
    running = True
    idx = np.array([1,7,37,43])
    #while the video is running
    while running:
        images=[]
        #Gets all calibrated images.
        for i in range(imagesNr):
            file="CalibrationImage"+str(i+1)+".jpg"
            images.append(cv2.imread(file))
        
        running, img =capture.read()
        imgGray=cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
        #find chessBoardCorners
        found,corners=cv2.findChessboardCorners(imgGray, patternSize)
        if (found!=0): #if the corners are found.
            srcPoints=[]
            for i in idx:
                srcPoints.append(corners[i][0]) # store points
            for i in range(len(images)):
                imgGray=cv2.cvtColor(images[i], cv2.COLOR_BGR2GRAY)
                nothing,calCorners=cv2.findChessboardCorners(imgGray, patternSize) #for each calibration image find the corners and paint.
                calPoints=[];
                width=(srcPoints[0][0]-srcPoints[1][0])/(srcPoints[1][0]-srcPoints[1][1])
                for j in idx:
                    calPoints.append(calCorners[j][0]);
                ip1 = np.array([[x,y] for (x,y) in srcPoints])#convert points for both src and cal points to np.array().
                ip2 =np.array([[x,y] for (x,y) in calPoints])#convert points for both src and cal points to np.array().
                H, mask =cv2.findHomography(ip1, ip2)#finds the homography from the two point sets.
                cam1 = Camera( np.hstack((camera_matrix,np.dot(camera_matrix,np.array([[0],[0],[-1]])))))
                cam2 = Camera(np.dot(H,cam1.P))
                A = dot(np.linalg.inv(camera_matrix),cam2.P[:,:3])
                A = array([A[:,0],A[:,1],cross(A[:,0],A[:,1])]).T
                cam2.P[:,:3] = dot(camera_matrix,A)
                box=cube_points((0,0,0,1),0.1) 
                box_cam1=cam1.project(SIGBTools.toHomogenious(box[:,:5]))
                box_cam2=cam2.project(SIGBTools.toHomogenious(box))
                linePoints=[]
                for j in range(len(box_cam2[0])/2):
                    p1=(int(box_cam2[0][j]),int(box_cam2[1][j]));
                    linePoints.append(p1)
                    cv2.circle(img,p1 , 5, (0,0,255))
                    cv2.circle(images[i],p1 , 5, (0,0,255))
                for j in range(len(linePoints)):
                    for k in range(len(linePoints)):
                        cv2.line(img, linePoints[j], linePoints[k], (0,0,255))
                        cv2.line(images[i], linePoints[j], linePoints[k], (0,0,255))
                fileName = 'ArgumentedCalibrationImage'+str(i+1)+".jpg"
                cv2.imwrite(fileName, images[i])
                #print("HOMOGRAPHY FOR IMAGE"+str(j)+": "+str(H))
        ch = cv2.waitKey(1)
        if(ch==27) or (ch==ord('q')): #ESC
            running = False
        cv2.namedWindow(str(i)+" Image")
        cv2.imshow(str(i)+" Image",img)
示例#4
0
def augmentImages():
    '''
    We augment arbitrary images with the chessboard with a cube
    using a two-camera approach
    '''
    # load calibration
    cam_calibration, distCoef = loadCameraCalibration()

    # choose points on the chessboard pattern
    idx = np.array([1, 7, 37, 43])

    # load calibration pattern and transform the image
    calibration_pattern = cv2.imread("Images/CalibrationPattern.png")
    calibration_pattern = cv2.resize(calibration_pattern, (640, 480))
    calibration_pattern = cv2.cvtColor(calibration_pattern, cv2.COLOR_BGR2GRAY)

    # get corners from the calibration pattern
    found, calibrationCorners = cv2.findChessboardCorners(calibration_pattern, (9, 6))

    # load images to be augmented
    images = []
    for i in range(1, 8):
        images.append(cv2.imread("Solutions/cam_calibration{}.jpg".format(i)))

    # augment the images one by one
    for image_id, image in enumerate(images):

        # find the same corners as we had found previously in the
        # chessboard pattern itself, only this one is in the video
        gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
        found, corners = cv2.findChessboardCorners(gray, (9, 6))

        if not found:
            continue

        # load up coords in the respective images
        imagePoints = []
        calibrationPoints = []
        for i in idx:
            p = corners[i][0]
            cp = calibrationCorners[i][0]
            imagePoints.append(p)
            calibrationPoints.append(cp)

        imagePoints = np.array(imagePoints)
        calibrationPoints = np.array(calibrationPoints)

#         cv2.imshow('calibration image', calibration_pattern)

        # Create 1st camera, this one is looking at the pattern image
        cam1 = Camera(hstack((cam_calibration, dot(cam_calibration, np.array([[0], [0], [-1]])))))
        cam1.factor()

        # Create the cube
        cube = cube_points([0, 0, 0.1], 0.3)

        # Project the bottom square of the cube, this will transform
        # point coordinates from the object space to the calibration
        # world space where the camera looks
        calibration_rect = cam1.project(SIGBTools.toHomogenious(cube[:, :5]))

        # Calculate the homography from the corners in the calibration image
        # to the same points in the image that we want to project the cube to
        homography = SIGBTools.estimateHomography(calibrationPoints, imagePoints)

        # Transform the rect from the calibration image world space to the final
        # image world space
        transRect = SIGBTools.normalizeHomogenious(dot(homography, calibration_rect))

        # Create the second camera, looking into the world of the final image
        cam2 = Camera(dot(homography, cam1.P))

        # Recalculate the projection matrix
        calibrationInverse = np.linalg.inv(cam_calibration)
        rot = dot(calibrationInverse, cam2.P[:, :3])

        # reassemble the rotation translation matrix
        r1, r2, t = tuple(np.hsplit(rot, 3))
        r3 = cross(r1.T, r2.T).T
        rotationTranslationMatrix = np.hstack((r1, r2, r3, t))

        # Create the projection matrix
        cam2.P = dot(cam_calibration, rotationTranslationMatrix)
        cam2.factor()

        # project the cube using the 2nd camera
        cube = cube_points([0, 0, 0.1], 0.3)
        box = cam2.project(SIGBTools.toHomogenious(cube))

        for i in range(1, 17):
            x1 = box[0, i - 1]
            y1 = box[1, i - 1]
            x2 = box[0, i]
            y2 = box[1, i]
            cv2.line(image, (int(x1), int(y1)), (int(x2), int(y2)), (0, 0, 255), 2)

        # save image
        cv2.imwrite("Solutions/augmentation{}.png".format(image_id), image)

        cv2.imshow("Test", image)
        cv2.waitKey(0)
示例#5
0
def update(img):
    cv2.namedWindow('Web cam')
    camera_matrix = np.load('numpyData/camera_matrix.npy')
    chessSquare_size = np.load('numpyData/chessSquare_size.npy')
    dist_coefs = np.load('numpyData/distortionCoefficient.npy')
    img_points = np.load('numpyData/img_points.npy')
    img_points_first = np.load('numpyData/img_points_first.npy')
    obj_points = np.load('numpyData/obj_points.npy')
    rotatioVectors = np.load('numpyData/rotatioVectors.npy')
    translationVectors = np.load('numpyData/translationVectors.npy')
    calImg=cv2.imread("01.png")
    pattern_size = (9, 6)
    idx = [0,8,45,53]
    image=copy(img)
    calCorners=[]
    img_points_first=np.asarray(img_points_first, np.float32)
    imageWhite = cv2.imread("Images/white.jpg")
    imageDown = cv2.imread("Images/Down.jpg")
    imageFaces = cv2.imread("Images/faces.psd")
    imageLeft = cv2.imread("Images/Left.jpg")
    imageRight = cv2.imread("Images/Right.jpg")
    imageTop = cv2.imread("Images/Top.jpg")
    imageUp = cv2.imread("Images/Up.jpg")
    textures = []
    textures.append(imageTop)
    textures.append(imageLeft)
    textures.append(imageRight)
    textures.append(imageDown)
    textures.append(imageUp)
    textures.append(imageFaces)
    for i in idx:
        calCorners.append(img_points_first[i])
    inputKey = cv2.waitKey(1)
    if Undistorting:  #Use previous stored camera matrix and distortion coefficient to undistort the image
        image=cv2.undistort(image, camera_matrix, dist_coefs)
    if (ProcessFrame):
        
        imgCorners=[]
        patternFound, imgPoints = cv2.findChessboardCorners(image, pattern_size)        
        if patternFound:            
            if ShowText:
                cv2.putText(image,str("frame:" + str(frameNumber)), (20,10),cv2.FONT_HERSHEY_PLAIN,1, (255, 255, 255))#Draw the text
            imgCorners=[]
            for i in idx:
                imgCorners.append(imgPoints[i][0])
            imgPoints=np.asarray(imgPoints, np.float32)
            ip1 = np.array([[x,y] for (x,y) in calCorners])
            ip2 = np.array([[x,y] for (x,y) in imgCorners])
            H, mask =cv2.findHomography(ip1, ip2)#finds the homography from the two point sets.
            if debug:
                pattern_points = np.zeros( (np.prod(pattern_size), 3), np.float32 )
                pattern_points[:,:2] = np.indices(pattern_size).T.reshape(-1, 2)
                pattern_points *= chessSquare_size
                obj_points = [pattern_points]
                obj_points.append(pattern_points)
                obj_points = np.array(obj_points,np.float64).T
                obj_points=obj_points[:,:,0].T
                found,rvecs_new,tvecs_new = GetObjectPos(obj_points, imgPoints,camera_matrix,dist_coefs);
                if found:
                    rt = np.hstack((cv2.Rodrigues(rvecs_new)[0],tvecs_new))
                    cam2 = Camera(np.dot(camera_matrix, rt))
                    cam2.factor()
            else:
                rotationVector=np.array(rotatioVectors)[0]
                translationVector=np.array(translationVectors)[0]
                rt = np.hstack((cv2.Rodrigues(rotationVector)[0], translationVector))
                cam2 = Camera( np.dot(H, rt))
            #loads the background
            backGround=copy(image)
            backup=copy(image)
            boxCam=cam2.project(toHomogenious(box))
            i = array([ [0,0,0,0],[1,1,1,1] ,[2,2,2,2]  ])  # indices for the first dim
            j = array([ [0,3,2,1],[0,3,2,1] ,[0,3,2,1]  ])  # indices for the second dim
            TopF = box[i,j]
            TopFace = boxCam[i,j]
            i = array([ [0,0,0,0],[1,1,1,1] ,[2,2,2,2]  ])  # indices for the first dim
            j = array([ [3,8,7,2],[3,8,7,2] ,[3,8,7,2]  ])  # indices for the second dim
            RightF = box[i,j]
            RightFace = boxCam[i,j]
            i = array([ [0,0,0,0],[1,1,1,1] ,[2,2,2,2]  ])  # indices for the first dim
            j = array([ [5,0,1,6],[5,0,1,6] ,[5,0,1,6]  ])  # indices for the second dim
            LeftF = box[i,j]
            LeftFace = boxCam[i,j]
            i = array([ [0,0,0,0],[1,1,1,1] ,[2,2,2,2]  ])  # indices for the first dim 
            j = array([ [5,8,3,0], [5,8,3,0] , [5,8,3,0] ])  # indices for the second dim
            UpF = box[i,j]
            UpFace = boxCam[i,j]
            i = array([ [0,0,0,0],[1,1,1,1] ,[2,2,2,2]  ])  # indices for the first dim
            j = array([ [1,2,7,6], [1,2,7,6], [1,2,7,6] ])  # indices for the second dim
            DownF = box[i,j]
            DownFace = boxCam[i,j]
            imagePoints = []
            mTex,nTex,h = textures[0].shape
            imageWhite=cv2.resize(imageWhite, (mTex,nTex))
            TopFace=np.array(TopFace)
            RightFace=np.array(RightFace)
            LeftFace=np.array(LeftFace)
            UpFace=np.array(UpFace)
            DownFace=np.array(DownFace)
            imagePoints.append([(float(TopFace[0][0]),float(TopFace[1][0])), 
                                (float(TopFace[0][1]),float(TopFace[1][1])),
                                (float(TopFace[0][3]),float(TopFace[1][3])),
                                (float(TopFace[0][2]),float(TopFace[1][2]))])
            imagePoints.append([(float(RightFace[0][0]),float(RightFace[1][0])),
                                (float(RightFace[0][1]),float(RightFace[1][1])),
                                (float(RightFace[0][3]),float(RightFace[1][3])),
                                (float(RightFace[0][2]),float(RightFace[1][2]))])
            imagePoints.append([(float(LeftFace[0][0]),float(LeftFace[1][0])), 
                                (float(LeftFace[0][1]),float(LeftFace[1][1])),
                                (float(LeftFace[0][3]),float(LeftFace[1][3])),
                                (float(LeftFace[0][2]),float(LeftFace[1][2]))])
            imagePoints.append([(float(UpFace[0][0]),float(UpFace[1][0])), 
                                (float(UpFace[0][1]),float(UpFace[1][1])),
                                (float(UpFace[0][3]),float(UpFace[1][3])),
                                (float(UpFace[0][2]),float(UpFace[1][2]))])
            imagePoints.append([(float(DownFace[0][0]),float(DownFace[1][0])), 
                                (float(DownFace[0][1]),float(DownFace[1][1])),
                                (float(DownFace[0][3]),float(DownFace[1][3])),
                                (float(DownFace[0][2]),float(DownFace[1][2]))])
            points=[]
            if ProjectPattern:
                for i in range(len(imagePoints)):
                    points.append(np.array([[x,y] for (x,y) in imagePoints[i]]))
                points=np.array(points)
                homographies=[]
                ip1=np.array([(float(0),float(0)),(float(nTex),float(0)),(float(0),mTex),(float(nTex),float(mTex))])
                for i in points:
                    Ht,mask = cv2.findHomography(ip1, i)
                    homographies.append(Ht)
                nI, mI , jI = image.shape
                overlays=[]
                whiteOverlays=[]
                counter=0
                for i in homographies:
                    overlays.append(cv2.warpPerspective(textures[counter], i,(mI,nI)))
                    whiteOverlays.append(cv2.warpPerspective(imageWhite, i,(mI,nI)))
                    counter=counter+1
                #splits the background finding color and shape.
                [RbackGround,GbackGround,BbackGround]=cv2.split(backGround)
                backGround=cv2.cvtColor(backGround, cv2.COLOR_RGB2GRAY)
                (w,h)= shape(backGround)
                [R,G,B]=cv2.split(image)
                sub = cv2.addWeighted(cv2.absdiff(G, GbackGround), 0.5, cv2.absdiff(R, RbackGround), 0.5, 0)
                sub = cv2.addWeighted(sub, 0.5, cv2.absdiff(B, BbackGround), 0.25, 0)
                textureImg=cv2.subtract(backup, backup)
                for i in whiteOverlays:
                    textureImg = cv2.addWeighted(textureImg, 1, i, 1,0)
                    textureImg=cv2.add(textureImg, textureImg)
                (thresh, sub) = cv2.threshold( sub, 255, 255, cv2.THRESH_BINARY )
                msk2=255-sub
                msk2=cv2.cvtColor(msk2, cv2.COLOR_GRAY2RGB)
                sub=cv2.cvtColor(sub, cv2.COLOR_GRAY2RGB)
                image=cv2.subtract(image, textureImg)
                result=copy(image)
                backup=copy(image)
                TopFaceCornerNormals, RightFaceCornerNormals, LeftFaceCornerNormals, UpFaceCornerNormals, DownFaceCornerNormals=CalculateFaceCornerNormals(TopFace,RightFace,LeftFace,UpFace,DownFace)
                shades = np.array([[TopF, TopFaceCornerNormals],[RightF, RightFaceCornerNormals],[LeftF, LeftFaceCornerNormals],[UpF, UpFaceCornerNormals],[DownF, DownFaceCornerNormals]])
                counter = 0
                #print boxCam
                type = np.array(['top','right','left','up','down'])
                for i in overlays:
                    image=copy(backup)
                    image = cv2.addWeighted(image, 1, i, 1,0)
                    if Shading:
                        image, visible = ShadeFace(image,shades[counter][0],shades[counter][1],cam2, type[counter])
                        if visible:
                            result=cv2.bitwise_or(result, image)
                    else:
                        result=cv2.bitwise_or(result, image)
                    counter = counter+1
                image=result
                b = np.array(boxCam)
            else:
                for i in range(len(imagePoints)):
                    for j in range(1,4):
                        cv2.line(image, (int(imagePoints[i][j][0]), int(imagePoints[i][j][1])),(int(imagePoints[i][j-1][0]),int(imagePoints[i][j-1][1])),(0,0,255) , 1)
                        cv2.line(image, (int(imagePoints[i][j-1][0]), int(imagePoints[i][j-1][1])),(int(imagePoints[i][4-j][0]),int(imagePoints[i][4-j][1])),(0,0,255) , 1)                    

    cv2.imshow('Web cam', image)