Exemple #1
0
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

# E.1 Compute histograms for all images in the dataset
impaths = tools.getImagePathsFromObjectsDataset('flower') # [ALREADY IMPLEMENTED]
histo = []

for i in list(xrange(len(impaths))):
    # print impaths[i] # get list of all flower images
    histo.append(week1.extractColorHistogram(array(imread(impaths[i]))))

# E.2 Compute distances between all images in the dataset
imdists = []
for i in range(0,60):
    for j in range(0,60):
        imdists.append([])
        imdists[i].append(week1.computeVectorDistance(histo[i], histo[j], 'chi2'))

# E.3 Given an image, rank all other images
query_id = int(14) # rnd.randint(0, 60) # get a random image for a query
sorted_id = np.argsort(imdists[query_id])
print sorted_id

# E.4 Showing results. First image is the query, the rest are the top-5 most similar images [ALREADY IMPLEMENTED]
fig = plt.figure()
ax = fig.add_subplot(2, 3, 1)
im = imread(impaths[query_id])
ax.imshow(im)
ax.axis('off')
ax.set_title('Query image')

for i in np.arange(1, 1+5):
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Step D. Compute distances between vectors [You should implement]
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
reload(week1)
# D.1 Open images and extract their RGB histograms
im1 = imread('../../data/objects/flower/1.jpg')
histo1 = week1.extractColorHistogram(im1)
print im1[:,:,0].flatten()
im2 = imread('../../data/objects/flower/3.jpg')
histo2 = week1.extractColorHistogram(im2)

# D.2 Compute euclidean distance: d=Σ(x-y)^2 
# Note: the ***smaller*** the value, the more similar the histograms
dist_euc = 0.0
dist_euc = np.sqrt(week1.computeVectorDistance(histo1, histo2, 'euclidean'))
print dist_euc
for i in range(len(histo1)):
    dist_euc += pow((histo1[i]-histo2[i]), 2)

print dist_euc

# D.3 Compute histogram intersection distance: d=Σmin(x, y)
# Note: the ***larger*** the value, the more similar the histograms
# dist_hi = ...# WRITE YOUR CODE HERE
dist_hi = 0
for i in range(len(histo1)):
    dist_hi += min(histo1[i], histo2[i])

print dist_hi
# D.4 Compute chi-2 similarity: d= Σ(x-y)^2 / (x+y)
Exemple #3
0
# D.6 PUT YOUR CODE INTO THE FUNCTION computeVectorDistance( vec1, vec2, dist_type ) IN week1.py
######

# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Step E. Rank images [You should implement]
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

# E.1 Compute histograms for all images in the dataset
impaths = tools.getImagePathsFromObjectsDataset('flower')  # [ALREADY IMPLEMENTED]
# histo = ...# WRITE YOUR CODE HERE
histo = [week1.extractColorHistogram(np.array(plt.imread(image)))
         for image in impaths]

# E.2 Compute distances between all images in the dataset
DIST_TYPE = 'euclidean'
imdists = [[week1.computeVectorDistance(h1, h2, DIST_TYPE)
            for h2 in histo]
           for h1 in histo]

# E.3 Given an image, rank all other images
query_id = rnd.randint(0, 59)  # get a random image for a query
# sorted_id = ... # Here you should sort the images according to how distant they are
sorted_id = sorted(range(len(imdists[query_id])),
                   key=lambda x: imdists[query_id][x],
                   reverse=True)

# E.4 Showing results. First image is the query, the rest are the top-5 most similar images [ALREADY IMPLEMENTED]
fig = plt.figure()
ax = fig.add_subplot(2, 3, 1)
im = plt.imread(impaths[query_id])
ax.imshow(im)
Exemple #4
0
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Step E. Rank images [You should implement]
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

# E.1 Compute histograms for all images in the dataset
impaths = tools.getImagePathsFromObjectsDataset('flower') # [ALREADY IMPLEMENTED]
histo = []
for i in list(xrange(len(impaths))):
    histo.append(week1.extractColorHistogram(array(imread(impaths[i]))))
    
# E.2 Compute distances between all images in the dataset
imdists = []
for i in range(0, 60):
    imdists.append([])
    for j in range(0, 60):
        imdists[i].append(week1.computeVectorDistance(histo[i], histo[j], 'euclidean'))
#print imdists[2]
    
# E.3 Given an image, rank all other images
query_id = rnd.randint(0, 59) # get a random image for a query
sorted_id = np.argsort(imdists[query_id])
print sorted_id

# E.4 Showing results. First image is the query, the rest are the top-5 most similar images [ALREADY IMPLEMENTED]
fig = plt.figure()
ax = fig.add_subplot(2, 3, 1)
im = imread(impaths[query_id])
ax.imshow(im)
ax.axis('off')
ax.set_title('Query image')