Skip to content

Commit

Permalink
2021 lab
Browse files Browse the repository at this point in the history
  • Loading branch information
siangyang committed Aug 1, 2021
1 parent 397bc8e commit e136bf7
Show file tree
Hide file tree
Showing 16 changed files with 114 additions and 30 deletions.
Binary file added Lab_1/Face_dataset/mean.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Lab_1/Face_dataset/recon_50.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Lab_1/GT/1-1/fig_grad_m.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Lab_1/GT/1-1/fig_grad_x.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Lab_1/GT/1-1/fig_grad_y.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Lab_1/GT/1-2/1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Lab_1/GT/1-2/2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Lab_1/GT/1-2/3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Lab_1/GT/1-2/7_2_n100.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Lab_1/GT/1-2/7_2_n3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Lab_1/GT/1-2/mean.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
11 changes: 9 additions & 2 deletions Lab_1/Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@ You can only use the python packages below:
- sklearn
- math

## Evaluation
For your output image, you could use eval.py to check your answer.
```
$ python3 eval.py --gt GT/1-1/fig_grad_x.jpg --out output/1-1/fig_grad_y.jpg
```
It will show that you "PASS!" or "FAIL." this question.

## 1. Image Filtering
1. Load "**fig.jpg**" with **gray** scale.
2. Compute and save the results of gradient generated by horizontal Sobel filter and vertical Sobel filter as **fig_grad_x.jpg** and **fig_grad_y.jpg** (with cv2.filter2D).
Expand All @@ -32,11 +39,11 @@ The dataset you need is under `Face_dataset/` directory, which contains 56×46 p

1. Perform PCA on the **training set**. Save the **mean face** and the first **three** eigenfaces.
- save to [mean.png], [1.png], [2.png], [3.png]
- Be careful the values in eigenvectors after PCA ! (*cv2.PCACompute()*)
- Be careful the values in eigenvectors after PCA ! (*sklearn.decomposition.PCA*, you don't need to specify n_components value)
- Bonus: you can implement your own PCA function !!
- hint:  255 * (shifted **x**/ max(shifted **x**))

2. Take **7_2.png**, and project it onto the above PCA eigenspace. Reconstruct this image using the first n = 3, 100 eigenfaces. For each n, compute the mean square error (MSE) between the reconstructed face image and **7_2.png**. Please save these reconstructed images, and record the corresponding MSE values.
2. Take **7_2.png**, and project it onto the above PCA eigenspace. Reconstruct this image using the first n = 3, 100 eigenfaces. For each n, compute the mean square error (MSE) between the reconstructed face image and **7_2.png**. Please save these reconstructed images , [7_2_n3.png] and [7_2_n100.png], and record the corresponding MSE values.

3. Apply the **k-nearest neighbors classifier** to recognize **test set** images. For simplicity, try k={1,3} and n = {3,100}. Report the recognition rate on the test set with different (k,n) pairs.
- hint: *sklearn.neighbors.KNeighborsClassifier()*
Expand Down
19 changes: 19 additions & 0 deletions Lab_1/eval.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import os
import numpy as np
import argparse
import cv2

def main():
parser = argparse.ArgumentParser()
parser.add_argument('--gt', help='ground truth path')
parser.add_argument('--out', help='your output file path')

args = parser.parse_args()
try:
if (cv2.imread(os.path.join(args.gt)) - cv2.imread(os.path.join(args.out))).sum() == 0.0: print("PASS!")
else: print("FAIL!")
except:
print("FAIL!")

if __name__ == '__main__':
main()
114 changes: 86 additions & 28 deletions Lab_2/main.py
Original file line number Diff line number Diff line change
@@ -1,28 +1,86 @@
import numpy as np
import numpy.linalg as LA
import cv2


# u, v are N-by-2 matrices, representing N corresponding points for v = T(u)
# this function should return a 3-by-3 homography matrix
def solve_homography(u, v):
# TODO
return H

def main():

# Input Initialization
corners = np.array([[190, 106], [256, 142], [193, 244], [258, 285]]) # Points [[x1,y1], [x2,y2], [x3,y3], [x4,y4]]
img_path = './screen.jpg'

# Output initialization (create an output array to save the result)
new_h, new_w = 300, 300

# TODO Solving homography matrix for backward warping (hints: instead of solve v = Hu, solve u = (H^-1)v)

# TODO Backward Warping



if __name__ == '__main__':
main()
import numpy as np
import numpy.linalg as LA
import cv2


# u, v are N-by-2 matrices, representing N corresponding points for v = T(u)
# this function should return a 3-by-3 homography matrix
def solve_homography(u, v):
"""
This function should return a 3-by-3 homography matrix,
u, v are N-by-2 matrices, representing N corresponding points for v = T(u)
:param u: N-by-2 source pixel location matrices
:param v: N-by-2 destination pixel location matrices
:return:
"""
# TODO: 1.forming A

# TODO: 2.solve H with A

return H

def warping(src, dst, H, ymin, ymax, xmin, xmax):
"""
Perform forward/backward warpping without for loops. i.e.
for all pixels in src(xmin~xmax, ymin~ymax), warp to destination
(xmin=0,ymin=0) source destination
|--------| |------------------------|
| | | |
| | warp | |
forward warp | | ---------> | |
| | | |
|--------| |------------------------|
(xmax=w,ymax=h)
for all pixels in dst(xmin~xmax, ymin~ymax), sample from source
source destination
|--------| |------------------------|
| | | (xmin,ymin) |
| | warp | |--| |
backward warp | | <--------- | |__| |
| | | (xmax,ymax)|
|--------| |------------------------|
:param src: source image
:param dst: destination output image
:param H:
:param ymin: lower vertical bound of the destination(source, if forward warp) pixel coordinate
:param ymax: upper vertical bound of the destination(source, if forward warp) pixel coordinate
:param xmin: lower horizontal bound of the destination(source, if forward warp) pixel coordinate
:param xmax: upper horizontal bound of the destination(source, if forward warp) pixel coordinate
:param direction: indicates backward warping or forward warping
:return: destination output image
"""

# TODO: 1.meshgrid the (x,y) coordinate pairs

# TODO: 2.reshape the destination pixels as N x 3 homogeneous coordinate

# TODO: 3.apply H_inv to the destination pixels and retrieve (u,v) pixels, then reshape to (ymax-ymin),(xmax-xmin)

# TODO: 4.calculate the mask of the transformed coordinate (should not exceed the boundaries of source image)

# TODO: 5.sample the source image with the masked and reshaped transformed coordinates

# TODO: 6. assign to destination image with proper masking


return dst

def main():

# Input Initialization
corners = np.array([[190, 106], [256, 142], [193, 244], [258, 285]]) # Points [[x1,y1], [x2,y2], [x3,y3], [x4,y4]]
img_path = './screen.jpg'

# Output initialization (create an output array to save the result)
new_h, new_w = 300, 300

# TODO Solving homography matrix for backward warping (hints: instead of solve v = Hu, solve u = (H^-1)v)

# TODO Backward Warping



if __name__ == '__main__':
main()
Binary file added Lab_2/out.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified python_opencv_lab_2021.pdf
Binary file not shown.

0 comments on commit e136bf7

Please sign in to comment.