Skip to content

Commit 0229102

Browse files
committed
A4 Q4 Done
1 parent 9c0f8ed commit 0229102

File tree

10 files changed

+42
-35
lines changed

10 files changed

+42
-35
lines changed
163 KB
Loading
102 KB
Loading
-117 KB
Loading
-151 KB
Loading
-212 KB
Binary file not shown.
-172 KB
Loading
124 KB
Loading

Assignment-4/Q4.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,21 +21,29 @@
2121
print("Image{}.shape: {}".format(3, Image3.shape))
2222

2323
# ----------------------------------------
24-
25-
# Image1 = cv2.resize(Image1)
24+
new_img = np.zeros((Image2.shape[0], Image1.shape[1]+Image2.shape[1]+Image3.shape[1], 3), dtype=np.uint8)
25+
new_img[0:Image2.shape[0], Image1.shape[1]:Image1.shape[1]+Image2.shape[1]] = Image2
26+
NewImage2 = new_img
27+
cv2.imwrite("./Data/Q4-output/NewImage2.png", NewImage2)
2628

2729
# Panarama Stiching ----------------------
30+
FinalImageSize = NewImage2.shape
31+
copySize = Image1.shape[1]
2832

2933
PS = PanoramaStiching()
3034

31-
Result1, Visualization1 = PS.StichImages(Image2, Image1)
35+
Result1, Visualization1 = PS.StichImages(Image1, NewImage2, FinalImageSize)
36+
Result1 = PS.Format(Result1, Image2, copySize)
3237
cv2.imwrite(SAVE + "result1.png", Result1)
3338
cv2.imwrite(SAVE + "Visualization1.png", Visualization1)
3439

35-
# Result2, Visualization2 = PS.StichImages(Image3, Image2)
40+
# Result2, Visualization2 = PS.StichImages(Image3, Image2, FinalImageSize)
3641
# cv2.imwrite(SAVE + "result2.png", Result2)
3742
# cv2.imwrite(SAVE + "Visualization2.png", Visualization2)
3843

39-
Result3, Visualization3 = PS.StichImages(Image3, Result1)
44+
copySize = Image1.shape[1] + Image2.shape[1]
45+
Result3, Visualization3 = PS.StichImages(Image3, Result1, FinalImageSize)
46+
Result3 = PS.Format2(Result3, Result1, copySize)
4047
cv2.imwrite(SAVE + "result3.png", Result3)
4148
cv2.imwrite(SAVE + "Visualization3.png", Visualization3)
49+
3 KB
Binary file not shown.

Assignment-4/utils.py

Lines changed: 29 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,28 @@ class PanoramaStiching:
1111
def __init__(self):
1212
return
1313

14-
# def combine_images(self, img0, img1, h_matrix):
15-
# points0 = np.array([[0, 0], [0, img0.shape[0]], [img0.shape[1], img0.shape[0]], [img0.shape[1], 0]], dtype=np.float32)
16-
# points0 = points0.reshape((-1, 1, 2))
17-
# points1 = np.array([[0, 0], [0, img1.shape[0]], [img1.shape[1], img0.shape[0]], [img1.shape[1], 0]], dtype=np.float32)
18-
# points1 = points1.reshape((-1, 1, 2))
19-
# points2 = cv2.perspectiveTransform(points1, h_matrix)
20-
# points = np.concatenate((points0, points2), axis=0)
21-
# [x_min, y_min] = np.int32(points.min(axis=0).ravel() - 0.5)
22-
# [x_max, y_max] = np.int32(points.max(axis=0).ravel() + 0.5)
23-
# H_translation = np.array([[1, 0, -x_min], [0, 1, -y_min], [0, 0, 1]])
24-
25-
# output_img = cv2.warpPerspective(img1, H_translation.dot(h_matrix), (x_max - x_min, y_max - y_min))
26-
# output_img[-y_min:img0.shape[0] - y_min, -x_min:img0.shape[1] - x_min] = img0
27-
# return output_img
28-
29-
def StichImages(self, img1, img2):
14+
# def RemoveBorder(self, image):
15+
# gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
16+
# _, thresh = cv2.threshold(gray, 1, 255, cv2.THRESH_BINARY)
17+
# contours, hierarchy = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
18+
# cnt = contours[0]
19+
# x,y,w,h = cv2.boundingRect(cnt)
20+
# crop = image[y : y+h, x : x+w]
21+
# return crop
22+
23+
def Format(self, res, img, width):
24+
print(res.shape)
25+
print(width, img.shape[1])
26+
res[0:img.shape[0], width : width+img.shape[1]] = img
27+
return res
28+
29+
def Format2(self, res, img, width):
30+
print(res.shape)
31+
print(width, img.shape[1])
32+
res[0:img.shape[0], 0 : width] = img[0:img.shape[0], 0 : width]
33+
return res
34+
35+
def StichImages(self, img1, img2, resultSize):
3036

3137
kp1, des1 = self.DetectKeyPoints(img1)
3238
kp2, des2 = self.DetectKeyPoints(img2)
@@ -38,23 +44,16 @@ def StichImages(self, img1, img2):
3844

3945
FinalMatches, Homography, Status = Matches
4046

41-
# Result = self.combine_images(img1, img2, Homography)
42-
4347
# stiching the images based on the Homography
44-
Result = cv2.warpPerspective(img1, Homography, (img1.shape[1] + img2.shape[1], max(img1.shape[0], img2.shape[0])))
48+
Result = cv2.warpPerspective(img1, Homography, (resultSize[1], resultSize[0]))
49+
50+
# Result = cv2.warpPerspective(img1, Homography, (img1.shape[1] + img2.shape[1], max(img1.shape[0], img2.shape[0])))
51+
cv2.imwrite("./Data/Q4-output/temp.png", Result)
4552
# cv2.imshow("Result", Result)
4653
# cv2.waitKey(0)
47-
# for i in range(img2.shape[0]):
48-
# for j in range(img2.shape[1]):
49-
# if img2[i, j, 0] == 0 and img2[i, j, 1] == 0 and img2[i, j, 2] == 0:
50-
# continue
51-
# Result[i, j, 0] = img2[i, j, 0]
52-
# Result[i, j, 1] = img2[i, j, 1]
53-
# Result[i, j, 2] = img2[i, j, 2]
54-
55-
# Result = cv2.resize(Result, None, fx=Result.shape[0], fy=Result.shape[1], interpolation = cv2.INTER_CUBIC)
56-
Result[0 : img2.shape[0], 0 : img2.shape[1]] = img2
57-
54+
# Result[0 : img2.shape[0], 0 : img2.shape[1]] = img2
55+
# Result = self.RemoveBorder(Result)
56+
5857
StichedImage = self.DrawMatches(img1, img2, kp1, kp2, FinalMatches, Status)
5958
return Result, StichedImage
6059

0 commit comments

Comments
 (0)