Exemple #1
0
def compare_frames_opf(video_name, distance_limit):
    all_kp = []
    all_dsc = []
    all_2d_points = []
    number_of_frames = vc.count_frames(video_name)
    count_desc = 0
    for i in range(number_of_frames-2):
        actual_frame = vc.capture_frame(video_name, i+1)
        old_frame = vc.capture_frame(video_name, i)
        actual_gray_frame = sft.to_gray(actual_frame)
        old_gray_frame = sft.to_gray(old_frame)

        frame_kp, frame_desc = sft.sift_features(old_gray_frame)

        old_frame_points = sift_kp_2d(frame_kp)

        new_frame_points, status, error = cv.calcOpticalFlowPyrLK(old_gray_frame, actual_gray_frame, old_frame_points, None, **lk_params)

        frame_kp, frame_desc, frame_2d_kp = kp_moviment(frame_kp, frame_desc, new_frame_points, old_frame_points, distance_limit)

        all_kp = all_kp + frame_kp
        all_dsc = all_dsc + frame_desc
        all_2d_points = all_2d_points + frame_2d_kp

        all_dsc, count_desc = aux.concatenate_descriptors(count_desc, all_dsc, all_2d_points, actual_gray_frame, old_gray_frame)
def bla(image1, image2):
    current_image_details = SIFT.get_key_points(image1)
    next_image_details = SIFT.get_key_points(image2)
    matches = SIFT.fetch_matches(current_image_details, next_image_details)
    homography, inv_homography, inlier_matches = RANSAC.ransac(
        matches,
        4,
        2000,
        current_image_details[0],
        next_image_details[0],
        inlier_threshold=0.5)
    return sticher.stitch(image1, image2, homography, inv_homography)
def pickMatchingPoints(im1, im2):
    # dispim1 = im1.copy()
    # dispim2 = im2.copy()

    # SIFT to find keypoints
    # the keypoints and descriptors are vectors/arrays of python wrappers of the
    # C++ classes (with very little documentation...)
    sift = SIFT()
    kpts1, descrs1 = sift.detectAndCompute(im1)
    kpts2, descrs2 = sift.detectAndCompute(im2)

    # SIFT to match keypoints
    # p1 is array with the points in first image, p2 the corresponding points in
    # the second image, kp_matches is an array of tuples, each tuple contains
    # matching keypoints (see documentation for the attributes of the class)
    p1, p2, kp_matches = matchDescriptors(kpts1, descrs1, kpts2, descrs2)

    # cv2.imshow('Image 1',dispim1)
    # cv2.imshow('Image 2',dispim2)

    # xy = zeros((4,2))
    # for i in range(n):
    #    print 'Click at point %s in image 1' % (i+1)
    #    x, y = ginput('Image 1', 1)
    #    marker(dispim1, x, y, 3, str(i+1))
    #    cv2.imshow('Image 1', dispim1)
    #    xy[i,0]=x
    #    xy[i,1]=y
    #
    # xaya = zeros((4,2))
    # for i in range(n):
    #    print 'Click at point %s in image 2' % (i+1)
    #    x, y = ginput('Image 2', 1)
    #    marker(dispim2, x, y, 3, str(i+1))
    #    cv2.imshow('Image 2', dispim2)
    #    xaya[i,0]=x
    #    xaya[i,1]=y

    # Uncomment the following to just use some hardcoded points
    # xy = array([[ 157, 32],
    #            [ 211, 37],
    #            [ 222,107],
    #            [ 147,124]])
    # xaya = array([[  6, 38],
    #              [ 56, 31],
    #              [ 82, 87],
    #              [ 22,118]])

    return p1, p2
Exemple #4
0
def compute_feature():
    features_path = "/home/ashish/dip/chap7/features_test/"
    images_path = "/home/ashish/dip/chap7/images_test/"

    images = imlist.get_imlist(images_path)  #Get the list of images
    nbr_images = len(images)

    kplist = []
    deslist = []

    for img in images:  #Create KP and desc files for each image
        img_txt = os.path.splitext(img)[0]

        kp_file = features_path + img_txt + "kp" + ".txt"
        des_file = features_path + img_txt + "des" + ".txt"

        kplist.append(kp_file)
        deslist.append(des_file)

        kp, des = SIFT.SIFT(images_path + img)

        np.savetxt(kp_file, kp, fmt="%s")
        np.savetxt(des_file, des, fmt="%s")

    return deslist
