In [52]:
#import
import matplotlib.pyplot as plt
from glob import glob
import numpy as np
import scipy.ndimage as ndimg
import skimage.filter as filter
import skimage.transform as transform
def layer_mean(img2d):
img1d = img2d[img2d<255]
return img1d.mean()
def hist_max(img2d):
img1d = img2d[img2d<255]
[n, bins, patches]= plt.hist(img1d, np.arange(255), fig)
return bins[n.argmax()]
def edge_length(img2d):
"gray input"
imgsize = float((img2d.shape[0]*img2d.shape[1]))
med_filter = ndimg.median_filter(img2d, size = (5,5))
edges = filter.canny(med_filter,3)
return edges.sum()/imgsize
def edge_sobel_h(img2d):
"gray input"
imgsize = float((img2d.shape[0]*img2d.shape[1]))
med_filter = ndimg.median_filter(img2d, size = (5,5))
edges_h = filter.hsobel(med_filter/255.)
return edges_h.sum()/imgsize
def edge_sobel_v(img2d):
"gray input"
imgsize = float((img2d.shape[0]*img2d.shape[1]))
med_filter = ndimg.median_filter(img2d, size = (5,5))
edges_v = filter.vsobel(med_filter/255.)
return edges_v.sum()/imgsize
def edge_sobel(img2d):
"gray input"
imgsize = float((img2d.shape[0]*img2d.shape[1]))
med_filter = ndimg.median_filter(img2d, size = (5,5))
edges = filter.sobel(med_filter/255.)
return edges.sum()/imgsize
def houghLine(img2d):
"gray input"
med_filter = ndimg.median_filter(img2d, size = (5,5))
edges = filter.sobel(med_filter/255.)
[H,theta,distances] = transform.hough_line(edges);
imgsize = float(len(theta)*len(distances))
return H.sum()/imgsize
def features(img2d, img2d_gray):
#red mean
red_mean = layer_mean(img2d[...,0])
green_mean = layer_mean(img2d[...,1])
blue_mean = layer_mean(img2d[...,2])
gray_mean = layer_mean(img2d_gray)
red_most = hist_max(img2d[...,0])
green_most = hist_max(img2d[...,1])
blue_most = hist_max(img2d[...,2])
gray_most = hist_max(img2d_gray)
length = edge_length(img2d_gray)
sobel_h = edge_sobel_h(img2d_gray)
sobel_v = edge_sobel_v(img2d_gray)
sobel = edge_sobel(img2d_gray)
hough = houghLine(img2d_gray)
return np.array([red_mean, green_mean, blue_mean, gray_mean,\
red_most,green_most,blue_most, gray_most,\
length, sobel_h, sobel_v, sobel, hough])
In [79]:
categ = ['airplanes','bat','bear']
fts = np.array([])
for categ_idx, subfolder in enumerate(categ):
fNames = glob('classifier/'+subfolder+'/*.jpg')
print len(fNames)
for i, fileName in enumerate(fNames):
img = ndimg.imread(fileName)
img_gray = ndimg.imread(fileName, flatten=True)
ft_temp = features(img, img_gray)
fts = np.concatenate((fts,ft_temp), axis = 1)
In [81]:
fts.shape
Out[81]:
In [69]:
fts = np.array([])
a = np.array([1,2])
print np.concatenate((fts,a), axis = 0)
In [30]:
result
Out[30]:
In [5]:
medfilted = ndimg.median_filter(img_gray,size=(5,5))
plt.figure()
plt.imshow(medfilted,cmap=plt.cm.gray);
In [6]:
edges = canny(medfilted/255.,3)
In [17]:
import skimage.filter as filter
sob = filter.sobel(medfilted/255.)
plt.figure()
plt.imshow(sob, cmap=plt.cm.gray)
plt.figure()
plt.hist(sob.flatten(),np.arange(100)/1000.);
In [18]:
[n,bins,patches] = plt.hist(img_gray.flatten(),np.arange(255))
plt.figure()
plt.plot(bins[:-1],n)
Out[18]:
In [19]:
bins[n.argmax()]
Out[19]:
In [163]:
np.arange(3)
Out[163]:
In [1]:
img_gray.shape
In [26]:
hist_max(img[...,0])
Out[26]:
In [56]:
plt.hist?
In [ ]: