def extract_features(image_path):
  img = cv2.imread(image_path, cv2.IMREAD_COLOR)
  surf = cv2.SIFT(400)
  kp, des = surf.detectAndCompute(img, None)
  print "extracted", image_path
def sift(*args, **kwargs):
    try:
        return cv2.xfeatures2d.SIFT_create(*args, **kwargs)
    except:
        return cv2.SIFT()
Exemple #3
0
def C2OPatch(image, dE, dAlpha, dBeta):
    ur"""
    
    
    Function for the :math:`C_2O` feature calculation relative to keypoint detection (from openCV's SIFT).

    This function uses the :py:func:`C2O` function on patch of 64*64 pixel around each keypoint.
      
    
    This function is called as shown below :
    
    .. code-block:: python
       :emphasize-lines: 3,5
    
       MatSigC2O = C2OPatch(image,kp)
    
    :param image: Path directory of the image on which you need the :math:`C_2O` feature calculation.
    :type image: string
    :param kp: Keypoints matrix from OpenCv's SIFT keypoint detection.
    :type kp: OpenCV keypoint structure array

    
    :return: The C2O signatures for each keypoint in an array.
    :rtype:  np.ndarray
    
    This function use :py:func:`C2O` 
    
    
    """

    imag = Image.open(image)
    imag = np.array(imag)
    a = Queue()

    rows, cols, plans = imag.shape

    #    print rows, cols , plans

    gray = cv2.cvtColor(imag, cv2.COLOR_RGB2GRAY)

    sift = cv2.SIFT()
    kp = sift.detect(gray, None)

    size = dE * dAlpha * dBeta

    matC2OPatch = np.zeros((len(kp), size))
    mat_kp = np.zeros([len(kp), 2])
    for i in range(len(kp)):
        mat_kp[i][0] = np.round(kp[i].pt[0])

    for j in range(len(kp)):
        mat_kp[j][1] = np.round(kp[j].pt[1])
    matkpret = np.zeros(np.shape(mat_kp))
    n = 0
    #    print np.max(mat_kp[:,1]) , np.min(mat_kp[:,1])

    for i in np.arange(0, len(kp) - 1):
        #        print i
        #        print mat_kp[i][0]-31 , mat_kp[i][0]+32 , mat_kp[i][1]-31 , mat_kp[i][1]+32, np.shape(imag)
        #        if (((mat_kp[i][0]-31)<0) | ((mat_kp[i][0]+32)>rows-1) | ((mat_kp[i][1]-31)<0) | ((mat_kp[i][1]+32)>cols-1)):
        if (((mat_kp[i][0] - 31) > 0) & ((mat_kp[i][0] + 32) < rows - 1) &
            ((mat_kp[i][1] - 31) > 0) & ((mat_kp[i][1] + 32) < cols - 1)):
            #            print i
            #        else:
            matkpret[n][0] = mat_kp[i][0]
            matkpret[n][1] = mat_kp[i][1]
            matC2OPatch[n, :] = C2O(
                imag[mat_kp[i][0] - 31:mat_kp[i][0] + 32,
                     mat_kp[i][1] - 31:mat_kp[i][1] + 32], 1, 0, 4, 6, 3, a)
            n = n + 1

    matC2OPatch = np.resize(matC2OPatch, (n, size))
    matkpret = np.resize(matkpret, (n, 2))
    return matkpret, matC2OPatch
Exemple #4
0
def sift(img1, img2):
    MIN_MATCH_COUNT = 10
    print img2.shape

    # Initiate sift detector
    sift = cv2.SIFT()

    # find the keypoints and descriptors with sift
    kp1, des1 = sift.detectAndCompute(img1, None)
    kp2, des2 = sift.detectAndCompute(img2, None)

    #   img2 = cv2.drawKeypoints(img2,kp2)
    #   cv2.imwrite('../results/img2_sift_keypoints.jpg', img2)

    FLANN_INDEX_KDTREE = 0
    index_params = dict(algorithm=FLANN_INDEX_KDTREE, trees=5)
    search_params = dict(checks=50)

    flann = cv2.FlannBasedMatcher(index_params, search_params)

    matches = flann.knnMatch(des1, des2, k=2)

    # store all the good matches as per Lowe's ratio test.
    good = []
    found = False
    M = None

    for m, n in matches:
        if m.distance < 0.7 * n.distance:
            good.append(m)
    print 'len(good):\t' + str(len(good))

    if len(good) > MIN_MATCH_COUNT:
        found = True
        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)
        # print 'len(src_pts)' + str(len(src_pts))
        # print 'len(dst_pts)' + str(len(dst_pts))

        M, mask = cv2.findHomography(src_pts, dst_pts, cv2.RANSAC, 5.0)

        h, w = img1.shape
        pts = np.float32([[0, 0], [0, h - 1], [w - 1, h - 1],
                          [w - 1, 0]]).reshape(-1, 1, 2)
        dst = cv2.perspectiveTransform(pts, M)

        # for pt in dst:
        #     p = pt[0]
        #     p = list(p)
        #     # p = [int(i) for i in p]
        #     print p
        #     cv2.circle(img2, tuple(p), 5, (255, 255, 255), -1)

        # cv2.polylines(img2,[np.int32(dst)],True,255,3)
        # cv2.imshow('detected',img2)
        # cv2.waitKey(0)
        # cv2.destroyAllWindows()

    else:
        found = False
        print "Not enough matches are found - %d/%d" % (len(good),
                                                        MIN_MATCH_COUNT)

    return good, found, M
