diff --git a/A1/code.py b/A1/code.py deleted file mode 100644 index a660dfc..0000000 --- a/A1/code.py +++ /dev/null @@ -1,85 +0,0 @@ -import cv2 -import numpy as np -import matplotlib.pyplot as plt -import pdb -from scipy import linalg - - -def find_corners(img): - """Harris corner detection to find the points in the image.""" - gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) - # find Harris corners - gray = np.float32(gray) - dist = cv2.cornerHarris(gray, 2, 3, 0.04) - dist = cv2.dilate(dist, None) - ret, dist = cv2.threshold(dist, 0.01*dist.max(), 255, 0) - dist = np.uint8(dist) - - # find centroids - ret, labels, stats, centroids = cv2.connectedComponentsWithStats(dist) - - # define the criteria to stop and refine the corners - criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 100, 0.001) - corners = cv2.cornerSubPix(gray, np.float32(centroids), (5, 5), (-1, -1), criteria) - - # Now draw them - res = np.hstack((centroids, corners)) - res = np.int0(res) - # img[res[:,1],res[:,0]]=[0,0,255] - for i in range(res.shape[0]): - x, y = res[i, 3], res[i, 2] - img[x-2:x+2, y-2:y+2] = [0, 255, 0] - - plt.imshow(img) - plt.show() - pdb.set_trace() - - -def ransac_camera_calibrate(image_points, world_points): - N = image_points.shape[0] - n = 100 - min_error = 10**8 - opt_P = np.zeros((3, 4)) - opt_i = -1 - for i in range(n): - m = np.random.choice(N, 6, replace=False) - i_points = image_points[m] - w_points = world_points[m] - P = camera_calibrate(i_points, w_points) - m_ = np.array(list(set(range(N)) - set(m))) - test_i_points = image_points[m_] - test_w_points = world_points[m_] - test_w_points = np.concatenate([test_w_points, np.ones((len(m_), 1))], axis=-1) - pdb.set_trace() - predicted_i_points = np.dot(P, test_w_points.T).T - predicted_i_points = predicted_i_points/predicted_i_points[:, -1].reshape(len(m_), 1) - error = ((predicted_i_points[:, :2] - test_i_points)**2).sum()/len(m_) - if error < min_error: - min_error = error - opt_P = P - opt_i = i - - print(min_error, opt_P, opt_i) - return opt_P - - -if __name__ == "__main__": - img = cv2.imread('Assignment1_Data/measurements.jpg') - world_points = [[36, 0, 0], [0, 0, 36], [0, 36, 0], - [36, 36, 0], [36, 0, 36], [0, 0, 72]] - image_points = [[396.505, 209.674], [473.951, 246.394], [486.636, 138.904], - [402.514, 132.227], [385.822, 237.047], [465.94, 277.77]] - P = camera_calibrate(image_points, world_points) - K, R, C = decompose_projection(P) - - image_points = [] - world_points = [] - with open('points.txt') as f: - for line in f: - line = line.strip().split(',') - world_points.append([float(i.strip()) for i in line[0:3]]) - image_points.append([float(i.strip()) for i in line[3:]]) - image_points = np.array(image_points) - world_points = np.array(world_points) - ransac_camera_calibrate(image_points, world_points) - # find_corners(img) diff --git a/A1/dlt.py b/A1/dlt.py index 32a5f36..293c843 100644 --- a/A1/dlt.py +++ b/A1/dlt.py @@ -70,6 +70,8 @@ def find_projection_error(P, image_points, world_points): [402.514, 132.227], [385.822, 237.047], [465.94, 277.77]]) projection_matrix = DLT_method(image_points, world_points) K, R, C = decompose_projection(projection_matrix) - + print(K) + print(R) + print(C) error = find_projection_error(projection_matrix, image_points, world_points) print(error) diff --git a/A1/own_camera.py b/A1/own_camera.py new file mode 100644 index 0000000..2c6208f --- /dev/null +++ b/A1/own_camera.py @@ -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() diff --git a/A1/radial.py b/A1/radial.py index e69de29..ee51ff6 100644 --- a/A1/radial.py +++ b/A1/radial.py @@ -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() diff --git a/A1/ransac.py b/A1/ransac.py index 08a7c8b..59a6ea0 100644 --- a/A1/ransac.py +++ b/A1/ransac.py @@ -1,7 +1,7 @@ import numpy as np # import pdb -from dlt import DLT_method, find_projection_error +from dlt import DLT_method, find_projection_error, decompose_projection def ransac_estimation(image_points, world_points, iter=500): @@ -28,6 +28,7 @@ def ransac_estimation(image_points, world_points, iter=500): if __name__ == "__main__": from points import image_points, world_points projection_matrix = ransac_estimation(image_points, world_points) - - error = find_projection_error(projection_matrix, image_points, world_points) - print(error) + K, R, C = decompose_projection(projection_matrix) + print(K) + print(R) + print(C) diff --git a/A1/wireframe.py b/A1/wireframe.py index d0a5b2a..c56e650 100644 --- a/A1/wireframe.py +++ b/A1/wireframe.py @@ -1,10 +1,12 @@ import cv2 import numpy as np import matplotlib.pyplot as plt +import os import pdb from dlt import DLT_method from ransac import ransac_estimation +# from zhang import zhang_calibration, predict_points_zhang def predict_points(projection_matrix, world_points): @@ -34,22 +36,42 @@ def plot_wireframe(image, points): return img +def plot_points(image, points): + img = image.copy() + for (x, y) in points: + if x < 0 or x >= img.shape[0] or y < 0 or y >= img.shape[1]: + continue + img = cv2.circle(img, (x, y), 50, (255, 0, 0), -1) + return img + + if __name__ == "__main__": from points import image_points, world_points image = cv2.imread('Assignment1_Data/IMG_5455.JPG') projection_matrix = DLT_method(image_points, world_points) - pred_points = predict_points(projection_matrix[0:6], world_points[0:6]) - img = plot_wireframe(image, pred_points) + pred_points = predict_points(projection_matrix, world_points[0:6]) + img = plot_points(image, pred_points) plt.imshow(img) plt.show() projection_matrix = ransac_estimation(image_points, world_points) - pred_points = predict_points(projection_matrix, world_points) - img = plot_wireframe(image, pred_points) + img = plot_points(image, pred_points) + plt.imshow(img) + plt.show() + + 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_wireframe(images[-1], pred_points) plt.imshow(img) plt.show() diff --git a/A1/zhang.py b/A1/zhang.py new file mode 100644 index 0000000..c3012b7 --- /dev/null +++ b/A1/zhang.py @@ -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)