def AugumentSequence(): #Loading callibration matrix K = np.load('Results/PMatrix.npy') #setting pattern size pattern_size = (9,6) #loading calibration images L_CP = cv2.imread('Results/L_CPSeq.jpg') #Frontal view #Getting cube points from cubePoints.py cubePoints = cube.cube_points([0,0,0.1],0.1) #Getting chesscorners for frontal view Fgray = cv2.cvtColor(L_CP,cv2.COLOR_BGR2GRAY) Ffound, Fcorners = cv2.findChessboardCorners(Fgray, pattern_size) FchessCorners = [(Fcorners[0,0,0],Fcorners[0,0,1]),(Fcorners[8,0,0],Fcorners[8,0,1]),(Fcorners[45,0,0],Fcorners[45,0,1]),(Fcorners[53,0,0],Fcorners[53,0,1])] #To open CV format FchessCorners = np.array([[x,y] for (x,y) in FchessCorners]) #Getting chesscorners for the sequence images cap = cv2.VideoCapture(0) running, I = cap.read() #Get camera model for first view cam1 = SIGBTools.Camera(hstack((K,dot(K,array([[0],[0],[-1]])) )) ) #imSize = np.shape(I) #videoWriter = cv2.VideoWriter("sequence.mp4", cv.FOURCC('D','I','V','X'), 30,(imSize[1], imSize[0]),True) while(running): running, I = cap.read() #converting to gray for better contrast gray = cv2.cvtColor(I,cv2.COLOR_BGR2GRAY) found, corners = cv2.findChessboardCorners(gray, pattern_size) if (found): #picking utmost corners IchessCorners = [(corners[0,0,0],corners[0,0,1]),(corners[8,0,0],corners[8,0,1]),(corners[45,0,0],corners[45,0,1]),(corners[53,0,0],corners[53,0,1])] #To openCV format IchessCorners = np.array([[x,y] for (x,y) in IchessCorners]) #Find homography between frontal image and current frame H,mask = cv2.findHomography(FchessCorners, IchessCorners) #Transofrm camera view camFrame = SIGBTools.Camera(dot(H,cam1.P)) A = dot(linalg.inv(K),camFrame.P[:,:3]) A = array([A[:,0],A[:,1],cross(A[:,0],A[:,1])]).T camFrame.P[:,:3] = dot(K,A) #Get cube projection points box_peojection = camFrame.project(SIGBTools.toHomogenious(cubePoints)) #Draw box p = box_peojection ''' Drawing the box manually ''' #bottom cv2.line(I, (int(p[0][1]), int(p[1][1])), (int(p[0][2]),int(p[1][2])),(255,255,0),2) cv2.line(I, (int(p[0][2]), int(p[1][2])), (int(p[0][3]),int(p[1][3])),(255,255,0),2) cv2.line(I, (int(p[0][3]), int(p[1][3])), (int(p[0][4]),int(p[1][4])),(255,255,0),2) cv2.line(I, (int(p[0][1]), int(p[1][1])), (int(p[0][4]),int(p[1][4])),(255,255,0),2) #connecting lines cv2.line(I, (int(p[0][4]), int(p[1][4])), (int(p[0][5]),int(p[1][5])),(255,255,0),2) cv2.line(I, (int(p[0][1]), int(p[1][1])), (int(p[0][6]),int(p[1][6])),(255,255,0),2) cv2.line(I, (int(p[0][2]), int(p[1][2])), (int(p[0][7]),int(p[1][7])),(255,255,0),2) cv2.line(I, (int(p[0][3]), int(p[1][3])), (int(p[0][8]),int(p[1][8])),(255,255,0),2) #top cv2.line(I, (int(p[0][5]), int(p[1][5])), (int(p[0][6]),int(p[1][6])),(255,255,0),2) cv2.line(I, (int(p[0][6]), int(p[1][6])), (int(p[0][7]),int(p[1][7])),(255,255,0),2) cv2.line(I, (int(p[0][7]), int(p[1][7])), (int(p[0][8]),int(p[1][8])),(255,255,0),2) cv2.line(I, (int(p[0][8]), int(p[1][8])), (int(p[0][9]),int(p[1][9])),(255,255,0),2) cv2.imshow('Augumentation',I) cv2.waitKey(1) return
def AugumentImages(): #Loading callibration matrix K = np.load('Results/PMatrix.npy') #setting pattern size pattern_size = (9,6) #loading calibration images L_CP = cv2.imread('Results/L_CP.jpg') #Frontal view #Getting cube points from cubePoints.py cubePoints = cube.cube_points([0,0,0.1],0.1) I1 = cv2.imread('Results/calibrationShoots1.jpg') I2 = cv2.imread('Results/calibrationShoots2.jpg') I3 = cv2.imread('Results/calibrationShoots3.jpg') I4 = cv2.imread('Results/calibrationShoots4.jpg') I5 = cv2.imread('Results/calibrationShoots5.jpg') Images = [I1, I2, I3, I4 ,I5] ImageH = [] #homographies from frontal view to H respectively #Getting chesscorners for frontal view Fgray = cv2.cvtColor(L_CP,cv2.COLOR_BGR2GRAY) Ffound, Fcorners = cv2.findChessboardCorners(Fgray, pattern_size) FchessCorners = [(Fcorners[0,0,0],Fcorners[0,0,1]),(Fcorners[8,0,0],Fcorners[8,0,1]),(Fcorners[45,0,0],Fcorners[45,0,1]),(Fcorners[53,0,0],Fcorners[53,0,1])] #To open CV format FchessCorners = np.array([[x,y] for (x,y) in FchessCorners]) #Getting chesscorners for the rest of pics for I in Images: #converting to gray for better contrast gray = cv2.cvtColor(I,cv2.COLOR_BGR2GRAY) #finding all corners on chessboard found, corners = cv2.findChessboardCorners(gray, pattern_size) #picking utmost corners IchessCorners = [(corners[0,0,0],corners[0,0,1]),(corners[8,0,0],corners[8,0,1]),(corners[45,0,0],corners[45,0,1]),(corners[53,0,0],corners[53,0,1])] #To openCV format IchessCorners = np.array([[x,y] for (x,y) in IchessCorners]) H,mask = cv2.findHomography(FchessCorners, IchessCorners) ImageH.append(H) cam1 = SIGBTools.Camera(hstack((K,dot(K,array([[0],[0],[-1]])) )) ) box_cam1 = cam1.project(SIGBTools.toHomogenious(cubePoints[:,:5])) cam2 = SIGBTools.Camera(dot(ImageH[3],cam1.P)) A = dot(linalg.inv(K),cam2.P[:,:3]) A = array([A[:,0],A[:,1],cross(A[:,0],A[:,1])]).T cam2.P[:,:3] = dot(K,A) box_cam2 = cam2.project(SIGBTools.toHomogenious(cubePoints)) print box_cam2 #figure() #imshow(I4) #plot(box_cam2[0,:],box_cam2[1,:],linewidth=3) #show() p=box_cam2 ''' Drawing the box manually ''' #bottom cv2.line(I4, (int(p[0][1]), int(p[1][1])), (int(p[0][2]),int(p[1][2])),(255,255,0),2) cv2.line(I4, (int(p[0][2]), int(p[1][2])), (int(p[0][3]),int(p[1][3])),(255,255,0),2) cv2.line(I4, (int(p[0][3]), int(p[1][3])), (int(p[0][4]),int(p[1][4])),(255,255,0),2) cv2.line(I4, (int(p[0][1]), int(p[1][1])), (int(p[0][4]),int(p[1][4])),(255,255,0),2) #connecting lines cv2.line(I4, (int(p[0][4]), int(p[1][4])), (int(p[0][5]),int(p[1][5])),(255,255,0),2) cv2.line(I4, (int(p[0][1]), int(p[1][1])), (int(p[0][6]),int(p[1][6])),(255,255,0),2) cv2.line(I4, (int(p[0][2]), int(p[1][2])), (int(p[0][7]),int(p[1][7])),(255,255,0),2) cv2.line(I4, (int(p[0][3]), int(p[1][3])), (int(p[0][8]),int(p[1][8])),(255,255,0),2) #top cv2.line(I4, (int(p[0][5]), int(p[1][5])), (int(p[0][6]),int(p[1][6])),(255,255,0),2) cv2.line(I4, (int(p[0][6]), int(p[1][6])), (int(p[0][7]),int(p[1][7])),(255,255,0),2) cv2.line(I4, (int(p[0][7]), int(p[1][7])), (int(p[0][8]),int(p[1][8])),(255,255,0),2) cv2.line(I4, (int(p[0][8]), int(p[1][8])), (int(p[0][9]),int(p[1][9])),(255,255,0),2) cv2.imshow('Dupa',I4) cv2.waitKey(10000) return
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)
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)
def AugumentSequence(): # Loading callibration matrix K = np.load("Results/PMatrix.npy") # setting pattern size pattern_size = (9, 6) # loading calibration images L_CP = cv2.imread("Results/L_CPSeq.jpg") # Frontal view # Getting cube points from cubePoints.py cubePoints = cube.cube_points([0, 0, 0.1], 0.1) # Getting chesscorners for frontal view Fgray = cv2.cvtColor(L_CP, cv2.COLOR_BGR2GRAY) Ffound, Fcorners = cv2.findChessboardCorners(Fgray, pattern_size) FchessCorners = [ (Fcorners[0, 0, 0], Fcorners[0, 0, 1]), (Fcorners[8, 0, 0], Fcorners[8, 0, 1]), (Fcorners[45, 0, 0], Fcorners[45, 0, 1]), (Fcorners[53, 0, 0], Fcorners[53, 0, 1]), ] # To open CV format FchessCorners = np.array([[x, y] for (x, y) in FchessCorners]) # Getting chesscorners for the sequence images cap = cv2.VideoCapture(0) running, I = cap.read() # Get camera model for first view cam1 = SIGBTools.Camera(hstack((K, dot(K, array([[0], [0], [-1]]))))) # imSize = np.shape(I) # videoWriter = cv2.VideoWriter("sequence.mp4", cv.FOURCC('D','I','V','X'), 30,(imSize[1], imSize[0]),True) while running: running, I = cap.read() # converting to gray for better contrast gray = cv2.cvtColor(I, cv2.COLOR_BGR2GRAY) found, corners = cv2.findChessboardCorners(gray, pattern_size) if found: # picking utmost corners IchessCorners = [ (corners[0, 0, 0], corners[0, 0, 1]), (corners[8, 0, 0], corners[8, 0, 1]), (corners[45, 0, 0], corners[45, 0, 1]), (corners[53, 0, 0], corners[53, 0, 1]), ] # To openCV format IchessCorners = np.array([[x, y] for (x, y) in IchessCorners]) # Find homography between frontal image and current frame H, mask = cv2.findHomography(FchessCorners, IchessCorners) # Transofrm camera view camFrame = SIGBTools.Camera(dot(H, cam1.P)) A = dot(linalg.inv(K), camFrame.P[:, :3]) A = array([A[:, 0], A[:, 1], cross(A[:, 0], A[:, 1])]).T camFrame.P[:, :3] = dot(K, A) # Get cube projection points box_peojection = camFrame.project(SIGBTools.toHomogenious(cubePoints)) # Draw box p = box_peojection """ Drawing the box manually """ # bottom cv2.line(I, (int(p[0][1]), int(p[1][1])), (int(p[0][2]), int(p[1][2])), (255, 255, 0), 2) cv2.line(I, (int(p[0][2]), int(p[1][2])), (int(p[0][3]), int(p[1][3])), (255, 255, 0), 2) cv2.line(I, (int(p[0][3]), int(p[1][3])), (int(p[0][4]), int(p[1][4])), (255, 255, 0), 2) cv2.line(I, (int(p[0][1]), int(p[1][1])), (int(p[0][4]), int(p[1][4])), (255, 255, 0), 2) # connecting lines cv2.line(I, (int(p[0][4]), int(p[1][4])), (int(p[0][5]), int(p[1][5])), (255, 255, 0), 2) cv2.line(I, (int(p[0][1]), int(p[1][1])), (int(p[0][6]), int(p[1][6])), (255, 255, 0), 2) cv2.line(I, (int(p[0][2]), int(p[1][2])), (int(p[0][7]), int(p[1][7])), (255, 255, 0), 2) cv2.line(I, (int(p[0][3]), int(p[1][3])), (int(p[0][8]), int(p[1][8])), (255, 255, 0), 2) # top cv2.line(I, (int(p[0][5]), int(p[1][5])), (int(p[0][6]), int(p[1][6])), (255, 255, 0), 2) cv2.line(I, (int(p[0][6]), int(p[1][6])), (int(p[0][7]), int(p[1][7])), (255, 255, 0), 2) cv2.line(I, (int(p[0][7]), int(p[1][7])), (int(p[0][8]), int(p[1][8])), (255, 255, 0), 2) cv2.line(I, (int(p[0][8]), int(p[1][8])), (int(p[0][9]), int(p[1][9])), (255, 255, 0), 2) cv2.imshow("Augumentation", I) cv2.waitKey(1) return
def AugumentImages(): # Loading callibration matrix K = np.load("Results/PMatrix.npy") # setting pattern size pattern_size = (9, 6) # loading calibration images L_CP = cv2.imread("Results/L_CP.jpg") # Frontal view # Getting cube points from cubePoints.py cubePoints = cube.cube_points([0, 0, 0.1], 0.1) I1 = cv2.imread("Results/calibrationShoots1.jpg") I2 = cv2.imread("Results/calibrationShoots2.jpg") I3 = cv2.imread("Results/calibrationShoots3.jpg") I4 = cv2.imread("Results/calibrationShoots4.jpg") I5 = cv2.imread("Results/calibrationShoots5.jpg") Images = [I1, I2, I3, I4, I5] ImageH = [] # homographies from frontal view to H respectively # Getting chesscorners for frontal view Fgray = cv2.cvtColor(L_CP, cv2.COLOR_BGR2GRAY) Ffound, Fcorners = cv2.findChessboardCorners(Fgray, pattern_size) FchessCorners = [ (Fcorners[0, 0, 0], Fcorners[0, 0, 1]), (Fcorners[8, 0, 0], Fcorners[8, 0, 1]), (Fcorners[45, 0, 0], Fcorners[45, 0, 1]), (Fcorners[53, 0, 0], Fcorners[53, 0, 1]), ] # To open CV format FchessCorners = np.array([[x, y] for (x, y) in FchessCorners]) # Getting chesscorners for the rest of pics for I in Images: # converting to gray for better contrast gray = cv2.cvtColor(I, cv2.COLOR_BGR2GRAY) # finding all corners on chessboard found, corners = cv2.findChessboardCorners(gray, pattern_size) # picking utmost corners IchessCorners = [ (corners[0, 0, 0], corners[0, 0, 1]), (corners[8, 0, 0], corners[8, 0, 1]), (corners[45, 0, 0], corners[45, 0, 1]), (corners[53, 0, 0], corners[53, 0, 1]), ] # To openCV format IchessCorners = np.array([[x, y] for (x, y) in IchessCorners]) H, mask = cv2.findHomography(FchessCorners, IchessCorners) ImageH.append(H) cam1 = SIGBTools.Camera(hstack((K, dot(K, array([[0], [0], [-1]]))))) box_cam1 = cam1.project(SIGBTools.toHomogenious(cubePoints[:, :5])) cam2 = SIGBTools.Camera(dot(ImageH[3], cam1.P)) A = dot(linalg.inv(K), cam2.P[:, :3]) A = array([A[:, 0], A[:, 1], cross(A[:, 0], A[:, 1])]).T cam2.P[:, :3] = dot(K, A) box_cam2 = cam2.project(SIGBTools.toHomogenious(cubePoints)) print box_cam2 # figure() # imshow(I4) # plot(box_cam2[0,:],box_cam2[1,:],linewidth=3) # show() p = box_cam2 """ Drawing the box manually """ # bottom cv2.line(I4, (int(p[0][1]), int(p[1][1])), (int(p[0][2]), int(p[1][2])), (255, 255, 0), 2) cv2.line(I4, (int(p[0][2]), int(p[1][2])), (int(p[0][3]), int(p[1][3])), (255, 255, 0), 2) cv2.line(I4, (int(p[0][3]), int(p[1][3])), (int(p[0][4]), int(p[1][4])), (255, 255, 0), 2) cv2.line(I4, (int(p[0][1]), int(p[1][1])), (int(p[0][4]), int(p[1][4])), (255, 255, 0), 2) # connecting lines cv2.line(I4, (int(p[0][4]), int(p[1][4])), (int(p[0][5]), int(p[1][5])), (255, 255, 0), 2) cv2.line(I4, (int(p[0][1]), int(p[1][1])), (int(p[0][6]), int(p[1][6])), (255, 255, 0), 2) cv2.line(I4, (int(p[0][2]), int(p[1][2])), (int(p[0][7]), int(p[1][7])), (255, 255, 0), 2) cv2.line(I4, (int(p[0][3]), int(p[1][3])), (int(p[0][8]), int(p[1][8])), (255, 255, 0), 2) # top cv2.line(I4, (int(p[0][5]), int(p[1][5])), (int(p[0][6]), int(p[1][6])), (255, 255, 0), 2) cv2.line(I4, (int(p[0][6]), int(p[1][6])), (int(p[0][7]), int(p[1][7])), (255, 255, 0), 2) cv2.line(I4, (int(p[0][7]), int(p[1][7])), (int(p[0][8]), int(p[1][8])), (255, 255, 0), 2) cv2.line(I4, (int(p[0][8]), int(p[1][8])), (int(p[0][9]), int(p[1][9])), (255, 255, 0), 2) cv2.imshow("Dupa", I4) cv2.waitKey(10000) return