Exemple #5
0
 def _sift_instance(self, edge_threshold=100):
     if hasattr(cv2, 'SIFT'):
         return cv2.SIFT(edgeThreshold=edge_threshold)
     return cv2.xfeatures2d.SIFT_create(edgeThreshold=edge_threshold)
Exemple #6
0
 def __init__(self, sift_kp_size=16, concat=True, kps_idx=numpy.arange(4, 31, 8)):
     self.concat = concat
     self.sift = cv2.SIFT()
     coordinates = list(itertools.product(kps_idx, kps_idx))
     self.kps = [cv2.KeyPoint(i, j, sift_kp_size) for (i, j) in coordinates]
def track_objects(vid_path, gaze, matches, verbose=True):
    """
    Tracks gaze over matches using sift feature matching in openCV.
    Saves a video of gaze plotted over each match as well as a .npy
    file of gaze coordinates in each match image's coordinates
    """
    if verbose:
        print "Tracking gaze over match images..."
        sys.stdout.write('  0.00%')

    gaze = pd.read_csv(gaze)
    gaze = gaze[
        ~gaze.vts_time.isnull()]  # only start tracking eyes once video starts

    matches = [{'path': m} for m in matches]

    vid = cv2.VideoCapture(vid_path)

    if OPENCV3:
        sift = cv2.xfeatures2d.SIFT_create()
        fourcc = cv2.VideoWriter_fourcc(*'mp4v')
        tot = vid.get(cv2.CAP_PROP_FRAME_COUNT) * 2.0
        fps = vid.get(cv2.CAP_PROP_FPS) * 2
    else:
        sift = cv2.SIFT()
        fourcc = cv2.cv.CV_FOURCC(*'mp4v')
        tot = vid.get(cv2.cv.CV_CAP_PROP_FRAME_COUNT) * 2.0
        fps = vid.get(cv2.cv.CV_CAP_PROP_FPS) * 2

    ind = 1

    # get gaze values
    gaze_val = gaze['gaze_pos_val'].values
    gaze_x = gaze['gaze_pos_x'].values
    gaze_y = gaze['gaze_pos_y'].values
    vts = gaze['vts_time'].values / 1000.

    # setup sift features and output for each match
    for i in range(len(matches)):
        matches[i]['name'] = os.path.basename(matches[i]['path']).split('.')[0]
        outfile = vid_path.split(
            '.mp4')[0] + '_match_%s.m4v' % matches[i]['name']
        matches[i]['img'] = cv2.imread(matches[i]['path'])
        matches[i]['sift'] = sift.detectAndCompute(
            cv2.cvtColor(matches[i]['img'], cv2.COLOR_BGR2GRAY), None)
        matches[i]['size'] = (matches[i]['img'].shape[1],
                              matches[i]['img'].shape[0])
        matches[i]['video'] = cv2.VideoWriter()
        matches[i]['video'].open(outfile, fourcc, fps, matches[i]['size'],
                                 True)
        # x, y pairs of gaze locations over object. Init as -1
        matches[i]['obj_gaze'] = np.ones((len(gaze_x), 2)) * -1

    while vid.isOpened():
        if OPENCV3:
            vid_time = vid.get(cv2.CAP_PROP_POS_MSEC)
        else:
            vid_time = vid.get(cv2.cv.CV_CAP_PROP_POS_MSEC)
        ret, frame = vid.read()
        if ret:
            frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
            for i in range(len(matches)):
                matches[i]['M'] = object_find(matches[i]['sift'], frame, 50)
            # 2 frames per original frame
            for j in range(2):
                # make sure eye tracking data exists for current frame
                if ind < len(vts):
                    # make sure eye tracking data is not ahead of video
                    if vts[ind] <= vid_time:
                        for match in matches:  # draw on every match image
                            img_cp = match['img'].copy()
                            if gaze_val[ind] == 0:  # if gaze point is valid
                                if match['M'] is not None:
                                    org_pos = np.array(
                                        (1920 * gaze_x[ind], 1080 *
                                         gaze_y[ind])).reshape(-1, 1, 2)
                                    trans_pos = cv2.perspectiveTransform(
                                        org_pos, match['M'])
                                    trans_pos = tuple(np.int32(trans_pos[0,
                                                                         0]))
                                    if (trans_pos[0] <= match['size'][0]
                                            and trans_pos[0] >= 0 and
                                            trans_pos[1] <= match['size'][1]
                                            and trans_pos[1] >= 0):
                                        cv2.circle(
                                            img_cp, trans_pos, 8, [255, 0, 0],
                                            -2
                                        )  # draw blue circle on current frame
                                        cv2.circle(
                                            match['img'], trans_pos, 8,
                                            [0, 255, 0],
                                            2)  # draw green circle as trace
                                        match['obj_gaze'][ind, :] = trans_pos
                            match['video'].write(img_cp)
                        ind += 1
                    else:
                        for match in matches:
                            match['video'].write(match['img'])
                else:
                    for match in matches:
                        match['video'].write(match['img'])
                if ind % 10 == 0 and verbose:
                    sys.stdout.write('\r' + '%6.2f%%' % ((ind / tot) * 100))
                    sys.stdout.flush()
        else:
            break
        # catch up gaze data in case it's behind
        try:
            while vts[ind + 1] < vid_time:
                ind += 1
        except IndexError:  # in case vts reaches end
            pass

    vid.release()
    for i in range(len(matches)):
        obj_gaze_path = vid_path.split(
            '.mp4')[0] + '_match_%s.npy' % matches[i]['name']
        matches[i]['video'].release()  # save video
        np.save(obj_gaze_path, matches[i]['obj_gaze'])  # save gaze points

    if verbose:
        print
        print "Done!"
