示例#1
0
def match_images(img1, img2):
    """Given two images, returns the matches"""
    detector = cv2.SURF(10, 5, 5)
    matcher = cv2.BFMatcher(cv2.NORM_L2)

    tic.k('img1 detect')
    kp1, desc1 = detector.detectAndCompute(img1, None)

    # print type(kp1), type(desc1)
    tic.k('img2 detect')

    # print kp1
    # print desc1.shape
    ''' kp = keypoint, kp[0].pt = #0 keypoint x axis, y axis point'''

    i = 1

    # f = open('keypoint.txt','w')

    # print 'x:',kp1[i].pt[0]
    # print 'y:', kp1[i].pt[1]
    # print 'pt:',kp1[i].pt
    # print 'size:',kp1[i].size
    # print 'angle:',kp1[i].angle
    # print 'response:',kp1[i].response
    # print 'octave:',kp1[i].octave
    # print 'class_id:', kp1[i].class_id
    #
    # for i in range(len(kp1)):
    #     f.write(str(kp1[i].pt[0]))
    #     f.write(str(kp1[i].pt[1]))
    #     f.write(str(kp1[i].size))
    #     f.write(str(kp1[i].angle))
    #     f.write(str(kp1[i].response))
    #     f.write(str(kp1[i].octave))
    #     f.write(str(kp1[i].class_id))
    #     f.write('\n')
    #
    # f.close()
    # print desc1[i][0]
    #
    # f = open('descriptor.txt','w')
    #
    # for i in range(len(kp1)):
    #     f.write(str(desc1[i]))
    #     f.write('\n')
    #
    # f.close()


    kp2, desc2 = detector.detectAndCompute(img2, None)
    # print 'img1 - %d features, img2 - %d features' % (len(kp1), len(kp2))

    raw_matches = matcher.knnMatch(desc1, trainDescriptors=desc2, k=2)  # 2
    kp_pairs = filter_matches(kp1, kp2, raw_matches)
    print len(kp_pairs)
    return kp_pairs
示例#2
0
from PIL import Image
from pylab import *
import cPickle as pickle
import glob
import os

import homography
import sift
import tic
import warp

imname = glob.glob('out_Photos/IMG_*.jpg')
siftname = [os.path.splitext(im)[0] + '.sift' for im in imname]

tic.k('start')

l, d = {}, {}
for i in range(len(imname)):
    l[i], d[i] = sift.read_or_compute(imname[i], siftname[i])

tic.k('loaded')

matches = {}
if not os.path.exists('out_ch03_pano.pickle'):
    for i in range(len(imname) - 1):
        matches[i] = sift.match(d[i + 1], d[i])
        # Slightly better matches, but ransac can handle the worse quality:
        #matches[i] = sift.match_twosided(d[i + 1], d[i])
    pickle.dump(matches, open('out_ch03_pano.pickle', 'wb'))
matches = pickle.load(open('out_ch03_pano.pickle', 'rb'))
import glob
import numpy
import os
import cPickle as pickle

import camera
import homography
import sfm
import sift
import tic

imname = glob.glob('out_corner/IMG_*.jpg')
#imname = glob.glob('out_alcatraz/*.jpg')
siftname = [os.path.splitext(im)[0] + '.sift' for im in imname]

tic.k('start')

# For out_corner, this increases feature count from 4k to 16k and matches from
# 100 to 170 (which helps quality, but also slows down the program a lot, from
# from 20s to 60s):
# (with twosided matching, matches go from 85 to 113 for out_corner)
# NOTE: delete caches after changing this!
histeq = False

l, d = {}, {}
for i in range(len(imname)):
  l[i], d[i] = sift.read_or_compute(imname[i], siftname[i], histeq)

tic.k('loaded sifts')

