MCPcopy Create free account
hub / github.com/drinkingcoder/NeuralMarker / blend_homography

Function blend_homography

evaluation_DVL.py:238–299  ·  view source on GitHub ↗
(scene_images, marker, source=None, blend_type='D', detector='SIFT', use_colormap=True)

Source from the content-addressed store, hash-verified

236 return blends, masks, blends_colormap
237
238def blend_homography(scene_images, marker, source=None, blend_type='D', detector='SIFT', use_colormap=True):
239 print('blend with homography')
240 if detector == 'ORB':
241 detect = cv2.ORB_create()
242 elif detector == 'SIFT':
243 detect = cv2.SIFT_create()
244 else:
245 raise ValueError('detector not implemented')
246 blends = []
247 masks = []
248 blends_colormap = []
249 bar = tqdm(enumerate(scene_images), total=len(scene_images))
250 for idx, scene in bar:
251 bar.set_description('Editing %d' % idx)
252 marker = cv2.resize(marker, (scene.shape[1], scene.shape[0]))
253 kp1, des1 = detect.detectAndCompute(marker, None)
254 kp2, des2 = detect.detectAndCompute(scene, None)
255 if des1 is None or des2 is None:
256 blends.append(None)
257 continue
258
259 if detector == 'ORB':
260 bf = cv2.BFMatcher(cv2.NORM_HAMMING, crossCheck=True)
261 matches = bf.match(des1, des2)
262
263 elif detector == 'SIFT':
264 FLANN_INDEX_KDTREE = 0
265 indexParams = dict(algorithm=FLANN_INDEX_KDTREE, trees=5)
266 searchParams = dict(checks=50)
267 flann = cv2.FlannBasedMatcher(indexParams, searchParams)
268 if len(des1)<2 or len(des2)<2:
269 blends.append(None)
270 continue
271 matches = flann.knnMatch(des1, des2, k=2)
272 matches = [m for m,n in matches if m.distance < 0.7*n.distance]
273 else:
274 raise ValueError('detector {} not implemented'.format(detector))
275 if len(matches) < 4:
276 blends.append(None)
277 continue
278
279 src_pts = np.float32([kp1[m.queryIdx].pt for m in matches[:50]]).reshape(-1,1,2)
280 dst_pts = np.float32([kp2[m.trainIdx].pt for m in matches[:50]]).reshape(-1,1,2)
281 M, _ = cv2.findHomography(src_pts, dst_pts, cv2.RANSAC, 5.0)
282 if M is None:
283 blends.append(None)
284 continue
285 out = cv2.warpPerspective(marker, M, (scene.shape[1], scene.shape[0]))
286
287 blend_i, mask_i = blend(out, source, scene, blend_type)
288
289 if use_colormap:
290 colormap = np.asarray(Image.open('./colormap.jpg'))
291 colormap = cv2.resize(colormap[:,:,::-1], (scene.shape[1], scene.shape[0]))
292 blend_colormap = cv2.warpPerspective(colormap, M, (scene.shape[1], scene.shape[0]))
293 blend_colormap, _ = blend(blend_colormap, source, scene, blend_type, use_colormap=use_colormap)
294 blends_colormap.append(blend_colormap)
295

Callers 1

evalFunction · 0.85

Calls 1

blendFunction · 0.70

Tested by

no test coverage detected