import cv2
import numpy

opencv_haystack =cv2.imread('michael_jordan.jpg')
opencv_needle =cv2.imread('temp.png')

ngrey = cv2.cvtColor(opencv_needle, cv2.COLOR_BGR2GRAY)
hgrey = cv2.cvtColor(opencv_haystack, cv2.COLOR_BGR2GRAY)

# build feature detector and descriptor extractor
hessian_threshold = 5000
detector = cv2.SIFT(hessian_threshold)
hkeypoints,hdescriptors = detector.detectAndCompute(hgrey,None)
nkeypoints,ndescriptors = detector.detectAndCompute(ngrey,None)

# extract vectors of size 64 from raw descriptors numpy arrays
rowsize = len(hdescriptors) / len(hkeypoints)
if rowsize > 1:
    hrows = numpy.array(hdescriptors, dtype = numpy.float32).reshape((-1, rowsize))
    nrows = numpy.array(ndescriptors, dtype = numpy.float32).reshape((-1, rowsize))
    #print hrows.shape, nrows.shape
else:
    hrows = numpy.array(hdescriptors, dtype = numpy.float32)
    nrows = numpy.array(ndescriptors, dtype = numpy.float32)
    rowsize = len(hrows[0])

# kNN training - learn mapping from hrow to hkeypoints index
samples = hrows
responses = numpy.arange(len(hkeypoints), dtype = numpy.float32)
#print len(samples), len(responses)
knn = cv2.KNearest()
Exemple #9
0
        open('dat_files/test_images_filenames.dat', 'r'))
    train_labels = cPickle.load(open('dat_files/train_labels.dat', 'r'))
    test_labels = cPickle.load(open('dat_files/test_labels.dat', 'r'))

    print 'Loaded ' + str(
        len(train_images_filenames
            )) + ' training images filenames with classes ', set(train_labels)
    print 'Loaded ' + str(
        len(test_images_filenames
            )) + ' testing images filenames with classes ', set(test_labels)

    # PCA components
    pca = PCA(n_components=20)

    # Create the SIFT detector object
    extractor = cv2.SIFT(nfeatures=100)

    # Create the SURF detector object
    # extractor = cv2.SURF(nfeaures = 100)

    num_images = 15
    apply_pca = True
    D, L = train_features(train_images_filenames, train_labels, extractor,
                          num_images, apply_pca)

    # Train a linear SVM classifier
    stdSlr = StandardScaler().fit(D)
    D_scaled = stdSlr.transform(D)

    kernel = 'linear'
    clf = train_svm(D_scaled, L, kernel)
