def correctPerspective(image):
   
   size = image.shape[1],image.shape[0]
   
   corners = findCorners(image)

   # visualise the chessboard corners onto the image
   annotatedImage = image.copy()
   cv2.drawChessboardCorners(annotatedImage,boardSize,corners,1) 
   cv2.imwrite('annotated.jpg',annotatedImage)
   
   rcCorners = corners.reshape(boardHeight,boardWidth,2)
   # now a 3-d array -- a row is new[x] and a column is new [:,x]
   
   # find top left corner point and bottom right corner point (NOT the same as min/max x and y):
   outerPoints = getOuterPoints(rcCorners)
   tl,tr,bl,br = outerPoints
   
   patternSize = np.array([
      np.sqrt(((tr - tl)**2).sum(0)),
      np.sqrt(((bl - tl)**2).sum(0)),
   ])
   
   inQuad = np.array(outerPoints,np.float32)
   
   outQuad = np.array([
      tl,
      tl + np.array([patternSize[0],0.0]),
      tl + np.array([0.0,patternSize[1]]),
      tl + patternSize,
   ],np.float32)
   
   transform = cv2.getPerspectiveTransform(inQuad,outQuad)
   transformed = cv2.warpPerspective(image,transform,size)

   # calculate DPI for the transformed image
   transformedCorners = cv2.perspectiveTransform(corners,transform)
   rcTransformedCorners = transformedCorners.reshape(boardHeight,boardWidth,2)
   outerPoints = getOuterPoints(rcTransformedCorners)
   tl,tr,bl,br = outerPoints
   transformedPatternSize = np.array([
      np.sqrt(((tr - tl)**2).sum(0)),
      np.sqrt(((bl - tl)**2).sum(0)),
   ])
   dpi = (transformedPatternSize / realSizeMM) * 25.4
   print 'dpi before aspect ratio correction',dpi
   
   # correct aspect ratio (stretch the dimension with the lowest resolution so we don't loose data needlessly)
   if dpi[1] > dpi[0]:
      fx = dpi[1]/dpi[0]
      fy = 1.0
      print 'final dots per inch',dpi[1]
   else:
      fx = 1.0
      fy = dpi[0]/dpi[1]
      print 'final dots per inch',dpi[0]
      
   final = cv2.resize(transformed,None,fx=fx,fy=fy)
   
   return final
Example #2
0
def draw_pattern(image, corners, ret):
    # Draw corners into image
    image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
    cv2.drawChessboardCorners(image, (columns, rows), corners, ret)
    image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)

    return image
	def GetImage(self):
		image = returnValidImage(getFrame(self.Cams), (getWidth(), getHeight()) )
		
		if(self.__init and self.searchingToggle.GetValue()):
			
			ret = False
			gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
			
			# Find the chess board corners
			result = self.pool.apply_async(cv2.findChessboardCorners, (gray, self.patternSize, None))
			try:
				ret, corners = result.get(timeout=self.tolerance/float(self.fps))
			except:
				pass

			# If found, add object points, image points (after refining them)
			
			if ret == True:
				self.steps += 1
				
				self.UpdateLabel()
				
				self.objPoints.append(self.objp)

				cv2.cornerSubPix(gray, corners, (11,11), (-1,-1), self.criteria)
				self.imgPoints.append(corners)

				# Draw and display the corners
				cv2.drawChessboardCorners(image, self.patternSize, corners, ret)
		
		self.__init = True
		
		return image
