def get_gabor_filter_features(im, ind, min_freq, max_freq):
    im_shape = im.shape
    # print 'The length of the shape is %s' % len(im_shape)
    num_dim_image = len(im_shape)
    if num_dim_image == 3:
        im_slice = im[:, :, ind]
    else:
        im_slice = im
    real_gab = gabor_filter(im_slice, min_freq)[0].flatten()
    # print real_gab.shape
    img_gab = gabor_filter(im_slice, min_freq)[1].flatten()
    # print img_gab.shape
    curr_flat_features = np.hstack((real_gab, img_gab))
    curr_freq = min_freq

    num_steps = int(math.log10(max_freq))
    for i in range(num_steps):
        curr_freq *= 10
        real_gab = gabor_filter(im_slice, curr_freq)[0].flatten()
        img_gab = gabor_filter(im_slice, curr_freq)[1].flatten()
        next_flat_features = np.hstack((real_gab, img_gab))
        curr_flat_features = np.hstack(
            (curr_flat_features, next_flat_features))
        # curr_flat_features = np.hstack((curr_flat_features, real_gab))
        if curr_freq > max_freq:
            break
    return curr_flat_features
 def getGaborFeatures(self, reshape):
     if reshape:
         self._feature = self._feature.resize((40,40),Image.ANTIALIAS)
         self._feature = gabor_filter(self._feature, frequency=0.8)[0]
         self._feature = numpy.asarray(self._feature)
         #self._feature = self._normalize(self._feature)
         self._feature = numpy.reshape(self._feature, (1,1600))
         return self._feature
     else:
         self._feature = self._feature.resize((40,40),Image.ANTIALIAS)
         self._feature = gabor_filter(self._feature, frequency=0.8)[0]
         self._feature = numpy.asarray(self._feature)
         #self._feature = self._normalize(self._feature)
         return self._feature       
def gabor(image, freqs=[0.4], theta=[0, 30, 60, 90, 120, 150]):
    #feats = []
    #for f, t in itertools.product(0.1, 30):
    #    feats.append(gabor_filter(image, f, theta=t))
    #return feats
    f = gabor_filter(image, 0.1, 120)
    return f[0] + np.sqrt(f[1] ** 2)
示例#4
0
def doGabor(im):
	#thetas = [0,np.pi/4, np.pi/2, np.pi/8, np.pi, np.pi+(np.pi/8),np.pi+(np.pi/4),np.pi+(np.pi/2)]
	#thetas = [0, np.pi/2, np.pi, np.pi*2]
	#thetas = [0,0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,-0.1,-0.2,-0.3,-0.4,-0.5,-0.6,-0.7,-0.8,-0.9]
	thetas = [0.0,np.pi/2]

	summedVals = np.zeros(im.shape)

	for i in range(len(thetas)):
		filt_real, filt_img = filters.gabor_filter(im, frequency=1.9, theta = thetas[i],bandwidth=0.5, n_stds=3)
		summedVals += filt_img
		print i

	#summedVals = equalize_hist(summedVals)
	summedVals = np.absolute(summedVals)
	#summedVals *= im
	maxVal = np.amax(summedVals)
	minVal = np.amin(summedVals)
	mapped = np.interp(summedVals, [minVal, maxVal],[0,255])
	out = np.uint8(mapped)
	bb = Image.fromarray(out)

	timestr = time.strftime("%Y%m%d-%H%M%S")
	bb.save("output/gabor"+timestr+".png")
	bb.show()
示例#5
0
def doGabor(imgPath, outImagePath):
    image = io.imread(imgPath)
    #grayImg = rgb2gray(image)
    im = image[:, :, 0]
    im = np.interp(im, [0, 255], [0, 1])

    #thetas = [0,np.pi/4, np.pi/2, np.pi/8, np.pi, np.pi+(np.pi/8),np.pi+(np.pi/4),np.pi+(np.pi/2)]
    #thetas = [0, np.pi/2, np.pi, np.pi*2]
    #thetas = [0,0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,-0.1,-0.2,-0.3,-0.4,-0.5,-0.6,-0.7,-0.8,-0.9]
    thetas = [0.0, np.pi / 2]

    summedVals = np.zeros(im.shape)

    for i in range(len(thetas)):
        filt_real, filt_img = filters.gabor_filter(im,
                                                   frequency=1.9,
                                                   theta=thetas[i],
                                                   bandwidth=0.5,
                                                   n_stds=3)
        summedVals += filt_img
        print(i)

    #summedVals = equalize_hist(summedVals)
    summedVals = np.absolute(summedVals)
    #summedVals *= im
    maxVal = np.amax(summedVals)
    minVal = np.amin(summedVals)
    mapped = np.interp(summedVals, [minVal, maxVal], [0, 255])
    out = np.uint8(mapped)
    bb = Image.fromarray(out)

    timestr = time.strftime("%Y%m%d-%H%M%S")
    bb.save(outImagePath)
