def correctSkew(self, original_img): ''' Applies skew matrix values to a given photo - Only creates dst if use_skew_correction = True or None - None implies this is being used for calibration purposes - If user has turned skew correction off, then it just returns the original image Args: original_img (string): photo which needs skew correction Returns: If using skew correction: corrected_img (string): photo corrected for skew Else: original_img (string): the unaltered original image ''' if self.use_skew_correction is None: cv2.imwrite("skew1.jpg", original_img) # This means that the image we are using for calibration is permanently rewritten with the skew matrix # corrected_img = cv2.imread("skew1.jpg", 0) dst = cv2.undistort(original_img, self.skew_mtx, self.skew_dist, None, self.skew_newcameramtx) cv2.imwrite("corrected.jpg", dst) corrected_img = cv2.imread("corrected.jpg",0) return dst elif self.use_skew_correction is True: cv2.imwrite("skew1.jpg", original_img) # This means that the image we are using for calibration is permanently rewritten with the skew matrix # corrected_img = cv2.imread("skew1.jpg", 0) dst = cv2.undistort(original_img, self.skew_mtx, self.skew_dist, None, self.skew_newcameramtx) cv2.imwrite("corrected.jpg", dst) corrected_img = cv2.imread("corrected.jpg",0) return corrected_img else: return original_img
def disparityCalc(self): ############# CALCULATE Disparity ############################ # 1 make the images grayscale gray_left = cv2.cvtColor(self.imgLeft, cv2.cv.CV_BGR2GRAY) gray_right = cv2.cvtColor(self.imgRight, cv2.cv.CV_BGR2GRAY) # 2 undistort the image pair undistorted_image_L = cv2.undistort(gray_left, self.intrinsic_matrixL, self.distCoeffL, None) undistorted_image_R = cv2.undistort(gray_right, self.intrinsic_matrixR, self.distCoeffR, None) # 3 --> calculate disparity images disparity_visual = self.getDisparity(imgLeft=undistorted_image_L, imgRight=undistorted_image_R, method="BM") disparity_visual = disparity_visual.astype(np.uint8) return disparity_visual
def calibrate_imgs(self): ''' Uses calibration matrices stored in file to calibrate rotate images ''' # Use OpenCV function to undistort images self.calib_image_left = cv2.undistort(self.rotated_image_left, self.calib_matrix_left[0], self.calib_matrix_left[1], None, self.calib_matrix_left[2]) self.calib_image_right = cv2.undistort(self.rotated_image_right, self.calib_matrix_right[0], self.calib_matrix_right[1], None, self.calib_matrix_right[2])
def captureNextFrame(self): """ Capture frame, convert to RGB, and return opencv image """ ret, frame=self.capture.read() if(ret==True): self.currentFrame=cv2.cvtColor(frame, cv2.COLOR_BAYER_GB2BGR) self.bwFrame = cv2.cvtColor(self.currentFrame, cv2.COLOR_BGR2GRAY) if self.calibration_loaded: self.currentFrame = cv2.undistort(self.currentFrame, self.camera_matrix, self.dist_coefs, None, self.new_camera_matrix) self.bwFrame = cv2.undistort(self.bwFrame, self.camera_matrix, self.dist_coefs, None, self.new_camera_matrix)
def videoMatch2(calibFile,videoFile,startFrame,endFrame): K, distort = readCalibrationData(calibFile) print(K) print(distort) cap = cv2.VideoCapture(videoFile) skipFrames(cap,startFrame) print("Current Frame: "+ str(cap.get(1))+"/"+ str(cap.get(7))) ret, firstFrame = cap.read() skipFrames(cap,endFrame-startFrame) ret, secondFrame = cap.read() patches = match(firstFrame.copy(),secondFrame.copy(),K,distort) cubePositions = [p.x for i,p in enumerate(patches) if randint(0,3)==0] #np.array([0,0,50,1])# #print(patches) #cv2.waitKey(0) cap.set(1,startFrame) for frame in range(startFrame,int(cap.get(7))): ret, img = cap.read() img = cv2.undistort(img,K,distort) corr = correspondences.getPatchCorrespondences(patches,img) #for c in corr: # cv2.circle(img, tuple(c[0].astype(int)),3, (0,0,255),-1) # cv2.line(img, tuple(c[0].astype(int)), tuple(c[1].pt1.astype(int)), (0,0,255),1) if(len(corr)>7): p = computervision.findTransformation(K, corr) for cube in cubePositions: projectCube(img,p,cube) cv2.imwrite("out/"+str(frame)+".png",img) cv2.imshow("test2", img) cv2.waitKey(1) cap.release()
def undistort_image(self, img, Kundistortion=None): """ Transform grayscale image such that radial distortion is removed. :param img: input image :type img: np.ndarray, shape=(n, m) or (n, m, 3) :param Kundistortion: camera matrix for undistorted view, None for self.K :type Kundistortion: array-like, shape=(3, 3) :return: transformed image :rtype: np.ndarray, shape=(n, m) or (n, m, 3) """ if Kundistortion is None: Kundistortion = self.K if self.calibration_type == 'opencv': return cv2.undistort(img, self.K, self.opencv_dist_coeff, newCameraMatrix=Kundistortion) elif self.calibration_type == 'opencv_fisheye': return cv2.fisheye.undistortImage(img, self.K, self.opencv_dist_coeff, Knew=Kundistortion) else: xx, yy = np.meshgrid(np.arange(img.shape[1]), np.arange(img.shape[0])) img_coords = np.array([xx.ravel(), yy.ravel()]) y_l = self.undistort(img_coords, Kundistortion) if img.ndim == 2: return griddata(y_l.T, img.ravel(), (xx, yy), fill_value=0, method='linear') else: channels = [griddata(y_l.T, img[:, :, i].ravel(), (xx, yy), fill_value=0, method='linear') for i in xrange(img.shape[2])] return np.dstack(channels)
def cal_undistort(img, objpoints, imgpoints): img_size = (img.shape[1], img.shape[0]) # Use cv2.calibrateCamera() and cv2.undistort() ret, mtx, dist, rvecs, tvecs = cv2.calibrateCamera(objpoints, imgpoints, img_size,None,None) dst = cv2.undistort(img, mtx, dist, None, mtx) return dst
def undistort(img, corners, profile_name): profile = load_calibration_profile(profile_name) img, corners = _uncrop(img, corners) if len(img.shape) == 3: # XXX Hack. Fix _uncrop! img = img[:, :, 1] if img.shape[:2] not in profile: raise ValueError('No calibration settings for input image size.') settings = profile[img.shape[:2]] height, width = img.shape[:2] # Undistort corners. OpenCV expects (x, y) and a 3D array. corners = np.array([np.fliplr(corners)]) undist_corners = cv2.undistortPoints(corners, settings.camera_matrix, settings.distort_coeffs, P=settings.camera_matrix) undist_corners = np.fliplr(undist_corners[0]) undist_img = cv2.undistort(img, settings.camera_matrix, settings.distort_coeffs) log.debug('Undistorted image, shape={}, calibration_profile={}'.format( img.shape, profile_name)) return undist_img, undist_corners
def _cleanImage(image): h, w = image.shape[:2] newcameramtx, roi=cv2.getOptimalNewCameraMatrix(camera_matrix,dist_coefs,(w,h),1,(w,h)) dst = cv2.undistort(image, camera_matrix, dist_coefs, None, newcameramtx) x,y,w,h = roi dst = dst[y:y+h, x:x+w] return dst
def getImage(): # initialize the camera and grab a reference to the raw camera capture camera = PiCamera() camera.resolution = (640, 480) camera.framerate = 32 rawCapture = PiRGBArray(camera, size=(640, 480)) # print("setting is end!") # allow the camera to warmup # time.sleep(0.1) dst = None # capture frames from the camera for frame in camera.capture_continuous(rawCapture, format="bgr", use_video_port=True): # grab the raw NumPy array representing the image, then initialize the timestamp # and occupied/unoccupied text image = frame.array mtx =np.array([[100, 0, 320], [0, 100, 240], [0, 0, 1]], dtype=np.float32) dist = np.array([-0.009, 0.0, 0.0, 0.0], dtype=np.float32) h, w = image.shape[:2] newcameramtx, roi=cv2.getOptimalNewCameraMatrix(mtx,dist,(w,h),1,(w,h)) # undistort dst = cv2.undistort(image, mtx, dist, None, newcameramtx) break return dst
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 main(file_list, outdir): # copy parameters to arrays K = np.array([[1743.23312, 0, 2071.06177], [0, 1741.57626, 1476.48298], [0, 0, 1]]) d = np.array([-0.307412748, 0.300929683, 0, 0, 0]) # just use first two terms (no translation) logging.debug("Starting loop") for image in file_list: logging.debug("var %s", image) imgname = image.split("/")[-1] new_image_path = os.path.join(outdir, imgname) if not os.path.exists(new_image_path): logging.debug("Undistorting %s . . . ", imgname) # read one of your images img = cv2.imread(image) h, w = img.shape[:2] # un-distort newcamera, roi = cv2.getOptimalNewCameraMatrix(K, d, (w, h), 0) newimg = cv2.undistort(img, K, d, None, newcamera) # cv2.imwrite("original.jpg", img) cv2.imwrite(new_image_path, newimg) # Write metadata old_meta = pyexiv2.ImageMetadata(image) new_meta = pyexiv2.ImageMetadata(new_image_path) old_meta.read() new_meta.read() old_meta.copy(new_meta) new_meta.write() else: logging.debug("Image already processed")
def undistort_image(imagepath, calib_file, visulization_flag): """ undistort the image and visualization :param imagepath: image path :param calib_file: includes calibration matrix and distortion coefficients :param visulization_flag: flag to plot the image :return: none """ mtx, dist = load_calibration(calib_file) img = cv2.imread(imagepath) # undistort the image img_undist = cv2.undistort(img, mtx, dist, None, mtx) img_undistRGB = cv2.cvtColor(img_undist, cv2.COLOR_BGR2RGB) if visulization_flag: imgRGB = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) f, (ax1, ax2) = plt.subplots(1, 2) ax1.imshow(imgRGB) ax1.set_title('Original Image', fontsize=30) ax1.axis('off') ax2.imshow(img_undistRGB) ax2.set_title('Undistorted Image', fontsize=30) ax2.axis('off') plt.show() return img_undistRGB
def undistortImage(self,img): if (self.calibrationData == False): return img h, w = img.shape[:2] img = cv2.undistort(img, np.asarray(self.calibrationData['camera_matrix']), np.asarray(self.calibrationData['dist_coeff']), None) return img
def undistort_brown_image(image, camera, new_camera): """Remove radial distortion from a brown image.""" height, width = image.shape[:2] K = camera.get_K_in_pixel_coordinates(width, height) distortion = np.array([camera.k1, camera.k2, camera.p1, camera.p2, camera.k3]) new_K = new_camera.get_K_in_pixel_coordinates(width, height) return cv2.undistort(image, K, distortion, new_K)
def unwarp(img, nx, ny, mtx, dist): img_size = (img.shape[1], img.shape[0]) corners = np.float32([[190,720],[589,457],[698,457],[1145,720]]) new_top_left=np.array([corners[0,0],0]) new_top_right=np.array([corners[3,0],0]) offset=[150,0] src = np.float32([corners[0],corners[1],corners[2],corners[3]]) dst = np.float32([corners[0]+offset,new_top_left+offset,new_top_right-offset ,corners[3]-offset]) src = np.float32([(575,464),(707,464),(258,682),(1049,682)]) dst = np.float32([(450,0),(img_size[0]-450,0),(450,img_size[1]),(img_size[0]-450,img_size[1])]) undist = cv2.undistort(img, mtx, dist, None, mtx) # Given src and dst points, calculate the perspective transform matrix M = cv2.getPerspectiveTransform(src, dst) Minv = cv2.getPerspectiveTransform(dst, src) # Warp the image using OpenCV warpPerspective() warped = cv2.warpPerspective(undist, M, img_size,flags=cv2.INTER_LINEAR) # Return the resulting image and matrix return warped, M, Minv
def read_left(): global l, left, newcamera while True: _, x = left.read() # x = cv2.GaussianBlur(x, (9, 9), 0) l = cv2.undistort(x, k, d, None, newcamera)
def doPreprocess(self): ''' All processing before analysing separate chambers ''' # Discarding color information if self.frame is None: return None frame = cv2.cvtColor(self.frame, cv2.cv.CV_RGB2GRAY); # Inverting frame if needed if self.invertImage : frame = cv2.bitwise_not(frame) if self.removeBarrel : distCoeff = np.zeros((4,1),np.float64) distCoeff[0,0] = self.removeBarrelCoef # assume unit matrix for camera cam = np.eye(3,dtype=np.float32) height, width = frame.shape[0],frame.shape[1], cam[0,2] = width/2.0 # define center x cam[1,2] = height/2.0 # define center y cam[0,0] = float(self.removeBarrelFocal) # define focal length x cam[1,1] = cam[0,0] # define focal length y # here the undistortion will be computed frame = cv2.undistort(frame,cam,distCoeff) self.signalNextFrame.emit(frame)
def cameraMatch(calibFile): K, distort = readCalibrationData(calibFile) print(K) print(distort) cap = cv2.VideoCapture(0) while(True): ret, firstFrame = cap.read() cv2.imshow("test2", firstFrame) if cv2.waitKey(1)!=-1: break print("first pic taken") while(True): ret, secondFrame = cap.read() cv2.imshow("test2", secondFrame) if cv2.waitKey(1)!=-1: break print("second pic taken") patches = match(firstFrame.copy(),secondFrame.copy(),K,distort) cubePositions = [p.x for i,p in enumerate(patches) if randint(0,1)==0] #np.array([0,0,50,1])# #print(patches) while(True): ret, img = cap.read() img = cv2.undistort(img,K,distort) corr = correspondences.getPatchCorrespondences(patches,img) #for c in corr: # cv2.circle(img, tuple(c[0].astype(int)),3, (0,0,255),-1) # cv2.line(img, tuple(c[0].astype(int)), tuple(c[1].pt1.astype(int)), (0,0,255),1) if(len(corr)>7): p = computervision.findTransformation(K, corr) for cube in cubePositions: projectCube(img,p,cube) cv2.imshow("test2", img) if cv2.waitKey(1)!=-1: break cap.release()
def warp(img): img = cv2.undistort(img, mtx, dist, None, mtx) preprocess_image = np.zeros_like(img[:,:,0]) gradx = abs_sobel_thresh(img, orient='x', thresh=(12, 255)) grady = abs_sobel_thresh(img, orient='y', thresh=(25, 255)) c_binary = color_thresh(img, sthresh=(60, 255), vthresh=(50, 255), lthresh=(75, 255)) preprocess_image[((gradx == 1) & (grady == 1) | (c_binary == 1))] = 255 img_size = (img.shape[1], img.shape[0]) # map trapezium view of the lane to bot_width = .76 # percentage of width of bottom of trapezium mid_width = .08 # top of trapezium width height_pct = .62 # height of trapezium %age of image ht bottom_trim = .935 # ignoring the hood of the car src = np.float32([ [img.shape[1]*(.5-mid_width/2), img.shape[0]*height_pct], [img.shape[1]*(.5+mid_width/2), img.shape[0]*height_pct], [img.shape[1]*(.5+bot_width/2), img.shape[0]*bottom_trim], [img.shape[1]*(.5-bot_width/2), img.shape[0]*bottom_trim], ]) # map to a rectangle offset = img.shape[1]*.25 dst = np.float32([ [offset, 0], [img.shape[1]-offset, 0], [img.shape[1]-offset, img.shape[0]], [offset, img.shape[0]] ]) M = cv2.getPerspectiveTransform(src, dst) Minv = cv2.getPerspectiveTransform(dst, src) warped = cv2.warpPerspective(preprocess_image, M, img_size, flags=cv2.INTER_LINEAR) return warped, M, Minv
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 calibrate(filenames): # 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) ....,(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 = [] for filename in filenames: # Find the chess board corners. If found, add object points, image points (after refining them) img = cv2.imread(filename) if img != None: print "Loaded " + repr(filename) else: print "Unable to load image " + repr(filename) continue images.append(img) gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) ret, corners = cv2.findChessboardCorners(gray, (7,6), None) if ret == True: objpoints.append(objp) corners2 = cv2.cornerSubPix(gray, corners, (11,11), (-1,-1), criteria) imgpoints.append(corners2) ret, mtx, dist, rvecs, tvecs = cv2.calibrateCamera(objpoints, imgpoints, gray.shape[::-1],None,None) print "Loaded all images and calbulated calibration" for i, img in enumerate(images): img = images[i] 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 cv2.imwrite( 'calibrated/out_' + str(i) + '.png', dst[ y : y+h, x : x+w ]) print "Outputted calibrated image: 'calibrated/out_" + str(i) + ".png'"
def undistort(mtx, dist, objpoints, imgpoints, rvecs, tvecs, img): #read in an image of choice #usefule for the reading in and segmenting of board to make hough lines easier 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) # crop the image x, y, w, h = roi dst = dst[y:y + h, x:x + w] cv2.imwrite('calibresult.png', dst) # undistort mapx, mapy = cv2.initUndistortRectifyMap(mtx, dist, None, newcameramtx, (w, h), 5) dst = cv2.remap(img, mapx, mapy, cv2.INTER_LINEAR) # crop the image x, y, w, h = roi dst = dst[y:y + h, x:x + w] cv2.imwrite('calibresult.png', dst) mean_error = 0 for i in xrange(len(objpoints)): imgpoints2, _ = cv2.projectPoints(objpoints[i], rvecs[i], tvecs[i], mtx, dist) error = cv2.norm(imgpoints[i], imgpoints2, cv2.NORM_L2) / len(imgpoints2) mean_error += error print "total error: ", mean_error / len(objpoints)
def recent_events(self, events): frame = events.get('frame') if not frame: return if self.collect_new: img = frame.img status, grid_points = cv2.findCirclesGrid(img, (4,11), flags=cv2.CALIB_CB_ASYMMETRIC_GRID) if status: self.img_points.append(grid_points) self.obj_points.append(self.obj_grid) self.collect_new = False self.count -=1 self.button.status_text = "{:d} to go".format(self.count) if self.count<=0 and not self.calculated: self.calculate() self.button.status_text = '' if self.window_should_close: self.close_window() if self.show_undistortion: adjusted_k,roi = cv2.getOptimalNewCameraMatrix(cameraMatrix= self.camera_intrinsics[0], distCoeffs=self.camera_intrinsics[1], imageSize=self.camera_intrinsics[2], alpha=0.5,newImgSize=self.camera_intrinsics[2],centerPrincipalPoint=1) self.undist_img = cv2.undistort(frame.img, self.camera_intrinsics[0], self.camera_intrinsics[1],newCameraMatrix=adjusted_k)
def undistort_image(image, shot): """Remove radial distortion from a perspective image.""" camera = shot.camera height, width = image.shape[:2] K = opencv_calibration_matrix(width, height, camera.focal) distortion = np.array([camera.k1, camera.k2, 0, 0]) return cv2.undistort(image, K, distortion)
def calibration(dis_frame): cameraMatrix=np.array([[133.77965, 0, 179.13740], [0, 133.75846, 111.34959], [0, 0, 1]]) distCoeffs=np.array([-0.31482, 0.10522, 0.000387, 0.00001367, -0.01685]) output_frame = cv2.undistort(dis_frame, cameraMatrix,distCoeffs,None) return output_frame
def run(self, camera=False): frame = cv2.namedWindow(FRAME_NAME) # Set callback cv2.setMouseCallback(FRAME_NAME, self.draw) if camera: cap = cv2.VideoCapture(0) for i in range(10): status, image = cap.read() else: image = cv2.imread('00000001.jpg') self.image = cv2.undistort(image, CMATRIX, DIST, None, NCMATRIX) # Get various data about the image from the user self.get_pitch_outline() self.get_zone('Zone_0', 'draw LEFT Defender') self.get_zone('Zone_1', 'draw LEFT Attacker') self.get_zone('Zone_2', 'draw RIGHT Attacker') self.get_zone('Zone_3', 'draw RIGHT Defender') self.get_goal('Zone_0') self.get_goal('Zone_3') print 'Press any key to finish.' cv2.waitKey(0) cv2.destroyAllWindows() # Write out the data # self.dump('calibrations/calibrate.json', self.data) tools.save_croppings(pitch=self.pitch, data=self.data)
def undistort_img(img, points3d, points2d): img_size = (img.shape[1], img.shape[0]) # Do camera calibration given object points and image points ret, mtx, dist, rvecs, tvecs = cv2.calibrateCamera(points3d, points2d, img_size, None, None) dst = cv2.undistort(img, mtx, dist, None, mtx) return dst
def UndistortImage(image, intrinsic_matrix, distCoeff): # 1 Undistort the Image undistorted_image = cv2.undistort(image, intrinsic_matrix, distCoeff, None) # 2 return the undistorted image #undistorted_imgList.append(undistorted_img) return undistorted_image
def undistort(image): """img: original RGB img to be undistorted Return undistorted image. """ undistort_image = cv2.undistort(image, camera_matrix, distortion_coeff, None, camera_matrix) return undistort_image