Exemple #5
0
def generate_descriptors():
# =============================================================================
#     Generates SIFT descriptors for every image/cell and applies KMeans
# =============================================================================
    for i in range(1,file.num_high_res + 1):
        parent = os.path.abspath(os.path.join(os.getcwd(), os.pardir))
        image_path = os.path.join(parent, "High_Res_Input_Images_Processed", "DAPI_%d.tif" % i)
        if (os.path.isfile(image_path)):
            print("Generating SIFT descriptors for image %d" % i)
            DAPI_image = SIFT.get_high_res_image("DAPI_%d.tif" % i)
            atubulin_image = SIFT.get_high_res_image("atubulin_%d.tif" % i)
            cell = SIFT.SIFT_image(DAPI_image, atubulin_image, i)
            cell.find_DAPI_desc()

            
    #DBSCAN.apply_DBSCAN(SIFT.desc_KMeans())
#    SIFT.desc_KMeans()
    for i in range(10):
        tSNE.apply_tSNE(SIFT.desc_KMeans()) # if want to run tSNE after SIFT instead of DBSCAN
Exemple #6
0
def avg(plan, n):
    res = to_numpy(plan[0])
    for frame in plan[1:]:
        res += to_numpy(frame)
    res = (res - np.min(res)) / (np.max(res) - np.min(res))
    res = from_numpy(res)

    cv2.imwrite('medoid' + str(n) + '.png', plan[np.argmax([SIFT.match_sift(res, frame, 0.9) for frame in plan])])

    cv2.imwrite('avg' + str(n) + '.png', res)
Exemple #7
0
def rep(plan, n):
    best_score = 0
    best_frame = None
    i = np.random.randint(len(plan))
    best = i
    curr_frame = plan[i]
    l = [SIFT.match_sift(curr_frame, frame, 0.9) for frame in plan[max(0, i - 10): i] + plan[i + 1: i + 11]]
    if np.max(l) > best_score:
        best_score = np.max(l)
        best_frame = plan[max(0, i - 10 + np.argmax(l))]
    np.argmax(l)
 def __init__(self, model_name, folder_path=None):
     self.model_name = model_name
     self.folder_path = folder_path
     self.split_windows = False
     self.model = None
     if self.model_name == 'LBP':
         self.model = LBP.LocalBinaryPatterns(8, 1)
         self.split_windows = True
     elif self.model_name == 'HOG':
         self.model = HOG.Hog(orientations=9, pixels_per_cell=(8, 8), cells_per_block=(2, 2))
     elif self.model_name == 'CM':
         self.model = ColorMoments.ColorMoments()
         self.split_windows = True
     elif self.model_name == 'SIFT':
         self.model = SIFT.SIFT()
Exemple #9
0
    def run(self):
        print("Processing No.", self.index, "image")
        time1 = time.time()

        sigma = 1.6
        num_intervals = 3
        assumed_blur = 0.5
        image_border_width = 5
        image = self.image.astype('float32')
        base_image = SIFT.generateBaseImage(image, sigma, assumed_blur)
        # print("Processing No.", self.index, "step1.")
        num_octaves = SIFT.computeNumberOfOctaves(base_image.shape)
        # print("Processing No.", self.index, "step2.")
        gaussian_kernels = SIFT.generateGaussianKernels(sigma, num_intervals)
        # print("Processing No.", self.index, "step3.")
        gaussian_images = SIFT.generateGaussianImages(base_image, num_octaves,
                                                      gaussian_kernels)
        # print("Processing No.", self.index, "step4.")
        dog_images = SIFT.generateDoGImages(gaussian_images)
        # print("Processing No.", self.index, "step5.")
        keypoints = SIFT.findScaleSpaceExtrema(gaussian_images, dog_images,
                                               num_intervals, sigma,
                                               image_border_width)
        # print("Processing No.", self.index, "step6.")
        keypoints = SIFT.removeDuplicateKeypoints(keypoints)
        # print("Processing No.", self.index, "step7.")
        keypoints = SIFT.convertKeypointsToInputImageSize(keypoints)
        # print("Processing No.", self.index, "step8.")
        descriptors = SIFT.generateDescriptors(keypoints, gaussian_images)
        # print("Processing No.", self.index, "finished.")

        threadLock.acquire()
        # 释放锁,开启下一个线程
        self.list.append([self.index, keypoints, descriptors])
        threadLock.release()

        time2 = time.time()
        c = time2 - time1
        print("Processing No.", self.index, " costs ", c)
