Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix some little problems #2

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
111 changes: 49 additions & 62 deletions camera_calibration/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,28 +61,15 @@ def calibrate(directory, rows, cols, space, win=11, save=True,
# Setup colored output
init()

if len(directory_out) and (directory_out[0] == '/' or
directory_out[0] == '\\'):
directory_out = directory_out[1:]
if len(directory_out) and (directory_out[-1] == '/' or
directory_out[-1] == '\\'):
directory_out = directory_out[:-1]

if len(directory) and (directory[0] == '/' or directory[0] == '\\'):
directory = directory[1:]
if len(directory) and (directory[-1] == '/' or directory[-1] == '\\'):
directory = directory[:-1]

print Fore.WHITE + Style.BRIGHT + Back.MAGENTA + "\nWelcome\n"
print(Fore.WHITE + Style.BRIGHT + Back.MAGENTA + "\nWelcome\n")

# Figure out where the images are and get all file_names for that directory
target_directory = os.path.join(os.getcwd(), directory)
directory_out = os.path.join(os.getcwd(), directory_out)
print "Searching for images in: " + target_directory
target_directory = directory
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should sanitize with expand user

print("Searching for images in: " + target_directory)
file_names = os.listdir(target_directory)
print "Found Images:"
print("Found Images:")
for name in file_names:
print "\t" + name
print("\t" + name)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would be great to at some point change all of these to f-strings, but not critical


if visualize:
print ("\nYou have enabled visualizations.\n\tEach visualization will "
Expand All @@ -91,9 +78,9 @@ def calibrate(directory, rows, cols, space, win=11, save=True,

# Check grid symmetry
if rows == cols:
print (Style.BRIGHT + Back.RED + "It is best to use an asymmetric grid,"
" rows and cols should be different")
print(Style.RESET_ALL)
print((Style.BRIGHT + Back.RED + "It is best to use an asymmetric grid,"
" rows and cols should be different"))
print((Style.RESET_ALL))
exit()

# prepare object points, like (0,0,0), (1,0,0), (2,0,0) ....,(6,5,0)
Expand All @@ -119,12 +106,12 @@ def calibrate(directory, rows, cols, space, win=11, save=True,

# Check if output directory exists, if not, make it.
if save:
print (Style.BRIGHT + Back.MAGENTA + "\nSaving output to: " +
directory_out)
print((Style.BRIGHT + Back.MAGENTA + "\nSaving output to: " +
directory_out))
if not os.path.exists(directory_out):
os.makedirs(directory_out)
print Style.BRIGHT + Back.GREEN + "\tMade a new output directory"
print "\n"
print(Style.BRIGHT + Back.GREEN + "\tMade a new output directory")
print("\n")

#########################################################################
image_size = None
Expand All @@ -136,7 +123,7 @@ def calibrate(directory, rows, cols, space, win=11, save=True,

# If the image_file isn't an image, move on
if img is not None:
print Style.BRIGHT + Back.CYAN + "searching image: " + image_file
print(Style.BRIGHT + Back.CYAN + "searching image: " + image_file)

if visualize:
cv2.imshow('Raw Image', img)
Expand All @@ -153,8 +140,8 @@ def calibrate(directory, rows, cols, space, win=11, save=True,

# If we found chessboard corners lets work on them
if re_projection_error:
print (Style.BRIGHT + Back.GREEN + "\tfound " +
("centers" if circles else "corners"))
print((Style.BRIGHT + Back.GREEN + "\tfound " +
("centers" if circles else "corners")))
object_points.append(object_point)

# Since this is a good image, we will take its size as the
Expand Down Expand Up @@ -191,8 +178,8 @@ def calibrate(directory, rows, cols, space, win=11, save=True,
corners2 = corners

image_points.append(corners2)
print (Style.BRIGHT + Back.GREEN +
"\t\tfound sub-pixel corners")
print((Style.BRIGHT + Back.GREEN +
"\t\tfound sub-pixel corners"))
new_color_image = \
cv2.drawChessboardCorners(color_image, (cols, rows),
corners2, re_projection_error)
Expand All @@ -210,25 +197,25 @@ def calibrate(directory, rows, cols, space, win=11, save=True,
color_image)
cv2.waitKey(5000)
else:
print (Style.BRIGHT + Back.RED + "\tcould not find " +
("Centers" if circles else "Corners"))
print "\n"
print((Style.BRIGHT + Back.RED + "\tcould not find " +
("Centers" if circles else "Corners")))
print("\n")

# Check how many good images we found
if number_found >= 10:
print (Style.BRIGHT + Back.GREEN + "Found " + str(number_found) +
" calibratable images.")
print((Style.BRIGHT + Back.GREEN + "Found " + str(number_found) +
" calibratable images."))
elif number_found == 0:
print (Style.BRIGHT + Back.RED + "Found " + str(number_found) +
" calibratable images. \nNow Exiting")
print(Style.RESET_ALL)
print((Style.BRIGHT + Back.RED + "Found " + str(number_found) +
" calibratable images. \nNow Exiting"))
print((Style.RESET_ALL))
exit()
else:
print (Style.BRIGHT + Back.YELLOW + "Found " + str(number_found) +
" calibratable images.")
print((Style.BRIGHT + Back.YELLOW + "Found " + str(number_found) +
" calibratable images."))

#######################################################################
print Style.BRIGHT + Back.CYAN + "Beginning Calibration"
print(Style.BRIGHT + Back.CYAN + "Beginning Calibration")

# Execute calibration
(re_projection_error, camera_matrix, distortion_coefficients,
Expand All @@ -241,7 +228,7 @@ def calibrate(directory, rows, cols, space, win=11, save=True,
camera_matrix, distortion_coefficients, (w, h), 1, (w, h))

# Go through all images and undistort them
print Style.BRIGHT + Back.CYAN + "Beginning Undistort"
print(Style.BRIGHT + Back.CYAN + "Beginning Undistort")
i = 0
for image_file in file_names:
image_file = os.path.join(target_directory, image_file)
Expand All @@ -251,7 +238,7 @@ def calibrate(directory, rows, cols, space, win=11, save=True,

# If the image_file isn't an image, move on
if img is not None:
print Style.BRIGHT + Back.CYAN + "undistorting image: " + image_file
print(Style.BRIGHT + Back.CYAN + "undistorting image: " + image_file)

if visualize:
cv2.imshow('Raw Image', img)
Expand All @@ -274,34 +261,34 @@ def calibrate(directory, rows, cols, space, win=11, save=True,
i += 1

#########################################################################
print "\n"
print Fore.WHITE + Style.BRIGHT + Back.MAGENTA + "Intrinsic Matrix:"
print Fore.WHITE + Style.BRIGHT + Back.MAGENTA + str(camera_matrix)
print "\n"
print Fore.WHITE + Style.BRIGHT + Back.MAGENTA + "Distortion Matrix:"
print (Fore.WHITE + Style.BRIGHT + Back.MAGENTA +
str(distortion_coefficients))
print "\n"
print Fore.WHITE + Style.BRIGHT + Back.MAGENTA + "Optimal Camera Matrix:"
print Fore.WHITE + Style.BRIGHT + Back.MAGENTA + str(new_camera_matrix)
print "\n"
print (Fore.WHITE + Style.BRIGHT + Back.MAGENTA +
"Optimal Camera Matrix Crop:")
print Fore.WHITE + Style.BRIGHT + Back.MAGENTA + str(roi)
print("\n")
print(Fore.WHITE + Style.BRIGHT + Back.MAGENTA + "Intrinsic Matrix:")
print(Fore.WHITE + Style.BRIGHT + Back.MAGENTA + str(camera_matrix))
print("\n")
print(Fore.WHITE + Style.BRIGHT + Back.MAGENTA + "Distortion Matrix:")
print((Fore.WHITE + Style.BRIGHT + Back.MAGENTA +
str(distortion_coefficients)))
print("\n")
print(Fore.WHITE + Style.BRIGHT + Back.MAGENTA + "Optimal Camera Matrix:")
print(Fore.WHITE + Style.BRIGHT + Back.MAGENTA + str(new_camera_matrix))
print("\n")
print((Fore.WHITE + Style.BRIGHT + Back.MAGENTA +
"Optimal Camera Matrix Crop:"))
print(Fore.WHITE + Style.BRIGHT + Back.MAGENTA + str(roi))

# Calculate Re-projection Error
tot_error = 0
for i in xrange(len(object_points)):
for i in range(len(object_points)):
image_points_2, _ = cv2.projectPoints(
object_points[i], rotation_vectors[i], translation_vectors[i],
camera_matrix, distortion_coefficients)
error = cv2.norm(image_points[i], image_points_2,
cv2.NORM_L2) / len(image_points_2)
tot_error += error
mean_error = tot_error / len(object_points)
print "\n"
print (Fore.WHITE + Style.BRIGHT + Back.MAGENTA + "Re-projection Error: " +
str(mean_error))
print("\n")
print((Fore.WHITE + Style.BRIGHT + Back.MAGENTA + "Re-projection Error: " +
str(mean_error)))

if save:
with open(os.path.join(directory_out, 'result.txt'), 'w') as \
Expand Down Expand Up @@ -337,5 +324,5 @@ def calibrate(directory, rows, cols, space, win=11, save=True,
result_json_file:
json.dump(json_dict, result_json_file, indent=4)

print(Style.RESET_ALL)
print((Style.RESET_ALL))
cv2.destroyAllWindows()
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def readme():
install_requires=[
'argparse',
'numpy',
'cv2',
'opencv-python',
'colorama'
],
zip_safe=True,
Expand Down