print '{} / {} features'.format(len(d[0]), len(d[1]))
示例#4
0
@author: atch
"""
import os
import cPickle as pickle
import tic
import sift
import vocabulary
from imtools import get_imageList

imlist = get_imageList("../local/data/JianDa1")
imcount = len(imlist)
print imlist
print imcount
featlist = [imlist[i][:-3] + 'sift' for i in range(imcount)]

tic.k('Start')
for i in range(imcount):
    if not os.path.exists(featlist[i]):
        sift.process_image(imlist[i], featlist[i])
tic.k('sift loaded')

voc = vocabulary.Vocabulary('JianDa1')  # ukbenchtest
voc.train(featlist, k=imcount, subsampling=10)
tic.k('train loaded')

# 保存词汇
#imagepkl = r"pickle\vocabulary.pkl"
imagepkl = r"../static\pickle\jianda1.pkl"
with open(imagepkl, 'wb') as f:
    pickle.dump(voc, f)
print imagepkl, 'is:', voc.name, voc.word_count


SUDOKU_PATH = '/Users/thakis/Downloads/data/sudoku_images/sudokus/'
imname = os.path.join(SUDOKU_PATH, 'sudoku8.jpg')

im = array(Image.open(imname).convert('L'))

# Ask user for corners.
figure()
imshow(im)
gray()
x = ginput(4)

# top left, top right, bottom right, bottom left
# Note: The book switches fp from (x, y) order in |x| to (y, x) order in
# fp. imtools.Htransform() expects (x, y) order, so don't do this switching
# here.
fp = array([array([p[0], p[1], 1]) for p in x]).T
tp = array([[0, 0, 1], [1000, 0, 1], [1000, 1000, 1], [0, 1000, 1]]).T
H = homography.H_from_points(tp, fp)

tic.k('starting warp')
im_g = imtools.Htransform(im, H, (1000, 1000))
tic.k('warped')

figure()
imshow(im_g)
gray()
show()
import md5
import numpy
import os
import cPickle as pickle

import camera
import homography
import sfm
import sift
import tic

imname = glob.glob('out_corner/IMG_*.jpg')
#imname = glob.glob('out_alcatraz/*.jpg')
siftname = [os.path.splitext(im)[0] + '.sift' for im in imname]

tic.k('start')

l, d = {}, {}
for i in range(len(imname)):
  l[i], d[i] = sift.read_or_compute(imname[i], siftname[i])

tic.k('loaded sifts')

print '{} / {} features'.format(len(d[0]), len(d[1]))

immd5 = md5.md5(''.join(imname)).hexdigest()
matchcache = 'out_ch05ex02_cache_matches_%s.pickle' % immd5
if not os.path.exists(matchcache):
  #matches = sift.match(d[0], d[1])
  matches = sift.match_twosided(d[0], d[1])
  pickle.dump(matches, open(matchcache, 'wb'))
示例#7
0
from PIL import Image
from pylab import *
import cPickle as pickle
import glob
import os
import numpy

import homography
import sift
import tic
import warp

imname = glob.glob('out_corner/IMG_*.jpg')
siftname = [os.path.splitext(im)[0] + '.sift' for im in imname]

tic.k('start')

l, d = {}, {}
for i in range(len(imname)):
    l[i], d[i] = sift.read_or_compute(imname[i], siftname[i])

tic.k('loaded sifts')

if not os.path.exists('out_ch03_ex07_match.pickle'):
    matches = sift.match(d[1], d[0])
    pickle.dump(matches, open('out_ch03_ex07_match.pickle', 'wb'))
matches = pickle.load(open('out_ch03_ex07_match.pickle', 'rb'))

tic.k('matched')

ndx = matches.nonzero()[0]
from PIL import Image
from pylab import *
import cPickle as pickle
import glob
import os
import numpy

import homography
import sift
import tic
import warp

imname = glob.glob('out_corner/IMG_*.jpg')
siftname = [os.path.splitext(im)[0] + '.sift' for im in imname]

tic.k('start')

l, d = {}, {}
for i in range(len(imname)):
  l[i], d[i] = sift.read_or_compute(imname[i], siftname[i])

tic.k('loaded sifts')


if not os.path.exists('out_ch03_ex06_match.pickle'):
  matches = sift.match(d[1], d[0])
  pickle.dump(matches, open('out_ch03_ex06_match.pickle', 'wb'))
matches = pickle.load(open('out_ch03_ex06_match.pickle', 'rb'))

tic.k('matched')
示例#9
0
import glob
import numpy
import os
import cPickle as pickle

import camera
import homography
import sfm
import sift
import tic

imname = glob.glob('out_corner/IMG_*.jpg')
#imname = glob.glob('out_alcatraz/*.jpg')
siftname = [os.path.splitext(im)[0] + '.sift' for im in imname]

tic.k('start')

# For out_corner, this increases feature count from 4k to 16k and matches from
# 100 to 170 (which helps quality, but also slows down the program a lot, from
# from 20s to 60s):
# (with twosided matching, matches go from 85 to 113 for out_corner)
# NOTE: delete caches after changing this!
histeq = False

l, d = {}, {}
for i in range(len(imname)):
    l[i], d[i] = sift.read_or_compute(imname[i], siftname[i], histeq)

tic.k('loaded sifts')

print '{} / {} features'.format(len(d[0]), len(d[1]))
示例#10
0
    if len(p1):
        explore_match(window_name, img1, img2, kp_pairs, status, H)


def db_connect(self):
    con = sqlite3.connect("f.db")
    cursor = con.cursor()
    cursor.execute("CREATE TABLE ")

###############################################################################
# Test Main
###############################################################################

if __name__ == '__main__':
    tic.k('start')
    """Test code: Uses the two specified"""
    # if len(sys.argv) < 3:
    #     print "No filenames specified"
    #     print "USAGE: find_obj.py <image1> <image2>"
    #     sys.exit(1)

    # fn1 = './data/s20160720_113416.jpg'
    # fn2 = './data/s20160720_113436.jpg'

    fn1 = './data/ss20160908_145558.jpg'
    fn2 = './data/ss20160908_145556.jpg'

    img1 = cv2.imread(fn1, 0)
    img2 = cv2.imread(fn2, 0)
示例#11
0
from PIL import Image
from pylab import *
import cPickle as pickle
import glob
import os

import homography
import sift
import tic
import warp


imname = glob.glob('out_Photos/IMG_*.jpg')[1:3]
siftname = [os.path.splitext(im)[0] + '.sift' for im in imname]

tic.k('start')

l, d = {}, {}
for i in range(len(imname)):
  l[i], d[i] = sift.read_or_compute(imname[i], siftname[i])

  # Only use features from the top of the image:
  #bettera, betterb = [], []
  #for j in range(len(l[i])):
  #  if l[i][j][1] < 900:
  #    bettera.append(l[i][j])
  #    betterb.append(d[i][j])
  #l[i] = array(bettera)
  #d[i] = array(betterb)

tic.k('loaded')
示例#12
0
import numpy
import os
from PIL import Image

from libsvm import svmutil

import ocr
import sudoku
import tic


# Load sudoku image, find cells.
tic.k('start')
SUDOKU_PATH = '/Users/thakis/Downloads/data/sudoku_images/sudokus/'
imname = os.path.join(SUDOKU_PATH, 'sudoku18.jpg')
vername = os.path.join(SUDOKU_PATH, 'sudoku18.sud')

im = numpy.array(Image.open(imname).convert('L'))

x = sudoku.find_sudoku_edges(im, axis=0)
y = sudoku.find_sudoku_edges(im, axis=1)
tic.k('found edges')


# Extract cells, run OCR.
OCR_PATH = '/Users/thakis/Downloads/data/sudoku_images/ocr_data/'
features, labels = ocr.load_ocr_data(os.path.join(OCR_PATH, 'training'))
problem = svmutil.svm_problem(labels, map(list, features))
param = svmutil.svm_parameter('-q -t 0')
model = svmutil.svm_train(problem, param)
tic.k('built OCR model')
示例#13
0
from PIL import Image
from pylab import *
import cPickle as pickle
import glob
import os

import homography
import sift
import tic
import warp


imname = glob.glob('out_Photos/IMG_*.jpg')
siftname = [os.path.splitext(im)[0] + '.sift' for im in imname]

tic.k('start')

l, d = {}, {}
for i in range(len(imname)):
  l[i], d[i] = sift.read_or_compute(imname[i], siftname[i])


tic.k('loaded')

matches = {}
if not os.path.exists('out_ch03_pano.pickle'):
  for i in range(len(imname) - 1):
    matches[i] = sift.match(d[i + 1], d[i])
    # Slightly better matches, but ransac can handle the worse quality:
    #matches[i] = sift.match_twosided(d[i + 1], d[i])
  pickle.dump(matches, open('out_ch03_pano.pickle', 'wb'))
import md5
import numpy
import os
import cPickle as pickle

import camera
import homography
import sfm
import sift
import tic

imname = glob.glob('out_corner/IMG_*.jpg')
#imname = glob.glob('out_alcatraz/*.jpg')
siftname = [os.path.splitext(im)[0] + '.sift' for im in imname]

tic.k('start')

l, d = {}, {}
for i in range(len(imname)):
    l[i], d[i] = sift.read_or_compute(imname[i], siftname[i])

tic.k('loaded sifts')

print '{} / {} features'.format(len(d[0]), len(d[1]))

immd5 = md5.md5(''.join(imname)).hexdigest()
matchcache = 'out_ch05ex02_cache_matches_%s.pickle' % immd5
if not os.path.exists(matchcache):
    #matches = sift.match(d[0], d[1])
    matches = sift.match_twosided(d[0], d[1])
    pickle.dump(matches, open(matchcache, 'wb'))
示例#15
0
import imtools
import tic

SUDOKU_PATH = '/Users/thakis/Downloads/data/sudoku_images/sudokus/'
imname = os.path.join(SUDOKU_PATH, 'sudoku8.jpg')

im = array(Image.open(imname).convert('L'))

# Ask user for corners.
figure()
imshow(im)
gray()
x = ginput(4)

# top left, top right, bottom right, bottom left
# Note: The book switches fp from (x, y) order in |x| to (y, x) order in
# fp. imtools.Htransform() expects (x, y) order, so don't do this switching
# here.
fp = array([array([p[0], p[1], 1]) for p in x]).T
tp = array([[0, 0, 1], [1000, 0, 1], [1000, 1000, 1], [0, 1000, 1]]).T
H = homography.H_from_points(tp, fp)

tic.k('starting warp')
im_g = imtools.Htransform(im, H, (1000, 1000))
tic.k('warped')

figure()
imshow(im_g)
gray()
show()