def SIFTAlignment(imgStack,refIndex, global_list):
    refImg = imgStack[refIndex]
    outStack = []
    idxStack = []

    for index in np.arange(len(imgStack)):
        if index == refIndex:
            outStack.append(imgStack[index])
            idxStack.append(index)
        else:
            print(index)
            currImg = imgStack[index]
            outImg, match_flag = SIFT.newAlignment(refImg, currImg, global_list[refIndex], global_list[index])
            if match_flag:
                outStack.append(outImg)
                idxStack.append(index)
                cv.imwrite('./mid/'+str(index)+'.jpg', outImg)
    return idxStack, outStack
Exemple #11
0
def compareEfficiency():
    folder = "DibujosMuestra/dataset2_reyCopia/reyCopia"
    imgs = load_imgs(folder)
    t_0 = time()
    for img in imgs:
        sift = SIFT.SIFT({
            "s": 3,  #Pg 9 of Lowe's paper
            "sigma": 1.6,  #Pg 10 of Lowe's paper
            "visual_debug": True,
            "img_name":
            "/home/alberto/Documents/CV/M0_SIFT/fotonoticia_20200402133510_420.jpg",  #Only works if img is not defined
            "img": img,
            "assumed_blur": 0.5,  #Pg 10 of Lowe's paper
            "detection_threshold": 10,  #???
            "contrast_threshold": 0.04,
            "eigenvalue_ratio": 10,
            "convergence_attempts": 5,
            "image_border_width": 5,
            "radius_factor": 3,
            "num_bins": 36,
            "peak_ratio": 0.8,
            "scale_factor": 1.5,
            "float_tol": 1e-7,
            "window_width": 4,
            "num_bins_descriptor": 8,
            "scale_multiplier": 3,
            "descriptor_max_val": 0.2
        })
        sift.calculateKeyPoints()
    print(f"The custom implementation needed {(time()-t_0)*1000} miliseconds")

    t_0 = time()
    for img in imgs:
        sift = cv2.SIFT_create()
        kp = sift.detect(img, None)
    print(f"The OpenCV implementation needed {(time()-t_0)*1000} miliseconds")
Exemple #12
0
import cv2 as opencv
import SIFT
import RANSAC
import sticher

boxes_image = opencv.imread("./project_images/Boxes.png")
image1 = opencv.imread("./project_images/Rainier1.png")
image2 = opencv.imread("./project_images/Rainier2.png")

boxes_details = SIFT.get_key_points(boxes_image, "1a")
image1_details = SIFT.get_key_points(image1, "1b")  # details is array 0 => keypoints and 1 => descriptor
image2_details = SIFT.get_key_points(image2, "1c")

matches = SIFT.fetch_matches(image1_details, image2_details)
matches_image = opencv.drawMatches(image1, image1_details[0], image2, image2_details[0], matches[:20], None, flags=2)
opencv.imwrite("./Output/2.png", matches_image)

homography, inv_homography, inlier_matches = RANSAC.ransac(matches, 4, 100, image1_details[0], image2_details[0], inlier_threshold=0.5)
inlier_image = opencv.drawMatches(image1, image1_details[0], image2, image2_details[0], inlier_matches, None, flags=2)
opencv.imwrite("./Output/3.png", inlier_image)

stitched_image = sticher.stitch(image1, image2, homography, inv_homography)
opencv.imwrite("./Output/4.png", stitched_image)
opencv.imshow("4.png", stitched_image)
opencv.waitKey(0)
print("Done")
Exemple #13
0
# -*- coding: utf-8 -*-

import SIFT
import numpy as np
import cv2