Exemple #10
0
def Detect(objectName,type):
    start = time.time()
    time.clock()
    elapsed = 0
    seconds = 10  # 20 S.
    MIN_MATCH_COUNT = 30
    setpath = '/home/uawsscu/PycharmProjects/Project2/image/' + objectName + '.jpg'
    detector = cv2.SIFT()

    FLANN_INDEX_KDITREE = 0
    flannParam = dict(algorithm=FLANN_INDEX_KDITREE, tree=5)
    flann = cv2.FlannBasedMatcher(flannParam, {})

    trainImg = cv2.imread(setpath, 0)
    trainKP, trainDesc = detector.detectAndCompute(trainImg, None)

    QUESTION_COUNT = -1
    while elapsed < seconds:
        QueryImgBGR = get_video()

        QueryImg = cv2.cvtColor(QueryImgBGR, cv2.COLOR_BGR2GRAY)
        queryKP, queryDesc = detector.detectAndCompute(QueryImg, None)
        matches = flann.knnMatch(queryDesc, trainDesc, k=2)

        goodMatch = []
        for m, n in matches:
            if m.distance < 0.65 * n.distance:
                goodMatch.append(m)
        if len(goodMatch) > MIN_MATCH_COUNT:
            if type == "question" :
                QUESTION_COUNT = 1
                print "Yes"
                break

            tp = []
            qp = []
            for m in goodMatch:
                tp.append(trainKP[m.trainIdx].pt)
                qp.append(queryKP[m.queryIdx].pt)
            tp, qp = np.float32((tp, qp))
            H, status = cv2.findHomography(tp, qp, cv2.RANSAC, 3.0)
            h, w = trainImg.shape
            trainBorder = np.float32([[[0, 0], [0, h - 1], [w - 1, h - 1], [w - 1, 0]]])
            queryBorder = cv2.perspectiveTransform(trainBorder, H)
            cv2.polylines(QueryImgBGR, [np.int32(queryBorder)], True, (0, 255, 0), 5)

        else:
            print "Not Enough -->> %d : %d" % (len(goodMatch), MIN_MATCH_COUNT)

        QueryImgBGR = cv2.drawKeypoints(QueryImgBGR, queryKP)
        cv2.imshow('result', QueryImgBGR)

        if cv2.waitKey(10) == ord('q'):
            break

        elapsed = time.time() - start
        time.sleep(1)

    if QUESTION_COUNT == -1 :
        print "NO"
    cv2.destroyAllWindows()
    [inst for inst_array in zwd_test_inst_mat for inst in inst_array]
)


def grey_convert(wd_array):
    """convert [0, 1] greyscale to [0, 255] (dark, white)"""
    return np.round((1 - wd_array) * 255).astype(np.uint8)


sigma = 1.4  # default: 1.6
contrastThreshold = 0.03  # default: 0.04
nOctaveLayers = 5  # default: 3
edgeThreshold = 7  # default: 10
sift = cv2.SIFT(
    sigma=sigma,
    contrastThreshold=contrastThreshold,
    nOctaveLayers=nOctaveLayers,
    edgeThreshold=edgeThreshold
)

print("work on SIFT")

def detect_SIFT(inst):
    inst.kp_list, inst.desc_list = sift.detectAndCompute(
        grey_convert(train_X[inst.ix].reshape(PIXEL_X, PIXEL_Y)),
        None
    )

map(detect_SIFT, flatten_inst_arary)
map(detect_SIFT, flatten_test_inst_arary)

TOP_K = 20
def SIFT(gray):

    sift = cv2.SIFT()
    kp = sift.detect(gray, None)
    return kp
Exemple #13
0
def test(input1, input2):
    print input2

    image1 = input1 + '.png'
    image2 = input2 + '.jpg'
    MIN_MATCH_COUNT = 30

    img1 = cv2.imread(image1)  # queryImage
    img2_total = cv2.imread(image2)  # trainImage

    circles = circleDetection(input2)

    sift = cv2.SIFT()
    kp1, des1 = sift.detectAndCompute(img1, None)

    num = -1

    img3 = img2_total
    points = []
    good_num = []
    for i in circles[0, :]:
        num += 1
        r = int(i[2] * 1.2)
        sx = max(i[0] - r, 0)
        sy = max(i[1] - r, 0)
        ex = min(i[0] + r, img2_total.shape[1])
        ey = min(i[1] + r, img2_total.shape[0])
        #print i
        # print sx, ex, sy, ey
        img2 = img2_total[sy:ey, sx:ex].copy()

        # print img2
        # print img2_total[sx:ex,sy:ey,:]

        # find the keypoints and descriptors with SIFT
        kp2, des2 = sift.detectAndCompute(img2, None)

        FLANN_INDEX_KDTREE = 0
        index_params = dict(algorithm=FLANN_INDEX_KDTREE, trees=5)
        search_params = dict(checks=50)

        flann = cv2.FlannBasedMatcher(index_params, search_params)

        try:
            matches = flann.knnMatch(des1, des2, k=2)
        except Exception, e:
            continue

        # store all the good matches as per Lowe's ratio test.
        good = []

        for m, n in matches:
            if m.distance < 0.72 * n.distance:
                good.append(m)
        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)
            try:
                M, mask = cv2.findHomography(src_pts, dst_pts, cv2.RANSAC, 3.0)
                matchesMask = mask.ravel().tolist()
                #showMatches(img1, img2, src_pts, dst_pts, matchesMask, 'ratiotest_'+input1+'_'+input2+'_'+str(num))

                #alignImages(img1, img2, M, input1+'_'+input2)
                points.append(
                    drawRetangle(img1, img2_total, src_pts, dst_pts,
                                 matchesMask, 0, 0, sx, sy))
                good_num.append(len(good))
            except Exception, e:
                print e
                pass