def gabor(image, freqs=[0.1, 0.4], thetas=[0, 30, 60, 90, 120, 150]):
    params = list(itertools.product(freqs, thetas))
    features = []
    for freq, theta in params:
        g = gabor_filter(image, freq, theta)
        features.append(g[0] + np.real(g[1]))
    features = np.asarray(features).swapaxes(0, 2)
    return (len(params), features)
示例#7
0
 def apply_gabor_kernels(image, kernels):
     number_of_kernels = len(kernels)
     third_dimension = 1 if len(image.shape) != 3 else image.shape[2]
     features = np.zeros((image.shape[0], image.shape[1], number_of_kernels * third_dimension))
     for i, kernel in enumerate(kernels):
         for j in range(third_dimension):
             feature_index = i*third_dimension + j
             image_dim = image if third_dimension == 1 else image[:, :, j]
             features[:, :, feature_index] = gabor_filter(image_dim, kernel[0], kernel[1])[0]
             features[:, :, feature_index] = fh.normalize(features[:, :, feature_index])
     return features
示例#8
0
文件: svm.py 项目: mdimarco/DeepCraft
def apply_filter(path, frequency, theta):
	global filtercounter
	filtercounter+=1
	print(frequency,theta,filtercounter)


	img = misc.imread(path)
	# HACK - MAJOR HACK - this forces all images to be the same size
	img = Image.fromarray(numpy.uint8(img)).convert('L')
	img = img.resize((10, 10), Image.ANTIALIAS)

	img = filters.gabor_filter(img, frequency=frequency, theta=theta)
	img = numpy.asarray(img)
	# Flatten the image
	return img.reshape(img.size)
示例#9
0
def apply_filter(path, frequency, theta):

	img = misc.imread(path)
	# HACK - MAJOR HACK - this forces all images to be the same size
	img = Image.fromarray(numpy.uint8(img)).convert('L')
	img_resized = img.resize((256, 256), Image.ANTIALIAS)

	#plt.figure()
	#plt.title('Raw Image')
	#io.imshow(numpy.asarray(img) )

	img_filtered = filters.gabor_filter(img_resized, frequency=frequency, theta=theta)
	#img = numpy.asarray(img)
	# Flatten the image
	return img_filtered
示例#10
0
def process_image(path_to_image, show_image=False):
    print(path_to_image)
    #number_of_thetas = 8 #
    #frequencies = (0.1,0.2,0.3,0.4,0.5)

    #print(path_to_image)
    img = io.imread(path_to_image, as_grey=True)
    img = transform.resize(img, (64, 64))

    if show_image:
        plt.figure()
        io.imshow(img)
        plt.figure()

    len_of_figure = len(frequencies)
    images = []
    for i, freq in enumerate(frequencies):
        size_images = []
        for j in range(number_of_thetas):
            real, imag = filters.gabor_filter(img, freq,
                                              np.pi / (number_of_thetas) * j)
            image = np.sqrt(imag**2 + real**2)
            shape = image.shape
            image = preprocessing.scale(image.flatten())  #zscore
            image = image.reshape(shape)
            image = transform.resize(image, (8, 8))
            if show_image:
                ax = plt.subplot(len_of_figure, number_of_thetas,
                                 i * number_of_thetas + j + 1)
                ax.axis('off')
                plt.imshow(image)
            size_images.append(image)
        combination = np.array(size_images).flatten()
        #print(i)
        images.append(combination.flatten())
        #print(combination.shape)
    if show_image:
        plt.show()

    return images
def process_image(path_to_image, show_image = False):
    print(path_to_image)
    #number_of_thetas = 8 #
    #frequencies = (0.1,0.2,0.3,0.4,0.5)

    #print(path_to_image)
    img = io.imread(path_to_image,as_grey=True)
    img = transform.resize(img,(64,64))

    if show_image:
        plt.figure()
        io.imshow(img)
        plt.figure()

    len_of_figure = len(frequencies)
    images = []
    for i,freq in enumerate(frequencies):
        size_images = []
        for j in range(number_of_thetas):
            real, imag = filters.gabor_filter(img,freq,np.pi/(number_of_thetas)*j)
            image = np.sqrt(imag**2+real**2)
            shape = image.shape
            image = preprocessing.scale(image.flatten()) #zscore
            image = image.reshape(shape)
            image = transform.resize(image,(8,8))
            if show_image:
                ax = plt.subplot(len_of_figure,number_of_thetas,i*number_of_thetas + j+1)
                ax.axis('off')
                plt.imshow(image)
            size_images.append(image)
        combination = np.array(size_images).flatten()
        #print(i)
        images.append(combination.flatten())
        #print(combination.shape)
    if show_image:
        plt.show()

    return images
