def matching_correction(self, image, image2): ''' Computes keypoints for two images and try to align image2 on image1 ''' #computing keypoints matching s = sift.SiftPlan(template=image, devicetype="gpu") kp1 = s.keypoints(image) kp2 = s.keypoints(image2) #image2 and image must have the same size m = sift.MatchPlan(devicetype="GPU") matching = m.match(kp2, kp1) N = matching.shape[0] #solving normals equations for least square fit X = numpy.zeros((2 * N, 6)) X[::2, 2:] = 1, 0, 0, 0 X[::2, 0] = matching.x[:, 0] X[::2, 1] = matching.y[:, 0] X[1::2, 0:3] = 0, 0, 0 X[1::2, 3] = matching.x[:, 0] X[1::2, 4] = matching.y[:, 0] X[1::2, 5] = 1 y = numpy.zeros((2 * N, 1)) y[::2, 0] = matching.x[:, 1] y[1::2, 0] = matching.y[:, 1] #A = numpy.dot(X.transpose(),X) #sol = numpy.dot(numpy.linalg.inv(A),numpy.dot(X.transpose(),y)) sol = numpy.dot(numpy.linalg.pinv(X), y) MSE = numpy.linalg.norm(y - numpy.dot( X, sol))**2 / N #value of the sum of residuals at "sol" return sol, MSE
def __init__(self, fname, devicetype=None, device=None, context=None): self.image_rgb = scipy.misc.imread(fname) self.image_bw = 0.299 * self.image_rgb[:, :, 0] + 0.587 * self.image_rgb[:, :, 1] + 0.114 * self.image_rgb[:, :, 2] if feature: self._sift_cpp = feature.SiftAlignment() self._sift_ocl = sift.SiftPlan(template=self.image_rgb, device=device, devicetype=devicetype, context=context) self.kp_cpp = numpy.empty(0) self.kp_ocl = numpy.empty(0) if SHOW_FIGURES == True: self.fig = pylab.figure() self.sp1 = self.fig.add_subplot(1, 2, 1) self.im1 = self.sp1.imshow(self.image_rgb) self.sp1.set_title("OpenCL: %s keypoint" % self.kp_ocl.size) self.sp2 = self.fig.add_subplot(1, 2, 2) self.im2 = self.sp2.imshow(self.image_bw, cmap="gray") self.sp2.set_title("C++: %s keypoint" % self.kp_cpp.size) self.fig.show() self.timing_cpp = None self.timing_ocl = None self.speedups = numpy.zeros((1, 3), dtype=numpy.float32)
def __init__(self, filename=None, devicetype=None, device=None, context=None, profile=False): if filename and os.path.exists(filename): self.filename = filename else: self.filename = utilstest.UtilsTest.getimage("wikipedia/commons/9/94/Esrf_grenoble.jpg") try: self.image_rgb = scipy.misc.imread(self.filename) except: import fabio self.image_rgb = fabio.open(self.filename).data if self.image_rgb.ndim != 2: self.image_bw = 0.299 * self.image_rgb[:, :, 0] + 0.587 * self.image_rgb[:, :, 1] + 0.114 * self.image_rgb[:, :, 2] else: self.image_bw = self.image_rgb if feature: self._sift_cpp = feature.SiftAlignment() self._sift_ocl = sift.SiftPlan(template=self.image_rgb, device=device, devicetype=devicetype, context=context, profile=profile) self.ctx = self._sift_ocl.ctx self.kp_cpp = numpy.empty(0) self.kp_ocl = numpy.empty(0) self.fig = pylab.figure() self.sp1 = self.fig.add_subplot(1, 2, 1) self.im1 = self.sp1.imshow(self.image_rgb) self.sp1.set_title("OpenCL: %s keypoint" % self.kp_ocl.size) self.sp2 = self.fig.add_subplot(1, 2, 2) self.im2 = self.sp2.imshow(self.image_bw, cmap="gray") self.sp2.set_title("C++: %s keypoint" % self.kp_cpp.size) self.fig.show() self.timing_cpp = None self.timing_ocl = None
def __init__(self, filename=None, devicetype=None, device=None, profile=False): if filename and os.path.exists(filename): self.filename = filename else: self.filename = "Esrf_grenoble.jpg" data = urllib2.urlopen( "http://upload.wikimedia.org/wikipedia/commons/9/94/Esrf_grenoble.jpg" ).read() open(self.filename, "wb").write(data) self.image_rgb = scipy.misc.imread(self.filename) if self.image_rgb.ndim != 2: self.image_bw = 0.299 * self.image_rgb[:, :, 0] + 0.587 * self.image_rgb[:, :, 1] + 0.114 * self.image_rgb[:, :, 2] else: self.image_bw = self.image_rgb if feature: self._sift_cpp = feature.SiftAlignment() self._sift_ocl = sift.SiftPlan(template=self.image_rgb, device=device, devicetype=devicetype, profile=profile) self.kp_cpp = numpy.empty(0) self.kp_ocl = numpy.empty(0) self.fig = pylab.figure() self.sp1 = self.fig.add_subplot(1, 2, 1) self.sp2 = self.fig.add_subplot(1, 2, 2) #elf.sp3 = self.fig.add_subplot(1, 2, 3) #elf.sp4 = self.fig.add_subplot(1, 2, 4) self.im1 = self.sp1.imshow(self.image_rgb) self.sp1.set_title("OpenCL: %s keypoint" % self.kp_ocl.size) self.im2 = self.sp2.imshow(self.image_bw, cmap="gray") self.sp2.set_title("C++: %s keypoint" % self.kp_cpp.size) self.fig.show() self.timing_cpp = None self.timing_ocl = None
img2 = fabio.open(sys.argv[2]).data except: logger.error("Unable to read input images") else: img1 = scipy.misc.lena() img2 = scipy.ndimage.rotate(img1, 10, reshape=False) ''' img1 = scipy.misc.imread("../test_images/fruit_bowl.png") tmp = scipy.misc.imread("../test_images/banana.png") img2 = numpy.zeros_like(img1) img2 = img2 + 255 d0 = (img1.shape[0] - tmp.shape[0])/2 d1 = (img1.shape[1] - tmp.shape[1])/2 img2[d0:-d0,d1:-d1-1] = numpy.copy(tmp) ''' plan = sift.SiftPlan(template=img1, devicetype="cpu") kp1 = plan.keypoints(img1) kp2 = plan.keypoints(img2) print("Keypoints for img1: %i\t img2: %i" % (kp1.size, kp2.size)) fig = pylab.figure() sp1 = fig.add_subplot(122) sp2 = fig.add_subplot(121) im1 = sp1.imshow(img1) im2 = sp2.imshow(img2) #match = feature.sift_match(kp1, kp2) mp = sift.MatchPlan(devicetype="cpu") matching = mp.match(kp1, kp2) print("After matching keeping %i" % matching.shape[0]) dx = matching[:, 1].x - matching[:, 0].x dy = matching[:, 1].y - matching[:, 0].y dangle = matching[:, 1].angle - matching[:, 0].angle
#here = os.path.dirname(os.path.abspath(__file__)) #there = os.path.join(here, "..", "build") #lib = [os.path.abspath(os.path.join(there, i)) for i in os.listdir(there) if "lib" in i][0] #sys.path.insert(0, lib) import sift_pyocl as sift import numpy import scipy.misc # try: lena = scipy.misc.imread(sys.argv[1]) except: try: import fabio lena = fabio.open(sys.argv[1]).data except: lena = scipy.misc.lena() logger.info("Using image lena from scipy") else: logger.info("Using image %s read with FabIO" % sys.argv[1]) else: logger.info("Using image %s read with SciPy" % sys.argv[1]) s = sift.SiftPlan(template=lena, profile=True, context=utilstest.ctx) kp = s.keypoints(lena) print kp[:10] if utilstest.args and not os.path.isfile(utilstest.args[0]): s.log_profile(utilstest.args[0]) else: s.log_profile()
import pylab lena2 = scipy.misc.lena() #shape = lena2.shape #lena2 = scipy.misc.imread("../aerial.tiff") #for other tests #filename = "/users/kieffer/Pictures/2010-01-22/11h59m14-Canon_DIGITAL_IXUS_850_IS-Pano21.jpg" #shape = 1001, 1599 #lena2 = scipy.misc.imread(filename)#, flatten=True) #shape = lena2.shape #lena = numpy.ascontiguousarray(lena2[:shape[0], 0:shape[1], :]) lena = lena2 print lena.shape # lena[:] = 0 # lena[100:110, 100:110] = 255 s = sift.SiftPlan(template=lena, profile=True, max_workgroup_size=128, device=(1, 0)) kpg = s.keypoints(lena) #except (Exception, pyopencl.RuntimeError) as error: # print error # s.log_profile() # sys.exit(0) kp = numpy.empty((kpg.size, 4), dtype=numpy.float32) kp[:, 0] = kpg.x kp[:, 1] = kpg.y kp[:, 2] = kpg.scale kp[:, 3] = kpg.angle #print "Non infinite", numpy.isfinite(kpg.angle).sum() s.log_profile() fig = pylab.figure() sp1 = fig.add_subplot(1, 2, 1)