img1 = cv2.imread('../../data/CarData/TrainImages/pos-1.pgm')
img2 = cv2.imread('../../data/CarData/TrainImages/pos-1.pgm')

sift1 = SIFT.SIFT_Obj(img1)
sift1.SIFT_Keypoints_Descriptors(plot_flag = False)

sift2 = SIFT.SIFT_Obj(img2)
sift2.SIFT_Keypoints_Descriptors(plot_flag = False)

matches, distances = SIFT.kmSIFTMAtches(sift1.descriptors, sift2.descriptors, knn = 10)

fMatches = np.transpose(np.vstack((range(sift2.descriptors.shape[0]), matches[:,0])))

SIFTCoord = SIFT.kmPlotSIFTMatches(np.hstack((img1,img2)), sift1.keyPoints, sift2.keyPoints, fMatches, img2.shape[1])
## Open test image
im_test = Image.open("../../data/test_sift.jpg")
image = np.array(im_test)

## Test
import SIFT
nb_levels = 5
k = 1.1
sigma = 1. / math.sqrt(2)
t_contrast = 1.2
t_edge = 10
wsize = 8

#%%
image = SIFT.rgb2gray(image)
Oct = SIFT.Octave(image, nb_levels, k, sigma)
#%%
print("Build octave")
Oct.build_octave()
for i in range(nb_levels):
    plt.figure()
    plt.imshow(Oct.octave[:, :, i], cmap='gray')
    plt.title("scale {}".format(i))
#%%
print("Log approximation")
Oct.log_approx()
for i in range(nb_levels - 1):
    plt.figure()
    plt.imshow(Oct.DOG[:, :, i], cmap='gray')
    plt.title("DOG {}".format(i))
Exemple #15
0
# -*- coding: utf-8 -*-
"""
Created on Sun Apr 21 17:12:04 2019

@author: VÕQUỐCHUY
"""

import numpy as np
import cv2
import sys
import convolution
import Harris
import SIFT

img = cv2.imread("./Input/flowers.jpeg", 0)
siftDetector = SIFT.SIFT(img, 3, 0.707, np.sqrt(2), 5, 4)
listOfOctaves = siftDetector.createImgs_AtMultipleOctaves()
#listOfDoG = siftDetector.calDOG_AtMultipleOctaves()
#listOfKeypoints = siftDetector.findApproxKeypoints()

listOfKeypoints = siftDetector.orientationAssignmentAndKeypointDescription()

#cv2.namedWindow('output',cv2.WINDOW_AUTOSIZE)
#cv2.imshow('output',img)
'''for i in range(len(listOfOctaves)):
    for j in range(len(listOfOctaves[i])):
        cv2.namedWindow('Output at octave'+str(i)+' scale '+str(j),cv2.WINDOW_AUTOSIZE)
        cv2.imshow('Output at octave'+str(i)+' scale '+str(j),listOfOctaves[i][j][0])'''