示例#12
0
results = []

freq = input("����gabor��frequency:")
theta = input("����gabor��theta(float:�Ƕ�):")
bandwidth = input("����gabor��bandwidth(float:Ĭ��1):")
sigma_x = input("����gabor��sigma_x(float:Ĭ��None):")
sigma_y = input("����gabor��sigma_y(float:Ĭ��None):")
n_stds = input("����gabor��n_stds(scalar:Ĭ��3):")
offset = input("����gabor��offset(float:Ĭ��0):")
mode = raw_input("����gabor��mode(��constant��, ��nearest��, ��reflect��, ��mirror��, ��wrap��:Ĭ��'reflect'):")
cval = input("����gabor��cval(scalar:Ĭ��0)")
is_test = raw_input("�Ƿ����Ե���ͼƬ(Y/N):")
# Plot a selection of the filter bank kernels and their responses.

results = []
if not os.path.exists(os.path.join(dir_path,flair_res_path)):
    os.mkdir(os.path.join(dir_path,flair_res_path))
if cmp(is_test,'Y') == 0:
    tstimg = img_as_float(io.imread(os.path.join(dir_path,flair_path,testfile)))
    reresult, result = gabor_filter(tstimg,frequency=freq,theta=theta/180.0*np.pi)
    res_fname = "%s_test_%s" %(testfile[:testfile.rfind('.')],testfile[testfile.rfind('.'):])
    io.imsave(os.path.join(dir_path,flair_res_path,res_fname),rescale_intensity(reresult,out_range=(-1.,1.)))
else:
    for str_root, lst_dir, lst_file in os.walk(os.path.join(dir_path,flair_path)):
        for img_name in lst_file:
            img = img_as_float(io.imread(os.path.join(dir_path,flair_path,img_name)))
            #gabor �˲���
            reresult, result = gabor_filter(img,frequency=freq,theta=theta/180.0*np.pi)
            res_fname = "%s%s" %(img_name[:img_name.rfind('.')],img_name[img_name.rfind('.'):])
            io.imsave(os.path.join(dir_path,flair_res_path,res_fname),rescale_intensity(reresult,out_range=(-1.,1.)))
def gabor_feature(image):
    filt_real, filt_imag = filters.gabor_filter(image, frequency=0.6)
    return filt_real
示例#14
0
offset = input("����gabor��offset(float:Ĭ��0):")
mode = raw_input(
    "����gabor��mode(��constant��, ��nearest��, ��reflect��, ��mirror��, ��wrap��:Ĭ��'reflect'):"
)
cval = input("����gabor��cval(scalar:Ĭ��0)")
is_test = raw_input("�Ƿ����Ե���ͼƬ(Y/N):")
# Plot a selection of the filter bank kernels and their responses.

results = []
if not os.path.exists(os.path.join(dir_path, flair_res_path)):
    os.mkdir(os.path.join(dir_path, flair_res_path))
if cmp(is_test, 'Y') == 0:
    tstimg = img_as_float(
        io.imread(os.path.join(dir_path, flair_path, testfile)))
    reresult, result = gabor_filter(tstimg,
                                    frequency=freq,
                                    theta=theta / 180.0 * np.pi)
    res_fname = "%s_test_%s" % (testfile[:testfile.rfind('.')],
                                testfile[testfile.rfind('.'):])
    io.imsave(os.path.join(dir_path, flair_res_path, res_fname),
              rescale_intensity(reresult, out_range=(-1., 1.)))
else:
    for str_root, lst_dir, lst_file in os.walk(
            os.path.join(dir_path, flair_path)):
        for img_name in lst_file:
            img = img_as_float(
                io.imread(os.path.join(dir_path, flair_path, img_name)))
            #gabor �˲���
            reresult, result = gabor_filter(img,
                                            frequency=freq,
                                            theta=theta / 180.0 * np.pi)
示例#15
0
img = data.camera()
edges1 = feature.canny(img)  # sigma=1
edges2 = feature.canny(img, sigma=3)  # sigma=3

plt.figure('canny', figsize=(8, 8))
plt.subplot(121)
plt.imshow(edges1, plt.cm.gray)

plt.subplot(122)
plt.imshow(edges2, plt.cm.gray)

plt.show()

# gabor
img = data.camera()
filt_real, filt_imag = filters.gabor_filter(img, frequency=0.6)

plt.figure('gabor', figsize=(8, 8))

plt.subplot(121)
plt.title('filt_real')
plt.imshow(filt_real, plt.cm.gray)

plt.subplot(122)
plt.title('filt-imag')
plt.imshow(filt_imag, plt.cm.gray)

plt.show()
# 水平垂直
img = data.camera()
edges1 = filters.sobel_h(img)