print('\n')
print(output_dir)

####################
#### Compute descriptors of the whole database
####################
dataBaseDescriptors = []
imageBaseIndex = []
imageBasePaths = []
im_nb = 0
des_nb = 0

if (args.features == "surf"):
    feat_extractor = cv2.SURF(400)
if (args.features == "sift"):
    feat_extractor = cv2.SIFT()

imagesNameList = open(imagesNameFile)
for imageName in imagesNameList:
    imagePath = img_dir + imageName[:-1] + ".jpg"
    print "Compute descriptors for : " + imagePath

    image = cv2.imread(imagePath)
    kp, des = feat_extractor.detectAndCompute(image, None)

    if (des != None):
        for descriptor in des:
            dataBaseDescriptors.append(descriptor)
            imageBaseIndex.append(im_nb)
            des_nb += 1
Exemple #15
0
Rickshaw_limit = 0
Rickshaw_max_limit = 70
Autorickshaw_limit = 0
Autorickshaw_max_limit = 70
descriptors = np.array([])
#creating a list of images 
path = "Mlt_data"
for root, dir_names, file_names in os.walk(path):
# print root, dir_names, file_names
	for name in dir_names:
		# print name
		if (name == "Person"):
			for file in os.listdir(path+"/"+name):
				if person_limit < person_max_limit :
					pic = cv2.imread(path+"/"+name+"/"+file)
					kp, des = cv2.SIFT().detectAndCompute(pic, None)
					if des == None:
						continue
					descriptors = np.append(descriptors, des)
					person_limit+=1
					# print path+"/"+name+"/"+file
		elif (name == "Bicycle"):
			for file in os.listdir(path+"/"+name):
				if Bicycle_limit < Bicycle_max_limit :
					pic = cv2.imread(path+"/"+name+"/"+file)
					kp, des = cv2.SIFT().detectAndCompute(pic, None)
					if des == None:
						continue
					descriptors = np.append(descriptors, des)
					Bicycle_limit+=1
					# print path+"/"+name+"/"+file
train_images_filenames = cPickle.load(
    open('./dataset/train_images_filenames.dat', 'r'))
test_images_filenames = cPickle.load(
    open('./dataset/test_images_filenames.dat', 'r'))
train_labels = cPickle.load(open('./dataset/train_labels.dat', 'r'))
test_labels = cPickle.load(open('./dataset/test_labels.dat', 'r'))

print(
    'Loaded ' + str(len(train_images_filenames)) +
    ' training images filenames with classes ', set(train_labels))
print(
    'Loaded ' + str(len(test_images_filenames)) +
    ' testing images filenames with classes ', set(test_labels))

# create the sift detector object
SIFT_detector = cv2.SIFT(nfeatures=100)

# read the just 30 train images per class
# extract sift keypoints and descriptors
# store descriptors in a python list of numpy arrays

Train_descriptors = []
Train_label_per_descriptor = []
Accuracy = []
Time = []
Cost = []

for i in range(len(train_images_filenames)):
    filename = train_images_filenames[i]
    if Train_label_per_descriptor.count(train_labels[i]) < 30:
        print('Reading image ' + filename)
