Exemplo n.º 1
0
def cof():
    im1 = np.array(Image.open(inpfilename).convert('L'))
    im2 = np.array(Image.open(inpfilename).convert('L'))
    wid = 5
    harrisim = harris.compute_harris_response(im1,5)
    filtered_coords1 = harris.get_harris_points(harrisim, wid+1)
    d1 = harris.get_descriptors(im1, filtered_coords1,wid)
    
    harrisim = harris.compute_harris_response(im2,5)
    filtered_coords2 = harris.get_harris_points(harrisim, wid+1)
    d2 = harris.get_descriptors(im2, filtered_coords2,wid)
    
    print 'starting matching'
    matches = harris.match_twosided(d1,d2)
    print "matches", matches
    
    figure()
    gray()
    harris.plot_matches(im1, im2, filtered_coords1, filtered_coords2, matches)
    print "show the image"
    show()
Exemplo n.º 2
0
# -*- coding: utf-8 -*-
"""
Created on Mon Aug 15 11:19:46 2016

@author: user
"""
from PIL import Image
from pylab import *
import harris

im = array(Image.open('../ch1/baby_16.jpg').convert('L'))
harrisim = harris.compute_harris_response(im)
filtered_coords = harris.get_harris_points(harrisim, 6)
harris.plot_harris_points(im, filtered_coords)
imshow(im)
wid = 5
im1 = array(Image.open('../ch1/baby_1.jpg').convert('L'))
harrisim = harris.compute_harris_response(im1, 5)
filtered_coords1 = harris.get_harris_points(harrisim, wid + 1)
d1 = harris.get_descriptors(im1, filtered_coords1, wid)
im2 = array(Image.open('../ch1/baby_16.jpg').convert('L'))
harrisim = harris.compute_harris_response(im2, 5)
filtered_coords2 = harris.get_harris_points(harrisim, wid + 1)
d2 = harris.get_descriptors(im2, filtered_coords2, wid)
print 'starting matching'
matches = harris.match_twosided(d1, d2)
figure()
gray()
harris.plot_matches(im1, im2, filtered_coords1, filtered_coords2, matches)
show()
This is the Harris point matching example in Figure 2-2.
"""

# Figure 2-2上面的图
im1 = array(Image.open("../data/crans_1_small.jpg").convert("L"))
im2 = array(Image.open("../data/crans_2_small.jpg").convert("L"))

# Figure 2-2下面的图
# im1 = array(Image.open("../data/sf_view1.jpg").convert("L"))
# im2 = array(Image.open("../data/sf_view2.jpg").convert("L"))

# resize to make matching faster
im1 = imresize(im1, (im1.shape[1] // 2, im1.shape[0] // 2))
im2 = imresize(im2, (im2.shape[1] // 2, im2.shape[0] // 2))

wid = 5
harrisim1 = harris.compute_harris_response(im1, 5)
filtered_coords1 = harris.get_harris_points(harrisim1, wid + 1)
d1 = harris.get_descriptors(im1, filtered_coords1, wid)

harrisim2 = harris.compute_harris_response(im2, 5)
filtered_coords2 = harris.get_harris_points(harrisim2, wid + 1)
d2 = harris.get_descriptors(im2, filtered_coords2, wid)

print('starting matching')
matches = harris.match_twosided(d1, d2)

figure()
gray()
harris.plot_matches(im1, im2, filtered_coords1, filtered_coords2, matches)
show()
__author__ = 'vahan'

from scipy import *
from scipy import signal
import Image
import filtertools
import harris

im = array(
    Image.open(
        '/home/vahan/PycharmProjects/visionForLegoGame/images/initialImage.jpg'
    ).convert("L"))
harrisim = harris.compute_harris_response(im)
filtered_coords = harris.get_harris_points(harrisim, 6)
harris.plot_harris_points(im, filtered_coords)
Exemplo n.º 5
0
import harris
import time

im1 = array(Image.open("wave1.jpg").convert("L"))
im2 = array(Image.open("wave2.jpg").convert("L"))

wid = 5

t = time.time()
harrisim = harris.compute_harris_response(im1, 5)

print "t for harrisim=", time.time() - t
t = time.time()

filtered_coords1 = harris.get_harris_points(harrisim, wid + 1)

print "t for get_harris_points=", time.time() - t
t = time.time()

d1 = harris.get_descriptors(im1, filtered_coords1, wid)

print "t for get_descriptors=", time.time() - t
t = time.time()

harrisim = harris.compute_harris_response(im2, 5)
filtered_coords2 = harris.get_harris_points(harrisim, wid + 1)
d2 = harris.get_descriptors(im2, filtered_coords2, wid)

print "starting matching"
matches = harris.match_twosided(d1, d2)
Exemplo n.º 6
0
# -*- coding: utf-8 -*-

import os
from PIL import Image
from numpy import *
from pylab import *
import harris

im1 = array(Image.open(os.path.abspath("data/cuaderno_1.jpg")).convert('L'))
im2 = array(Image.open(os.path.abspath("data/cuaderno_2.jpg")).convert('L'))

wid = 50
harrisim = harris.compute_harris_response(im1, 5)
filtered_coords1 = harris.get_harris_points(harrisim, wid + 1, 0.2)
d1 = harris.get_descriptors(im1, filtered_coords1, wid)

harrisim = harris.compute_harris_response(im2, 5)
filtered_coords2 = harris.get_harris_points(harrisim, wid + 1, 0.2)
d2 = harris.get_descriptors(im2, filtered_coords2, wid)

print('starting matching')
matches = harris.match_twosided(d1, d2)

figure()
gray()
harris.plot_matches(im1, im2, filtered_coords1, filtered_coords2, matches)
show()
Exemplo n.º 7
0
from PIL import Image
from pylab import *
import harris
import imtools

im = array(Image.open('board.jpeg').convert('L'))
#im, _ = imtools.histeq(im)
harrisim = harris.compute_harris_response(im)
figure()
gray()
imshow(harrisim)

filtered_coords = harris.get_harris_points(harrisim, threshold=0.1)
harris.plot_harris_points(im, filtered_coords)
                            queue, global_size, local_size,
                            gpu_image_Wxx_final, 
                            gpu_image_Wyy_final, 
                            gpu_image_Wxy_final, 
                            gpu_image_filter_out,
                            width, height, 
                            zero_kernel

                            )



        #Output the final Harris Matrix to the CPU
        cl.enqueue_copy(queue, Harris_Matrix, gpu_image_filter_out, is_blocking=False)
        points = get_harris_points(Harris_Matrix)
        end = time.time()

        #Store the time to run the entire openCL version
        output_times_openCL[i] = end - start

        #Store the time to run the entire serial version
        with Timer() as serial_time:
            harris = run_harris(host_image)
        output_times_serial[i] = serial_time.interval

    
    ######################################################################
    # test comparision for accuracy vs. harris.py Serial implementation by 
    # "Programming Computer Vision with Python"  by Jan Erik Solem
    ######################################################################
Exemplo n.º 9
0
def harris_features(im):
    response = cv2.cornerHarris(im, 7, 5, 0.05)
    points = harris.get_harris_points(response)
    desc = harris.get_descriptors(im, points)
    return points, desc
from numpy import array
from PIL import Image
import harris

# Try running the following commands on an example image:

im = Image.open('sudoku_image_02.png').convert("L")
#im = im.rotate(5)#, expand=True)
im = array(im)
harrisim = harris.compute_harris_response(im)
filtered_coords = harris.get_harris_points(harrisim, 6)
harris.save_harris_points(im, filtered_coords)
Exemplo n.º 11
0
def coner():
    im = np.array(Image.open(inpfilename).convert('L'))
    harrisim = harris.compute_harris_response(im)
    filtered_coords = harris.get_harris_points(harrisim,6)
    harris.plot_harris_points(im, filtered_coords)
Exemplo n.º 12
0
from pylab import *
from numpy import *
from PIL import Image

import harris

"""
Example of detecting Harris corner points (Figure 2-1 in the book).
"""

# open image
im = array(Image.open('2.png').convert('L'))

# detect corners and plot
harrisim = harris.compute_harris_response(im)
filtered_coords = harris.get_harris_points(harrisim, 10, threshold=0.01)
harris.plot_harris_points(im, filtered_coords)

# plot only 200 strongest
harris.plot_harris_points(im, filtered_coords[:200])
Exemplo n.º 13
0
from PIL import Image
import harris
from numpy import array
from pylab import *
import sift
import harris

imname = '3.jpg'
im = array(Image.open(imname).convert('L'))
sift.process_image(imname, '3.sift')
locs, desc = sift.read_features_from_file('3.sift')
harrisim = harris.compute_harris_response(im)
harris_coords = harris.get_harris_points(harrisim)
figure()
gray()
subplot(1, 3, 1)
imshow(im)
axis('off')
subplot(1, 3, 2)
sift.plot_feature(im, locs)
subplot(1, 3, 3)
harris.plot_harris_points(im, harris_coords)
show()




def harris_get_corners(num_runs = 100, image_file = '1.tif'):

	'''
	This funciton is the final and most efficent implementation of our parallel harris corner detection implemenatation in openCL
	
	INPUTS:
	image_file1: <TYPE STRING> the image to be analyzed
	num_runs: <TYPE INT> number of iterations to have the algorithm run, for profiling against serial version

	OUTPUT:
	1) two images saved to your directory with points labled, one image is from serial version and one from parallel version
	2) the harris points from the serial and parallel implementations
	3) average of times from serial and parallel implemenatations
	'''
	output_times_openCL = np.zeros(num_runs)
	output_times_serial = np.zeros(num_runs)


    #Initalize loop to get average of times
	for i in range(num_runs):
        # List our platforms
		platforms = cl.get_platforms()

		#Define the number of runs to get average of run times



		# Create a context with all the devices
		devices = platforms[0].get_devices()
		context = cl.Context(devices)

		# Create a queue for transferring data and launching computations.
		# Turn on profiling to allow us to check event times.
		queue = cl.CommandQueue(context, context.devices[0],
		                        properties=cl.command_queue_properties.PROFILING_ENABLE)
		program = cl.Program(context, open('harris_corner_detection_final.cl').read()).build(options='')


		#Load in image to be analyzed
		host_image = np.array(Image.open(str(image_file)).convert('L')).astype(np.float32)[::1, ::1].copy()

		#start time after image load for consistancy
		start = time.time()

		sigma = 1 #Define the standard deviation for the gauussian
		#Generate the 1D first dimensional gaussian kernel
		filter_kernel_derivative = np.asarray(generate_weights(sigma), order = 1).astype(np.float32)
		#Generate the 1D zero derivative gaussian kernel
		filter_kernel_zero = np.asarray(generate_weights(sigma, order = 0)).astype(np.float32)
		#Determine the length of the entire weight vector based on the sigma of the gaussian
		weight_length = len(filter_kernel_derivative) #should be 9 with sigma = 1
		#This is the number of neighbors for each analyzed pixel, should be even number
		window = (weight_length - 1) #window is 8
		#the halo is the number of nieghbors on each side of the analyzed pixelsd
		halo = np.int32(window / 2.)



		host_image_filtered = np.zeros_like(host_image)
		gpu_image_in = cl.Buffer(context, cl.mem_flags.READ_WRITE, host_image.size * 4)
		zero_derivative_out = cl.Buffer(context, cl.mem_flags.READ_WRITE, host_image.size * 4)
		first_derivative_out = cl.Buffer(context, cl.mem_flags.READ_WRITE, host_image.size * 4)
		derivative_kernel_x = cl.Buffer(context, cl.mem_flags.READ_WRITE, filter_kernel_derivative.size * 4)
		zero_kernel = cl.Buffer(context, cl.mem_flags.READ_WRITE, filter_kernel_zero.size * 4)

		Harris_Matrix = np.zeros_like(host_image)

		# Intermediate storage area, between Derivative of Gaussian and Gaussian Filter

		local_size = (int(halo), int(halo)) # 2D local_size
		global_size = tuple([round_up(g, l) for g, l in zip(host_image.shape[::-1], local_size)]) # shape

		width = np.int32(host_image.shape[1])
		height = np.int32(host_image.shape[0])

		local_memory = cl.LocalMemory(4 * ((local_size[0] + (halo * 2)) * (local_size[1] + 1)))
		local_buffer_zero_1 = cl.LocalMemory(4 * (np.shape(filter_kernel_zero)[0] + 1))   
		local_buffer_first_1 = cl.LocalMemory(4 * (np.shape(filter_kernel_derivative)[0] + 1))      

		buf_width = np.int32(local_size[0] + window)
		buf_height = np.int32(local_size[1] + window)

		cl.enqueue_copy(queue, gpu_image_in, host_image, is_blocking=False)
		cl.enqueue_copy(queue, derivative_kernel_x, filter_kernel_derivative, is_blocking=False)
		cl.enqueue_copy(queue, zero_kernel, filter_kernel_zero, is_blocking=False)

		########################################### First Kernel ##################################
		#                          This Kernel takes the first derivative of a guasisan           #
		#                  of the image in the y-direction (axis = 0) and zero Derivatives        #
		#                         of a gaussian in the y-direction (axis = 0)                     #
		########################################### First Kernel ##################################

		#Execute Derivative of Gaussian Function
		program.gaussian_first_axis(

		                    queue, global_size, local_size,
		                    gpu_image_in, 
		                    zero_derivative_out, 
		                    first_derivative_out, 
		                    local_memory, width, 
		                    height, buf_width, buf_height, halo, 
		                    derivative_kernel_x, zero_kernel,
		                    local_buffer_first_1, local_buffer_zero_1
		                    )


		########################################### Second Kernel ##################################
		#                          This Kernel takes the first derivative of a guasisan            #
		#                  of the image in the x-direction (axis = 1) and zero Derivatives         #
		#                         of a gaussian in the x-direction (axis = 1)                      #
		########################################### Second Kernel ##################################


		#allocate local memory buffers for the two filters used in the second kernel
		local_memory_axis2_1 = cl.LocalMemory(4 * ((local_size[0]) * (local_size[1] + (halo * 2) + 1)))
		local_memory_axis2_2 = cl.LocalMemory(4 * ((local_size[0]) * (local_size[1] + (halo * 2) + 1)))
		local_buffer_zero_2 = cl.LocalMemory(4 * (np.shape(filter_kernel_zero)[0] + 1))   
		local_buffer_first_2 = cl.LocalMemory(4 * (np.shape(filter_kernel_derivative)[0] + 1))   
		#allocate memory for the output of the second kernel 
		gpu_image_Wxx_derivative_out = cl.Buffer(context, cl.mem_flags.READ_WRITE, host_image.size * 4)
		gpu_image_Wyy_derivative_out = cl.Buffer(context, cl.mem_flags.READ_WRITE, host_image.size * 4)
		gpu_image_Wxy_derivative_out = cl.Buffer(context, cl.mem_flags.READ_WRITE, host_image.size * 4)

		#Execute Derivative of Gaussian Function
		program.gaussian_second_axis(

		                    queue, global_size, local_size, 
		                    zero_derivative_out, 
		                    first_derivative_out, 
		                    gpu_image_Wxx_derivative_out, 
		                    gpu_image_Wyy_derivative_out, 
		                    gpu_image_Wxy_derivative_out,
		                    local_memory_axis2_1, local_memory_axis2_2,
		                    width, 
		                    height, buf_width, buf_height, halo, 
		                    derivative_kernel_x, zero_kernel,
		                    local_buffer_first_2, local_buffer_zero_2

		                    )

		########################################### Third Kernel ###################################
		#                          This Kernel applies a gaussian to the product of the            #
		#                          parital derivatives in the y-direction (axis = 0)               #
		#                                                                                          #
		########################################### Third Kernel ###################################


		#load in the local memory buffer allocation for all the compents fo the harris matrix
		local_memory_filter_Wxx = cl.LocalMemory(4 * ((local_size[0] + (halo * 2)) * (local_size[1] + 1)))
		local_memory_filter_Wyy = cl.LocalMemory(4 * ((local_size[0] + (halo * 2)) * (local_size[1] + 1)))
		local_memory_filter_Wxy = cl.LocalMemory(4 * ((local_size[0] + (halo * 2)) * (local_size[1] + 1)))
		local_buffer_zero_3 = cl.LocalMemory(4 * (np.shape(filter_kernel_zero)[0] + 1))   


		#allocate memory for the output of the third kernel 
		gpu_image_Wxx_third_out = cl.Buffer(context, cl.mem_flags.READ_WRITE, host_image.size * 4)
		gpu_image_Wyy_third_out = cl.Buffer(context, cl.mem_flags.READ_WRITE, host_image.size * 4)
		gpu_image_Wxy_third_out = cl.Buffer(context, cl.mem_flags.READ_WRITE, host_image.size * 4)


		# Execute gaussian filter on all component matrices and calculate Harris Matrix
		program.filter_first_axis_second_pass(

		                    queue, global_size, local_size, 
		                    gpu_image_Wxx_derivative_out, 
		                    gpu_image_Wyy_derivative_out, 
		                    gpu_image_Wxy_derivative_out,
		                    gpu_image_Wxx_third_out, 
		                    gpu_image_Wyy_third_out, 
		                    gpu_image_Wxy_third_out, 
		                    local_memory_filter_Wxx, 
		                    local_memory_filter_Wyy, 
		                    local_memory_filter_Wxy,
		                    halo, width, height, buf_width, buf_height, 
		                    zero_kernel, local_buffer_zero_3

		                    )


		########################################### Fourth Kernel ##################################
		#                          This Kernel applies a gaussian to the product of the            #
		#                          parital derivatives in the x-direction (axis = 1)               #
		#                          and computes the final Harris Matrix for the output             #
		########################################### Fourth Kernel ##################################

		#Allocate local memory buffer for fourth kernel
		local_memory_filter_Wxx_2 = cl.LocalMemory(4 * ((local_size[0]) * (local_size[1] + (halo * 2)) + 1))
		local_memory_filter_Wyy_2 = cl.LocalMemory(4 * ((local_size[0]) * (local_size[1] + (halo * 2)) + 1))
		local_memory_filter_Wxy_2 = cl.LocalMemory(4 * ((local_size[0]) * (local_size[1] + (halo * 2)) + 1))
		local_buffer_zero_4 = cl.LocalMemory(4 * (np.shape(filter_kernel_zero)[0]))   

		# Allocate memory to store output from fourth kernel, this is the final Harris Matrix
		gpu_image_filter_out = cl.Buffer(context, cl.mem_flags.READ_WRITE, host_image.size * 4)

		program.filter_second_axis_second_pass(

		                    queue, global_size, local_size, 
		                    gpu_image_Wxx_third_out, 
		                    gpu_image_Wyy_third_out, 
		                    gpu_image_Wxy_third_out, 
		                    gpu_image_filter_out,
		                    local_memory_filter_Wxx_2, 
		                    local_memory_filter_Wyy_2, 
		                    local_memory_filter_Wxy_2,
		                    halo, width, height, buf_width, buf_height, 
		                    zero_kernel, local_buffer_zero_4

		                    )



		#Output the final Harris Matrix to the CPU
		cl.enqueue_copy(queue, Harris_Matrix, gpu_image_filter_out, is_blocking=False)
		points = get_harris_points(Harris_Matrix)
		end = time.time()

		#Store the time to run the entire openCL version
		output_times_openCL[i] = end - start

		#Store the time to run the entire serial version
		with Timer() as serial_time:
		    harris = run_harris(host_image)
		output_times_serial[i] = serial_time.interval



	########################################################################
	# test comparision for accuracy vs. harris.py Serial implementation by #
	# "Programming Computer Vision with Python"  by Jan Erik Solem         #
	########################################################################

	print '-------------Check Plots: Saved to the Directory--------------------------'
	plot_harris_points(host_image, points, im_name = 'Harris openCL Image')
	response = compute_harris_response(host_image, sigma=1)
	serial_points = get_harris_points(response, min_dist=10, threshold=0.1)
	plot_harris_points(host_image, serial_points, im_name = 'Harris Serial Image')
	print '--------------------------------------------------------------------------'

	print '-------------Check For Correctness----------------------------------------'
	pt_x = np.random.randint(np.shape(host_image)[0])
	pt_y = np.random.randint(np.shape(host_image)[1])
	print 'openCL Harris Matrix Random Point Check:', Harris_Matrix[pt_x, pt_y]
	print 'Serial Baseline Harris Matrix Random Point Check:', response[pt_x, pt_y]
	print 'Number of openCL points:', np.shape(points)
	print 'Number of Serial Points:', np.shape(serial_points)
	print 'Are the two lists of corner points the same?', (np.array(serial_points) == np.array(points)).all()
	print '--------------------------------------------------------------------------'



	print '-------------Check Timing Comparision-------------------------------------'
	mean_openCL = output_times_openCL.mean()
	mean_serial = output_times_serial.mean()
	print 'Time to run openCL', mean_openCL
	print 'Time to run Serial', mean_serial
	print '--------------------------------------------------------------------------'


	return Harris_Matrix, serial_points, points, mean_openCL, mean_serial
Exemplo n.º 15
0
axis('off')
show()

# CORNER DETECTION
sigma = 5
min_dist = 3
treshold = 0.05
harrisim = harris.compute_harris_response(im, sigma)

figure()
gray()
contour(harrisim, origin='image')
axis('equal')
axis('off')
show()
filtered_coords = harris.get_harris_points(harrisim, min_dist, treshold)
harris.plot_harris_points(im, filtered_coords)

# MATCHING
sigma = 5
min_dist = 8
treshold_harris = 0.5
width = 5

im1 = array(Image.open('image_1.png').convert('L'))
im2 = array(Image.open('image_2.png').convert('L'))

harrisim1 = harris.compute_harris_response(im1, sigma)
filtered_coords1 = harris.get_harris_points(harrisim1, min_dist,
                                            treshold_harris)
harris.plot_harris_points(im1, filtered_coords1)
Exemplo n.º 16
0
import numpy as np
import matplotlib.pyplot as plt
from PIL import Image

from harris import compute_harris_response, get_harris_points, \
    get_descriptors, match_two_sided, plot_matches, match

img1 = np.array(Image.open('images/house.jpg').convert('L'))
img2 = np.array(Image.open('images/house2.jpg').convert('L'))
wid = 100

harris_img = compute_harris_response(img1, 5)
filtered_coords1 = get_harris_points(harris_img, wid + 1)
d1 = get_descriptors(img1, filtered_coords1, wid)

harris_img = compute_harris_response(img2, 5)
filtered_coords2 = get_harris_points(harris_img, wid + 1)
d2 = get_descriptors(img2, filtered_coords2, wid)

print('[INFO] Starting Matching ')
matches = match(d1, d2)
print('[INFO] match complete')

plt.figure()
plt.gray()
plot_matches(img1, img2, filtered_coords1, filtered_coords2, matches)
plt.show()