Пример #1
0
def test_fast_homography():
    img = rgb2gray(data.lena()).astype(np.uint8)
    img = img[:, :100]
    
    theta = np.deg2rad(30)
    scale = 0.5
    tx, ty = 50, 50

    H = np.eye(3)
    S = scale * np.sin(theta)
    C = scale * np.cos(theta)

    H[:2, :2] = [[C, -S], [S, C]]
    H[:2, 2] = [tx, ty]

    for mode in ('constant', 'mirror', 'wrap'):
        print 'Transform mode:', mode

        p0 = homography(img, H, mode=mode, order=1)
        p1 = fast_homography(img, H, mode=mode)
        p1 = np.round(p1)

        ## import matplotlib.pyplot as plt
        ## f, (ax0, ax1, ax2, ax3) = plt.subplots(1, 4)
        ## ax0.imshow(img)
        ## ax1.imshow(p0, cmap=plt.cm.gray)
        ## ax2.imshow(p1, cmap=plt.cm.gray)
        ## ax3.imshow(np.abs(p0 - p1), cmap=plt.cm.gray)
        ## plt.show()

        d = np.mean(np.abs(p0 - p1))
        print "delta=", d
        assert d < 0.2
Пример #2
0
def phog(image, bin, angle, L, roi):
    """ phog -- Function that prepares data structures and histogram indexing and then
        calls other functions to execute the pryamidal histogram of gradient code.
        
        inputs: image -- String that names the file path to the image to compute on.
                bin -- Number of components to use for each subwindow histogram
                angle -- 180 or 360 if you want half-circle or whole-circle angles, respectively.
                L -- Number of pyramid levels to use.
                roi -- List with 4 entries, [top_y, bottom_y, left_x, right_x], that bounds the
                       image region for which you want to compute the HoG feature.
                       
        outputs: p -- A long vector that contains the pHoG descriptor.
    """
    Img = imread(image)
    if len(Img.shape) == 3:
        G = clr.rgb2gray(Img)
    else:
        G = Img

    if np.sum(G) > 100:
        E = canny(G)
        GradientX, GradientY = np.gradient(np.double(G))

        Gr = np.sqrt(GradientX**2 + GradientY**2)
        GradientX[GradientX == 0] = 1 * 10**(-5)

        YX = np.divide(GradientY, GradientX)
        if angle == 180:
            A = (np.arctan(YX + np.pi / 2.0) * 180) / np.pi
        if angle == 360:
            A = (np.arctan2(GradientY, GradientX) + np.pi) * 180 / np.pi

        # Function call
        bh, bv = binMatrix(A, E, Gr, angle, bin)

    else:
        bh = np.zeros((G.shape[0], G.shape[1]))
        bv = np.zeros((G.shape[0], G.shape[1]))

    bh_roi = bh[roi[0]:roi[1], roi[2]:roi[3]]
    bv_roi = bv[roi[0]:roi[1], roi[2]:roi[3]]

    # Function call to actually extract the HoG vector.
    p = phogDescriptor(bh_roi, bv_roi, L, bin)

    return p
Пример #3
0
def phog(image, bin, angle, L, roi):
    """ phog -- Function that prepares data structures and histogram indexing and then
        calls other functions to execute the pryamidal histogram of gradient code.
        
        inputs: image -- String that names the file path to the image to compute on.
                bin -- Number of components to use for each subwindow histogram
                angle -- 180 or 360 if you want half-circle or whole-circle angles, respectively.
                L -- Number of pyramid levels to use.
                roi -- List with 4 entries, [top_y, bottom_y, left_x, right_x], that bounds the
                       image region for which you want to compute the HoG feature.
                       
        outputs: p -- A long vector that contains the pHoG descriptor.
    """
    Img = imread(image)
    if len(Img.shape) == 3:
        G = clr.rgb2gray(Img)
    else:
        G = Img
    
    if np.sum(G) > 100:
        E = canny(G)
        GradientX, GradientY = np.gradient(np.double(G))
        
        Gr = np.sqrt(GradientX**2 + GradientY**2)
        GradientX[GradientX==0] = 1*10**(-5)
        
        YX = np.divide(GradientY,GradientX)
        if angle == 180:
            A = (np.arctan(YX + np.pi/2.0)*180)/np.pi
        if angle == 360:
            A = (np.arctan2(GradientY,GradientX)+np.pi)*180/np.pi
        
        # Function call
        bh, bv = binMatrix(A,E,Gr,angle,bin)
        
    else:
        bh = np.zeros((G.shape[0],G.shape[1]))
        bv = np.zeros((G.shape[0],G.shape[1]))
        
    bh_roi = bh[roi[0]:roi[1], roi[2]:roi[3]]
    bv_roi = bv[roi[0]:roi[1], roi[2]:roi[3]]
    
    # Function call to actually extract the HoG vector.
    p = phogDescriptor(bh_roi,bv_roi,L,bin)
    
    return p