Exemple #17
0
def extract_r_t_f(im1, im2, mtx, dst):

    """
    This function will find the essential matrix from an estimation of the 
    fundamental matrix and from that we shall extract rotation and translation
    vectors.  This information may later be used to help determine the depth of 
    various objects in the image. In the course of finding the fundamental matrix
    we are also getting a second layer of false match elimination.

    Inputs:
        im1 -> first image file path
        im2 -> second image file path

    Returns:
        pts1, pts2 -> list of points that based on their position match well
                      enough with other image points to make the fundamental matrix
        T -> translation matrix
        R -> rotation matrix
        F -> fundamental matrix

    Reference: 
        A large amount of the code in this function (anything from essential matrix on)
        comes from code developed here: https://gist.github.com/jensenb/8668000
    """
    # K = [[ 598.28339238    0.          338.53221923]
    #    [   0.          595.80675436  230.06429972]
    #    [   0.            0.            1.        ]]

    #d = [[ 0.10643171 -0.55556305 -0.00786038  0.00290519  0.98123148]]

    #d the distortion matrix with three extra zeros on the end...we're not yet sure why the zeros are there
    # d = np.array([ 0.10643171, -0.55556305, -0.00786038,  0.00290519,  0.98123148, 0.0, 0.0, 0.0]).reshape(1,8)
    # #K the camera matrix (contains intrinsic camrea parameters)
    # K = np.array([598.28339238, 0.0, 338.53221923, 0.0, 595.80675436,  230.06429972, 0.0, 0.0, 1.0]).reshape(3,3)
    d = dst 
    K = mtx

    #K_inv is the inverse of K
    K_inv = np.linalg.inv(K)
    
    img1 = cv2.imread(im1,0)  #queryimage # left image
    img2 = cv2.imread(im2,0) #trainimage # right image

    sift = cv2.SIFT()

    # find the keypoints and descriptors with SIFT
    kp1, des1 = sift.detectAndCompute(img1,None)
    kp2, des2 = sift.detectAndCompute(img2,None)

    # FLANN parameters
    FLANN_INDEX_KDTREE = 0
    index_params = dict(algorithm = FLANN_INDEX_KDTREE, trees = 5)
    search_params = dict(checks=50)

    flann = cv2.FlannBasedMatcher(index_params,search_params)
    matches = flann.knnMatch(des1,des2,k=2)

    good = []
    pts1 = []
    pts2 = []

    # ratio test as per Lowe's paper
    for i,(m,n) in enumerate(matches):
        if m.distance < 0.8*n.distance:
            good.append(m)
            pts2.append(kp2[m.trainIdx].pt)
            pts1.append(kp1[m.queryIdx].pt)

    pts1 = np.int32(pts1)
    pts2 = np.int32(pts2)

    pts1 = np.array([[float(pt[0]), float(pt[1])] for pt in pts1])
    pts2 = np.array([[float(pt[0]), float(pt[1])] for pt in pts2])


    F, mask = cv2.findFundamentalMat(pts1,pts2,cv2.FM_LMEDS)

    # We select only inlier points
    pts1 = pts1[mask.ravel()==1]
    pts1 = [np.append(pt, 0.0) for pt in pts1]
    pts2 = pts2[mask.ravel()==1]
    pts2 = [np.append(pt, 0.0) for pt in pts2]

    #Decompsing the essential matrix from the fundamental matrix based on MATH!
    E = K.T.dot(F).dot(K)

    #Singular value decomposition
    #U and V are the unitary matrices -> not clear on what these are
    #S are the singular values
    U, S, V = np.linalg.svd(E)
    W = np.array([0.0, -1.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0]).reshape(3, 3)

    R = U.dot(W).dot(V)
    T = U[:, 2]

    if not in_front_of_both_cameras(pts1, pts2, R, T):
        # Second choice: R = U * W * Vt, T = -u_3
        T = - U[:, 2]
        if not in_front_of_both_cameras(pts1, pts2, R, T):
            # Third choice: R = U * Wt * Vt, T = u_3
            R = U.dot(W.T).dot(V)
            T = U[:, 2]
     
            if not in_front_of_both_cameras(pts1, pts2, R, T):
                # Fourth choice: R = U * Wt * Vt, T = -u_3
                T = - U[:, 2]
    
    return R, T, F, pts1, pts2
Exemple #18
0
def _sift_extract(image):
    sift = cv2.SIFT()
    keypoints, descriptors = sift.detectAndCompute(image, None)
    return keypoints, descriptors
Exemple #19
0
start = time.time()

# read the train and test files
train_images_filenames = cPickle.load(open('train_images_filenames.dat', 'r'))
test_images_filenames = cPickle.load(open('test_images_filenames.dat', 'r'))
train_labels = cPickle.load(open('train_labels.dat', 'r'))
test_labels = cPickle.load(open('test_labels.dat', 'r'))
print 'Loaded ' + str(
    len(train_images_filenames
        )) + ' training images filenames with classes ', set(train_labels)
print 'Loaded ' + str(
    len(test_images_filenames
        )) + ' testing images filenames with classes ', set(test_labels)

# create the SIFT detector object
SIFTdetector = cv2.SIFT(nfeatures=300)

# extract SIFT keypoints and descriptors
# store descriptors in a python list of numpy arrays
Train_descriptors = []
Train_label_per_descriptor = []
for i in range(len(train_images_filenames)):
    filename = train_images_filenames[i]
    print 'Reading image ' + filename
    ima = cv2.imread(filename)
    gray = cv2.cvtColor(ima, cv2.COLOR_BGR2GRAY)
    kpt, des = SIFTdetector.detectAndCompute(gray, None)
    Train_descriptors.append(des)
    Train_label_per_descriptor.append(train_labels[i])
    print str(len(kpt)) + ' extracted keypoints and descriptors'
Exemple #20
0
        np.array([
            tempM0[:, 1][i][0] / tempM0[:, 1][i][1]
            for i in np.arange(tempM0.shape[0])
        ]) < ratio)
    matches = np.transpose(matches)[0]

    ptsA = []
    ptsB = []

    for i in matches:
        ptsA.append(tempM0[i][0][0].data[0])
        ptsB.append(kpsB[i])

    return np.array(ptsA), np.array(ptsB)


