@@ -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