Exemplo n.º 1
0
 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
Exemplo n.º 2
0
    def siftAlign(self):
        '''
        Call SIFT to align images
        Assume that all the images have the same dimensions !
        '''

        mp = sift.MatchPlan(devicetype=self.devicetype)

        #TODO: place the following in a separate routine (in SIFT module ?)
        kernel_path = "openCL/transform.cl"
        kernel_src = open(kernel_path).read()
        program = pyopencl.Program(
            self.ctx,
            kernel_src).build()  #.build('-D WORKGROUP_SIZE=%s' % wg_size)
        wg = 8, 8  #FIXME: hard-coded

        i = 0
        for img in os.listdir(self.save_folder):
            if i == 0:  #compute SIFT keypoints on the first image
                i = 1
                plan = sift.SiftPlan(template=img, devicetype=self.devicetype)
                kp_first = plan.keypoints(img)
            else:
                kp = plan.keypoints(img)
                m = mp.match(kp_first, kp)
                sol = self.matchingCorrection(m)

                correction_matrix = numpy.zeros((2, 2), dtype=numpy.float32)
                correction_matrix[0] = sol[0:2, 0]
                correction_matrix[1] = sol[3:5, 0]
                matrix_for_gpu = correction_matrix.reshape(
                    4, 1)  #for float4 struct
                offset_value[0] = sol[2, 0]
                offset_value[1] = sol[5, 0]

                img, image_height, image_width = self.imageReshape(img)
                gpu_image = pyopencl.array.to_device(self.queue, img)
                gpu_output = pyopencl.array.empty(self.queue,
                                                  (image_height, image_width),
                                                  dtype=numpy.float32,
                                                  order="C")
                gpu_matrix = pyopencl.array.to_device(self.queue,
                                                      matrix_for_gpu)
                gpu_offset = pyopencl.array.to_device(self.queue, offset_value)
                image_height, image_width = numpy.int32(
                    (image_height, image_width))
                output_height, output_width = image_height, image_width

                if i == 1: shape = calc_size((output_width, output_height), wg)
                k1 = program.transform(self.queue, shape, wg, gpu_image.data,
                                       gpu_output.data, gpu_matrix.data,
                                       gpu_offset.data, image_width,
                                       image_height, output_width,
                                       output_height, fill_value, mode)
                res = gpu_output.get()

                #                scipy.misc.imsave(self.aligned_folder + "/frame" + str(i) +".png", res)
                i += 1
Exemplo n.º 3
0
 def __init__(self, filename=None, devicetype=None, device=None, profile=False):
     if filename and os.path.exists(filename):
         self.filename = filename
     else:
         self.filename = UtilsTest.getimage("wikipedia/commons/9/94/Esrf_grenoble.jpg")
     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.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
Exemplo n.º 4
0
import sift
import numpy
import scipy.misc
img = scipy.misc.imread("../mathlab/nightsky_modified.png")

siftp = sift.SiftPlan(img.shape, img.dtype, devicetype="cpu")
kp = siftp.keypoints(img)
#kp.sort(order=["scale", "angle", "x", "y"])
print kp
Exemplo n.º 5
0
        "Feature is not available to compare results with C++ implementation")
    feature = None
    res = numpy.empty(0)

img1 = scipy.misc.imread("../../test_match/wiw2.jpg")
img2 = scipy.misc.imread("../../test_match/waldo.jpg")
'''
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="gpu")
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()
m = mp.match(kp1, kp2)
print("After matching keeping %i" % m.shape[0])
fig.show()
#raw_input("Enter")
for i in range(m.shape[0]):
Exemplo n.º 6
0
#!/usr/bin/python
import sys, os
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
import numpy
import scipy.misc

#lena = scipy.misc.lena()
lena = scipy.misc.imread("../../test_images/ESR032h.jpg")

s = sift.SiftPlan(template=lena, profile=True, devicetype="GPU")
kp = s.keypoints(lena)
print kp.shape
s.log_profile()
Exemplo n.º 7
0
testimgfile = r'O:\DataTeam\FindTheFish\Data\train\train\ALB\YVB\img_00003.jpg'
imgtest = array(Image.open(testimgfile).convert('L'))
figure()
gray()
imshow(imgtest)

imcorr = signal.correlate2d(imgtest, levelimage, boundary='wrap')
imshow(imcorr)
plot(imcorr[:, 1400])

imcolor = imtools.denoise(imgray, imgray)

## SIFT

image_rgb = scipy.misc.imread(imgname)
sift_ocl = sift.SiftPlan(template=image_rgb, device=GPU)
kp = sift_ocl.keypoints(image_rgb)
kp.sort(order=["scale", "angle", "x", "y"])
print kp

img = cv2.imread(imgname)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
sift = cv2.xfeatures2d.SIFT_create()
kp = sift.detect(imgray, None)
imsift = cv2.drawKeypoints(imgray, kp, imgray)
cv2.imwrite('sift_keypoints.jpg', imsift)

imsiftkp = cv2.drawKeypoints(gray,
                             kp,
                             imsift,
                             flags=cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)