■ 이미지를 이어 붙이는 방법을 보여준다.
▶ 예제 코드 (PY)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
import matplotlib import numpy as np import skimage.color as color import skimage.feature as feature import skimage.io as io import skimage.measure as measure import skimage.transform as transform matplotlib.use("TkAgg") io.use_plugin("matplotlib") imageNDArray1 = io.imread("source1.png") imageNDArray2 = io.imread("source2.png") orb = feature.ORB(n_keypoints = 1000, fast_threshold = 0.05) orb.detect_and_extract(imageNDArray1) keypointNDArray1 = orb.keypoints descriptorNDArray1 = orb.descriptors orb.detect_and_extract(imageNDArray2) keypointNDArray2 = orb.keypoints descriptorNDArray2 = orb.descriptors matchNDArray = feature.match_descriptors(descriptorNDArray1, descriptorNDArray2, cross_check = True) sourceNDArray = keypointNDArray2[matchNDArray[:, 1]][:, ::-1] targetNDArray = keypointNDArray1[matchNDArray[:, 0]][:, ::-1] projectiveTransform, inlierNDArray = measure.ransac((sourceNDArray, targetNDArray), transform.ProjectiveTransform, min_samples = 4, residual_threshold = 2) y, x = imageNDArray2.shape[:2] cornerNDArray = np.array([[0, 0], [0, y], [x, 0],[x, y]]) warpedCornerNDArray = projectiveTransform(cornerNDArray) allCornerNDArray = np.vstack((warpedCornerNDArray, cornerNDArray)) minimumCornerNDArray = np.min(allCornerNDArray, axis = 0) maximumCornerNDArray = np.max(allCornerNDArray, axis = 0) outputShapeNDArray = (maximumCornerNDArray - minimumCornerNDArray) outputShapeNDArray = np.ceil(outputShapeNDArray[::-1]) similarityTransform = transform.SimilarityTransform(translation = -minimumCornerNDArray) warpImageNDArray1 = transform.warp(imageNDArray1, similarityTransform.inverse, output_shape = outputShapeNDArray, cval = -1) warpImageNDArray2 = transform.warp(imageNDArray2, (projectiveTransform + similarityTransform).inverse, output_shape = outputShapeNDArray, cval = -1) maskImageNDArray1 = (warpImageNDArray1 != -1) warpImageNDArray1[~maskImageNDArray1] = 0 alphaImageNDArray1 = np.dstack((color.gray2rgb(warpImageNDArray1), maskImageNDArray1)) maskImageNDArray2 = (warpImageNDArray2 != -1) warpImageNDArray2[~maskImageNDArray2] = 0 alphaImageNDArray2 = np.dstack((color.gray2rgb(warpImageNDArray2), maskImageNDArray2)) mergeImageNDArray = (alphaImageNDArray1 + alphaImageNDArray2) alphaImageNDArray = mergeImageNDArray[..., 3] mergeImageNDArray /= np.maximum(alphaImageNDArray, 1)[..., np.newaxis] io.imshow(mergeImageNDArray) io.show() |