-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
168 additions
and
94 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import cv2 | ||
import numpy as np | ||
import matplotlib.pyplot as plt | ||
import os | ||
import pdb | ||
|
||
from dlt import DLT_method, decompose_projection | ||
from ransac import ransac_estimation | ||
from zhang import zhang_calibration, predict_points_zhang | ||
from wireframe import predict_points, plot_points | ||
|
||
if __name__ == "__main__": | ||
# Use Zhangs method to find image points and world points | ||
folder = "myimages/" | ||
image_names = ["img_0.jpg", "img_1.jpg", "img_2.jpg", "img_3.jpg", "img_4.jpg", | ||
"img_5.jpg", "img_6.jpg", "img_7.jpg", "img_8.jpg", "img_9.jpg"] | ||
images = [cv2.imread(os.path.join(folder, x)) for x in image_names] | ||
|
||
ret, mtx, dist, rvecs, tvecs, objpoints, imgpoints = zhang_calibration(images, size=(15, 10)) | ||
|
||
pred_points = predict_points_zhang(rvecs[-1], tvecs[-1], mtx, dist, objpoints[-1])[0].reshape(150, 2) | ||
img = plot_points(images[-1], pred_points) | ||
|
||
plt.imshow(img) | ||
plt.show() | ||
print(mtx) | ||
|
||
image_points = imgpoints[-1].reshape(150, 2).astype(int) | ||
world_points = objpoints[-1].astype(int) | ||
projection_matrix = DLT_method(image_points[0:6], world_points[0:6]) | ||
K, C, R = decompose_projection(projection_matrix) | ||
print(K) | ||
print(C) | ||
print(R) | ||
pred_points = predict_points(projection_matrix, world_points) | ||
img = plot_points(images[-1], pred_points) | ||
|
||
plt.imshow(img) | ||
plt.show() | ||
|
||
projection_matrix = ransac_estimation(image_points, world_points) | ||
K, C, R = decompose_projection(projection_matrix) | ||
print(K) | ||
print(C) | ||
print(R) | ||
pred_points = predict_points(projection_matrix, world_points) | ||
img = plot_points(images[-1], pred_points) | ||
plt.imshow(img) | ||
plt.show() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import cv2 | ||
import matplotlib.pyplot as plt | ||
import numpy as np | ||
import pdb | ||
|
||
def remove_distortion(calib_img, img_points, world_points, distort_img): | ||
"""Removes the distortion from an image by finding camera properties.""" | ||
gray = cv2.cvtColor(calib_img, cv2.COLOR_BGR2GRAY) | ||
# pdb.set_trace() | ||
ret, mtx, dist, rvecs, tvecs = cv2.calibrateCamera(np.array([world_points]), np.array([img_points]), | ||
gray.shape[::-1], None, None) | ||
h, w = distort_img.shape[:2] | ||
newcameramtx, roi = cv2.getOptimalNewCameraMatrix(mtx, dist, (w, h), 1, (w, h)) | ||
dst = cv2.undistort(distort_img, mtx, dist, None, newcameramtx) | ||
x, y, w, h = roi | ||
dst = dst[y:y+h, x:x+w] | ||
|
||
return dst | ||
|
||
|
||
if __name__ == "__main__": | ||
from points import world_points, image_points | ||
calib_img = cv2.imread('Assignment1_Data/IMG_5455.JPG') | ||
|
||
distort_img = cv2.imread('Assignment1_Data/IMG_5455.JPG') | ||
dst = remove_distortion(calib_img, image_points, world_points, distort_img) | ||
|
||
plt.imshow(distort_img) | ||
plt.show() | ||
|
||
plt.imshow(dst) | ||
plt.show() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import cv2 | ||
import numpy as np | ||
import os | ||
import matplotlib.pyplot as plt | ||
|
||
from wireframe import plot_points | ||
|
||
|
||
def find_img_points(img, size=(8, 6)): | ||
criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 30, 0.001) | ||
objp = np.zeros((size[0]*size[1], 3), np.float32) | ||
objp[:, :2] = np.mgrid[0:size[0], 0:size[1]].T.reshape(-1, 2) | ||
|
||
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) | ||
ret, corners = cv2.findChessboardCorners(gray, size, None) | ||
if ret: | ||
corners2 = cv2.cornerSubPix(gray, corners, (11, 11), (-1, -1), criteria) | ||
return objp, corners2, gray.shape | ||
return None, None, None | ||
|
||
|
||
def zhang_calibration(images, size): | ||
objpoints = [] | ||
imgpoints = [] | ||
for i, img in enumerate(images): | ||
print("Processing image ", i) | ||
objp, imgp, shape = find_img_points(img, size) | ||
if objp is not None and imgp is not None: | ||
objpoints.append(objp) | ||
imgpoints.append(imgp) | ||
ret, mtx, dist, rvecs, tvecs = cv2.calibrateCamera(objpoints, imgpoints, shape[::-1], None, None) | ||
return ret, mtx, dist, rvecs, tvecs, objpoints, imgpoints | ||
|
||
|
||
def predict_points_zhang(rvecs, tvecs, mtx, dist, world_points): | ||
img_points = cv2.projectPoints(world_points, rvecs, tvecs, mtx, dist) | ||
return img_points | ||
|
||
|
||
if __name__ == "__main__": | ||
folder = "Assignment1_Data/" | ||
image_names = ["IMG_5456.JPG", "IMG_5457.JPG", "IMG_5458.JPG", "IMG_5459.JPG", "IMG_5460.JPG", | ||
"IMG_5461.JPG", "IMG_5462.JPG", "IMG_5463.JPG", "IMG_5464.JPG", "IMG_5465.JPG", | ||
"IMG_5466.JPG", "IMG_5467.JPG", "IMG_5468.JPG", "IMG_5469.JPG", "IMG_5470.JPG"] | ||
images = [cv2.imread(os.path.join(folder, x)) for x in image_names] | ||
|
||
ret, mtx, dist, rvecs, tvecs, objpoints, imgpoints = zhang_calibration(images, size=(8, 6)) | ||
pred_points = predict_points_zhang(rvecs[-1], tvecs[-1], mtx, dist, objpoints[-1])[0].reshape(48, 2) | ||
img = plot_points(images[-1], pred_points) | ||
|
||
plt.imshow(img) | ||
plt.show() | ||
print(mtx) |