Пример #4
0
 def test_tv_denoise_2d(self):
     """
     Apply the TV denoising algorithm on the lena image provided
     by scipy
     """
     # lena image
     lena = color.rgb2gray(data.lena())
     # add noise to lena
     lena += 0.5 * lena.std()*np.random.randn(*lena.shape)
     # denoise
     denoised_lena = filter.tv_denoise(lena, weight=60.0)
     # which dtype?
     assert denoised_lena.dtype in [np.float, np.float32, np.float64]
     from scipy import ndimage
     grad = ndimage.morphological_gradient(lena, size=((3,3)))
     grad_denoised = ndimage.morphological_gradient(denoised_lena, size=((3,3)))
     # test if the total variation has decreased
     assert np.sqrt((grad_denoised**2).sum()) < np.sqrt((grad**2).sum()) / 2
     denoised_lena_int = filter.tv_denoise(lena.astype(np.int32), \
             weight=60.0, keep_type=True)
     assert denoised_lena_int.dtype is np.dtype('int32')
Пример #5
0
norm of the gradient of the image, and minimizing the total variation
typically produces "posterized" images with flat domains separated by
sharp edges.

It is possible to change the degree of posterization by controlling
the tradeoff between denoising and faithfulness to the original image.

"""

import numpy as np
import matplotlib.pyplot as plt

from scikits.image import data, color, img_as_ubyte
from scikits.image.filter import tv_denoise

l = img_as_ubyte(color.rgb2gray(data.lena()))
l = l[230:290, 220:320]

noisy = l + 0.4 * l.std() * np.random.random(l.shape)

tv_denoised = tv_denoise(noisy, weight=10)

plt.figure(figsize=(12,2.8))

plt.subplot(131)
plt.imshow(noisy, cmap=plt.cm.gray, vmin=40, vmax=220)
plt.axis('off')
plt.title('noisy', fontsize=20)
plt.subplot(132)
plt.imshow(tv_denoised, cmap=plt.cm.gray, vmin=40, vmax=220)
plt.axis('off')
Пример #6
0
    # Select a random number K of features, then sample K features from the annotations for this image.
    # Form the smallest bounding box that encompasses those features (slightly enlarged) and that will be the seed patch.
    num_features_to_use = random.choice(range(2, 11))
    rand_sample_vector = np.random.permutation(
        np.hstack(
            (np.ones(num_features_to_use),
             np.zeros(len(key_vals) - num_features_to_use)))).astype(np.int32)

    keypoints_to_use = [
        elem for i, elem in enumerate(key_vals) if rand_sample_vector[i] == 1
    ]
    seed_patch = obtain_bounding_patch(keypoints_to_use, M, N)

    if len(random_image.shape) == 3:
        seed_patch_im = clr.rgb2gray(random_image)
    else:
        seed_patch_im = np.copy(random_image)

    seed_patch_im = 255 * seed_patch_im[seed_patch[0]:seed_patch[1],
                                        seed_patch[2]:seed_patch[3]]
    im_out = open(
        "/home/ely/projects/faces/data/poselets/seed_patches/seed_" +
        str(num_poselets_trained) + ".png", 'w')
    im_writer = png.Writer(seed_patch_im.shape[1],
                           seed_patch_im.shape[0],
                           greyscale=True)
    im_writer.write(im_out, np.asarray(seed_patch_im))
    im_out.close()

    cur_poselet_dir = "/home/ely/projects/faces/data/poselets/close_training_patches/poselet_" + str(
Пример #7
0
 M = random_image.shape[0]; N = random_image.shape[1];
 
 annotations = retrieve_annotations(random_file, data_entries, url_list, training_directory)
 key_vals = [(float(annotations[elem]),float(annotations[elem+1]), int(annotations[elem+2])) for elem in range(2,len(annotations),3)]
 
 
 # Select a random number K of features, then sample K features from the annotations for this image.
 # Form the smallest bounding box that encompasses those features (slightly enlarged) and that will be the seed patch.
 num_features_to_use = random.choice(range(2,11))
 rand_sample_vector = np.random.permutation(np.hstack( (np.ones(num_features_to_use), np.zeros(len(key_vals) - num_features_to_use)))).astype(np.int32)
     
 keypoints_to_use = [elem for i,elem in enumerate(key_vals) if rand_sample_vector[i] == 1]
 seed_patch = obtain_bounding_patch(keypoints_to_use,M,N)
 
 if len(random_image.shape) == 3:
     seed_patch_im = clr.rgb2gray(random_image)
 else:
     seed_patch_im = np.copy(random_image)
 
 seed_patch_im = 255 * seed_patch_im[seed_patch[0]:seed_patch[1],seed_patch[2]:seed_patch[3]]
 im_out = open("/home/ely/projects/faces/data/poselets/seed_patches/seed_"+str(num_poselets_trained)+".png",'w')
 im_writer = png.Writer(seed_patch_im.shape[1], seed_patch_im.shape[0], greyscale=True)
 im_writer.write(im_out, np.asarray(seed_patch_im))
 im_out.close()
 
 cur_poselet_dir = "/home/ely/projects/faces/data/poselets/close_training_patches/poselet_"+str(num_poselets_trained)+"/"
 cmd = "mkdir "+cur_poselet_dir
 os.system(cmd)
 
 
 
Пример #8
0
    
    # Loop over all of the images.
    num_file_success = 0
    num_images_to_test = 10
    while num_file_success < num_images_to_test:
        found_file = 0
        while not(found_file):
            test_indx = random.choice(range(len(url_list)))
            cur_file_name = get_local_file_name_from_url(test_indx,url_list,training_directory)
            if os.path.isfile(cur_file_name):
                found_file = 1
        
        
        original_image = imread(cur_file_name)
        if len(original_image.shape) == 3:
            original_image = clr.rgb2gray(original_image)
        
        height, width = original_image.shape
        
        binary_output = np.zeros((height,width))
        score_output = np.zeros((height,width))
       
        angles = original_image.ravel().astype(np.float32)
        hog_output = np.zeros((1,81)).ravel().astype(np.float32)

        angles_device = cu.mem_alloc(angles.nbytes)
        cu.memcpy_htod( angles_device,  angles )

        output_hog_device = cu.mem_alloc(hog_output.nbytes)
        cu.memcpy_htod( output_hog_device, hog_output)
        
Пример #9
0
img_prefix = "/home/ely/projects/faces/src/demo_"
img_names = ["medium.jpg", "large.jpg", "big.jpg", "huge.jpg", "mega.jpg"]
img_sizes = [64, 128, 256, 512, 1024]

# Set HoG parameters, number of bins, maximum angle, and number of pyramid levels
bins = 9; angle = 180; L = 3; num_spatial_bins = 3;
phog_times = []; vhog_times = []; triggs_times = [];
gpu_keypoint_times = []; gpu_patch_times = [];
num_plots = 5

# Get timing info for each of the patch-based CPU versions
print "Timing the CPU versions ..."
for indx in range(len(img_sizes)):
    cur_image = imread(img_prefix+img_names[indx])
    if len(cur_image.shape) == 3:
        cur_image = clr.rgb2gray(cur_image)
    height, width = cur_image.shape
    
    st_time = time.time()
    p = phog.phog(img_prefix+img_names[indx], bins, angle, L, [0,height,0,width])
    ed_time = time.time() - st_time
    phog_times.append(ed_time)
    
    st_time = time.time()
    v = vhog.hog(cur_image, bins, num_spatial_bins, angle, [0,height,0,width])
    ed_time = time.time() - st_time
    vhog_times.append(ed_time)
    
    
# Get timing info for the Dalal and Triggs CPU C-code version.
# For this, only use the huge demo image and increase the size of the
Пример #10
0
angle = 180
L = 3
num_spatial_bins = 3
phog_times = []
vhog_times = []
triggs_times = []
gpu_keypoint_times = []
gpu_patch_times = []
num_plots = 5

# Get timing info for each of the patch-based CPU versions
print "Timing the CPU versions ..."
for indx in range(len(img_sizes)):
    cur_image = imread(img_prefix + img_names[indx])
    if len(cur_image.shape) == 3:
        cur_image = clr.rgb2gray(cur_image)
    height, width = cur_image.shape

    st_time = time.time()
    p = phog.phog(img_prefix + img_names[indx], bins, angle, L,
                  [0, height, 0, width])
    ed_time = time.time() - st_time
    phog_times.append(ed_time)

    st_time = time.time()
    v = vhog.hog(cur_image, bins, num_spatial_bins, angle,
                 [0, height, 0, width])
    ed_time = time.time() - st_time
    vhog_times.append(ed_time)

# Get timing info for the Dalal and Triggs CPU C-code version.
Пример #11
0
    # Loop over all of the images.
    num_file_success = 0
    num_images_to_test = 10
    while num_file_success < num_images_to_test:
        found_file = 0
        while not (found_file):
            test_indx = random.choice(range(len(url_list)))
            cur_file_name = get_local_file_name_from_url(
                test_indx, url_list, training_directory)
            if os.path.isfile(cur_file_name):
                found_file = 1

        original_image = imread(cur_file_name)
        if len(original_image.shape) == 3:
            original_image = clr.rgb2gray(original_image)

        height, width = original_image.shape

        binary_output = np.zeros((height, width))
        score_output = np.zeros((height, width))

        angles = original_image.ravel().astype(np.float32)
        hog_output = np.zeros((1, 81)).ravel().astype(np.float32)

        angles_device = cu.mem_alloc(angles.nbytes)
        cu.memcpy_htod(angles_device, angles)

        output_hog_device = cu.mem_alloc(hog_output.nbytes)
        cu.memcpy_htod(output_hog_device, hog_output)