Skip to content

Commit

Permalink
A1 complete.
Browse files Browse the repository at this point in the history
  • Loading branch information
chrizandr committed Feb 3, 2019
1 parent 1b7fcdc commit 2eae332
Show file tree
Hide file tree
Showing 7 changed files with 168 additions and 94 deletions.
85 changes: 0 additions & 85 deletions A1/code.py

This file was deleted.

4 changes: 3 additions & 1 deletion A1/dlt.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
49 changes: 49 additions & 0 deletions A1/own_camera.py
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()
32 changes: 32 additions & 0 deletions A1/radial.py
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()
9 changes: 5 additions & 4 deletions A1/ransac.py
Original file line number Diff line number Diff line change
@@ -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):
Expand All @@ -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)
30 changes: 26 additions & 4 deletions A1/wireframe.py
Original file line number Diff line number Diff line change
@@ -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):
Expand Down Expand Up @@ -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()
53 changes: 53 additions & 0 deletions A1/zhang.py
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)

0 comments on commit 2eae332

Please sign in to comment.