'''for i in range(len(listOfDoG)):
    for j in range(len(listOfDoG[i])):
        cv2.namedWindow('Output at octave'+str(i)+' scale '+str(j),cv2.WINDOW_AUTOSIZE)
Exemple #16
0
    # Calculando tempo em milisegundos
    lr2m_times.append(round((end - start) * 1000, 2))

    # Executando algoritmo com RGB
    start = t.time()
    image = lr2m.lr2MatchingRGB(A, B)
    end = t.time()
    lr2m.saveImage(testsPath + "LR2M_RGB_" + i[0], image)

    # Calculando tempo em milisegundos
    lr2mRGB_times.append(round((end - start) * 1000, 2))

    # Executando SIFT
    start = t.time()
    image = sift.SIFT(A_SIFT, B_SIFT)
    end = t.time()
    sift.saveImage(testsPath + "SIFT_" + i[0], image)

    # Calculando tempo em milisegundos
    SIFT_times.append(round((end - start) * 1000, 2))

    # Definindo valores para gráfico
    testCount += 1
    values.append("T" + str(testCount))

# print(values)
# print(lr2m_times)
# print(lr2mRGB_times)
# print(SIFT_times)
Exemple #17
0
        if matches_21[int(matches_12[n])] != n:
            matches_12[n] = 0
    return matches_12


for img in images:
    img_txt = os.path.splitext(img)[0]
    featlist.append(img_txt + "feat" + ".txt")
    deslist.append(img_txt + "des" + ".txt")

    feature_file = img_txt + "feat" + ".txt"
    des_file = img_txt + "des" + ".txt"

    f_kp = open(img_txt + "feat" + ".txt", 'w+')
    f_des = open(img_txt + "des" + ".txt", 'w+')
    kp, des = SIFT.SIFT(path + img)

    np.savetxt(feature_file, kp, fmt="%s")
    np.savetxt(des_file, des, fmt="%s")

nbr_images = len(images)
matchscores = np.zeros((nbr_images, nbr_images))
for i in range(nbr_images):
    for j in range(i, nbr_images):
        #print 'comparing', imlist[i], imlist[j]
        l1 = read_features_from_file(featlist[i])
        l2 = read_features_from_file(featlist[j])
        d1 = read_features_from_file(deslist[i])
        d2 = read_features_from_file(deslist[j])

        des1 = d1.astype(np.float)
Exemple #18
0
     START=ini(img,level)
     cv2.imshow("Game",img)
         
     if cv2.waitKey(1) & 0xFF == ord('q'):
         break
 elif DETECTED==False:
     cv2.imshow("Game",loading)
     if cv2.waitKey(1) & 0xFF == ord('q'):
         break
     [DETECTED,points]= detect_joints(img,level)
     START=DETECTED#caLL detection
     PLAYING= DETECTED
     print("detect",DETECTED)
 elif (PLAYING==True):
     # print("before",points)
     points[1:3]= SIFT.track_it(img,[points[1],points[2]],frameCount)
     # print("after",points)
     # state = check_state(points,game[level-1])
     state=[False,False]
     
     if state[0]==True:
         PLAYING=False
         cv2.imshow("Game",win)
     elif state[1]==True:
         cv2.imshow("Game",lose)
     else:
         gamecpy=np.copy(game[level-1])
         
         for i in range(len(points)):
             if level==1 and i==0:
                 continue
Exemple #19
0
import SIFT

print("current working space:", os.getcwd())
print('running ......')

# img = cv.cvtColor(cv.resize(cv.imread('../Data/imgs/apple1.jpg'), (256, 256)), cv.COLOR_BGR2GRAY)
# time0 = time.time()
# feats = SIFT.sift_features(img)
# print("sift_features:", time.time()-time0)
# kpts = [cv.KeyPoint(feat.img_pt[0], feat.img_pt[1], 1) for feat in feats]
# cv.imshow('sift features 0', cv.drawKeypoints(img, kpts, img, color=(0,0,255)))

img = cv.cvtColor(cv.resize(cv.imread('../Data/imgs/apple1.jpg'), (512, 512)),
                  cv.COLOR_BGR2GRAY)
time0 = time.time()
feats = SIFT.sift_features(img)
print("sift_features:", time.time() - time0)
kpts = [cv.KeyPoint(feat.img_pt[0], feat.img_pt[1], 1) for feat in feats]
cv.imshow('sift features 1', cv.drawKeypoints(img,
                                              kpts,
                                              img,
                                              color=(0, 0, 255)))

img = cv.cvtColor(cv.resize(cv.imread('../Data/imgs/apple1.jpg'), (512, 512)),
                  cv.COLOR_BGR2GRAY)
cvsift = cv.SIFT_create()
time0 = time.time()
kp, des = cvsift.detectAndCompute(img, None)
print("cv sift_features:", time.time() - time0)
cv.imshow('cv sift features 0',
          cv.drawKeypoints(img, kp, img, color=(0, 0, 255)))
Exemple #20
0
        "contrast_threshold":0.04,
        "eigenvalue_ratio":10,
        "img":None,
        "convergence_attempts":5,
        "image_border_width":5,
        "radius_factor":3, 
        "num_bins":36, 
        "peak_ratio":0.8, 
        "scale_factor":1.5,
        "float_tol":1e-7,
        "window_width":4, 
        "num_bins_descriptor":8, 
        "scale_multiplier":3, 
        "descriptor_max_val":0.2
    }


if __name__=="__main__":
    sift=SIFT.SIFT(params)
    sift.calculateKeyPoints()
    #print(sift.DoG)
    for i,octave in enumerate(sift.octaves):
        for j,img in enumerate(octave):
            im = Image.fromarray(img).convert('RGB')
            im.save(f'imagesTest/Octave{i}img{j}.jpg')

    for i,octave in enumerate(sift.DoG):
        for j,img in enumerate(octave):
            im = Image.fromarray(img*25).convert('RGB')
            im.save(f'imagesTest/DoGOctave{i}img{j}.jpg')        
Exemple #21
0
def SIFTAlignment(imgStack, refIndex, global_list):
    refImg = imgStack[refIndex]
    outStack = []
    idxStack = []

    for index in np.arange(len(imgStack)):
        if index == refIndex:
            outStack.append(imgStack[index])
            idxStack.append(index)
        else:
            currImg = imgStack[index]
            outImg, match_flag = SIFT.newAlignment(refImg, currImg,
                                                   global_list[refIndex],
                                                   global_list[index])
            if match_flag:
                outStack.append(outImg)
                idxStack.append(index)
                cv.imwrite('./mid/' + str(index) + '.jpg', outImg)
    return idxStack, outStack


# def main():
#     global_list = []
#     time_1 = time.time()
#     print(time_1)
#     fileFolderPath = './test_image/Hall/'
#     exposure_times,img_list,filenames = getImageStackAndExpos(fileFolderPath)
#
#
#     refImgIndex= 14 # getRefImage(img_list)
#
#     threads = []
#
#     threadID = 0
#     # for idx in range(4):
#     for img in img_list:
#         # img = img_list[idx]
#         image = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
#         thread = myThread(threadID, image, global_list)
#         thread.start()
#         threads.append(thread)
#         threadID += 1
#
#     for t in threads:
#         t.join()
#
#     time_2 = time.time()
#     c = time_2 - time_1
#     print("Totoal time :", c)
#
#     global_list.sort(key=lambda x: x[0])
#     global_list = np.array(global_list)[:,1:3]
#
#
#     refImgIndex = chooseRef.getRefImage_br(img_list)
#     cv.imwrite("ref.jpg", img_list[refImgIndex])
#     res_img_list = []
#     res_exp_list = []
#     idx_list, res_img_list = SIFTAlignment(img_list,refImgIndex, global_list)
#
#     for idx in idx_list:
#         # res_img_list.append(image_list[idx])
#         res_exp_list.append(exposure_times[idx])
#
#
#
#
#
#
#     # print(global_list)
#
# if __name__ == '__main__':
#     main()
Exemple #22
0
 def SIFT_Alg(self):
     SIFT.start_SIFT()
Exemple #23
0
T_ncc = 0.66
T_max_global = 0.96
f_count = 400
error = 0.4
pts_per_trial = 6
percent_prob = 0.999
G_Noise = 1
"""
###########################################

###########################################
# Computing SIFT keypoints and Descriptor
###########################################
"""

img1, kp1, desp1 = S.SIFT_detector(img_1, f_count, sigma, layers)
img2, kp2, desp2 = S.SIFT_detector(img_2, f_count, sigma, layers)
img3, kp3, desp3 = S.SIFT_detector(img_3, f_count, sigma, layers)
img4, kp4, desp4 = S.SIFT_detector(img_4, f_count, sigma, layers)
img5, kp5, desp5 = S.SIFT_detector(img_5, f_count, sigma, layers)
###########################################
# Computing correspondence using NCC
###########################################
Coord_NCC12 = S.NCC(kp1, desp1, kp2, desp2, T_ncc, T_max_global)
img = IM.plotting(Coord_NCC12, img1, img2)
cv2.imwrite("Pair1.jpg", img)
Coord_NCC23 = S.NCC(kp2, desp2, kp3, desp3, T_ncc, T_max_global)
img = IM.plotting(Coord_NCC23, img2, img3)
cv2.imwrite("Pair2.jpg", img)
Coord_NCC34 = S.NCC(kp3, desp3, kp4, desp4, T_ncc, T_max_global)
img = IM.plotting(Coord_NCC34, img3, img4)