if __name__ == '__main__':
    # figuring out how to get to the img directory
    path = os.path.realpath(__file__)
    path = path[:path.rindex('/')]
    #importing 2 placeholder images
    imA = cv2.imread(path + '/img/tree.jpeg')
    imB = cv2.imread(path + '/img/home.jpeg')
    # getting the grayscale
    imAG = cv2.cvtColor(imA, cv2.COLOR_BGR2GRAY)
    imBG = cv2.cvtColor(imB, cv2.COLOR_BGR2GRAY)
    # get the kp and descriptors
    kpA, desA = cv2.SIFT().detectAndCompute(imAG, None)
    kpB, desB = cv2.SIFT().detectAndCompute(imBG, None)
    # match the keypoints and kaboom
    mptsA, mptsB = matchKeypoints(kpA, kpB, desA, desB)
Exemple #21
0
def generateCorners(img):
    sift = cv.SIFT()
    kp, des = sift.detectAndCompute(img.gray, None)
    img.keypoints = kp
    img.descriptors = des
    return img
Exemple #22
0
import cv2

if __name__ == "__main__":
    image_folder = sys.argv[1]  # location (directory) of image files
    feat_detect = sys.argv[2]  # only support SIFT extract & detect for now...
    out_folder = sys.argv[3]  # location (directory) where yaml files will go

    if not image_folder.endswith('/'):
        image_folder += '/'

    image_names = []  # list of image file names
    image_keypoints = [
    ]  # list of lists of keypoints associated with each image
    image_descriptors = []  # list of lists of descriptors

    features = cv2.SIFT()  # opencv SIFT feature extractor

    for im in os.listdir(image_folder):
        if im.startswith('.'):
            continue

        pic = cv2.imread(image_folder + im, 0)

        # extract features from pic
        # keypoints: x,y locations
        # descriptors: feature vectors
        keypoints, descriptors = features.detectAndCompute(pic, None)

        # convert from np.float32 to in
        descriptors = descriptors.astype(int)
#SIFT ALGORITHM
import Tkinter as Tk
from skimage.measure import structural_similarity as ssim
import matplotlib.pyplot as plt
import numpy as np
import cv2
import time

img1 = cv2.imread('dataset\obama.jpg', cv2.IMREAD_COLOR)
img2 = cv2.imread('dataset1\obama1.jpg', cv2.IMREAD_COLOR)

gray1 = cv2.cvtColor(img1, cv2.COLOR_BGR2GRAY)
gray2 = cv2.cvtColor(img2, cv2.COLOR_BGR2GRAY)

#SIFT
detector = cv2.SIFT()

keypoints1 = detector.detect(gray1, None)
keypoints2 = detector.detect(gray2, None)

outimg1 = cv2.drawKeypoints(gray1, keypoints1)
outimg2 = cv2.drawKeypoints(gray2, keypoints2)

cv2.imshow('img1', outimg1)
cv2.imshow('img2', outimg2)

#kp,des = sift.compute(gray,kp)
kp1, des1 = detector.compute(gray1, keypoints1)
kp2, des2 = detector.compute(gray2, keypoints2)

print kp1
def describeSIFT(image):
    sift = cv2.SIFT()
    kp, des = sift.detectAndCompute(image, None)
    return kp, des
Exemple #25
0
def describe(path):
    img = cv2.imread(path)
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    sift = cv2.SIFT()
    kp, des = sift.detectAndCompute(gray, None)
    return kp, des
Exemple #26
0
def sift_count(img):
    sift = cv2.SIFT(edgeThreshold=100)
    kp, des = sift.detectAndCompute(img, None)
    return len(kp)
'''
def objdraw(matchesMask,img1,img2,keypts1,keypts2):
    draw_params = dict(matchColor = (0,255,0), # draw matches in green color
                singlePointColor = None,
                matchesMask = matchesMask, # draw only inliers
                flags = 2)
    #print the below image to show corresponding points and transformations 
    img3 = cv2.drawMatches(img1,keypts1,img2,keypts2,good,None,**draw_params)

    #plt.imshow(img2, 'gray'),plt.show()
    cv2.imshow('img3',img3)
'''
#########################################################################

#surf=cv2.xfeatures2d.SIFT_create()
surf = cv2.SIFT()

#############
host = "192.168.0.100:8080"
if len(sys.argv) > 1:
    host = sys.argv[1]

hoststr = 'http://' + host + '/video'
print 'Streaming ' + hoststr

stream = urllib2.urlopen(hoststr)

bytes = ''
#############

