def plotMatches(frame1, kp1, frame2, kp2, matches, nFrame): imMatches = drawMatches(frame1, kp1, frame2, kp2, matches) imName = "Matches between frame " + str(nFrame) + " and frame " + str(nFrame + 1) # cv2.namedWindow(imName, cv2.WINDOW_AUTOSIZE); cv2.namedWindow(imName, cv2.WINDOW_NORMAL) cv2.resizeWindow(imName, 1280, 480) cv2.imshow(imName, imMatches) cv2.waitKey(0) cv2.destroyWindow(imName) cv2.imwrite("frame matching.png", imMatches)
def plotMatches(frame1, kp1, frame2, kp2, matches, nFrame): imMatches = drawMatches(frame1, kp1, frame2, kp2, matches) imName = "Matches between frame " + str(nFrame) + " and frame " + str( nFrame + 1) #cv2.namedWindow(imName, cv2.WINDOW_AUTOSIZE); cv2.namedWindow(imName, cv2.WINDOW_NORMAL) cv2.resizeWindow(imName, 1280, 480) cv2.imshow(imName, imMatches) cv2.waitKey(0) cv2.destroyWindow(imName) cv2.imwrite("frame matching.png", imMatches)
def compute_location(self, kp1, des1, kp2, des2): """ compute the global location of center of current image :param kp1: captured keyPoints :param des1: captured descriptions :param kp2: map keyPoints :param des2: map descriptions :return: global pose """ good = [] pose = None if des1 is not None and des2 is not None: if len(des2) > 0: matches = self.matcher.knnMatch(des1, des2, k=2) for match in matches: if len(match) > 1 and match[0].distance < MATCH_RATIO * match[1].distance: good.append(match[0]) # Need to draw only good matches, so create a mask # print 'compute_location' gray2 = cv2.cvtColor(self.map_image,cv2.COLOR_BGR2GRAY) gray1 = cv2.cvtColor(self.cur_image,cv2.COLOR_BGR2GRAY) out = drawMatches(gray1,kp1,gray2,kp2,good) cv2.namedWindow('Matches', cv2.WINDOW_NORMAL) cv2.imshow('Matches',out) if len(good) > MIN_MATCH_COUNT: src_pts = np.float32([kp1[m.queryIdx].pt for m in good]).reshape(-1, 1, 2) dst_pts = np.float32([kp2[m.trainIdx].pt for m in good]).reshape(-1, 1, 2) transform = cv2.estimateRigidTransform(src_pts, dst_pts, False) if transform is not None: transformed_center = cv2.transform(CAMERA_CENTER, transform) # get global pixel transformed_center = [transformed_center[0][0][0] / METER_TO_PIXEL, # map to global pose (MAP_PIXEL_HEIGHT - 1 - transformed_center[0][0][1]) / METER_TO_PIXEL] yaw = np.arctan2(transform[1, 0], transform[0, 0]) # get global heading # correct the pose if the drone is not level z = math.sqrt(self.z ** 2 / (1 + math.tan(self.angle_x) ** 2 + math.tan(self.angle_y) ** 2)) offset_x = np.tan(self.angle_x) * z offset_y = np.tan(self.angle_y) * z global_offset_x = math.cos(yaw) * offset_x + math.sin(yaw) * offset_y global_offset_y = math.sin(yaw) * offset_x + math.cos(yaw) * offset_y pose = [transformed_center[0] + global_offset_x, transformed_center[1] + global_offset_y, z, yaw] return pose, len(good)
def addSecondToFirst(imga, imgb): images = [ # cv2.cvtColor(imga, cv2.COLOR_RGB2GRAY).astype(np.uint8), # cv2.cvtColor(imgb, cv2.COLOR_RGB2GRAY).astype(np.uint8) imga, imgb ] imageKpDes = [sift.detectAndCompute(img, None) for img in images] matches = bf.knnMatch(imageKpDes[0][1], imageKpDes[1][1], k=2) per20 = len(matches) / 6 good = sorted(matches, key=lambda (m, n): m.distance - n.distance)[:per20] good = [m[0] for m in good] leftPts = np.float32([imageKpDes[0][0][m.queryIdx].pt for m in good]) rightPts = np.float32([imageKpDes[1][0][m.trainIdx].pt for m in good]) H, mask = cv2.findHomography(rightPts, leftPts, cv2.RANSAC) cornerRT = np.float32([images[1].shape[1], 0]) cornerRB = np.float32([images[1].shape[1], images[1].shape[0]]) corners = np.float32([cornerRT, cornerRB]) corners = np.float32([corners]) corners = cv2.perspectiveTransform(corners, H) rightBorder = max(corners[0][0][0], corners[0][1][0]) imgH = cv2.warpPerspective(images[1], H, (rightBorder, images[0].shape[0])) # cv2.imshow('sad',imgH) imgLines = drawMatches(images[0], imageKpDes[0][0], images[1], imageKpDes[1][0], good) cv2.imshow('img',imgLines) cv2.waitKey() resSize = (images[0].shape[0], rightBorder) res = np.zeros(resSize, images[0].dtype) for i in range(0, resSize[0]): for j in range(0, images[0].shape[1]): res[i, j] = images[0][i, j] for i in range(0, resSize[0]): for j in range(0, imgH.shape[1]): res[i, j] = max(imgH[i, j], res[i, j]) return res
def main(): image1 = cv2.imread(filename1) if image1 is None: print 'Unable to open file ', filename1 return image2 = cv2.imread(filename2) if image2 is None: print 'Unable to open file ', filename2 return detector = cv2.ORB_create(nfeatures=5000, scoreType=cv2.ORB_FAST_SCORE, edgeThreshold=4) kp1 = detector.detect(image1, None) kp1, des1 = detector.compute(image1, kp1) kp2 = detector.detect(image2, None) kp2, des2 = detector.compute(image2, kp2) index_params = dict(algorithm=6, table_number=6, key_size=12, multi_probe_level=1) search_params = dict(checks=50) matcher = cv2.FlannBasedMatcher(index_params, search_params) matches = matcher.knnMatch(des1, des2, k=2) print len(matches) good = [] for match in matches: if len(match ) > 1 and match[0].distance < MATCH_RATIO * match[1].distance: good.append(match[0]) # Need to draw only good matches, so create a mask # print 'compute_location' gray1 = cv2.cvtColor(image1, cv2.COLOR_BGR2GRAY) gray2 = cv2.cvtColor(image2, cv2.COLOR_BGR2GRAY) #out = drawMatches(gray1,kp1,gray2,kp2,(match[0] for match in matches)) out = drawMatches(gray1, kp1, gray2, kp2, good) cv2.namedWindow('Matches', cv2.WINDOW_NORMAL) cv2.imshow('Matches', out) cv2.waitKey(0) cv2.destroyAllWindows()
def main(): image1 = cv2.imread(filename1) if image1 is None: print 'Unable to open file ', filename1 return image2 = cv2.imread(filename2) if image2 is None: print 'Unable to open file ', filename2 return #create a single feature map presuming map is not too much bigger than viewport # the edgeThreshold and patchSize can be tuned if the gap between cell is too large # MS Did just that, set edgeThreshold from 31 to 8 to get more features detector = cv2.ORB_create(nfeatures=MAP_FEATURES, scoreType=cv2.ORB_FAST_SCORE, edgeThreshold=8) kp1 = detector.detect(image1, None) kp1, des1 = detector.compute(image1, kp1) kp2 = detector.detect(image2, None) kp2, des2 = detector.compute(image2, kp2) index_params = dict(algorithm=6, table_number=6, key_size=12, multi_probe_level=1) search_params = dict(checks=50) matcher = cv2.FlannBasedMatcher(index_params, search_params) matches = matcher.knnMatch(des1, des2, k=2) good = [] for match in matches: if len(match ) > 1 and match[0].distance < MATCH_RATIO * match[1].distance: good.append(match[0]) gray1 = cv2.cvtColor(image1, cv2.COLOR_BGR2GRAY) gray2 = cv2.cvtColor(image2, cv2.COLOR_BGR2GRAY) out = drawMatches(gray1, kp1, gray2, kp2, good) cv2.namedWindow('Map comparison', cv2.WINDOW_NORMAL) cv2.imshow('Map comparison', out) cv2.waitKey(0) cv2.destroyAllWindows()
def getContours(frame): num2 = cv2.imread("number_two.jpg", cv2.IMREAD_GRAYSCALE) #bwframe = cv2.imread("number_two.jpg", cv2.IMREAD_GRAYSCALE) bwframe = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) sift = cv2.SIFT() kp1, des1 = sift.detectAndCompute(num2, None) kp2, des2 = sift.detectAndCompute(bwframe, None) bf = cv2.BFMatcher() matches = bf.knnMatch(des1, des2, k=2) good = [] for m, n in matches: if m.distance < .75 * n.distance: good.append([m]) #img = cv2.drawKeypoints(frame, kp2) img3 = drawMatches(num2, kp1, bwframe, kp2, good) plt.imshow(img3), plt.show() cv2.imshow("blah", img) cv2.waitKey(0)
def demo(imgfile0,imgfile1,rth0,rth1,mth): # read image, convert to gray #img = io.imread(sys.argv[1]); img=io.imread("data/plane.bmp") img0 = io.imread(imgfile0) img1 = io.imread(imgfile1) print("orig0 shape: "+str(img0.shape)) print("orig1 shape: "+str(img1.shape)) gry0 = rgb2gray(img0) gry1 = rgb2gray(img1) print("gray0 shape: "+str(gry0.shape)) print("gray1 shape: "+str(gry1.shape)) # get list of locations of interest points feat0 = harris(gry0,gk(3,3,1),rth0) feat1 = harris(gry1,gk(3,3,1),rth1) print("number of features0: "+str(len(feat0))) print("number of features1: "+str(len(feat1))) # make sift descriptors des0 = sift(gry0,feat0) des1 = sift(gry1,feat1) showFeatures(img0,feat0,des0) showFeatures(img1,feat1,des1) M = matchSIFT(des0,des1,40,mth) img2 = drawMatches(img0,feat0,img1,feat1,M) io.imshow( img2,vmin=0,vmax=255,cmap="gray") io.show()
def main(): image1 = cv2.imread(filename1) if image1 is None: print 'Unable to open file ', filename1 return image2 = cv2.imread(filename2) if image2 is None: print 'Unable to open file ', filename2 return #create a single feature map presuming map is not too much bigger than viewport # the edgeThreshold and patchSize can be tuned if the gap between cell is too large # MS Did just that, set edgeThreshold from 31 to 8 to get more features detector = cv2.ORB_create(nfeatures=MAP_FEATURES, scoreType=cv2.ORB_FAST_SCORE, edgeThreshold=8) kp1 = detector.detect(image1, None) kp1, des1 = detector.compute(image1, kp1) kp2 = detector.detect(image2, None) kp2, des2 = detector.compute(image2, kp2) gray1 = cv2.cvtColor(image1,cv2.COLOR_BGR2GRAY) gray2 = cv2.cvtColor(image2,cv2.COLOR_BGR2GRAY) index_params = dict(algorithm=6, table_number=6, key_size=12, multi_probe_level=1) search_params = dict(checks=50) matcher = cv2.FlannBasedMatcher(index_params, search_params) matches = matcher.knnMatch(des1, des2, k=2) good=[] for match in matches: if len(match) > 1 and match[0].distance < MATCH_RATIO * match[1].distance: good.append(match[0]) print len(good) if len(good) > MIN_MATCH_COUNT: src_pts = np.float32([kp1[m.queryIdx].pt for m in good]).reshape(-1, 1, 2) dst_pts = np.float32([kp2[m.trainIdx].pt for m in good]).reshape(-1, 1, 2) transform = cv2.estimateRigidTransform(dst_pts, src_pts, False) print transform if transform is not None: #print transform pixel_center = cv2.transform(CAMERA_CENTER, transform) # get global pixel #print pixel_center location = (int(pixel_center[0][0][0]),int(pixel_center[0][0][1])) cv2.circle(gray1,location,30,255,3) else: print 'Unable to compute transform from matches' out = drawMatches(gray1,kp1,gray2,kp2,good) cv2.namedWindow('Map comparison', cv2.WINDOW_NORMAL) cv2.imshow('Map comparison',out) cv2.waitKey(0) cv2.destroyAllWindows()
# find the keypoints and descriptors with SIFT kp1, des1 = orb.detectAndCompute(img1, None) kp2, des2 = orb.detectAndCompute(img2, None) # create BFMatcher object bf = cv2.BFMatcher(cv2.NORM_HAMMING, crossCheck=True) # Match descriptors. matches = bf.match(des1, des2) # Sort them in the order of their distance. matches = sorted(matches, key=lambda x: x.distance) # Draw first 10 matches. #img3 = cv2.drawMatches(img1,kp1,img2,kp2,matches[:10], flags=2) img3 = drawMatches(img1, kp1, img2, kp2, matches[:40]) #add img to plot plt.subplot(nrows, ncols, 7), plt.imshow(cv2.cvtColor(img3, cv2.COLOR_BGR2RGB), cmap='gray') plt.title("Sift") plt.xticks([]) plt.yticks([]) ##FLANN imgFlann1 = cv2.imread('GMIT1.jpg', 0) imgFlann2 = cv2.imread('GMIT2.jpg', 0) # Initiate SIFT detector siftFlann = cv2.SIFT()
kpmax = 0 for im in imlist: t = cv2.imread('pos/'+im,0) # find the keypoints and descriptors with SIFT kp1, des1 = orb.detectAndCompute(t,None) kp2, des2 = orb.detectAndCompute(test_image_part,None) # create BFMatcher object bf = cv2.BFMatcher(cv2.NORM_L2, crossCheck=True) # Match descriptors. matches = bf.match(des1,des2) # Sort them in the order of their distance. matches = sorted(matches, key = lambda x:x.distance) if (kp1 > kpmax): kpmax = kp1 lowbf_matches = len(matches) outmax = drawMatches.drawMatches(t, kp1, test_image_part, kp2, matches[:10]) lowbf_kp = kpmax cv2.imshow('Matched Features', outmax) cv2.waitKey(0) cv2.destroyAllWindows() allfiles=os.listdir(os.getcwd()+'\\neg2') imlist=[filename for filename in allfiles if filename[-4:] in [".png",".PNG"]] N=len(imlist) kpmax = 0 for im in imlist: t = cv2.imread('neg2/'+im,0) kp1, des1 = orb.detectAndCompute(t,None) kp2, des2 = orb.detectAndCompute(test_image_part,None) bf = cv2.BFMatcher(cv2.NORM_L2, crossCheck=True)
kpmax = 0 for im in imlist: t = cv2.imread('pos/' + im, 0) # find the keypoints and descriptors with SIFT kp1, des1 = orb.detectAndCompute(t, None) kp2, des2 = orb.detectAndCompute(test_image_part, None) # create BFMatcher object bf = cv2.BFMatcher(cv2.NORM_L2, crossCheck=True) # Match descriptors. matches = bf.match(des1, des2) # Sort them in the order of their distance. matches = sorted(matches, key=lambda x: x.distance) if (kp1 > kpmax): kpmax = kp1 lowbf_matches = len(matches) outmax = drawMatches.drawMatches(t, kp1, test_image_part, kp2, matches[:10]) lowbf_kp = kpmax cv2.imshow('Matched Features', outmax) cv2.waitKey(0) cv2.destroyAllWindows() allfiles = os.listdir(os.getcwd() + '\\neg2') imlist = [ filename for filename in allfiles if filename[-4:] in [".png", ".PNG"] ] N = len(imlist) kpmax = 0 for im in imlist: t = cv2.imread('neg2/' + im, 0) kp1, des1 = orb.detectAndCompute(t, None)
plt.title('Shi Tomasi') plt.xticks([]) plt.yticks([]) # Plot imgOrb plt.subplot(3, 1, 3) plt.imshow(cv2.cvtColor(imgOrb, cv2.COLOR_BGR2RGB), cmap='gray') plt.title('Orb') plt.xticks([]) plt.yticks([]) plt.show() # Demonstrate feature matching # Convert the second image to grayscale imgOrig2 = cv2.imread(imgName2) imgGray2 = cv2.cvtColor(imgOrig2, cv2.COLOR_BGR2GRAY) kp2, des2 = orb.detectAndCompute(imgGray2, None) # Draw only keypoints location, not size and orientation imgOrb2 = cv2.drawKeypoints(imgOrig2, kp1, color=(0, 0, 255)) # Brute force matching bf = cv2.BFMatcher(cv2.NORM_HAMMING, crossCheck=True) matches = bf.match(des1, des2) # Sort them in the order of their distance matches = sorted(matches, key=lambda x: x.distance) img3 = drawMatches(imgGray1, kp1, imgGray2, kp2, matches[:20])
def runSIFT(test_folder_name, test1_img_name, test2_img_name): NUM_GOOD_MATCH = 20 img = cv2.imread( "images/{test_folder_name}/{test1_img_name}".format( test_folder_name=test_folder_name, test1_img_name=test1_img_name), 1) img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) imgToMatch = cv2.imread( "images/{test_folder_name}/{test2_img_name}".format( test_folder_name=test_folder_name, test2_img_name=test2_img_name), 1) imgToMatch_gray = cv2.cvtColor(imgToMatch, cv2.COLOR_BGR2GRAY) # cv2.imshow("img",img) # cv2.waitKey(0) # cv2.imshow("imgToMatch", imgToMatch) # cv2.waitKey(0) sift = cv2.SIFT() features1, desc1 = sift.detectAndCompute(img_gray, None) features2, desc2 = sift.detectAndCompute(imgToMatch_gray, None) FLANN_INDEX_KDTREE = 1 flann_params = dict(algorithm=FLANN_INDEX_KDTREE, trees=5) matcher = cv2.FlannBasedMatcher(flann_params, {}) count_for_best_matches_in_knn = 2 matches = matcher.knnMatch(desc1, trainDescriptors=desc2, k=count_for_best_matches_in_knn) matches = filter_matches(matches) matches = sorted(matches, key=lambda match: match.distance) print "len(matches)", len(matches) print "len(features1)", len(features1) print "len(features2)", len(features2) test_patches = [] matches_found = [] for i in range(0, NUM_GOOD_MATCH): img1_idx = matches[i].queryIdx img2_idx = matches[i].trainIdx # x is col, y is row (x1, y1) = features1[img1_idx].pt (x2, y2) = features2[img2_idx].pt size1 = features1[img1_idx].size size2 = features2[img2_idx].size this_test_patch = comparePatches.Patch(int(y1), int(x1), int(size1), initialize_features=False) test_patches.append(this_test_patch) this_match_found = comparePatches.Patch(int(y2), int(x2), int(size2), initialize_features=False) matches_found.append(this_match_found) img_with_test_patches = comparePatches.drawPatchesOnImg(np.copy(img), test_patches, mark_sequence=True, show=False) cv2.imwrite("testSIFT/test_patches_{savefilename}.jpg".format(\ savefilename = test_folder_name + test1_img_name[0:test1_img_name.find(".")] + test2_img_name[0:test2_img_name.find(".")]), img_with_test_patches) match_img = drawMatches.drawMatches(np.copy(img), features1, np.copy(imgToMatch), features2, matches[:NUM_GOOD_MATCH], draw_size=True) cv2.imshow("match_img", match_img) cv2.waitKey(0) cv2.imwrite( "testSIFT/{savefilename}.jpg".format( savefilename=test_folder_name + test1_img_name[0:test1_img_name.find(".")] + test2_img_name[0:test2_img_name.find(".")]), match_img) return img, imgToMatch, test_patches, matches_found
def main(): if len(sys.argv) > 1: filename1 = sys.argv[1] filename2 = sys.argv[2] else: filename1 = 'poster18.jpg' filename2 = 'poster17.jpg' image1 = cv2.imread(filename1) if image1 is None: print 'Unable to open file ', filename1 return image2 = cv2.imread(filename2) if image2 is None: print 'Unable to open file ', filename2 return detector = cv2.ORB_create(nfeatures=5000, scoreType=cv2.ORB_FAST_SCORE, edgeThreshold=4) kp1 = detector.detect(image1, None) kp1, des1 = detector.compute(image1, kp1) kp2 = detector.detect(image2, None) kp2, des2 = detector.compute(image2, kp2) index_params = dict(algorithm=6, table_number=6, key_size=12, multi_probe_level=1) search_params = dict(checks=50) matcher = cv2.FlannBasedMatcher(index_params, search_params) matches = matcher.knnMatch(des1, des2, k=2) good = [] for match in matches: if len(match ) > 1 and match[0].distance < MATCH_RATIO * match[1].distance: good.append(match[0]) print "Good Matches: ", len(good) src_pts = np.float32([kp1[m.queryIdx].pt for m in good]).reshape(-1, 1, 2) dst_pts = np.float32([kp2[m.trainIdx].pt for m in good]).reshape(-1, 1, 2) transform = cv2.estimateRigidTransform(dst_pts, src_pts, False) np.set_printoptions(suppress=True) print(transform) # Need to draw only good matches, so create a mask # print 'compute_location' gray1 = cv2.cvtColor(image1, cv2.COLOR_BGR2GRAY) gray2 = cv2.cvtColor(image2, cv2.COLOR_BGR2GRAY) #out = drawMatches(gray1,kp1,gray2,kp2,(match[0] for match in matches)) out = drawMatches(gray1, kp1, gray2, kp2, good) cv2.namedWindow('Matches', cv2.WINDOW_NORMAL) cv2.imshow('Matches', out) cv2.waitKey(0) cv2.destroyAllWindows()
kp1, des1 = ed.extract_surf(path1, hessian=500, octave=3, octaveLayers=2, ext=False) kp2, des2 = ed.extract_surf(path2, hessian=500, octave=3, octaveLayers=2, ext=False) #find good matches good = ed.matcher(kp1, kp2, des1, des2) #draw matches img3 = dm.drawMatches(cv2.imread(path1, 0), kp1, cv2.imread(path2, 0), kp2, good) #write image cv2.imwrite(savename, img3) print float(len(good)) / float(min(len(des1), len(des2))) #compute mispatching percentge tmp = [img3, float(len(good)) / float(min(len(des1), len(des2))), savename] saved_surf.append(tmp) pickle.dump(saved_surf, open("saved_surf", "wb")) ######################################################## ################################### ########################## ############################# #SIFT features saved_sift = []
if len(good_matches_temp) < 4: print "Not enough good matches to estimate a homography." sys.exit() # estimate homography pts_temp = np.float32([kp_temp[m.queryIdx].pt for m in good_matches_temp]).reshape(-1,1,2) pts_test = np.float32([kp_test[m.trainIdx].pt for m in good_matches_temp]).reshape(-1,1,2) H_temp, mask_temp = cv2.findHomography(pts_temp, pts_test, cv2.RANSAC, 5.0) # draw boundary of temp code temp_h, temp_w = gray_temp.shape corners_temp = np.float32([[0, 0], [0, temp_h-1], [temp_w-1, temp_h-1], [temp_w-1, 0]]).reshape(-1,1,2) corners_test_temp = cv2.perspectiveTransform(corners_temp, H_temp) cv2.polylines(gray_test, [np.int32(corners_test_temp)], True, 120, 5) # filter out inliers matchesMask_temp = mask_temp.ravel().tolist() inliers = [] for i, m in enumerate(matchesMask_temp): if m == 1: inliers.append(good_matches_temp[i]) # print elapsed time print "Total elapsed time: " + str(time.time() - starttime) + " seconds" # show images drawMatches.drawMatches(gray_temp, kp_temp, gray_test, kp_test, inliers)
# find the keypoints and descriptors with SIFT kp1, des1 = orb.detectAndCompute(img1, None) kp2, des2 = orb.detectAndCompute(img2, None) # create BFMatcher object bf = cv2.BFMatcher(cv2.NORM_HAMMING, crossCheck=True) # Match descriptors. matches = bf.match(des1, des2) # Sort them in the order of their distance. matches = sorted(matches, key=lambda x: x.distance) #draw matches and display img3 = drawMatches(img1, kp1, img2, kp2, matches[:20]) #PART 2 =============== imgNew = cv2.imread('ExampleImage4.jpg') #convert the image to HSV space hsv_img = cv2.cvtColor(imgNew, cv2.COLOR_BGR2HSV) #split the image into each channel (H, S, V) h, s, v = cv2.split(hsv_img) #display each channel nrows = 2 ncols = 2