def main(input_file, intrinsics_file, output_file): # read the image image = plt.imread(input_file) # read the camera intrinsics K = np.loadtxt(intrinsics_file) # get user input for three points on a distorted line in the image print "Find a line in the image that has been curved due to radial", print "distortion. Select three x, y pixel coordinates and enter them here." x1 = input("x1: ") y1 = input("y1: ") x2 = input("x2: ") y2 = input("y2: ") x3 = input("x3: ") y3 = input("y3: ") # calculate the radial distortion coefficient k1 = calculate_k1(K, x1, y1, x2, y2, x3, y3) print print "Estimated value of k1:", k1 # and finally, undistort the image using the estimated value of k1 undistort_image(image, K, k1, output_file)
def encode_image_for_response(img, width, height, undistort_mapping): if undistort_mapping is not None: img = undistort.undistort_image(img, undistort_mapping) return image_utils.encode_image( img, encoding=".webp", encode_params=[cv2.IMWRITE_WEBP_QUALITY, 20], shape=(width, height), )
def fn(frame): nonlocal mapx, mapy, roi, first_frame if first_frame: first_frame = False frame_height, frame_width = frame.shape[0], frame.shape[1] (mapx, mapy), roi, _ = undistort.get_undistort_mapping( frame_width, frame_height, cam_attr ) # write over write frame and use the corrected write_frame = undistort.undistort_image(frame, (mapx, mapy)) # if homography is not None: # write_frame = calib.transform_image(write_frame, homography, screen_x_res) return write_frame
def callback(self,data): #global cont try: cv_image = self.bridge.imgmsg_to_cv2(data, "bgr8") except CvBridgeError as e: print(e) #if(cont%15==0): (rows,cols,channels) = cv_image.shape m = cv2.getRotationMatrix2D(((cols-1)/2.0, (rows-1)/2.0), 90, 1) cv_image = cv2.warpAffine(cv_image, m, (cols, rows)) cv_image= ud.undistort_image(cv_image) cv_image = realzado.realzado(cv_image) #rospy.loginfo(cont) #cv2.imshow("Image window", cv_image) #cv2.waitKey(3) try: self.image_pub.publish(self.bridge.cv2_to_imgmsg(cv_image, "bgr8")) except CvBridgeError as e: print(e)
def process_image(img): """ Pipeline for processing each frame of the video """ # ------------------------- define camera matrix and distortion coefficient mtx = np.array([[1.15777818e+03, 0.00000000e+00, 6.67113857e+02], [0.00000000e+00, 1.15282217e+03, 3.86124583e+02], [0.00000000e+00, 0.00000000e+00, 1.00000000e+00]]) dist_coeff = np.array( [[-0.24688507, -0.02373155, -0.00109831, 0.00035107, -0.00259868]]) # --------------------------------------------------------- undistort image undist = undistort_image(img, mtx, dist_coeff) # --------------------------------------------------- generate binary image hls = create_binary_image(undist) # ------------------------------------- transform to bird's eye perspective warped, src, dst = do_perspective_transform(hls, line_fit) # ---------------------------------------------------------- fit polynomial leftx, lefty, rightx, righty, l_left_seg, l_right_seg,\ leftx_base, rightx_base = find_lanes(warped) # ------------------------------ color detected lane pixels in warped image lane_pix = color_lane_pixels(np.zeros_like(warped), leftx, lefty, rightx, righty) # ------------------------------------- unwarp image of colored lane pixels lane_pix_unwarped = warp_image(lane_pix, dst, src) # ------superpose image of unwarped lane pixels image on original image and # -----------------------------add curvature and lane departure information res_img = final_image(img, lane_pix_unwarped, img.shape[0], leftx, lefty, rightx, righty, l_left_seg, l_right_seg, src, leftx_base, rightx_base) return res_img
# ------------------------------------------------------ first calibrate camera # get camera matrix and distortion coefficients mtx, dist_coeff = calibrate_camera() # --------------------------------------------------------------- import images # read in all images with names with pattern *.jpg images = glob.glob('../test_images/*.jpg') # loop over images for file_name in images: img = mpimg.imread(file_name) # --------------------------------------------------------- undistort image undist = undistort_image(img, mtx, dist_coeff) # - convert to hls color space and apply threshold to generate binary image binary = create_binary_image(undist) # ------------------------------------- transform to bird's eye perspective warped, src, dst = do_perspective_transform(binary, Fit()) # ---------------------------------------------------------- fit polynomial leftx_base, rightx_base = find_starting_points(warped) leftx, lefty, rightx, righty, l_left_seg, l_right_seg, output_img =\ find_lane_pixels(warped, leftx_base, rightx_base) # ------------------------------ color detected lane pixels in warped image lane_pix = color_lane_pixels(np.zeros_like(warped), leftx, lefty, rightx, righty)
def take_tmp_photo(self): raw_path, persp_path, raw_image = self.take_raw_photo() undistorted = undistort_image(raw_image) os.remove(raw_path) return undistorted
def take_photo(self): _, persp_path, raw_image = self.take_raw_photo() undistorted = undistort_image(raw_image) cv2.imwrite(persp_path, undistorted) self.last_photo = persp_path return persp_path, undistorted
import glob import matplotlib.image as mpimg from camera_calibration import calibrate_camera from undistort import undistort_image from visualization import save_result_image # get calibration matrix mtx, dst = calibrate_camera() # read in all images with names with pattern calibration*.jpg images = glob.glob('../camera_cal/calibration*.jpg') # loop over images and undistort them for file_name in images: # read in image img = mpimg.imread(file_name) # undistort image undist_img = undistort_image(img, mtx, dst) # save resulting image save_result_image(undist_img, "../output_images/undistorted_cal_images", file_name, "-undist")
cap = cv2.VideoCapture('media/mejorado2.webm') while True: #leemos un frame lo invertimos 90º y lo guardamos rval, frame = cap.read() if rval: frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) # *** Esto para preprocesar captura de video de cámara MASHI rows, cols = frame.shape # rota la imagen 90ª en sentido antihoraio m = cv2.getRotationMatrix2D(((cols - 1) / 2.0, (rows - 1) / 2.0), 90, 1) frame = cv2.warpAffine(frame, m, (cols, rows)) # Corrige distorsión por ojo de pez frame = ud.undistort_image(frame) # *** Hasta aquí preprocesamiento por el MASHI #invierte la imagen con respecto al eje vertical frame = cv2.flip(frame, 1, 0) #redimensionar la imagen mini = cv2.resize( frame, (int(frame.shape[1] / size), int(frame.shape[0] / size))) """buscamos las coordenadas de los rostros (si los hay) y guardamos su posicion""" faces = face_cascade.detectMultiScale(mini) for i in range(len(faces)): face_i = faces[i] (x, y, w, h) = [v * size for v in face_i]