img2 = cv2.imread('pi1.jpg', 0)
Exemple #28
0
def find_all_sift(im_source, im_search, min_match_count=4, maxcnt=0):
    '''
    使用sift算法进行多个相同元素的查找
    Args:
        im_source(string): 图像、素材
        im_search(string): 需要查找的图片
        threshold: 阈值,当相识度小于该阈值的时候,就忽略掉
        maxcnt: 限制匹配的数量

    Returns:
        A tuple of found [(point, rectangle), ...]
        A tuple of found [{"point": point, "rectangle": rectangle, "confidence": 0.76}, ...]
        rectangle is a 4 points list
    '''
    sift = cv2.SIFT(edgeThreshold=100)
    flann = cv2.FlannBasedMatcher({
        'algorithm': FLANN_INDEX_KDTREE,
        'trees': 5
    }, dict(checks=50))

    kp_sch, des_sch = sift.detectAndCompute(im_search, None)
    if len(kp_sch) < min_match_count:
        return None

    kp_src, des_src = sift.detectAndCompute(im_source, None)
    if len(kp_src) < min_match_count:
        return None

    h, w = im_search.shape[1:]

    result = []
    while True:
        # 匹配两个图片中的特征点,k=2表示每个特征点取2个最匹配的点
        matches = flann.knnMatch(des_sch, des_src, k=2)
        good = []
        for m, n in matches:
            # 剔除掉跟第二匹配太接近的特征点
            if m.distance < 0.9 * n.distance:
                good.append(m)

        if len(good) < min_match_count:
            break

        sch_pts = np.float32([kp_sch[m.queryIdx].pt
                              for m in good]).reshape(-1, 1, 2)
        img_pts = np.float32([kp_src[m.trainIdx].pt
                              for m in good]).reshape(-1, 1, 2)

        # M是转化矩阵
        M, mask = cv2.findHomography(sch_pts, img_pts, cv2.RANSAC, 5.0)
        matches_mask = mask.ravel().tolist()

        # 计算四个角矩阵变换后的坐标,也就是在大图中的坐标
        h, w = im_search.shape[:2]
        pts = np.float32([[0, 0], [0, h - 1], [w - 1, h - 1],
                          [w - 1, 0]]).reshape(-1, 1, 2)
        dst = cv2.perspectiveTransform(pts, M)

        # trans numpy arrary to python list
        # [(a, b), (a1, b1), ...]
        pypts = []
        for npt in dst.astype(int).tolist():
            pypts.append(tuple(npt[0]))

        lt, br = pypts[0], pypts[2]
        middle_point = (lt[0] + br[0]) / 2, (lt[1] + br[1]) / 2

        result.append(
            dict(
                result=middle_point,
                rectangle=pypts,
                confidence=(matches_mask.count(1), len(good)
                            )  #min(1.0 * matches_mask.count(1) / 10, 1.0)
            ))

        if maxcnt and len(result) >= maxcnt:
            break

        # 从特征点中删掉那些已经匹配过的, 用于寻找多个目标
        qindexes, tindexes = [], []
        for m in good:
            qindexes.append(m.queryIdx)  # need to remove from kp_sch
            tindexes.append(m.trainIdx)  # need to remove from kp_img

        def filter_index(indexes, arr):
            r = np.ndarray(0, np.float32)
            for i, item in enumerate(arr):
                if i not in qindexes:
                    r = np.append(r, item)
            return r

        kp_src = filter_index(tindexes, kp_src)
        des_src = filter_index(tindexes, des_src)

    return result
Exemple #29
0
#!/usr/bin/env python
# -*- coding: utf-8 -*-

# import auto
# import template
# import sift

import cv2

_sift = cv2.SIFT()


def _cv2open(filename, arg=1):
    if isinstance(filename, basestring):
        obj = cv2.imread(filename, arg)
    else:
        obj = filename
    return obj


def sift_point_count(image_file):
    im = _cv2open(image_file)
    if im == None:
        return 0
    kp_sch, des_sch = _sift.detectAndCompute(im, None)
    return len(kp_sch)


# def find(img, template, rect=None, method='auto'):
#     '''
#     @return rect, ratio
Exemple #30
0
# -*- coding: utf-8 -*-
"""
Created on Sun May 20 11:53:21 2018

@author: Fakrul-IslamTUSHAR
"""

import numpy as np
import cv2 as cv
from matplotlib import pyplot as plt
img1 = cv.imread('20587902_8dbbd4e51f549ff0_MG_R_CC_ANON_2.tif',0)          # queryImage
img2 = cv.imread('20587902_8dbbd4e51f549ff0_MG_R_CC_ANON.tif',0) # trainImage
# Initiate SIFT detector
sift = cv.SIFT()
# find the keypoints and descriptors with SIFT
kp1, des1 = sift.detectAndCompute(img1,None)
kp2, des2 = sift.detectAndCompute(img2,None)
# BFMatcher with default params
bf = cv.BFMatcher()
matches = bf.knnMatch(des1,des2, k=2)
# Apply ratio test
good = []
for m,n in matches:
    if m.distance < 0.75*n.distance:
        good.append([m])
# cv.drawMatchesKnn expects list of lists as matches.
img3 = cv.drawMatchesKnn(img1,kp1,img2,kp2,good,flags=2)
plt.imshow(img3),plt.show()