Example #4
0
def get_obect_img_pts(board_img, debug=False):
    
    board_img_grey = cv2.cvtColor(board_img, cv2.COLOR_BGR2GRAY)
    
    # Find chessboard corners
    found, corners = cv2.findChessboardCorners(
      image=board_img_grey,
      patternSize=board_size,
      flags=cv2.CALIB_CB_ADAPTIVE_THRESH | cv2.CALIB_CB_FILTER_QUADS | cv2.CALIB_CB_FAST_CHECK)
    
    # Refine corner locations
    cv2.cornerSubPix(
      image=board_img_grey,
      corners=corners,
      winSize=(11, 11),
      zeroZone=(-1, -1),
      criteria=(cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 30, 0.1))
    
    if debug:
        chessboard_debug_img = np.copy(board_img)
        chessboard_debug_img = cv2.cvtColor(chessboard_debug_img,cv2.COLOR_BGR2GRAY)
        chessboard_debug_img = cv2.cvtColor(chessboard_debug_img,cv2.COLOR_GRAY2BGR)
        cv2.drawChessboardCorners(chessboard_debug_img, board_size, corners, found)
        cv2.imshow("board", chessboard_debug_img)
        cv2.waitKey()
    
    # Correctly arrange arrays of corresponding points
    object_pts = np.zeros((np.prod(board_size), 3), np.float32)
    object_pts[:, :2] = np.indices(board_size).T.reshape(-1, 2) * square_mm
    image_pts = corners.reshape(-1, 2)
    
    return object_pts, image_pts
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)
def record_single_camera(video, name):
    ret, frame = video.read()

    shape = frame.shape

    i = 0
    while ret is True and i < 30:
        gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
        ret, corners = cv2.findChessboardCorners(gray, (6, 7), None)
        if ret is True:
            cv2.imwrite("test_data/videos/calibrator/%s/%s.jpeg" % (name, str(i).zfill(3)), frame)
            cv2.cornerSubPix(gray, corners, (11, 11), (-1, -1), CRITERIA)
            cv2.drawChessboardCorners(frame, (6, 7), corners, ret)
        cv2.imshow('frame', frame)
        k = cv2.waitKey(1) & 0xFF
        if k == ord('q'):
            break
        if ret is True:
            keep = raw_input("Keep this frame? (y/n) ") == "y"
            if keep is True:
                print i
                i = i + 1
        ret, frame = video.read()

    cv2.destroyAllWindows()

    pass
 def detect_squares(self, images):
     pattern_size = (7, 6)
     pattern_points = np.zeros( (np.prod(pattern_size), 3), np.float32 )
     pattern_points[:,:2] = np.indices(pattern_size).T.reshape(-1, 2)
     pattern_points *= self.square_size
 
     obj_points = []
     img_points = []
     h, w = 0, 0
     for fn in images:
         print 'processing %s...' % fn,
         img = cv2.imread(os.path.join(os.path.dirname(os.path.realpath(__file__)), "cal", fn), 0)
         h, w = img.shape[:2]
         found, corners = cv2.findChessboardCorners(img, pattern_size)
         if found:
             term = ( cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_COUNT, 30, 0.1 )
             cv2.cornerSubPix(img, corners, (5, 5), (-1, -1), term)
         if self.debug_dir:
             vis = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR)
             cv2.drawChessboardCorners(vis, pattern_size, corners, found)
         if not found:
             print 'chessboard not found'
             continue
         img_points.append(corners.reshape(-1, 2))
         obj_points.append(pattern_points)
         
         print 'ok'
     print "Calibration is a go... please wait.."
     camera_matrix = np.zeros((3, 3))
     dist_coefs = np.zeros(4)
     img_n = len(img_points)
     rms, camera_matrix, dist_coefs, rvecs, tvecs = cv2.calibrateCamera(obj_points, img_points, (w, h), camera_matrix, dist_coefs)
     return (obj_points, img_points, (w, h), camera_matrix, dist_coefs, rvecs, tvecs)
     """print "RMS:", rms
Example #8
0
def computeCameraMatrix():
    counter = int(x=1)

    h, w = 0, 0
    for fname in images:
        img = cv2.imread(fname)
        gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
        h, w = gray.shape[:2]

        # Find the chess board corners
        ret, corners = cv2.findChessboardCorners(gray, pattern_size, None)
        # If found, add object points, image points (after refining them)
        if ret == True:
            objpoints.append(objp)
            cv2.cornerSubPix(gray, corners, (11, 11), (-1, -1), criteria)

            imgpoints.append(corners)
            # Draw and display the corners
            cv2.drawChessboardCorners(img, pattern_size, corners, ret)
            cv2.imshow("img", img)

            rms, camera_matrix, dist_coefs, rvecs, tvecs = cv2.calibrateCamera(objpoints, imgpoints, (w, h))

            newcameramtx, roi = cv2.getOptimalNewCameraMatrix(camera_matrix, dist_coefs, (w, h), 1, (w, h))

            dst = cv2.undistort(gray, camera_matrix, dist_coefs, None, newcameramtx)
            cv2.imshow("undistort image", dst)
            cv2.waitKey(100)
            counter = counter + 1
        else:
            print ("No corners found on Picture " + str(counter))

    cv2.destroyAllWindows()
def dispImages(img):
    if CALC_TRANSFORM:
        (found, corners) = cv2.findChessboardCorners(
            img,
            boardSize,
            flags = cv2.CALIB_CB_ADAPTIVE_THRESH | cv2.CALIB_CB_FAST_CHECK | \
                    cv2.CALIB_CB_NORMALIZE_IMAGE
        )

        if not found:
            rospy.loginfo("ERROR: cannot find chess board in image")
            return;

        transform = calcTransform(img, corners, boardSize)
        print transform
        transformed = cv2.warpPerspective(img, transform, (img.shape[1],img.shape[0]))

        cv2.drawChessboardCorners(img, boardSize, corners, found)
    else:
        global prev_transform
        transformed = cv2.warpPerspective(img, prev_transform, (img.shape[1], img.shape[0]))

    cv2.imshow('Transformed', transformed)
    cv2.imshow('Plain', img)
    cv2.imwrite('plain.png', img)
    cv2.imwrite('transformed.png', transformed)
def getPoints(inMask, outDir, patternSize):
    """Process all images matching inMask and output debug images to outDir"""
    imageNames = glob.glob(inMask)
    objPoints = []
    imgPoints = []
    # Set the criteria for the cornerSubPix algorithm
    term = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_COUNT, 30, 0.1)
    passed = 0
    # Process all images
    for fileName in imageNames:
        image = cv2.imread(fileName, 0)
        found, corners, patternPoints = findCorners(image, patternSize)
        # Found corners
        if found:
            logger.debug("Chessboard found in: %s" % fileName)
            cv2.cornerSubPix(image, corners, (5, 5), (-1, -1), term)
            vis = cv2.cvtColor(image, cv2.COLOR_GRAY2BGR)
            # Draw the corners
            cv2.drawChessboardCorners(vis, patternSize, corners, found)
            path, name, ext = splitFileName(fileName)
            writeFileName = "%s%s-python.bmp" % (outdir, name)
            logger.debug("Writing debug image: %s" % writeFileName)    
            # Write off marked up images
            cv2.imwrite(writeFileName, vis)
            # Add image and object points to lists for calibrateCamera
            imgPoints.append(corners.reshape(-1, 2))
            objPoints.append(patternPoints)
            passed += 1          
        else:
            logger.warning("Chessboard not found in: %s" % fileName)
    logger.info("Images passed cv2.findChessboardCorners: %d" % passed)
    # We assume all images the same size, so we use last one
    h, w = image.shape[:2]    
    return h, w, objPoints, imgPoints
Example #11
0
def findMarkers(gray):
	# Find the chess board corners
	if marker_checkerboard == True:
		ret, corners = cv2.findChessboardCorners(gray, marker_size,None)
		print 'chess - found corners: ' + str(corners.size) + ' ' + str(ret)
	else:
		ret, corners = cv2.findCirclesGridDefault(gray, marker_size,None,cv2.CALIB_CB_ASYMMETRIC_GRID)
		print 'circles - found corners: ' + str(corners.size) + ' ' + str(ret)
		print corners

	# If found, add object points, image points (after refining them)
	if ret == True: 
		objpoints.append(objp)
		#corners2 = corners
		#cv2.cornerSubPix(gray,corners2,(11,11),(-1,-1),criteria)
		#print (corners2-corners)
		imgpoints.append(corners)
		
		# Draw and display the corners
		cv2.drawChessboardCorners(gray, marker_size, corners,ret)
		cv2.imshow('camera',gray)
		cv2.waitKey(500)
	else:
		print 'Couldn\'t find markers'
		
	return ret
def get_transform():
    # termination criteria
    criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 30, 0.001)
    pattern_size = (9, 6)
    pattern_points = np.zeros( (np.prod(pattern_size), 3), np.float32 )
    pattern_points[:,:2] = np.indices(pattern_size).T.reshape(-1, 2)
    # pattern_points *= square_size
    obj_points = []
    img_points = []
    w,h = 0,0

    for fname in os.listdir('chessboard'):
        if fname[0] == '.': continue
        img = cv2.imread('chessboard/' + fname)
        gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
        h, w = img.shape[:2]
        found, corners = cv2.findChessboardCorners(gray, pattern_size)
        print found
        if found:
            corners2 = cv2.cornerSubPix(gray,corners,(11,11),(-1,-1),criteria)
        
            img_points.append(corners.reshape(-1, 2))
            obj_points.append(pattern_points)
        
            vis = cv2.cvtColor(gray, cv2.COLOR_GRAY2BGR)
            cv2.drawChessboardCorners(vis, pattern_size, corners, found)
        
            # cv2.imshow('img', vis)
            # cv2.waitKey(500)

    ret, mtx, dist, rvecs, tvecs = cv2.calibrateCamera(obj_points, img_points, (w,h), None, None)
    newcameramtx, roi = cv2.getOptimalNewCameraMatrix(mtx,dist,(w,h),1,(w,h))
    return mtx, roi, newcameramtx, dist
def find_board_corners(board_directories):
    """Returns an array of chessboard corners in an image from each distance of 50cm, 100cm
    and 450cm in that order."""

    print "Finding board corners..."
    flags = cv2.CALIB_CB_ADAPTIVE_THRESH
    pattern_size = (7,5)
    image_coords = []
    corner_images = []
    for directory in board_directories:
        img_name = 'cam1_frame_1.bmp'
        print "Finding corners in", os.path.join(directory, img_name)
        #board_image = cv2.imread(os.path.join(directory, img_name), cv2.CV_LOAD_IMAGE_COLOR)
        board_image = cv2.imread(os.path.join(directory, img_name),0)
        (pattern_was_found, corners) = cv2.findChessboardCorners(board_image, pattern_size, flags=flags)
        if pattern_was_found:
            cv2.cornerSubPix(board_image, corners, (4, 4),  (-1, -1), (cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 100, 1e-7))
        if not pattern_was_found and corners == None:
            try:
                corners = np.loadtxt(os.path.join(directory, 'cam1_frame_1_corners.txt'),delimiter=',')
                pattern_was_found = True
                corners = corners.astype('float32')
            except IndexError:
                print 'No corners found! Please find them yourself in Photoshop'
                sys.exit(-1)
        if not pattern_was_found and not corners==None:
            print "Not all corners found! Find them yourself!"
            corners = CornerPicker.main(board_image.copy(), corners)
        corner_image = board_image.copy()
        corners = corners.squeeze()
        cv2.drawChessboardCorners(corner_image, pattern_size, corners, pattern_was_found)
        image_coords.append(corners)
        corner_images.append(corner_image)

    return np.concatenate(image_coords), corner_images
def calibrate_camera_interactive(images, objp, boardSize):
    # Arrays to store object points and image points from all the images.
    objectPoints = []    # 3d point in real world space
    imagePoints = []    # 2d points in image plane

    test_image = cv2.imread(images[0])
    imageSize = (test_image.shape[1], test_image.shape[0])

    # Read images
    for fname in images:
        img = cv2.imread(fname)
        ret, corners = cvh.extractChessboardFeatures(img, boardSize)

        # If chessboard corners are found, add object points and image points
        if ret == True:
            objectPoints.append(objp)
            imagePoints.append(corners)

            # Draw and display the corners
            cv2.drawChessboardCorners(
                    img, boardSize, corners, ret )
            cv2.imshow("img", img)
            cv2.waitKey(100)

    # Calibration
    reproj_error, cameraMatrix, distCoeffs, rvecs, tvecs = cv2.calibrateCamera(
            objectPoints, imagePoints, imageSize )
    distCoeffs = distCoeffs.reshape((-1))    # convert to vector
    
    return reproj_error, cameraMatrix, distCoeffs, rvecs, tvecs, \
            objectPoints, imagePoints, imageSize
Example #15
0
def find_checkerboard_corners(image, interior_corners, side_length, sub_pixel = False):
    """
    Method: find_checkerboard_corners

    Parameters:
    image - ndarray of the image data
    interior_corners - dimensions of the interior corners
    side_length - length of a side of a checkerboard square

    Return: A tuple of the form (pattern_found, corners, imageData), where pattern_found is a
    boolean representing if the checkerboard pattern was found,  corners is a
    numpy array of the corner coordinates, and imageData is a numpy array of the image with
    the corners drawn on it
    """
    imageData = image.data.copy()
    
    pattern_found, corners = cv2.findChessboardCorners(imageData, interior_corners)

    if pattern_found:
        # reshpae corners because comes out of findChessboardCorners in an odd
        # fashion
        corners = np.reshape(corners, (corners.shape[0], corners.shape[2]))

        if sub_pixel:
            raise NotImplementedExcpetion();
    
        cv2.drawChessboardCorners(imageData, interior_corners, corners, pattern_found)

    return pattern_found, corners, imageData
Example #16
0
 def drawChessboard(self, img=None):
     '''
     draw a grid fitting to the last added image 
     on this one or an extra image
     img == None
         ==False -> draw chessbord on empty image
         ==img
     ''' 
     if img is None:
         img = self.img
     elif type(img) == bool and img == False:
         img = np.zeros(shape=(self.img.shape), dtype=self.img.dtype)
     else:
         img = imread(img, dtype='uint8')
     gray = False  
     if img.ndim == 2:
         gray=True
         #need a color 8 bit image
         img = cv2.cvtColor(img,cv2.COLOR_GRAY2BGR)
     # Draw and display the corners
     cv2.drawChessboardCorners(img, self.opts['size'], 
                               self.opts['imgPoints'][-1], 
                               self.opts['foundPattern'][-1])
     if gray:
         img = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) 
     return img
Example #17
0
    def processImage(fn):
        print('processing %s... ' % fn)
        img = cv.imread(fn, 0)
        if img is None:
            print("Failed to load", fn)
            return None

        assert w == img.shape[1] and h == img.shape[0], ("size: %d x %d ... " % (img.shape[1], img.shape[0]))
        found, corners = cv.findChessboardCorners(img, pattern_size)
        if found:
            term = (cv.TERM_CRITERIA_EPS + cv.TERM_CRITERIA_COUNT, 30, 0.1)
            cv.cornerSubPix(img, corners, (5, 5), (-1, -1), term)

        if debug_dir:
            vis = cv.cvtColor(img, cv.COLOR_GRAY2BGR)
            cv.drawChessboardCorners(vis, pattern_size, corners, found)
            _path, name, _ext = splitfn(fn)
            outfile = os.path.join(debug_dir, name + '_chess.png')
            cv.imwrite(outfile, vis)

        if not found:
            print('chessboard not found')
            return None

        print('           %s... OK' % fn)
        return (corners.reshape(-1, 2), pattern_points)
Example #18
0
File: mycv.py Project: yieku/Python
def loadImagePoints(imageDir, width, height, debug=False):
    imageSize = (0, 0)
    imagePoints = []
    nimages = 0
    images = cvtools.loadImages(imageDir)
    for imageName in images:
        # 读取图片时转换为灰度图像
        im = cv2.imread(imageName, 0)
        if debug:
            print "正在处理图像:",imageName
        imageSize = (im.shape[1], im.shape[0])
        retval, corners = cv2.findChessboardCorners(im, (width, height))
        if retval:
            cv2.cornerSubPix(im, corners, (11, 11), (-1, -1), (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 30, 0.1))
            nimages = nimages + 1
            if debug:
                im = cv2.cvtColor(im, cv2.COLOR_GRAY2BGR)
                cv2.drawChessboardCorners(im, (width, height), corners, retval)
                cv2.namedWindow(imageName, cv2.WINDOW_NORMAL)
                cv2.imshow(imageName, im)
                # print corners.reshape(-1,2)
                cv2.waitKey()
            imagePoints.append(corners.reshape(-1, 2))
    cv2.destroyAllWindows()
    print "imageSize is", imageSize;
    return imagePoints, nimages, imageSize
	def processImage(self, image_msg):
		if self.lock.testandset():
			vehicle_detected_msg_out = BoolStamped()
			try:
				image_cv=self.bridge.imgmsg_to_cv2(image_msg,"bgr8")
			except CvBridgeError as e:
				print e
			start = rospy.Time.now()
			params = cv2.SimpleBlobDetector_Params()
			params.minArea = self.blobdetector_min_area
			params.minDistBetweenBlobs = self.blobdetector_min_dist_between_blobs
			simple_blob_detector = cv2.SimpleBlobDetector(params)
			(detection, corners) = cv2.findCirclesGrid(image_cv,
					self.circlepattern_dims, flags=cv2.CALIB_CB_SYMMETRIC_GRID,
					blobDetector=simple_blob_detector)
			elapsed_time = (rospy.Time.now() - start).to_sec()
			self.pub_time_elapsed.publish(elapsed_time)
			vehicle_detected_msg_out.data = detection
			self.pub_detection.publish(vehicle_detected_msg_out)
			if self.publish_circles:
				cv2.drawChessboardCorners(image_cv, 
						self.circlepattern_dims, corners, detection)
				image_msg_out = self.bridge.cv2_to_imgmsg(image_cv, "bgr8")
				self.pub_circlepattern_image.publish(image_msg_out)
			self.lock.unlock()
Example #20
0
File: calib.py Project: s-shin/aroi
def get_chessboard_points(
        img, pattern_rows, pattern_columns, square_size=1, draw=False):
    """
    :param img: input image
    :param pattern_rows: the number of rows in chessboard
    :param pattern_columns: the number of columns in chessboard
    :param square_size: each square size of chessboard
    :param draw: draw chessboard corners to ``img``
    """
    size = (pattern_rows, pattern_columns)
    found, corners = cv2.findChessboardCorners(img, size)
    if not found:
        return None
    if draw:
        cv2.drawChessboardCorners(img, size, corners, found)
    cv2.cornerSubPix(
        cv2.cvtColor(img, cv2.COLOR_BGR2GRAY),
        corners, (3, 3), (-1, -1),
        (cv2.TERM_CRITERIA_MAX_ITER | cv2.TERM_CRITERIA_EPS, 20, 0.03))
    # (y, 1, 2) -> (y, 2) by reshape(-1, 2)
    img_points = corners.reshape(-1, 2)
    obj_points = np.zeros((np.prod(size), 3), np.float32)
    obj_points[:,:2] = np.indices(size).T.reshape(-1, 2)
    obj_points *= square_size
    return img_points, obj_points
def find_circle_centers(board_directories):
    """Returns an array of chessboard corners in an image from each distance of 50cm, 100cm
    and 450cm in that order."""

    print "Finding board corners..."
    pattern_size = (4,11)
    image_coords = []
    corner_images = []
    for directory in board_directories:
        img_name = 'cam1_frame_1.bmp'
        print "Finding circles in", os.path.join(directory, img_name)
        board_image = cv2.imread(os.path.join(directory, img_name),1)  # CHANGED: Loading as RGB
        [pattern_was_found,corners] = cv2.findCirclesGridDefault(board_image,pattern_size,flags=cv2.CALIB_CB_ASYMMETRIC_GRID)
        corners = corners.squeeze()  # CHANGED: moved squeeze to before corner checking instead of after
        if not pattern_was_found and corners == None:
            try:
                corners = np.loadtxt(os.path.join(directory, 'cam1_frame_1_corners.txt'),delimiter=',')
                pattern_was_found = True
                corners = corners.astype('float32')
            except IndexError:
                print 'No corners found! Please find them yourself in Photoshop'
                sys.exit(-1)
        if not pattern_was_found and not corners==None:
            print "Not all corners found! Find them yourself!"
            corners = CornerPicker.main(board_image.copy(), corners, pattern_size)
        corner_image = board_image.copy()
        cv2.drawChessboardCorners(corner_image, pattern_size, corners, pattern_was_found)
        cv2.imwrite('./tmp.png',corner_image)
        image_coords.append(corners)
        corner_images.append(corner_image)

    return np.concatenate(image_coords), corner_images
 def add_sample(self, image, debug=False):
     if not self._image_size:
         self._image_size = image.shape[:2]
     found, corners = self.get_chessboard_corners(image, self._pattern_size)
     if found:
         if len(self._image_points) == 0:
             self._image_points.append(corners)
             rvec, tvec = self.get_pose(self.create_pattern_points(), corners)
             self._circle_points.append(tvec.reshape(3))
         else:
             flipped_corners = self.identify_flip(corners)
             self._image_points.append(flipped_corners)
             rvec, tvec = self.get_pose(self.create_pattern_points(), flipped_corners)
             self._circle_points.append(tvec.reshape(3))
         if debug:
             color_image = cv2.cvtColor(image, cv2.COLOR_GRAY2BGR)
             cv2.drawChessboardCorners(color_image, self._pattern_size, corners, found)
             cv2.imshow(self.win_name, color_image)
             cv2.waitKey(2000)
             print('Next chessboard')
     else:
         cv2.imshow(self.win_name, image)
         cv2.waitKey()
         print('Chessboard not found')
     return found
    def calibration(self):

        for fname in self.img_list:
            img = cv2.imread(fname)
            grey = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

            ret, corners = cv2.findChessboardCorners(grey, self.size, None)
            cv2.drawChessboardCorners(img, self.size, corners,ret)

            # if found, show imgs
            if ret:
                criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 30, 0.001)
                cv2.cornerSubPix(grey,corners,(11,11),(-1,-1),criteria)
                self.imgpoints.append(corners)
                self.objpoints.append(self.objp)
                self.img_list_detected.append(fname)
                print fname

            cv2.imshow('img',img)
            cv2.waitKey(500)

        cv2.destroyAllWindows()


        # Step 2: Calibration
        # shape[::-1]: (480,640) => (640,480)
        ret, cmx, dist, rvecs, tvecs = cv2.calibrateCamera(
            self.objpoints, self.imgpoints, grey.shape[::-1],None,None)
        print cmx
        print dist
        # save calibration result
        np.savez('./calibFile/calib.npz', cmx=cmx, dist=dist, rvecs=rvecs, tvecs=tvecs)
Example #24
0
 def _show_corners(self, image, corners):
     """Show chessboard corners found in image."""
     temp = image
     cv2.drawChessboardCorners(temp, (self.rows, self.columns), corners, True)
     window_name = "Chessboard"
     cv2.imshow(window_name, temp)
     if cv2.waitKey(0):
         cv2.destroyWindow(window_name)
Example #25
0
def PreviewChessboard( image ) :
	# Find the chessboard corners on the image
	found, corners = cv2.findChessboardCorners( image, pattern_size, flags = cv2.CALIB_CB_FAST_CHECK )
#	found, corners = cv2.findCirclesGridDefault( image, pattern_size, flags = cv2.CALIB_CB_ASYMMETRIC_GRID )
	# Draw the chessboard corners on the image
	if found : cv2.drawChessboardCorners( image, pattern_size, corners, found )
	# Return the image with the chessboard if found
	return image
Example #26
0
        def captureUndistortMap(self):
                img = cv.QueryFrame(self.capture)
                cv.ShowImage(self.windowname,img)
                [found corners] = cv2.findCirclesGrid(img, self.calibtarget_dim, flags = cv2.CALIB_CB_ASYMMETRIC_GRID)
                cv2.drawChessboardCorners(img, self.calibtarget_dim, corners, found)

                if cv.WaitKey(10) & 0xff == 'c':
                        return (corners)
Example #27
0
def detect_points_from_pattern(img_mask, is_thermal=False):
    square_size = 2.15
    pattern_size = (9, 6)
    pattern_points = np.zeros( (np.prod(pattern_size), 3), np.float32 )
    pattern_points[:,:2] = np.indices(pattern_size).T.reshape(-1, 2)
    pattern_points *= square_size

    from glob import glob
    import re
    img_names = glob(img_mask)
    img_names = sorted(img_names, key=lambda name: re.search('(left|right|top|bottom|thermal)(.*)\.(jpg|JPG)', name).groups()[1])
#        debug_dir = None
    obj_points = []
    img_points = []
    valid = []
#         h, w = 0, 0
    for i, fn in enumerate(img_names):
        print 'processing %s...' % fn,
        img_rgb = cv2.imread(fn)
#             h, w = img.shape[:2]
        cv2.imshow('original',img_rgb)
        img = cv2.cvtColor(img_rgb, cv2.COLOR_RGB2GRAY)
        if is_thermal:
            cv2.imshow('original',img)
            cv2.waitKey()
            cv2.imshow('negative', 255-img)
            cv2.waitKey()
            found, corners = cv2.findChessboardCorners(255-img, pattern_size)
#                                         flags=cv2.cv.CV_CALIB_CB_ADAPTIVE_THRESH)
#                     flags=cv2.cv.CV_CALIB_CB_NORMALIZE_IMAGE)
        else:
            found, corners = cv2.findChessboardCorners(img, pattern_size)
        
        if found:
            term = ( cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_COUNT, 30, 0.1 )
            cv2.cornerSubPix(img, corners, (5, 5), (-1, -1), term)
            if is_thermal:
                vis = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR)
            else:
                vis = img_rgb
            cv2.drawChessboardCorners(vis, pattern_size, corners, found)
            cv2.imshow('vis', vis)
            cv2.waitKey()
            if is_thermal:
                cv2.imwrite('/home/yuncong/Desktop/thermal_debug'+str(i)+'.png', vis)
            else:
                cv2.imwrite('/home/yuncong/Desktop/debug'+str(i)+'.png', vis)

        else:
            print 'chessboard not found'
            continue
        img_points.append(corners.reshape(-1, 2))
        obj_points.append(pattern_points)
        valid.append(i)

        print 'ok'
    return img_points, obj_points, valid
Example #28
0
def save_image_with_grid_drawn(n,img,dims,points,found,check_img_folder):
	'''save a copy of the image with circle grid points identified	
	'''
				
	drawn_boards = img.copy()
	cv2.drawChessboardCorners(drawn_boards, dims, points, found)
	cv2.imwrite(check_img_folder + 'images_used/' +  'cam1_frame_'+str(n+1)+'.bmp', drawn_boards)

	return None
def calibrate_relative_poses_interactive(image_sets, cameraMatrixs, distCoeffss, imageSizes, boardSizes, board_scales, board_rvecs, board_tvecs):
    """
    Make an estimate of the relative poses (as 4x4 projection matrices) between many cameras.
    Base these relative poses to the first camera.
    
    Each camera should be looking at its own chessboard,
    use different sizes of chessboards if a camera sees chessboard that are not associated with that camera.
    'board_scales' scales the chessboard-units to world-units.
    'board_rvecs' and 'board_tvecs' transform the rescaled local chessboard-coordinates to world-coordinates.
    
    The inverse of the reprojection error is used for weighting.
    """
    num_cams = len(image_sets)
    num_images = len(image_sets[0])
    reproj_error_max = 0
    
    # Preprocess object-points of the different boards
    board_objps = []
    for boardSize, board_scale, board_rvec, board_tvec in zip(
            boardSizes, board_scales, board_rvecs, board_tvecs ):
        objp = np.ones((np.prod(boardSize), 4))
        objp[:, 0:3] = calibration_tools.grid_objp(boardSize) * board_scale
        objp = objp.dot(trfm.P_from_R_and_t(cvh.Rodrigues(board_rvec), np.array(board_tvec).reshape(3, 1))[0:3, :].T)
        board_objps.append(objp)
    
    # Calculate all absolute poses
    Ps = np.zeros((num_images, num_cams, 4, 4))
    weights = np.zeros((num_images, 1, 1, 1))
    for i, images in enumerate(zip(*image_sets)):
        reproj_error = 0
        for c, (image, cameraMatrix, distCoeffs, imageSize, boardSize, board_objp) in enumerate(zip(
                images, cameraMatrixs, distCoeffss, imageSizes, boardSizes, board_objps )):
            img = cv2.imread(image)
            ret, corners = cvh.extractChessboardFeatures(img, boardSize)
            if not ret:
                print ("Error: Image '%s' didn't contain a chessboard of size %s." % (image, boardSize))
                return False, None
            
            # Draw and display the corners
            cv2.drawChessboardCorners(
                    img, boardSize, corners, ret )
            cv2.imshow("img", img)
            cv2.waitKey(100)
            
            ret, rvec, tvec = cv2.solvePnP(board_objp, corners, cameraMatrix, distCoeffs)
            Ps[i, c, :, :] = trfm.P_from_R_and_t(cvh.Rodrigues(rvec), tvec)
            reproj_error = max(reprojection_error(    # max: worst case
                    [board_objp], [corners], cameraMatrix, distCoeffs, [rvec], [tvec] )[1], reproj_error)
        reproj_error_max = max(reproj_error, reproj_error_max)
        weights[i] = 1. / reproj_error
    
    # Apply weighting on Ps, and rebase against first camera
    Ps *= weights / weights.sum()
    Ps = Ps.sum(axis=0)
    Pref_inv = trfm.P_inv(Ps[0, :, :])    # use first cam as reference
    return True, [P.dot(Pref_inv) for P in Ps], reproj_error_max
Example #30
0
def add_chessboard_corners(img,rows=8,cols=6):
  gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
  ret, corners = cv2.findChessboardCorners(gray, (rows,cols),None)

  # If found, add object points, image points (after refining them)
  if ret == True:
      criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 30, 0.001)
      cv2.cornerSubPix(gray,corners,(11,11),(-1,-1),criteria)
      # Draw and display the corners
      cv2.drawChessboardCorners(img, (rows,cols), corners,ret)
Example #31
0
                                          cv2.COLOR_BGR2GRAY)
    ret, corners = cv2.findCirclesGrid(
        im_with_keypoints, (4, 11), None,
        flags=cv2.CALIB_CB_ASYMMETRIC_GRID)  # Find the circle grid

    if ret == True:
        objpoints.append(
            objp)  # Certainly, every loop objp is the same, in 3D.

        corners2 = cv2.cornerSubPix(im_with_keypoints_gray, corners, (11, 11),
                                    (-1, -1),
                                    criteria)  # Refines the corner locations.
        imgpoints.append(corners2)

        # Draw and display the corners.
        im_with_keypoints = cv2.drawChessboardCorners(img, (4, 11), corners2,
                                                      ret)
        found += 1

    cv2.imshow("img", im_with_keypoints)  # display
    cv2.waitKey(10)

# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()

ret, mtx, dist, rvecs, tvecs = cv2.calibrateCamera(objpoints, imgpoints,
                                                   gray.shape[::-1], None,
                                                   None)

# It's very important to transform the matrix to list.
data = {
Example #32
0
 def draw_object(self, img):
     """Draw the object if found into img"""
     cv2.drawChessboardCorners(img, self.patternSize, self.corners,
                               self.patternFound)
for fname in images:
    img = cv2.imread(fname)
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

    # Find the chess board corners
    ret, corners = cv2.findChessboardCorners(gray, (args.m, args.n), None)

    # If found, add object points, image points (after refining them)
    if ret == True:
        objpoints.append(objp)

        cv2.cornerSubPix(gray, corners, (11, 11), (-1, -1), criteria)
        imgpoints.append(corners)

        # Draw and display the corners
        cv2.drawChessboardCorners(img, (args.m, args.n), corners, ret)
print("success :", len(objpoints))

cv2.destroyAllWindows()

ret, mtx, dist, rvecs, tvecs = cv2.calibrateCamera(objpoints, imgpoints,
                                                   gray.shape[::-1], None,
                                                   None)

print(ret)
print(mtx)
print(dist)
print(rvecs)
print(tvecs)

utils.create_muldir('results', 'results/intrinsic',
Example #34
0
    def load_calib_imgs(self, img_path, clean=False):
        """
        Load calibration JPGs from directory & get chessboard. Be sure to
        call init_chessboard() first.

        INPUTS
            img_path -- str -- path to calibration jpegs
            (optional)
            clean -- bool -- Whether or not to go through sanitization; False

        EXCEPTIONS
            raises RuntimeError: when chessboard hasn't been properly
                initialized by constructor or `init_chessboard`.

        ALGORITHM
            Finds all files ending in '.jpg' and loads them. Objp should have
            been handled in init_chessboard()
        """
        if type(img_path) != str:
            raise TypeError('img_path must be str')
        if not (type(clean) == bool or clean in (0, 1)):
            raise TypeError('clean must be bool')
        if self.img_arr is not None or self.calibpath is None:
            raise RuntimeError('Did you call init_chessboard() first?')

        # Find calibration images and process
        potential_files = listdir(img_path)
        fn_imgs = [
            img_path + '/' + f for f in potential_files
            if f[-4:].lower() == '.jpg'
        ]
        imageshape = cv_imread(fn_imgs[0]).shape
        self.h = imageshape[0]
        self.w = imageshape[1]

        # Save images in calib path
        rawpath = self.calibpath + '/raw'  # copy to current calibration dir
        if rawpath != img_path:
            if not isdir(rawpath):
                if os_exists(rawpath):
                    os_remove(rawpath)
                mkdir(rawpath)
            for f in fn_imgs:
                copyfile(f, rawpath + '/' + basename(f))
        # corners frames for debugging
        cpath = self.calibpath + '/corners'  # save drawn corners for debug
        mkdir(cpath)
        logging.info('saving raw frames to \'%s\'' % rawpath)
        logging.info('saving corners frames to \'%s\'' % cpath)

        # Load images
        self.img_arr = zeros((self.h, self.w, 1, len(fn_imgs)), uint8)
        for i in range(len(fn_imgs)):
            f = fn_imgs[i]
            if imageshape[-1] == 3:
                self.img_arr[..., i] = cvtColor(cv_imread(f),
                                                COLOR_RGB2GRAY)[..., newaxis]
            else:
                self.img_arr[..., i] = cv_imread(f)

        # Chessboard computations
        logging.debug('finding chessboards...')
        for i in range(self.img_arr.shape[-1]):
            gray = self.img_arr[..., i].copy()
            corners = self._find_chessboard(gray)
            if corners is None:
                logging.error('Failed to find chessboard at frame \'%s\'' %
                              str(i + 1).zfill(5))
                continue
            self.corners_arr.append(corners)
            self.objpoints.append(self.objp)  # 3d position (same for all?)

            # save chessboard images for debugging
            #   cvt to rgb for color chessboard
            fn_c = cpath + ('/f%s' % str(i + 1).zfill(5)) + '.jpg'
            gray_color = cvtColor(gray, COLOR_GRAY2RGB)
            img_corners = drawChessboardCorners(gray_color, self.boardsize,
                                                corners, 1)
            cv_imwrite(fn_c, img_corners, (IMWRITE_JPEG_QUALITY, 100))

        # Go through chessboards to make sure okay
        if clean:
            basepath = dirname(cpath)
            self.clean_calib_imgs(basepath=basepath)
        logging.debug('load_calib_imgs() done!')
def __start_video_calibration(videoFN, cols, rows, skip, dim, objp, objpoints,
                              imgpoints, matrix_filename, distortion_filename,
                              start_video):
    """
    Calculate the calibration matrix and dist array for a given video and output these results to there speciefied files.

    Parameters
    ----------
    - videoFN -- filename of the video.
    - cols -- number of colums on the calibratrion paper.
    - rows -- number of rows on the calibratrion paper.
    - skip -- number of frames to skip when searching for the chessboard corners. (lower = more longer calculation time)
    - dim -- the dimention of the black squares in tht calibration paper in mm.
    - objp -- array containing the objectpoints.
    - objpoints -- array containing the objectpoints.
    - imgpoints -- array containing the imgpoints.
    - matrix_filename -- filename for the matix parameter
    - distortion_filename -- filename for the distortion parameter
    - start_video -- output a video for debuging.

    Returns
    -------
    Nothing
    """

    criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, dim, 0.001)

    cap = cv2.VideoCapture(videoFN)
    if not cap.isOpened():
        logger.warning("Videofile not found")
        return

    count = 0

    logger.info("Start analysing video")
    while cap.isOpened():

        success, frame = cap.read()

        if success:
            gray_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
            ret, corners = cv2.findChessboardCorners(gray_frame, (cols, rows),
                                                     None)

            if ret:
                count += 1
                objpoints.append(objp)

                corners2 = cv2.cornerSubPix(gray_frame, corners, (11, 11),
                                            (-1, -1), criteria)

                imgpoints.append(corners2)
                if start_video:
                    cv2.drawChessboardCorners(frame, (cols, rows), corners2,
                                              ret)
                    cv2.imshow('frame', frame)

        else:
            break

        for i in range(0, skip):
            cap.read()

    logger.info("Done Analysing video, got {} usable images".format(count))

    cap.release()
    logger.debug("Releasing video")

    logger.debug("Starting calibration calculations")
    ret, mtx, dist, rvecs, tvecs = cv2.calibrateCamera(objpoints, imgpoints,
                                                       gray_frame.shape[::-1],
                                                       None, None)
    logger.info("Got matrix")

    logger.info("Got dist")

    logger.info("Writing matrix to {}".format(matrix_filename))
    np.savetxt(matrix_filename, mtx, delimiter=',')
    logger.info("Writing dist to {}".format(distortion_filename))
    np.savetxt(distortion_filename, dist, delimiter=',')
Example #36
0
    def _process_frame(self, frame):
        """Processes each frame

            If recording mode is on (self.recording==True), this method will
            perform all the hard work of the camera calibration process:
            - for every frame, until enough frames have been processed:
                - find the chessboard corners
                - refine the coordinates of the detected corners
            - after enough frames have been processed:
                - estimate the intrinsic camera matrix and distortion
                  coefficients

            :param frame: current RGB video frame
            :returns: annotated video frame showing detected chessboard corners
        """
        # if we are not recording, just display the frame
        if not self.recording:
            return frame

        # else we're recording
        img_gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY).astype(np.uint8)

        if self.record_cnt < self.record_min_num_frames:
            # need at least some number of chessboard samples before we can
            # calculate the intrinsic matrix
            ret, corners = cv2.findChessboardCorners(img_gray,
                                                     self.chessboard_size,
                                                     None)

            if ret:
                cv2.drawChessboardCorners(frame, self.chessboard_size, corners,
                                          ret)

                # refine found corners
                criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER,
                            30, 0.01)
                cv2.cornerSubPix(img_gray, corners, (9, 9), (-1, -1), criteria)

                self.obj_points.append(self.objp)
                self.img_points.append(corners)
                self.record_cnt += 1

        else:
            # we have already collected enough frames, so now we want to
            # calculate the intrinsic camera matrix (K) and the distortion
            # vector (dist)
            print "Calibrating..."
            ret, K, dist, rvecs, tvecs = cv2.calibrateCamera(
                self.obj_points, self.img_points,
                (self.imgHeight, self.imgWidth), None, None)
            print "K=", K
            print "dist=", dist

            # double-check reconstruction error (should be as close to zero as
            # possible)
            mean_error = 0
            for i in xrange(len(self.obj_points)):
                img_points2, _ = cv2.projectPoints(self.obj_points[i],
                                                   rvecs[i], tvecs[i], K, dist)
                error = cv2.norm(self.img_points[i], img_points2,
                                 cv2.NORM_L2) / len(img_points2)
                mean_error += error

            print "mean error=", mean_error

            self.recording = False
            self._reset_recording()
            self.button_calibrate.Enable()

        return frame
Example #37
0
def calibrate_camera(camera_folder='camera_cal',
                     output_folder='output_images',
                     test_folder='test_images'):
    '''
    Camera calibration
    '''
    if (os.path.isfile('calibration.p')):
        calibration = pickle.load(open('calibration.p', 'rb'))
        ret, mtx, dist, rvecs, tvecs = calibration
    else:
        criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 30,
                    0.001)
        objpoints = []
        imgpoints = []
        objp = np.zeros((6 * 9, 3), np.float32)
        objp[:, :2] = np.mgrid[0:9, 0:6].T.reshape(-1, 2)

        camera_images = glob.glob(camera_folder + '/*.jpg')
        test_images = glob.glob(test_folder + '/*.jpg')

        for imagefile in camera_images:
            img = cv2.imread(imagefile)
            gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
            ret, corners = cv2.findChessboardCorners(gray, (9, 6), None)

            if ret == True:
                objpoints.append(objp)
                corners2 = cv2.cornerSubPix(gray, corners, (11, 11), (-1, -1),
                                            criteria)
                imgpoints.append(corners2)
                img_board = cv2.drawChessboardCorners(img, (9, 6), corners2,
                                                      ret)

                filename = os.path.basename(imagefile)
                cv2.imwrite('./' + output_folder + '/camera/calib/' + filename,
                            img_board)

        calibration = cv2.calibrateCamera(objpoints, imgpoints,
                                          gray.shape[::-1], None, None)
        ret, mtx, dist, rvecs, tvecs = calibration
        pickle.dump(calibration, open('calibration.p', 'wb'))

        for imagefile in camera_images:
            filename = os.path.basename(imagefile)
            img = cv2.imread(imagefile)
            h, w = img.shape[:2]
            newcameramtx, roi = cv2.getOptimalNewCameraMatrix(
                mtx, dist, (w, h), 1, (w, h))
            dst = cv2.undistort(img, mtx, dist, None, newcameramtx)
            x, y, w, h = roi
            dst = dst[y:y + h, x:x + w]
            cv2.imwrite('./' + output_folder + '/camera/undist/' + filename,
                        dst)

        for testfile in test_images:
            filename = os.path.basename(testfile)
            img = cv2.imread(testfile)
            h, w = img.shape[:2]
            dst = cv2.undistort(img, mtx, dist)

            # Draw perspective polygon
            pts = np.array([[516, 460], [756, 460], [1200, 720], [100, 720]],
                           np.int32)
            pts = pts.reshape((-1, 1, 2))
            cv2.polylines(dst, [pts], True, (0, 0, 255))

            cv2.imwrite('./' + output_folder + '/test/' + filename, dst)

    return ret, mtx, dist, rvecs, tvecs
Example #38
0
    h, w = 0, 0
    for fn in img_names:
        print('processing %s...' % fn, )
        img = cv2.imread(fn, 0)
        if img is None:
            print("Failed to load", fn)
            continue

        h, w = img.shape[:2]
        found, corners = cv2.findChessboardCorners(img, pattern_size)
        if found:
            term = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_COUNT, 30, 0.1)
            cv2.cornerSubPix(img, corners, (5, 5), (-1, -1), term)
        if debug_dir:
            vis = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR)
            cv2.drawChessboardCorners(vis, pattern_size, corners, found)
            path, name, ext = splitfn(fn)
            cv2.imwrite('%s/%s_chess.bmp' % (debug_dir, name), vis)
        if not found:
            print('chessboard not found')
            continue
        img_points.append(corners.reshape(-1, 2))
        obj_points.append(pattern_points)

        print('ok')

    rms, camera_matrix, dist_coefs, rvecs, tvecs = cv2.calibrateCamera(
        obj_points, img_points, (w, h), None, None)
    np.savez('../camera_info',
             camera_matrix=camera_matrix,
             dist_coefs=dist_coefs.ravel())
Example #39
0
    def perform_calibration(self):

        print "Begin Calibration"

        criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 30,
                    0.001)
        success = 0

        #===========================STEP 1: PREPARE CALIBRATION PARAMETERS===========================
        print "Step 1: Preparing parameters"

        if (self.pattern_type == "CHESS_BOARD"):
            objp = np.zeros((6 * 9, 3), np.float32)
            objp[:, :2] = np.mgrid[0:9, 0:6].T.reshape(-1, 2)
        elif (self.pattern_type == "CIRCLE_GRID"):
            pattern_width = 7
            pattern_height = 7
            #~ pattern_separation = 0.01594
            pattern_separation = 0.0185
            objp = np.zeros((pattern_width * pattern_height, 3), np.float32)

            for i in range(pattern_width):
                for j in range(pattern_height):
                    objp[i * pattern_height + j, 0] = pattern_separation * i
                    objp[i * pattern_height + j, 1] = pattern_separation * j
                    objp[i * pattern_height + j, 2] = 0

        objpoints = []  # 3d point in real world space
        imgpoints = []  # 2d points in image plane.

        for n in range(20):
            print "Loading frame %d" % n
            #~ file_name = self.calibration_file + str(n) + ".jpg"
            file_name = self.calibration_file + str(n) + ".png"
            frame = cv2.imread(file_name)

            if frame is None:
                break

            gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

            # Find the chess board corners
            if (self.pattern_type == "CHESS_BOARD"):
                ret, corners = cv2.findChessboardCorners(gray, (9, 6), None)
            elif (self.pattern_type == "CIRCLE_GRID"):
                ret, corners = cv2.findCirclesGrid(gray, (7, 7), None)

            # If found, add object points, image points (after refining them)
            if ret == True:
                objpoints.append(objp)
                cv2.cornerSubPix(gray, corners, (11, 11), (-1, -1), criteria)
                imgpoints.append(corners)

                if self.debug == True:
                    # Draw and display the corners
                    if (self.pattern_type == "CHESS_BOARD"):
                        cv2.drawChessboardCorners(frame, (9, 6), corners, ret)
                    elif (self.pattern_type == "CIRCLE_GRID"):
                        cv2.drawChessboardCorners(frame, (7, 7), corners, ret)
                    cv2.imshow('img', frame)
                    cv2.waitKey(0)

                success = success + 1

            n = n + 1

        #===========================STEP 2: PERFORM CALIBRATION===========================
        print "Number of successful pattern detection: %d" % success

        if (success > 5):
            print "Step 2: Begin calibration"
            self.reproj_err, self.mtx, self.dist, rvecs, tvecs = cv2.calibrateCamera(
                objpoints, imgpoints, gray.shape[::-1], None, None)
            self.calibrated = True
Example #40
0
    img = cv2.imread(fname)
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    size = gray.shape[::-1]
    ret, corners = cv2.findChessboardCorners(gray, (29, 17), None)

    if ret:
        obj_points.append(objp)

        corners2 = cv2.cornerSubPix(gray, corners, (5, 5), (-1, -1),
                                    criteria)  # 在原角点的基础上寻找亚像素角点
        if corners2 is not None:
            img_points.append(corners2)
        else:
            img_points.append(corners)

        cv2.drawChessboardCorners(img, (7, 6), corners,
                                  ret)  # 记住,OpenCV的绘制函数一般无返回值
        cv2.imshow('img', img)
        cv2.waitKey(50)

print(len(img_points))
cv2.destroyAllWindows()

# 标定
ret, mtx, dist, rvecs, tvecs = cv2.calibrateCamera(obj_points, img_points,
                                                   size, None, None)

print("ret:", ret)
print("mtx:\n", mtx)  # 内参数矩阵
print("dist:\n", dist)  # 畸变系数   distortion cofficients = (k_1,k_2,p_1,p_2,k_3)
print("rvecs:\n", rvecs)  # 旋转向量  # 外参数
print("tvecs:\n", tvecs)  # 平移向量  # 外参数
objpoints = []  # 在世界坐标系中的三维点
imgpoints = []  # 在图像平面的二维点

images = glob.glob('calib/*.jpg')
for fname in images:
    img = cv2.imread(fname)
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    # 找到棋盘格角点
    ret, corners = cv2.findChessboardCorners(gray, (w, h), None)
    # 如果找到足够点对,将其存储起来
    if ret == True:
        cv2.cornerSubPix(gray, corners, (11, 11), (-1, -1), criteria)
        objpoints.append(objp)
        imgpoints.append(corners)
        # 将角点在图像上显示
        cv2.drawChessboardCorners(img, (w, h), corners, ret)
        cv2.imshow('findCorners', img)
        cv2.waitKey(1)
cv2.destroyAllWindows()

# 标定
ret, mtx, dist, rvecs, tvecs = cv2.calibrateCamera(objpoints, imgpoints,
                                                   gray.shape[::-1], None,
                                                   None)

# 去畸变
img2 = cv2.imread('calib/00169.jpg')
h, w = img2.shape[:2]
newcameramtx, roi = cv2.getOptimalNewCameraMatrix(mtx, dist, (w, h), 0,
                                                  (w, h))  # 自由比例参数
dst = cv2.undistort(img2, mtx, dist, None, newcameramtx)
Example #42
0
    def main(self):
        # termination criteria
        criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 30,
                    0.001)

        # prepare object points, like (0,0,0), (1,0,0), (2,0,0) ....,(row,col,0)
        objp = np.zeros((self.__row * self.__col, 3), np.float32)
        objp[:, :2] = np.mgrid[0:self.__row, 0:self.__col].T.reshape(-1, 2)

        # Arrays to store object points and image points from all the images.
        objpoints = []  # 3d point in real world space
        imgpoints = []  # 2d points in image plane.
        cnt = 0

        try:
            while True:
                if self.__cam_type == 'USB':
                    ret, self.__img_raw_cam = self.cap.read()
                    if not ret:
                        print("video reading error")
                        break
                elif self.__cam_type == 'REALSENSE':
                    # Wait for a coherent pair of frames: depth and color
                    frames = self.pipeline.wait_for_frames()
                    color_frame = frames.get_color_frame()

                    # Convert images to numpy arrays
                    self.__img_raw_cam = np.asanyarray(color_frame.get_data())

                if self.__img_raw_cam != []:
                    key = cv2.waitKey(1) & 0xFF
                    if key == ord('\r'):  # ENTER
                        gray = cv2.cvtColor(self.__img_raw_cam,
                                            cv2.COLOR_BGR2GRAY)

                        # Find the chess board corners
                        ret, corners = cv2.findChessboardCorners(
                            gray, (self.__col, self.__row), None)
                        if ret == True:
                            # If found, add object points, image points (after refining them)
                            corners2 = cv2.cornerSubPix(
                                gray, corners, (11, 11), (-1, -1), criteria)

                            # Draw and display the corners
                            self.__img_raw_cam = cv2.drawChessboardCorners(
                                self.__img_raw_cam, (self.__col, self.__row),
                                corners2, ret)
                            cnt += 1
                            objpoints.append(objp)
                            imgpoints.append(corners2)
                            np.save("corners_8x6", imgpoints)
                            print(imgpoints)
                            print("Corner captured: %d trials" % (cnt))
                        else:
                            print("Corner not captured, try again")
                    elif key == ord('q'):  # ESD
                        break

                    cv2.imshow("Image", self.__img_raw_cam)
        finally:
            if self.__cam_type == 'USB':
                self.cap.release()
                cv2.destroyAllWindows()
            elif self.__cam_type == 'REALSENSE':
                # Stop streaming
                self.pipeline.stop()
            if objpoints != [] and imgpoints != []:
                ret, mtx, dist, rvecs, tvecs = cv2.calibrateCamera(
                    objpoints, imgpoints, gray.shape[::-1], None, None)
                np.savez(self.__filename,
                         ret=ret,
                         mtx=mtx,
                         dist=dist,
                         rvecs=rvecs,
                         tvecs=tvecs)
                print("Calibration data has been saved to", self.__filename)
                print("mtx", mtx)
            else:
                print("Calibration data is empty")
Example #43
0
    img = cv2.imread(fname)
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

    # Find the chess board corners
    ret, corners = cv2.findChessboardCorners(gray, (9, 6), None)

    # If found, add object points, image points (after refining them)
    if ret == True:
        objpoints.append(objp)

        corners2 = cv2.cornerSubPix(gray, corners, (11, 11), (-1, -1),
                                    criteria)
        imgpoints.append(corners2)

        # Draw and display the corners
        img = cv2.drawChessboardCorners(img, (9, 6), corners2, ret)
        cv2.imshow('img', img)
        cv2.waitKey(500)

cv2.destroyAllWindows()

ret, mtx, dist, rvecs, tvecs = cv2.calibrateCamera(objpoints, imgpoints,
                                                   gray.shape[::-1], None,
                                                   None)

img = cv2.imread('./test/temp2.jpeg')
h, w = img.shape[:2]
newcameramtx, roi = cv2.getOptimalNewCameraMatrix(mtx, dist, (w, h), 1, (w, h))

# undistort
dst = cv2.undistort(img, mtx, dist, None, newcameramtx)
Example #44
0
    s_img = cv2.cvtColor(s_img, cv2.COLOR_BGR2GRAY)
    w_img = cv2.cvtColor(w_img, cv2.COLOR_BGR2GRAY)

    s_ret, s_corners = cv2.findChessboardCorners(s_img, (args.m, args.n), None)
    w_ret, w_corners = cv2.findChessboardCorners(w_img, (args.m, args.n), None)

    if s_ret == True and w_ret == True:
        s_corners = cv2.cornerSubPix(s_img, s_corners, (11, 11), (-1, -1),
                                     criteria)
        w_corners = cv2.cornerSubPix(w_img, w_corners, (11, 11), (-1, -1),
                                     criteria)

        s_img_w_reprojected = np.copy(s_img)

        s_img = cv2.drawChessboardCorners(s_img, (args.m, args.n), s_corners,
                                          s_ret)
        w_img = cv2.drawChessboardCorners(w_img, (args.m, args.n), w_corners,
                                          w_ret)

        sum_err = 0
        for i in range(len(s_corners)):
            s_p_I = s_corners[i][0]
            w_p_I = w_corners[i][0]
            print '----------------point %d---------------' % i
            print 's_p_I:\n', s_p_I, '\nw_p_I:\n', w_p_I
            s_p_B = I2B_with_z_constraint(s_p_I, s_C2I, s_C2B)
            w_p_B = I2B_with_z_constraint(w_p_I, w_C2I, w_C2B)
            print 's_p_B:\n', s_p_B, '\nw_p_B:\n', w_p_B
            err = np.linalg.norm(s_p_B - w_p_B)
            print '\nerr :', err
            sum_err += err
        cv2.CALIB_CB_FAST_CHECK + cv2.CALIB_CB_NORMALIZE_IMAGE)
    """
    If desired number of corner are detected,
    we refine the pixel coordinates and display 
    them on the images of checker board
    """
    if ret == True:
        objpoints.append(objp)
        # refining pixel coordinates for given 2d points.
        corners2 = cv2.cornerSubPix(gray, corners, (11, 11), (-1, -1),
                                    criteria)

        imgpoints.append(corners2)

        # Draw and display the corners
        img = cv2.drawChessboardCorners(img, CHECKERBOARD, corners2, ret)

    cv2.imshow('img', img)
    cv2.waitKey(0)

cv2.destroyAllWindows()

h, w = img.shape[:2]
"""
Performing camera calibration by 
passing the value of known 3D points (objpoints)
and corresponding pixel coordinates of the 
detected corners (imgpoints)
"""
ret, mtx, dist, rvecs, tvecs = cv2.calibrateCamera(objpoints, imgpoints,
                                                   gray.shape[::-1], None,
        # Find the chess board corners
        ret, corners = cv2.findChessboardCorners(gray, (10,7),None)

        # If found, add object points, Left and Right image points (after refining them)
        if ret == True:
            corners2 = cv2.cornerSubPix(gray,corners,(11,11),(-1,-1),criteria)
            if head == "stereo_L":
                objpoints.append(objp)
                L_imgpoints.append(corners2)
                print('L_imgpoints pic =', pic)
            elif head == "stereo_R":
                R_imgpoints.append(corners2)
                print('R_imgpoints pic =', pic)

            # Draw and display the corners
            img = cv2.drawChessboardCorners(img, (10,7), corners2,ret)
            cv2.imshow('image',img)
            cv2.waitKey(50)

# using stereoCalibrate to get Rotation, Translation, Essential, Fundation 
termination_criteria_extrinsics = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 100, 0.001)
(rms_stereo, stereo_camera_matrix_l, stereo_dist_coeffs_l, stereo_camera_matrix_r, stereo_dist_coeffs_r, R, T, E, F) = \
    cv2.stereoCalibrate(objpoints, L_imgpoints, R_imgpoints, L_intrinsic, L_distortion, R_intrinsic, R_distortion,  gray.shape[::-1], criteria=termination_criteria_extrinsics, flags=cv2.CALIB_FIX_INTRINSIC)

print('R = ', R)
print('T = ', T)
print('E = ', E)
print('F = ', F)

np.save('rotation_matrix.npy', R)
np.save('translation_vector.npy', T)
Example #47
0
# Prepare object points, like (0,0,0),(1,0,0),...
objp = np.zeros((6*9,3), np.float32)
objp[:,:2] = np.mgrid[0:9,0:6].T.reshape(-1,2) # x,y coordinates

for fname in images:
    # Read in each image
    image = mpimg.imread(fname)
    gray = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)

    # Find the chessboard corners
    ret, corners = cv2.findChessboardCorners(gray, (9,6), None)

    if ret == True:
        imgpoints.append(corners)
        objpoints.append(objp)

        # Draw and display the corners
        cor_image = cv2.drawChessboardCorners(image, (9,6), corners, ret)
        scipy.misc.imsave('camera_cal/corners/'+fname.split('/')[1], cor_image)

# Save image points and object points to pickle file
points_dist = {}
points_dist['objpoints'] = objpoints
points_dist['imgpoints'] = imgpoints

with open('camera_cal/points_dist_pickle.p', 'wb') as handle:
    pickle.dump(points_dist, handle, protocol=pickle.HIGHEST_PROTOCOL)

# with open('camera_cal/points_dist_pickle.p', 'rb') as handle:
#     b = pickle.load(handle)
Example #48
0
        print_matrix(np.linalg.inv(R) @ t, "t")

        # Camera undistortion
        height, width = img.shape[:2]
        K_, roi = cv.getOptimalNewCameraMatrix(K, dist, (width, height), 1,
                                               (width, height))
        img_u = cv.undistort(img, K, dist, None, K_)

        P = construct_P_matrix(K, R, t.flatten())
        xy_grid = project_xyz(P, to_homogeneous_coordinates(grid.T))
        img_grid = draw_markers(
            img,
            xy_grid[:-1, :],
            color=(0, 255, 0),
            marker_type=cv.MARKER_CROSS,
            marker_size=10,
        )

        # Visualise
        cv.drawChessboardCorners(img, grid_dims, corners2, found)
        cv.imshow("preview", img)
        # cv.imshow("preview", img_grid)
    else:
        cv.imshow("preview", img)

    key = cv.waitKey(20)
    if key == 27 or key == ord("q"):  # exit on ESC
        break

cv.destroyAllWindows()
  # Read in each image
  img = cv2.imread(image) 

  # Convert image to grayscale
  gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)

  # Find the chessboard corners
  ret, corners = cv2.findChessboardCorners(gray, (9,6), None)

  # If corners are found, add object points, image points
  if ret == True:
    imgpoints.append(corners)
    objpoints.append(objp)

    # Draw corners on the chessboard
    withcorners = cv2.drawChessboardCorners(img, (9,6), corners, ret)
 
    # Show the chessboard images with the corners drawn on them (one-by-one)
    # cv2.imshow('FRAME',drawcorners)
    # cv2.waitKey(0)
    # cv2.destroyAllWindows()

    # Show the chessboard images with the corners drawn on them (as a 4x4 grid)
    axs[i].imshow(withcorners)
plt.show()

# Camera calibration, it takes in object, image points and shape of the input image ..
# and returns the distortion coefs (dist), camera matrix to transform 3D obj points to 2D image points ..
# also returns the position of the camera in the world (rotation and translational matrices (rvecs, tvecs))
ret, mtx, dist, rvecs, tvecs = cv2.calibrateCamera(objpoints, imgpoints, gray.shape[::-1], None, None)
Example #50
0
    def calibrateIt(self):

        print(2)

        # Chessboard dimensions
        number_of_squares_X = 10  # Number of chessboard squares along the x-axis
        number_of_squares_Y = 7  # Number of chessboard squares along the y-axis
        nX = number_of_squares_X - 1  # Number of interior corners along x-axis
        nY = number_of_squares_Y - 1  # Number of interior corners along y-axis

        # Store vectors of 3D points for all chessboard images (world coordinate frame)
        object_points = []

        # Store vectors of 2D points for all chessboard images (camera coordinate frame)
        image_points = []

        # Set termination criteria. We stop either when an accuracy is reached or when
        # we have finished a certain number of iterations.
        criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 30,
                    0.001)

        # Define real world coordinates for points in the 3D coordinate frame
        # Object points are (0,0,0), (1,0,0), (2,0,0) ...., (5,8,0)
        object_points_3D = np.zeros((nX * nY, 3), np.float32)

        # These are the x and y coordinates
        object_points_3D[:, :2] = np.mgrid[0:nY, 0:nX].T.reshape(-1, 2)
        print(3339999999999999)
        images = glob.glob('static/calibration/*.jpg')

        for image_file in images:
            image = cv2.imread(image_file)

        #image = self.Zoom(image,2)
        img1 = image
        #image = cv2.imread('1622798624-214896_undistorted.jpg')
        gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
        ret, jpeg = cv2.imencode('.jpg', image)

        # Find the corners on the chessboard
        success, corners = cv2.findChessboardCorners(gray, (nY, nX), None)

        images = glob.glob('static/calibration/*.jpg')

        print(9999999999999)
        # Go through each chessboard image, one by one
        for image_file in images:
            print(image_file)

            # Load the image
            image = cv2.imread(image_file)

            # Convert the image to grayscale
            gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

            # Find the corners on the chessboard
            success, corners = cv2.findChessboardCorners(gray, (nY, nX), None)

            # If the corners are found by the algorithm, draw them
            if success == True:

                # Append object points
                object_points.append(object_points_3D)

                # Find more exact corner pixels
                corners_2 = cv2.cornerSubPix(gray, corners, (11, 11), (-1, -1),
                                             criteria)

                # Append image points
                image_points.append(corners)

                # Draw the corners
                cv2.drawChessboardCorners(image, (nY, nX), corners_2, success)

            # Display the image. Used for testing.
            #cv2.imshow("Image", image)

            # Display the window for a short period. Used for testing.
            #cv2.waitKey(200)

        distorted_image = image

        # Perform camera calibration to return the camera matrix, distortion coefficients, rotation and translation vectors etc
        ret, mtx, dist, rvecs, tvecs = cv2.calibrateCamera(
            object_points, image_points, gray.shape[::-1], None, None)

        # Get the dimensions of the image
        height, width = distorted_image.shape[:2]

        # Refine camera matrix
        # Returns optimal camera matrix and a rectangular region of interest
        optimal_camera_matrix, roi = cv2.getOptimalNewCameraMatrix(
            mtx, dist, (width, height), 1, (width, height))

        # Undistort the image

        # Create the output file name by removing the '.jpg' part

        calib_result_pickle = {}
        calib_result_pickle["mtx"] = mtx
        calib_result_pickle["optimal_camera_matrix"] = optimal_camera_matrix
        calib_result_pickle["dist"] = dist
        calib_result_pickle["rvecs"] = rvecs
        calib_result_pickle["tvecs"] = tvecs
        pickle.dump(calib_result_pickle,
                    open("static/uploads/camera_calib_pickle.p", "wb"))

        return calib_result_pickle
Example #51
0
    def record_calib_imgs(self, **kwargs):
        """
        Provides photobooth-esque countdown interface. Saves frames to calib
        path in subdirectories `raw/` and `corners/`. Be sure to
        initialize the chessboard first.

        INPUTS
            (optional)
            cam       -- str/int -- camera descriptor for VideoCapture; '/dev/psEye'
            nframes   -- int     -- number of frames to record for calibration; 15
            w         -- int     -- width (px) to set camera frame; 320
            h         -- int     -- height (px) to set camera frame; 240
            fps       -- int     -- frames per second to set camera; 100
            countdown -- int     -- seconds to countdown before recording frame; 3

        EXCEPTIONS
            raises RuntimeError when chessboard hasn't been properly initialized
                by constructor or `init_chessboard`.
        """
        cam = kwargs.get('cam', '/dev/psEye')
        nframes = kwargs.get('nframes', 15)
        w = kwargs.get('w', 320)
        h = kwargs.get('h', 240)
        countdown = kwargs.get('countdown', 3)
        if type(nframes) != int:
            raise TypeError('nframes must be integer')
        if type(countdown) != int:
            raise TypeError('countdown must be integer')
        if self.img_arr is not None or self.calibpath is None:
            raise RuntimeError('Did you call init_chessboard() first?')
        cap = open_camera(cam, w, h, 100)  # this handles asserts
        self.w = int(cap.get(CAP_PROP_FRAME_WIDTH))
        self.h = int(cap.get(CAP_PROP_FRAME_HEIGHT))
        self.img_arr = zeros((self.h, self.w, 1, nframes),
                             dtype=uint8)  # raw frames
        clist = []  # corners frames
        self.corners_arr = []
        self.objpoints = []

        # Recording
        sc = 0  # "sample count"
        timer_ref = now()
        timer = lambda: 1 + int(countdown + timer_ref - now()
                                )  # countDOWN 3,2,1
        try:  # try/except to make sure camera device gets released
            img = zeros(self.img_arr.shape[:-1])  # for immediate cv_imshow
            while sc < nframes:
                # Display at top so can always exit
                cv_imshow('capture', img)
                press = waitKey(20)
                if press in (113, 81, 27):  # q, Q, esc:
                    logging.debug('quitting record')
                    break

                # Find chessboard, if possible
                ret, raw = cap.read()
                if not ret:
                    logging.error('Failed to access frame')
                    timer_ref = now()  # reset timer when things go wrong
                    continue
                gray = cvtColor(raw, COLOR_RGB2GRAY)
                corners = self._find_chessboard(gray)

                # Compute visual feedback
                if corners is None:  # alert to unfindable chessboard
                    img = raw.copy()
                    cv_putText(img,
                               'NO CHESSBOARD', (5, 15),
                               1,
                               1, (0, 0, 255),
                               thickness=2)
                    timer_ref = now(
                    )  # reset timer when chessboard isn't viable
                else:  # show countdown and progess
                    board1 = drawChessboardCorners(raw, self.boardsize,
                                                   corners, ret)
                    img = board1.copy()
                    cv_putText(img,
                               'T-%ds' % timer(), (5, 15),
                               1,
                               1, (0, 0, 255),
                               thickness=2)
                cv_putText(img,
                           '%d/%d' % (sc + 1, nframes), (5, 30),
                           1,
                           1, (0, 0, 255),
                           thickness=2)

                # Capture image
                if timer() <= 0:
                    # image saving
                    self.img_arr[..., sc] = gray.copy()[..., newaxis]
                    clist.append(board1)

                    # for camera calibration
                    self.corners_arr.append(corners)
                    self.objpoints.append(self.objp)

                    # program progess/display
                    img = zeros(raw.shape, dtype=uint8) + 255  # "flash" camera
                    sc += 1
                    timer_ref = now()

            # Save images to file
            if self.calibpath is None:
                self._create_calib_path()
            # Create save directories
            rawpath = self.calibpath + '/raw'
            cpath = self.calibpath + '/corners'
            for p in (rawpath, cpath):
                if not isdir(p):
                    if os_exists(p):
                        logging.warning(
                            '\'%s\' exists, but is not directory. Overwriting.'
                            % p)
                        os_remove(p)
                    mkdir(p)
            for i in range(nframes):
                fn_raw = rawpath + ('/f%s' % str(i + 1).zfill(5)) + '.jpg'
                fn_c = cpath + ('/f%s' % str(i + 1).zfill(5)) + '.jpg'
                cv_imwrite(fn_raw, self.img_arr[..., i],
                           (IMWRITE_JPEG_QUALITY, 100))
                cv_imwrite(fn_c, clist[i], (IMWRITE_JPEG_QUALITY, 100))

        # Close Capture
        except Exception as e:
            logging.error(e)
        finally:
            cap.release()
            destroyAllWindows()
            logging.debug('released \'%s\'' % cam)
Example #52
0
def get_stereo_points(left_images, right_images, draw):
    objpoints = []  # 3d point in real world space
    imgpoints_left = []  # 2d points in image plane.
    imgpoints_right = []

    # prepare object points, like (0,0,0), (1,0,0), (2,0,0) ....,(6,5,0)
    objp = np.zeros((c.NUM_SQUARES, 3), np.float32)
    objp[:, :2] = np.mgrid[0:c.CHECKER_ROWS, 0:c.CHECKER_COLS].T.reshape(-1, 2)
    objp *= c.GRID_SPACING_MM

    for i, (left_name, right_name) in enumerate(zip(left_images,
                                                    right_images)):
        left_img = cv2.imread(left_name)
        left_gray = cv2.cvtColor(left_img, cv2.COLOR_BGR2GRAY)
        left_ret, left_corners = cv2.findChessboardCorners(
            left_gray, (c.CHECKER_ROWS, c.CHECKER_COLS), None)

        print(i)
        if left_ret:
            if left_corners[0, 0, 0] > left_corners[-1, 0, 0]:
                left_corners = left_corners[::-1, :, :]
            left_corners = left_corners.astype(np.float32)
        else:
            print('left bad')

        right_img = cv2.imread(right_name)
        right_gray = cv2.cvtColor(right_img, cv2.COLOR_BGR2GRAY)
        right_ret, right_corners = cv2.findChessboardCorners(
            right_gray, (c.CHECKER_ROWS, c.CHECKER_COLS), None)

        if right_ret:
            if right_corners[0, 0, 0] > right_corners[-1, 0, 0]:
                right_corners = right_corners[::-1, :, :]
            right_corners = right_corners.astype(np.float32)
        else:
            print('right bad')

        if left_ret and right_ret:
            objpoints.append(objp)
            left_corners2 = cv2.cornerSubPix(left_gray, left_corners,
                                             c.WINDOW_SIZE, (-1, -1),
                                             c.INTRINSIC_CRIT)
            imgpoints_left.append(left_corners2)

            right_corners2 = cv2.cornerSubPix(right_gray, right_corners,
                                              c.WINDOW_SIZE, (-1, -1),
                                              c.INTRINSIC_CRIT)
            imgpoints_right.append(right_corners2)

            if draw:
                left_img_cv = cv2.drawChessboardCorners(
                    left_img, (c.CHECKER_ROWS, c.CHECKER_COLS), left_corners2,
                    left_ret)
                cv2.imshow(os.path.split(left_name)[1], left_img_cv)

                right_img_cv = cv2.drawChessboardCorners(
                    right_img, (c.CHECKER_ROWS, c.CHECKER_COLS),
                    right_corners2, right_ret)
                cv2.imshow(os.path.split(right_name)[1], right_img_cv)
                cv2.waitKey(0)
                cv2.destroyAllWindows()
    return objpoints, imgpoints_left, imgpoints_right, \
           right_gray.shape[::-1]
def draw_corners(img, corners):
    return cv2.drawChessboardCorners(img, (nx, ny), corners, True)
def Calibration(video, yamlFiles):
    __mtL = mc.Calib(video[0])
    __mtR = mc.Calib(video[1])
    __sh = mc.Show()
    __mtL.findCorners(TypeOfPattern=1)
    __mtR.findCorners(TypeOfPattern=1)
    out = cv2.VideoWriter('CalibCyrcle100.avi',
                          cv2.VideoWriter_fourcc(*'XVID'), 5,
                          (__mtL.width * 2, __mtL.height))
    while (1):
        if (__mtL.cap.isOpened) and (__mtR.cap.isOpened):
            retL, __mtL.frames = __mtL.cap.read()
            retR, __mtR.frames = __mtR.cap.read()
            if (retL == True) and (retR == True):
                print("----------------------------------------")
                #gray = cv2.cvtColor(__mtL.frames, cv2.COLOR_BGR2GRAY)
                retCL, circlesL = cv2.findCirclesGrid(
                    __mtL.frames, (4, 11),
                    flags=(cv2.CALIB_CB_ASYMMETRIC_GRID +
                           cv2.CALIB_CB_CLUSTERING),
                    blobDetector=__mtL.detector)
                #__mtL.frames = cv2.cvtColor(gray, cv2.COLOR_GRAY2BGR)
                retCR, circlesR = cv2.findCirclesGrid(
                    __mtR.frames, (4, 11),
                    flags=(cv2.CALIB_CB_ASYMMETRIC_GRID +
                           cv2.CALIB_CB_CLUSTERING),
                    blobDetector=__mtR.detector)
                __mtL.fNum = round(__mtL.cap.get(cv2.CAP_PROP_POS_FRAMES))
                __mtR.fNum = round(__mtR.cap.get(cv2.CAP_PROP_POS_FRAMES))
                print("fNum: ", __mtL.fNum, " Left: ", retCL, " Right: ",
                      retCR)
                if retCL == True:
                    __mtL.numGrid.append(__mtL.fNum)
                    __mtL.objpoints.append(__mtL.objp)
                    __mtL.imgpoints.append(circlesL)
                    __mtL.PatNum = __mtL.PatNum + 1
                    cv2.drawChessboardCorners(__mtL.frames, (4, 11), circlesL,
                                              retCL)
                    print("Circles found on image " + str(__mtL.fNum) + ".")
                if retCR == True:
                    __mtR.numGrid.append(__mtR.fNum)
                    __mtR.objpoints.append(__mtR.objp)
                    __mtR.imgpoints.append(circlesR)
                    __mtR.PatNum = __mtR.PatNum + 1
                    cv2.drawChessboardCorners(__mtR.frames, (4, 11), circlesR,
                                              retCR)
                    print("Circles found on image " + str(__mtR.fNum) + ".")
                __mtL.addTexttoFramePattern(__mtL.frames, (0, 255, 0))
                __mtR.addTexttoFramePattern(__mtR.frames, (0, 0, 255))
                __sh.show(__mtL.frames, __mtR.frames)
                C = cv2.hconcat([__mtL.frames, __mtR.frames])
                out.write(C)

                if __mtL.PatNum != []:
                    if int(__mtL.PatNum) == 150:
                        print("Left calibration")
                        __mtL.calibrate()
                        __mtL.saveToFile(yamlFiles[0])
                if __mtR.PatNum != []:
                    if int(__mtR.PatNum) == 150:
                        print("Right calibration")
                        __mtR.calibrate()
                        __mtR.saveToFile(yamlFiles[1])

        k = cv2.waitKey(30) & 0xff
        if k == 27:  # (ASCII value for ESC is 27 - exit program)
            __mtL.calibrate()
            __mtR.calibrate()
            __mtL.saveToFile(yamlFiles[0])
            __mtR.saveToFile(yamlFiles[1])
            out.release()
            print('ESC')
        elif k == 32:
            while (cv2.waitKey(1) == 'p'):
                out.release()
                print("pause")
    __mtR.calibrate()
    __mtR.saveToFile(yamlFiles[1])
    __mtL.calibrate()
    __mtL.saveToFile(yamlFiles[0])

    out.release()
    __mtL.cap.release()
    __mtR.cap.release()
    print("done")
    cv2.waitKey(0)

    # Find the circle grid centres
    ret, corners = cv2.findCirclesGrid(mask_morphed,
                                       circle_grid,
                                       flags=cv2.CALIB_CB_SYMMETRIC_GRID +
                                       cv2.CALIB_CB_CLUSTERING,
                                       blobDetector=detector)

    # If found, add object points, image points
    if ret:
        imgpoints = np.flipud(corners)

        # draw circle centres
        circle_img = raw_img.copy()
        cv2.drawChessboardCorners(circle_img, circle_grid, corners, ret)
        cv2.imshow("img", circle_img)
        cv2.waitKey(0)

        # print out pixel coords and real world coords
        for (im_coords, obj_coords) in zip(imgpoints, objpoints):
            print("img: {0} obj: {1}".format(im_coords, obj_coords))

        # initial Homography matrix where the  circle grid origin is wrt the device
        # the next part is creating a matrix that warps and translates the raw image to a birds eye view that
        # could be used for visualising to the user or stitching together to get a high resolution top down view of the entire shed floor
        init_H, status = cv2.findHomography(imgpoints, objpoints)

        # getting coords to translate the birds eye view image into the full frame without cropping
        pixel_coords_of_raw_image_corners = np.array([[[0, 0], [raw_img.shape[1], 0], \
                                                [0, raw_img.shape[0]], [raw_img.shape[1], raw_img.shape[0]]]], np.float32)
Example #56
0
image = find_image('left02.jpg')
img = cv2.imread(image)
print(img.shape)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# Find the chess board corners
ret, corners = cv2.findChessboardCorners(gray, (7, 6), None)
print('corners shape {}'.format(corners.shape))

# If found, add object points, image points (after refining them)
if ret:
    objpoints.append(objp)
    # cv2.cornerSubPix(gray, corners, (11, 11), (-1, -1), criteria)
    imgpoints.append(corners)
    # Draw and display the corners
    cv2.drawChessboardCorners(img, (7, 6), corners, ret)
    cv2.imshow('img', img)
    cv2.waitKey(0)

cv2.destroyAllWindows()

ret, mtx, dist, rvecs, tvecs = cv2.calibrateCamera(objpoints, imgpoints,
                                                   gray.shape[::-1], None,
                                                   None)
np.savez_compressed('B.npz',
                    ret=ret,
                    mtx=mtx,
                    dist=dist,
                    rvecs=rvecs,
                    tvecs=tvecs)
print('ret: \n{}\n'.format(ret))
import numpy as np
import cv2 as cv
import glob

# termination criteria
criteria = (cv.TERM_CRITERIA_EPS + cv.TERM_CRITERIA_MAX_ITER, 30, 0.001)

# prepare object points, like (0,0,0), (1,0,0), (2,0,0) ....,(6,5,0)
objp = np.zeros((6*7, 3), np.float32)
objp[:,:2] = np.mgrid[0:7,0:6].T.reshape(-1,2)

# Arrays to store object points and image points from all the images.
objpoints = [] # 3d point in real world space
imgpoints = [] # 2d points in image plane.
images = glob.glob('./07CalibrationData/*.jpg')
for fname in images:
    img = cv.imread(fname)
    gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
    # Find the chess board corners
    ret, corners = cv.findChessboardCorners(gray, (7,6), None)
    # If found, add object points, image points (after refining them)
    if ret == True:
        objpoints.append(objp)
        corners2 = cv.cornerSubPix(gray,corners, (11,11), (-1,-1), criteria)
        imgpoints.append(corners)
        # Draw and display the corners
        cv.drawChessboardCorners(img, (7,6), corners2, ret)
        cv.imshow('img', img)
        cv.waitKey(500)
cv.destroyAllWindows()
        img = cv2.imread(fname)
        gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

        # Find the chess board corners
        ret, corners = cv2.findChessboardCorners(
            gray, chessboardDimension,
            cv2.CALIB_CB_ADAPTIVE_THRESH + cv2.CALIB_CB_NORMALIZE_IMAGE)
        if ret:
            if not auto_mode:
                imgCopy = img.copy()

                corners2 = cv2.cornerSubPix(gray, corners, (3, 3), (-1, -1),
                                            criteria)

                imgCopy = cv2.drawChessboardCorners(imgCopy,
                                                    chessboardDimension,
                                                    corners2, ret)
                corners2 = cv2.cornerSubPix(gray, corners, (3, 3), (-1, -1),
                                            criteria)
                cv2.imshow('img', imgCopy)

                while True:
                    key = cv2.waitKey(10) & 0xFF
                    if ord('s') == key:
                        # imgPoints.append(corners)
                        objPoints.append(objp)
                        imgPoints.append(corners2)
                        break
                    elif ord('d') == key:
                        break
            else:
Example #59
0
import cv2
import matplotlib.pyplot as plt
import numpy as np

img = cv2.imread('3.jpg')

img_blur = cv2.blur(img, (5, 5))

img_gray = cv2.cvtColor(img_blur, cv2.COLOR_BGR2GRAY)

ret, img_threshold = cv2.threshold(img_gray, 127, 255, cv2.THRESH_BINARY)

found, corners = cv2.findChessboardCorners(img_threshold, (7, 7))

img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
cv2.drawChessboardCorners(img, (7, 7), corners, found)

plt.imshow(img)
plt.show()
#print(corners)

# If found, add object points, image points
if ret == True:
    objpoints.append(objp)
    imgpoints.append(corners)

# calibrate camera
et2, mtx, dist, rvecs, tvecs = cv2.calibrateCamera(objpoints, imgpoints,
                                                   img.shape[1::-1], None,
                                                   None)

#undistort
undist = cv2.undistort(img, mtx, dist, None, mtx)

cv2.drawChessboardCorners(img_orig, (nx, ny), corners, ret)

src = np.float32([corners[0], corners[nx - 1], corners[-1], corners[-nx]])
offset = 100
img_size = (gray.shape[1], gray.shape[0])
dst = np.float32([[offset, offset], [img_size[0] - offset, offset],
                  [img_size[0] - offset, img_size[1] - offset],
                  [offset, img_size[1] - offset]])
M = cv2.getPerspectiveTransform(src, dst)
warped = cv2.warpPerspective(undist, M, img_size)


def unwarp(img, mtx, dist):
    undist = cv2.undistort(img, mtx, dist, None, mtx)

    src = np.float32([corners[0], corners[nx - 1], corners[-1], corners[-nx]])