In [1]:
from __future__ import division
# These are all the modules we'll be using later. Make sure you can import them
# before proceeding further.
from __future__ import print_function
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import numpy as np
import os
import sys
import scipy
import cv2
from IPython.display import display, Image
from scipy import ndimage
from sklearn.linear_model import LogisticRegression
from six.moves.urllib.request import urlretrieve
from six.moves import cPickle as pickle
from glob import glob
import math
import tifffile as tiff
import PIL
from sklearn.cross_validation import train_test_split
from IPython.display import display, Image
from scipy.ndimage import filters
from scipy.spatial.distance import cdist,pdist,squareform
import scipy.spatial.distance as dist

# Config the matlotlib backend as plotting inline in IPython
%matplotlib inline


C:\Users\alvaro.joao.silvino\AppData\Local\Continuum\Anaconda2\lib\site-packages\tifffile.py:276: UserWarning: ImportError: No module named '_tifffile'. Loading of some compressed images will be very slow. Tifffile.c can be obtained at http://www.lfd.uci.edu/~gohlke/
  "ImportError: No module named '_tifffile'. "

In [2]:
datasets = [ f for f in glob('datasets/*')  ]
datasets


Out[2]:
['datasets\\blur_data-2',
 'datasets\\fluorescent_8_bit-9',
 'datasets\\metallic_16_bit-11',
 'datasets\\mondrian_16_bit-11',
 'datasets\\objects_16_bit-11',
 'datasets\\specular_16_bit-11',
 'datasets\\znovabase']

In [3]:
def img2array(im):
    if im.mode != 'RGB':
        im = im.convert(mode='RGB')
    return np.fromstring(im.tobytes(), dtype='uint8').reshape((im.size[1], im.size[0], 3))

def getBaseAndTarget(dataset,isTif18=True):
    data = []
    target = []
    for i,classe in enumerate(glob('%s/*'%dataset)):
        for f in glob('%s/*'%classe):
            if isTif18:
                data.append(tiff.imread(f))
            else:
                data.append(img2array(PIL.Image.open(f)))

            target.append(i)
    return data,target

def normalize(arr):
    ''' Function to normalize an input array to 0-1 '''
    arr_min = arr.min()
    arr_max = arr.max()
    return (arr - arr_min) / (arr_max - arr_min)
def normalize1(arr):
    """
    Linear normalization
    http://en.wikipedia.org/wiki/Normalization_%28image_processing%29
    """
    print("here")
    arr = arr.astype('float')
    # Do not touch the alpha channel
    for i in range(3):
        minval = arr[...,i].min()
        maxval = arr[...,i].max()
        if minval != maxval:
            arr[...,i] -= minval
            arr[...,i] *= (65535.0/(maxval-minval))
    return arr

In [4]:
def getHistogramP(image,logMethod=math.log,derivateMethod=filters.gaussian_laplace,sigma=2):
    
    image_R = image[:,:,0]
    image_G = image[:,:,1]
    image_B = image[:,:,2]
    ln_image_R = map(logMethod,image_R.ravel()+0.001)
    gaussina_laplace_ln_image_R =  derivateMethod(ln_image_R,sigma)

    ln_image_G = map(logMethod,image_G.ravel()+0.001)
    gaussina_laplace_ln_image_G =  derivateMethod(ln_image_G,sigma)

    ln_image_B = map(logMethod,image_B.ravel()+0.001)
    gaussina_laplace_ln_image_B =  derivateMethod(ln_image_B,sigma)
    
    data2 = np.vstack((gaussina_laplace_ln_image_R.flat, gaussina_laplace_ln_image_G.flat,
                       gaussina_laplace_ln_image_B.flat)).astype(np.float).T
    H, edges = np.histogramdd(data2, bins = (16, 16, 16),normed=False)

    return np.float32(cv2.normalize(H).flatten())

    
def getHistogramM(image,logMethod=math.log,derivateMethod=filters.gaussian_laplace,sigma=2):
    
    image_R = image[:,:,0]
    image_G = image[:,:,1]
    image_B = image[:,:,2]
    
    R = np.array(image_R.ravel()+0.0001,dtype=float)
    G = np.array(image_G.ravel()+0.0001,dtype=float)
    B = np.array(image_B.ravel()+0.0001,dtype=float)
    
    ln_image_R_divideG = map(logMethod,np.divide(R,G))
    gaussina_laplace_ln_image_R_divideG =  derivateMethod(ln_image_R_divideG,sigma)

    ln_image_B_divideG = map(logMethod,np.divide(B,G))
    gaussina_laplace_ln_image_B_divideG =  derivateMethod(ln_image_B_divideG,sigma)
    
    data2 = np.vstack((gaussina_laplace_ln_image_R_divideG.flat, gaussina_laplace_ln_image_B_divideG.flat)).astype(np.float).T
    H, edges = np.histogramdd(data2, bins = (16, 16),normed=False)
    return np.float32(cv2.normalize(H).flatten())

    
def getHistogramThip(image,logMethod=math.log,derivateMethod=filters.gaussian_laplace,sigma=2):
    
    image_R = image[:,:,0]
    image_G = image[:,:,1]
    image_B = image[:,:,2]
    
    
    R = np.array(image_R.ravel()+0.0001,dtype=float)
    G = np.array(image_G.ravel()+0.0001,dtype=float)
    B = np.array(image_B.ravel()+0.0001,dtype=float)
    
    ln_image_R = map(logMethod,R)
    gaussina_laplace_ln_image_R =  derivateMethod(ln_image_R,sigma)

    ln_image_G = map(logMethod,G)
    gaussina_laplace_ln_image_G =  derivateMethod(ln_image_G,sigma)

    ln_image_B = map(logMethod,B)
    gaussina_laplace_ln_image_B =  derivateMethod(ln_image_B,sigma)
    
    thip1 = np.array(map(lambda (x,y):math.atan2(x,y),zip(gaussina_laplace_ln_image_R,gaussina_laplace_ln_image_G)))
    thip2 = np.array(map(lambda (x,y):math.atan2(x,y),zip(gaussina_laplace_ln_image_G,gaussina_laplace_ln_image_B)))
    data2 = np.vstack((thip1.flat, thip2.flat)).astype(np.float).T
    H, edges = np.histogramdd(data2, bins = (16, 16),normed=False)
    return np.float32(cv2.normalize(H).flatten())
      
def getHistogramThipM(image,logMethod=math.log,derivateMethod=filters.gaussian_laplace,sigma=2):
    
    
    image_R = image[:,:,0]
    image_G = image[:,:,1]
    image_B = image[:,:,2]
    
    
    R = np.array(image_R.ravel()+0.0001,dtype=float)
    G = np.array(image_G.ravel()+0.0001,dtype=float)
    B = np.array(image_B.ravel()+0.0001,dtype=float)
    
    ln_image_R_divideG = map(logMethod,np.divide(R,G))
    gaussina_laplace_ln_image_R_divideG =  derivateMethod(ln_image_R_divideG,sigma)

    ln_image_B_divideG = map(logMethod,np.divide(G,B))
    gaussina_laplace_ln_image_B_divideG =  derivateMethod(ln_image_B_divideG,sigma)

    
    thipm = np.array(map(lambda (x,y):math.atan2(x,y),zip(gaussina_laplace_ln_image_R_divideG,gaussina_laplace_ln_image_B_divideG)))
    data2 = thipm.flat
    H, edges = np.histogramdd(data2, bins = (16),normed=False)
    return np.float32(cv2.normalize(H).flatten())

def getHistogramThipMWithDerivateAndSquare(image,logMethod=math.log,derivateMethod=filters.gaussian_laplace,sigma=2):
    
    image_R = image[:,:,0]
    image_G = image[:,:,1]
    image_B = image[:,:,2]
    
    R = np.array(image_R.ravel()+0.0001,dtype=float)
    G = np.array(image_G.ravel()+0.0001,dtype=float)
    B = np.array(image_B.ravel()+0.0001,dtype=float)
    
    
    ln_image_R_divideG = map(logMethod,np.divide(R,G))
    gaussina_laplace_ln_image_R_divideG =  derivateMethod(ln_image_R_divideG,sigma)
    ln_image_B_divideG = map(logMethod,np.divide(G,B))
    gaussina_laplace_ln_image_B_divideG =  derivateMethod(ln_image_B_divideG,sigma)
    
    thipm = np.array(map(lambda (x,y):math.atan2(x,y),zip(gaussina_laplace_ln_image_R_divideG,gaussina_laplace_ln_image_B_divideG)))
    
    gaussina_laplace_thipm =  derivateMethod(thipm,sigma)
    
#     upPart = map(lambda x:math.pow(x,2),derivateMethod(gaussina_laplace_ln_image_R_divideG,sigma))
#     bottonPart = map(lambda (x,y):math.sqrt(math.pow(x,2)+math.pow(y,2)),
#                      zip(gaussina_laplace_ln_image_R_divideG,gaussina_laplace_ln_image_B_divideG))

#     finalPart = np.array(map(lambda (x,y):x/(y+0000.1),zip(upPart,bottonPart)))
    finalPart = np.array(map(lambda x:math.pow(x,2),gaussina_laplace_thipm))

    data2 = finalPart.flat

    H, edges = np.histogramdd(data2, bins = (16),normed=False)
    return np.float32(cv2.normalize(H).flatten())

In [5]:
#Computing histogram
def computeHistogram(train,test,method,*args):
    train_histo = {}
    test_histo = {}

    for i,imageTrain in enumerate(train):
        train_histo[i]= method(imageTrain,*args)
    for i,imageTest in enumerate(test):
        test_histo[i]= method(imageTest,*args)
    
    return train_histo,test_histo

def calcRanks(train_histogram_dict,train_target,test_histogram_dict,test_target,nr=1):
    allTestRanked = []
    for test_id in test_histogram_dict:
        allDistances=[]
        for train_id in train_histogram_dict:
            d = dist.euclidean(train_histogram_dict[train_id], test_histogram_dict[test_id])
            allDistances.append((train_id,d,train_target[train_id]))
        allDistances = sorted(allDistances,key=lambda x:x[1],reverse=False)
        allTestRanked.append((test_id,[ i+1 for i,(x,d,target) in enumerate(allDistances) if  target == test_target[test_id]][:]))
    return allTestRanked

In [6]:
import scipy
def nar(ranks,n):
    result = 0
    nr = len(ranks)
    for (ri) in ranks:
        result += ri-((nr*(nr+1))/2)
    result*=(1/(n*nr))
    
    return result
def anar(allRanks):
    n = len(allRanks)*len(allRanks[0][1])
    return scipy.mean(map(lambda (x,y):nar(y,n),allRanks))

In [7]:
def calcKnn(allRanks,k=1):
    hits = 0
    for (i,rankedImage) in allRanks:
        votes = 0
        for rank in rankedImage[:k]:
            if rank in range(1,k+1):
                votes+=1
        if votes>k/2:
            hits+=1
    return hits/len(allRanks)

In [8]:
def plot_gallery(title, images, n_col, n_row,image_shape = (28, 28)):
    plt.figure(figsize=(2. * n_col, 2.26 * n_row))
    plt.suptitle(title, size=16)
    for i, comp in enumerate(images):
        plt.subplot(n_row, n_col, i + 1)
        vmax = max(comp.max(), -comp.min())
        plt.imshow(normalize(comp.reshape(image_shape)), cmap=plt.cm.gray,            
                   vmin=-vmax, vmax=vmax)
        plt.xticks(())
        plt.yticks(())
    plt.subplots_adjust(0.01, 0.05, 0.99, 0.93, 0.04, 0.)
    plt.show()

In [85]:
ranked = [(0, [1, 17, 19, 20, 21, 22, 23, 24]), (1, [1, 2, 3, 4, 6, 7, 13, 24]), (2, [1, 2, 34, 36, 44, 45, 46, 48]), (3, [1, 2, 3, 4, 5, 6, 7, 16]), (4, [1, 2, 3, 4, 5, 6, 7, 21]), (5, [2, 6, 9, 10, 12, 14, 16, 19])]
rr=[(0, [23, 39, 46, 60, 64, 71, 105, 108, 144, 196]), (1, [2, 5, 8, 32, 33, 48, 73, 188, 198, 199]), (2, [2, 3, 11, 64, 79, 95, 107, 125, 148, 160]), (3, [7, 12, 21, 54, 58, 87, 138, 168, 170, 173]), (4, [8, 50, 73, 75, 81, 82, 131, 133, 134, 157]), (5, [16, 23, 33, 64, 66, 71, 77, 78, 81, 108]), (6, [3, 7, 21, 33, 41, 77, 89, 95, 132, 164]), (7, [18, 21, 34, 40, 56, 99, 135, 144, 145, 169]), (8, [1, 3, 4, 59, 61, 68, 69, 70, 78, 113]), (9, [1, 2, 15, 17, 19, 20, 24, 30, 45, 53]), (10, [1, 29, 40, 42, 54, 82, 102, 149, 151, 172]), (11, [5, 11, 17, 22, 25, 32, 34, 38, 48, 92]), (12, [4, 5, 80, 87, 128, 151, 159, 170, 171, 174]), (13, [1, 12, 17, 23, 32, 44, 48, 50, 106, 122]), (14, [97, 109, 118, 120, 133, 145, 155, 168, 180, 186]), (15, [45, 53, 66, 79, 80, 82, 94, 107, 123, 148]), (16, [16, 17, 21, 57, 61, 65, 68, 144, 153, 166]), (17, [5, 15, 37, 39, 93, 115, 134, 138, 151, 195]), (18, [25, 31, 38, 101, 110, 123, 159, 167, 180, 190]), (19, [1, 17, 20, 21, 26, 48, 51, 79, 83, 161])]
print(calcKnn(ranked))
print(anar(ranked))
print(anar(rr))


0.833333333333
-0.470486111111
0.111975

Tabela 1 Utilização de uma parte da base Saimon Fraser

- 20 Objetos sem filtros

- 20 objetos com filtro gaussiano para embassar com desvio = 2


In [75]:
print(datasets[4])
np_images,target = getBaseAndTarget(datasets[4],isTif18=True)

images_train, images_test, target_train, target_test = train_test_split(np_images, target, 
                                                                        test_size=len(set(target)), random_state=42,stratify=target)

images_train = map(lambda x:filters.gaussian_filter(x,(2,2,0), order=0),images_test)
target_train = target_test
print(len(images_train),len(images_test))
target_test


datasets\objects_16_bit-11
20 20
Out[75]:
[2, 13, 1, 19, 8, 15, 3, 6, 18, 12, 7, 0, 4, 11, 10, 17, 14, 5, 9, 16]

In [76]:
train_histoP,test_histoP = computeHistogram(images_train,images_test,getHistogramP,math.log,filters.gaussian_laplace)
allTestRanked = calcRanks(train_histoP,target_train,test_histoP,target_test)
print(allTestRanked)
r = anar(allTestRanked)
print("knn para %.3f k=1"%calcKnn(allTestRanked,k=1))

print("P 3D gaussian_laplace %.3f"%r)

train_histoP,test_histoP = computeHistogram(images_train,images_test,getHistogramM,math.log,filters.gaussian_laplace)
allTestRanked = calcRanks(train_histoP,target_train,test_histoP,target_test)
print(allTestRanked)
r = anar(allTestRanked)
print("knn para %.3f k=1"%calcKnn(allTestRanked,k=1))

print("M 2D gaussian_laplace %.3f"%r)

train_histoP,test_histoP = computeHistogram(images_train,images_test,getHistogramThip,math.log,filters.gaussian_laplace)
allTestRanked = calcRanks(train_histoP,target_train,test_histoP,target_test)
print(allTestRanked)
r = anar(allTestRanked)
print("knn para %.3f k=1"%calcKnn(allTestRanked,k=1))

print("Thi P 2D gaussian_laplace %.3f"%r)

train_histoP,test_histoP = computeHistogram(images_train,images_test,getHistogramThipM,math.log,filters.gaussian_laplace)
allTestRanked = calcRanks(train_histoP,target_train,test_histoP,target_test)
print(allTestRanked)
r = anar(allTestRanked)
print("knn para %.3f k=1"%calcKnn(allTestRanked,k=1))

print("Thi M 1D gaussian_laplace %.3f"%r)


[(0, [13]), (1, [1]), (2, [10]), (3, [13]), (4, [7]), (5, [6]), (6, [6]), (7, [15]), (8, [1]), (9, [14]), (10, [7]), (11, [2]), (12, [8]), (13, [18]), (14, [1]), (15, [17]), (16, [16]), (17, [12]), (18, [20]), (19, [17])]
knn para 0.150 k=1
P 3D gaussian_laplace 0.460
[(0, [19]), (1, [1]), (2, [9]), (3, [15]), (4, [3]), (5, [12]), (6, [7]), (7, [19]), (8, [19]), (9, [14]), (10, [10]), (11, [2]), (12, [15]), (13, [10]), (14, [12]), (15, [9]), (16, [6]), (17, [9]), (18, [4]), (19, [18])]
knn para 0.050 k=1
M 2D gaussian_laplace 0.483
[(0, [2]), (1, [3]), (2, [1]), (3, [3]), (4, [2]), (5, [1]), (6, [2]), (7, [1]), (8, [1]), (9, [1]), (10, [1]), (11, [2]), (12, [2]), (13, [1]), (14, [1]), (15, [1]), (16, [4]), (17, [1]), (18, [1]), (19, [1])]
knn para 0.600 k=1
Thi P 2D gaussian_laplace 0.030
[(0, [2]), (1, [6]), (2, [2]), (3, [4]), (4, [1]), (5, [1]), (6, [4]), (7, [2]), (8, [3]), (9, [1]), (10, [2]), (11, [7]), (12, [4]), (13, [1]), (14, [1]), (15, [2]), (16, [1]), (17, [1]), (18, [4]), (19, [2])]
knn para 0.350 k=1
Thi M 1D gaussian_laplace 0.077

- Tabela 2

Base de dados fluorescent_8_bit


In [86]:
print(datasets[1])
np_images,target = getBaseAndTarget(datasets[1],isTif18=False)
images_train, images_test, target_train, target_test = train_test_split(np_images, target, 
                                                                        test_size=len(set(target)), random_state=42,stratify=target)
print(len(images_train),len(images_test))
target_test


datasets\objects_16_bit-11
200 20
Out[86]:
[2, 13, 1, 19, 8, 15, 3, 6, 18, 12, 7, 0, 4, 11, 10, 17, 14, 5, 9, 16]

In [74]:
train_histoP,test_histoP = computeHistogram(images_train,images_test,getHistogramP,math.log,filters.gaussian_laplace)
allTestRanked = calcRanks(train_histoP,target_train,test_histoP,target_test)
r = anar(allTestRanked)
print("P 3D gaussian_laplace knn para %.3f k=1"%calcKnn(allTestRanked,k=1))
print("P 3D gaussian_laplace knn para %.3f k=3"%calcKnn(allTestRanked,k=3))
print("P 3D gaussian_laplace knn para %.3f k=5"%calcKnn(allTestRanked,k=5))
print("P 3D gaussian_laplace %.3f"%r)
print(allTestRanked)

train_histoP,test_histoP = computeHistogram(images_train,images_test,getHistogramM,math.log,filters.gaussian_laplace)
allTestRanked = calcRanks(train_histoP,target_train,test_histoP,target_test)
r = anar(allTestRanked)
print("M 2D gaussian_laplace  knn para %.3f k=1"%calcKnn(allTestRanked,k=1))
print("M 2D gaussian_laplace  knn para %.3f k=3"%calcKnn(allTestRanked,k=3))
print("M 2D gaussian_laplace  knn para %.3f k=5"%calcKnn(allTestRanked,k=4))
print("M 2D gaussian_laplace %.3f"%r)
print(allTestRanked)

train_histoP,test_histoP = computeHistogram(images_train,images_test,getHistogramThip,math.log,filters.gaussian_laplace)
allTestRanked = calcRanks(train_histoP,target_train,test_histoP,target_test)
r = anar(allTestRanked)
print("Thi P 2D gaussian_laplace knn para %.3f k=1"%calcKnn(allTestRanked,k=1))
print("Thi P 2D gaussian_laplace knn para %.3f k=3"%calcKnn(allTestRanked,k=3))
print("Thi P 2D gaussian_laplace knn para %.3f k=5"%calcKnn(allTestRanked,k=5))
print("Thi P 2D gaussian_laplace %.3f"%r)
print(allTestRanked)

train_histoP,test_histoP = computeHistogram(images_train,images_test,getHistogramThipM,math.log,filters.gaussian_laplace)
allTestRanked = calcRanks(train_histoP,target_train,test_histoP,target_test)
r = anar(allTestRanked)
print("Thi M 1D gaussian_laplace knn para %.3f k=1"%calcKnn(allTestRanked,k=1))
print("Thi M 1D gaussian_laplace knn para %.3f k=3"%calcKnn(allTestRanked,k=3))
print("Thi M 1D gaussian_laplace knn para %.3f k=5"%calcKnn(allTestRanked,k=5))

print("Thi M 1D gaussian_laplace %.3f"%r)
print(allTestRanked)


P 3D gaussian_laplace knn para 0.833 k=1
P 3D gaussian_laplace knn para 0.667 k=3
P 3D gaussian_laplace knn para 0.500 k=5
P 3D gaussian_laplace -0.470
[(0, [1, 17, 19, 20, 21, 22, 23, 24]), (1, [1, 2, 3, 4, 6, 7, 13, 24]), (2, [1, 2, 34, 36, 44, 45, 46, 48]), (3, [1, 2, 3, 4, 5, 6, 7, 16]), (4, [1, 2, 3, 4, 5, 6, 7, 21]), (5, [2, 6, 9, 10, 12, 14, 16, 19])]
M 2D gaussian_laplace  knn para 0.667 k=1
M 2D gaussian_laplace  knn para 0.333 k=3
M 2D gaussian_laplace  knn para 0.000 k=5
M 2D gaussian_laplace -0.284
[(0, [1, 4, 13, 14, 18, 28, 37, 38]), (1, [3, 5, 8, 15, 29, 32, 34, 35]), (2, [1, 8, 11, 12, 13, 14, 32, 39]), (3, [1, 2, 42, 43, 45, 46, 47, 48]), (4, [2, 5, 17, 27, 33, 34, 38, 43]), (5, [1, 2, 7, 15, 23, 27, 39, 42])]
Thi P 2D gaussian_laplace knn para 1.000 k=1
Thi P 2D gaussian_laplace knn para 1.000 k=3
Thi P 2D gaussian_laplace knn para 1.000 k=5
Thi P 2D gaussian_laplace -0.645
[(0, [1, 2, 3, 4, 5, 6, 14, 22]), (1, [1, 2, 3, 4, 5, 6, 7, 8]), (2, [1, 2, 3, 4, 5, 6, 7, 8]), (3, [1, 2, 3, 4, 5, 6, 7, 8]), (4, [1, 2, 3, 4, 5, 6, 7, 8]), (5, [1, 2, 3, 4, 5, 6, 7, 14])]
Thi M 1D gaussian_laplace knn para 1.000 k=1
Thi M 1D gaussian_laplace knn para 1.000 k=3
Thi M 1D gaussian_laplace knn para 1.000 k=5
Thi M 1D gaussian_laplace -0.605
[(0, [1, 2, 4, 11, 13, 14, 26, 27]), (1, [1, 2, 3, 4, 6, 7, 10, 20]), (2, [1, 2, 3, 4, 5, 7, 8, 16]), (3, [1, 2, 3, 4, 5, 6, 7, 17]), (4, [1, 2, 3, 4, 5, 6, 7, 8]), (5, [1, 2, 3, 4, 5, 6, 10, 24])]

Tabela 3 - Utilização de uma parte da base Saimon Fraser

- 20 Objetos sem tranformação afim

- 20 Objetos com tranformação afim


In [81]:
print(datasets[4])
np_images,target = getBaseAndTarget(datasets[4],isTif18=True)

images_train, images_test, target_train, target_test = train_test_split(np_images, target, 
                                                                        test_size=len(set(target)), random_state=42,stratify=target)

pts1 = np.float32([[50,50],[200,50],[50,200]])
pts2 = np.float32([[10,100],[200,50],[100,250]])
M = cv2.getAffineTransform(pts1,pts2)

images_train = map(lambda x:cv2.warpAffine(x,M,x.shape[:2]),images_test)
target_train = target_test
print(len(images_train),len(images_test))
target_test


datasets\objects_16_bit-11
20 20
Out[81]:
[2, 13, 1, 19, 8, 15, 3, 6, 18, 12, 7, 0, 4, 11, 10, 17, 14, 5, 9, 16]

In [82]:
train_histoP,test_histoP = computeHistogram(images_train,images_test,getHistogramP,math.log,filters.gaussian_laplace)
allTestRanked = calcRanks(train_histoP,target_train,test_histoP,target_test)
print(allTestRanked)
r = anar(allTestRanked)
print("knn para %.3f k=1"%calcKnn(allTestRanked,k=1))
print("P 3D gaussian_laplace %.3f"%r)

train_histoP,test_histoP = computeHistogram(images_train,images_test,getHistogramM,math.log,filters.gaussian_laplace)
allTestRanked = calcRanks(train_histoP,target_train,test_histoP,target_test)
print(allTestRanked)
r = anar(allTestRanked)
print("knn para %.3f k=1"%calcKnn(allTestRanked,k=1))
print("M 2D gaussian_laplace %.3f"%r)

train_histoP,test_histoP = computeHistogram(images_train,images_test,getHistogramThip,math.log,filters.gaussian_laplace)
allTestRanked = calcRanks(train_histoP,target_train,test_histoP,target_test)
print(allTestRanked)
r = anar(allTestRanked)
print("knn para %.3f k=1"%calcKnn(allTestRanked,k=1))
print("Thi P 2D gaussian_laplace %.3f"%r)

train_histoP,test_histoP = computeHistogram(images_train,images_test,getHistogramThipM,math.log,filters.gaussian_laplace)
allTestRanked = calcRanks(train_histoP,target_train,test_histoP,target_test)
print(allTestRanked)
r = anar(allTestRanked)
print("knn para %.3f k=1"%calcKnn(allTestRanked,k=1))
print("Thi M 1D gaussian_laplace %.3f"%r)


[(0, [1]), (1, [4]), (2, [8]), (3, [1]), (4, [3]), (5, [18]), (6, [9]), (7, [1]), (8, [9]), (9, [18]), (10, [16]), (11, [14]), (12, [1]), (13, [20]), (14, [16]), (15, [5]), (16, [9]), (17, [7]), (18, [3]), (19, [18])]
knn para 0.200 k=1
P 3D gaussian_laplace 0.403
[(0, [4]), (1, [17]), (2, [9]), (3, [4]), (4, [3]), (5, [10]), (6, [7]), (7, [17]), (8, [18]), (9, [6]), (10, [10]), (11, [5]), (12, [15]), (13, [4]), (14, [6]), (15, [1]), (16, [10]), (17, [16]), (18, [4]), (19, [9])]
knn para 0.050 k=1
M 2D gaussian_laplace 0.388
[(0, [2]), (1, [3]), (2, [3]), (3, [5]), (4, [1]), (5, [2]), (6, [2]), (7, [3]), (8, [1]), (9, [1]), (10, [5]), (11, [1]), (12, [1]), (13, [1]), (14, [4]), (15, [5]), (16, [3]), (17, [8]), (18, [1]), (19, [1])]
knn para 0.400 k=1
Thi P 2D gaussian_laplace 0.083
[(0, [4]), (1, [1]), (2, [11]), (3, [7]), (4, [2]), (5, [4]), (6, [8]), (7, [4]), (8, [5]), (9, [1]), (10, [10]), (11, [3]), (12, [9]), (13, [1]), (14, [9]), (15, [12]), (16, [5]), (17, [15]), (18, [4]), (19, [2])]
knn para 0.150 k=1
Thi M 1D gaussian_laplace 0.243

Tabela 4 - Utilização da base Real World Blurred Data

- 20 Objetos não embasados

- 20 Objetos embasados produzidos pela câmera


In [77]:
print(datasets[0])
np_images,target = getBaseAndTarget(datasets[0],False)
print(len(np_images))
images_train, images_test, target_train, target_test = train_test_split(np_images, target, 
                                                                        test_size=len(set(target)), random_state=42,stratify=target)
print(len(images_train),len(images_test))
target_test


datasets\blur_data-2
40
20 20
Out[77]:
[19, 10, 7, 12, 9, 5, 17, 0, 3, 1, 13, 8, 15, 6, 16, 4, 2, 11, 18, 14]

In [78]:
train_histoP,test_histoP = computeHistogram(images_train,images_test,getHistogramP,math.log,filters.gaussian_laplace)
allTestRanked = calcRanks(train_histoP,target_train,test_histoP,target_test)
print(allTestRanked)
r = anar(allTestRanked)
print("knn para %.3f k=1"%calcKnn(allTestRanked,k=1))

print("P 3D gaussian_laplace %.3f"%r)

train_histoP,test_histoP = computeHistogram(images_train,images_test,getHistogramM,math.log,filters.gaussian_laplace)
allTestRanked = calcRanks(train_histoP,target_train,test_histoP,target_test)
print(allTestRanked)
r = anar(allTestRanked)
print("knn para %.3f k=1"%calcKnn(allTestRanked,k=1))
print("M 2D gaussian_laplace %.3f"%r)

train_histoP,test_histoP = computeHistogram(images_train,images_test,getHistogramThip,math.log,filters.gaussian_laplace)
allTestRanked = calcRanks(train_histoP,target_train,test_histoP,target_test)
print(allTestRanked)
r = anar(allTestRanked)
print("knn para %.3f k=1"%calcKnn(allTestRanked,k=1))
print("Thi P 2D gaussian_laplace %.3f"%r)

train_histoP,test_histoP = computeHistogram(images_train,images_test,getHistogramThipM,math.log,filters.gaussian_laplace)
allTestRanked = calcRanks(train_histoP,target_train,test_histoP,target_test)
print(allTestRanked)
r = anar(allTestRanked)
print("knn para %.3f k=1"%calcKnn(allTestRanked,k=1))
print("Thi M 1D gaussian_laplace %.3f"%r)


[(0, [13]), (1, [2]), (2, [6]), (3, [14]), (4, [1]), (5, [1]), (6, [14]), (7, [19]), (8, [3]), (9, [18]), (10, [13]), (11, [1]), (12, [7]), (13, [1]), (14, [20]), (15, [6]), (16, [10]), (17, [16]), (18, [2]), (19, [8])]
knn para 0.200 k=1
P 3D gaussian_laplace 0.388
[(0, [6]), (1, [2]), (2, [11]), (3, [7]), (4, [3]), (5, [20]), (6, [2]), (7, [18]), (8, [14]), (9, [4]), (10, [8]), (11, [7]), (12, [8]), (13, [4]), (14, [20]), (15, [2]), (16, [4]), (17, [15]), (18, [2]), (19, [4])]
knn para 0.000 k=1
M 2D gaussian_laplace 0.353
[(0, [1]), (1, [1]), (2, [1]), (3, [1]), (4, [1]), (5, [1]), (6, [1]), (7, [1]), (8, [2]), (9, [9]), (10, [2]), (11, [1]), (12, [1]), (13, [1]), (14, [1]), (15, [1]), (16, [1]), (17, [1]), (18, [1]), (19, [1])]
knn para 0.850 k=1
Thi P 2D gaussian_laplace 0.025
[(0, [2]), (1, [1]), (2, [2]), (3, [1]), (4, [1]), (5, [4]), (6, [1]), (7, [2]), (8, [3]), (9, [4]), (10, [2]), (11, [2]), (12, [1]), (13, [1]), (14, [1]), (15, [1]), (16, [2]), (17, [1]), (18, [1]), (19, [1])]
knn para 0.550 k=1
Thi M 1D gaussian_laplace 0.035

Tabela 5

Todos os objetos reunidos nas bases de imagens Saimon Fraser 56 objetos 616 imagens ao total


In [9]:
print(datasets[-1])
np_images,target = getBaseAndTarget(datasets[-1],isTif18=True)

images_train, images_test, target_train, target_test = train_test_split(np_images, target, 
                                                                        test_size=len(set(target)), random_state=42,stratify=target)
print(len(images_train),len(images_test))
target_test


datasets\znovabase
560 56
Out[9]:
[18,
 6,
 39,
 48,
 52,
 23,
 16,
 2,
 36,
 8,
 13,
 21,
 41,
 37,
 31,
 27,
 53,
 40,
 20,
 15,
 51,
 34,
 32,
 38,
 29,
 14,
 0,
 24,
 17,
 49,
 12,
 33,
 50,
 43,
 22,
 55,
 5,
 9,
 10,
 19,
 54,
 47,
 42,
 25,
 45,
 35,
 30,
 26,
 1,
 44,
 11,
 3,
 46,
 28,
 4,
 7]

In [10]:
train_histoP,test_histoP = computeHistogram(images_train,images_test,getHistogramP,math.log,filters.gaussian_laplace)
allTestRanked = calcRanks(train_histoP,target_train,test_histoP,target_test)
r = anar(allTestRanked)
print("P 3D gaussian_laplace knn para %.3f k=1"%calcKnn(allTestRanked,k=1))
print("P 3D gaussian_laplace knn para %.3f k=3"%calcKnn(allTestRanked,k=3))
print("P 3D gaussian_laplace knn para %.3f k=5"%calcKnn(allTestRanked,k=5))
print("P 3D gaussian_laplace %.3f"%r)
print(allTestRanked)

train_histoP,test_histoP = computeHistogram(images_train,images_test,getHistogramM,math.log,filters.gaussian_laplace)
allTestRanked = calcRanks(train_histoP,target_train,test_histoP,target_test)
r = anar(allTestRanked)
print("M 2D gaussian_laplace  knn para %.3f k=1"%calcKnn(allTestRanked,k=1))
print("M 2D gaussian_laplace  knn para %.3f k=3"%calcKnn(allTestRanked,k=3))
print("M 2D gaussian_laplace  knn para %.3f k=5"%calcKnn(allTestRanked,k=4))
print("M 2D gaussian_laplace %.3f"%r)
print(allTestRanked)

train_histoP,test_histoP = computeHistogram(images_train,images_test,getHistogramThip,math.log,filters.gaussian_laplace)
allTestRanked = calcRanks(train_histoP,target_train,test_histoP,target_test)
r = anar(allTestRanked)
print("Thi P 2D gaussian_laplace knn para %.3f k=1"%calcKnn(allTestRanked,k=1))
print("Thi P 2D gaussian_laplace knn para %.3f k=3"%calcKnn(allTestRanked,k=3))
print("Thi P 2D gaussian_laplace knn para %.3f k=5"%calcKnn(allTestRanked,k=5))
print("Thi P 2D gaussian_laplace %.3f"%r)
print(allTestRanked)

train_histoP,test_histoP = computeHistogram(images_train,images_test,getHistogramThipM,math.log,filters.gaussian_laplace)
allTestRanked = calcRanks(train_histoP,target_train,test_histoP,target_test)
r = anar(allTestRanked)
print("Thi M 1D blu knn para %.3f k=1"%calcKnn(allTestRanked,k=1))
print("Thi M 1D gaussian_laplace knn para %.3f k=3"%calcKnn(allTestRanked,k=3))
print("Thi M 1D gaussian_laplace knn para %.3f k=5"%calcKnn(allTestRanked,k=5))

print("Thi M 1D gaussian_laplace %.3f"%r)
print(allTestRanked)


P 3D gaussian_laplace knn para 0.393 k=1
P 3D gaussian_laplace knn para 0.125 k=3
P 3D gaussian_laplace knn para 0.054 k=5
P 3D gaussian_laplace 0.253
[(0, [37, 46, 87, 110, 121, 132, 149, 154, 175, 391]), (1, [18, 61, 76, 155, 156, 164, 199, 263, 316, 354]), (2, [1, 44, 79, 211, 314, 323, 329, 388, 444, 459]), (3, [1, 11, 27, 91, 114, 140, 155, 219, 243, 379]), (4, [40, 100, 103, 337, 461, 467, 506, 509, 531, 532]), (5, [7, 142, 151, 206, 245, 303, 329, 344, 366, 367]), (6, [1, 3, 22, 54, 66, 80, 81, 204, 318, 409]), (7, [5, 7, 32, 56, 80, 110, 206, 428, 498, 530]), (8, [1, 6, 7, 11, 22, 72, 151, 183, 439, 451]), (9, [15, 38, 66, 98, 99, 173, 192, 207, 287, 369]), (10, [1, 15, 136, 147, 154, 172, 173, 232, 255, 288]), (11, [1, 2, 3, 6, 10, 15, 59, 74, 236, 389]), (12, [219, 229, 350, 442, 456, 457, 461, 477, 478, 507]), (13, [1, 42, 46, 51, 57, 60, 193, 210, 258, 260]), (14, [2, 4, 6, 11, 12, 80, 155, 158, 189, 212]), (15, [9, 140, 177, 278, 279, 281, 292, 305, 471, 478]), (16, [7, 12, 32, 111, 114, 122, 212, 265, 457, 467]), (17, [8, 12, 136, 138, 194, 269, 373, 393, 419, 426]), (18, [7, 45, 74, 81, 203, 225, 248, 270, 403, 442]), (19, [4, 9, 72, 74, 80, 96, 122, 146, 165, 194]), (20, [1, 22, 38, 70, 95, 111, 114, 240, 252, 256]), (21, [11, 73, 176, 200, 230, 316, 389, 431, 436, 466]), (22, [3, 13, 179, 207, 302, 336, 392, 405, 427, 504]), (23, [1, 167, 280, 292, 342, 370, 478, 490, 515, 523]), (24, [5, 78, 92, 96, 115, 129, 157, 166, 179, 492]), (25, [1, 4, 10, 22, 66, 121, 139, 165, 226, 347]), (26, [6, 36, 66, 93, 180, 228, 238, 263, 281, 314]), (27, [1, 9, 46, 50, 52, 53, 58, 68, 91, 113]), (28, [2, 4, 6, 10, 66, 77, 82, 88, 143, 149]), (29, [230, 350, 375, 377, 389, 414, 449, 468, 483, 533]), (30, [1, 3, 13, 14, 15, 18, 30, 110, 131, 329]), (31, [2, 12, 18, 19, 71, 88, 112, 122, 232, 270]), (32, [14, 15, 38, 72, 75, 100, 252, 350, 518, 534]), (33, [80, 82, 273, 347, 365, 427, 445, 472, 480, 508]), (34, [81, 100, 111, 118, 120, 169, 186, 251, 357, 378]), (35, [169, 177, 183, 195, 197, 429, 433, 434, 477, 527]), (36, [1, 12, 13, 19, 37, 80, 84, 99, 142, 241]), (37, [38, 48, 94, 104, 128, 223, 305, 320, 328, 389]), (38, [1, 5, 99, 128, 166, 208, 217, 220, 257, 278]), (39, [19, 30, 45, 62, 83, 160, 226, 230, 319, 417]), (40, [1, 38, 299, 333, 345, 450, 458, 469, 476, 501]), (41, [1, 187, 189, 191, 192, 321, 445, 449, 463, 491]), (42, [1, 12, 42, 300, 312, 328, 346, 418, 471, 551]), (43, [1, 2, 3, 4, 34, 45, 56, 106, 153, 170]), (44, [11, 15, 138, 156, 193, 244, 368, 424, 450, 550]), (45, [17, 20, 90, 248, 285, 359, 446, 457, 486, 492]), (46, [5, 14, 43, 57, 64, 71, 74, 106, 172, 308]), (47, [12, 28, 56, 122, 151, 154, 157, 171, 293, 342]), (48, [1, 2, 4, 43, 64, 68, 85, 116, 278, 349]), (49, [1, 2, 6, 134, 154, 172, 198, 208, 278, 470]), (50, [1, 2, 154, 237, 313, 352, 428, 435, 442, 494]), (51, [69, 77, 103, 125, 145, 194, 237, 248, 272, 282]), (52, [113, 120, 145, 175, 183, 195, 214, 239, 272, 334]), (53, [159, 336, 510, 531, 539, 540, 551, 553, 555, 559]), (54, [8, 19, 23, 65, 82, 91, 137, 278, 329, 385]), (55, [1, 89, 92, 112, 121, 235, 264, 275, 300, 306])]
M 2D gaussian_laplace  knn para 0.214 k=1
M 2D gaussian_laplace  knn para 0.107 k=3
M 2D gaussian_laplace  knn para 0.036 k=5
M 2D gaussian_laplace 0.285
[(0, [15, 36, 50, 95, 163, 170, 240, 298, 340, 413]), (1, [6, 18, 25, 34, 154, 204, 244, 300, 316, 323]), (2, [18, 210, 245, 257, 272, 278, 281, 287, 525, 531]), (3, [11, 19, 29, 72, 134, 165, 247, 267, 404, 431]), (4, [6, 7, 13, 47, 49, 119, 126, 129, 159, 180]), (5, [5, 114, 225, 252, 277, 292, 342, 354, 417, 440]), (6, [1, 167, 170, 403, 437, 499, 501, 523, 533, 542]), (7, [10, 73, 115, 156, 171, 208, 226, 270, 303, 316]), (8, [8, 16, 18, 22, 23, 25, 33, 38, 114, 129]), (9, [2, 8, 32, 40, 86, 255, 266, 274, 335, 344]), (10, [2, 10, 23, 25, 43, 228, 235, 412, 417, 422]), (11, [6, 13, 29, 62, 74, 240, 267, 329, 333, 357]), (12, [1, 178, 259, 276, 410, 432, 446, 469, 473, 505]), (13, [5, 280, 369, 407, 445, 496, 553, 554, 555, 560]), (14, [8, 10, 192, 202, 221, 228, 240, 387, 408, 431]), (15, [1, 30, 147, 160, 188, 207, 360, 385, 424, 446]), (16, [4, 318, 352, 378, 382, 414, 416, 435, 445, 474]), (17, [284, 466, 491, 492, 493, 520, 528, 529, 538, 543]), (18, [6, 35, 176, 179, 186, 230, 258, 272, 281, 323]), (19, [26, 42, 138, 144, 175, 197, 238, 247, 277, 441]), (20, [38, 47, 104, 190, 209, 210, 244, 245, 263, 333]), (21, [180, 210, 212, 220, 259, 283, 361, 437, 456, 523]), (22, [6, 14, 19, 129, 137, 143, 148, 169, 173, 228]), (23, [3, 127, 393, 414, 451, 459, 510, 539, 554, 558]), (24, [247, 260, 263, 277, 282, 283, 285, 292, 354, 553]), (25, [7, 11, 64, 65, 68, 204, 395, 454, 459, 464]), (26, [71, 80, 89, 93, 114, 128, 130, 217, 231, 246]), (27, [8, 34, 83, 100, 117, 131, 164, 239, 264, 297]), (28, [1, 2, 3, 5, 14, 144, 167, 184, 187, 191]), (29, [3, 10, 24, 28, 76, 117, 352, 395, 475, 481]), (30, [1, 2, 26, 273, 274, 281, 304, 502, 507, 508]), (31, [1, 23, 28, 118, 123, 131, 135, 209, 334, 433]), (32, [18, 37, 171, 198, 215, 255, 277, 307, 366, 524]), (33, [8, 33, 93, 128, 203, 268, 405, 411, 433, 464]), (34, [47, 49, 56, 76, 97, 103, 104, 174, 201, 297]), (35, [3, 12, 34, 44, 59, 148, 162, 315, 349, 424]), (36, [1, 230, 256, 258, 310, 345, 378, 409, 481, 523]), (37, [55, 59, 77, 161, 168, 183, 238, 331, 342, 476]), (38, [3, 4, 130, 140, 173, 357, 395, 445, 494, 499]), (39, [2, 123, 132, 155, 173, 174, 188, 201, 242, 294]), (40, [5, 6, 20, 28, 124, 197, 206, 355, 439, 440]), (41, [1, 2, 3, 5, 6, 10, 20, 129, 185, 225]), (42, [5, 9, 10, 19, 40, 64, 208, 238, 243, 498]), (43, [1, 2, 82, 95, 101, 110, 121, 123, 139, 146]), (44, [1, 6, 14, 262, 508, 516, 518, 519, 522, 536]), (45, [1, 3, 5, 12, 20, 96, 479, 482, 484, 510]), (46, [13, 74, 100, 103, 107, 248, 264, 279, 320, 401]), (47, [3, 5, 30, 71, 190, 237, 270, 311, 329, 341]), (48, [46, 71, 110, 114, 133, 156, 189, 274, 308, 382]), (49, [1, 2, 6, 8, 9, 29, 81, 383, 452, 469]), (50, [2, 7, 9, 12, 22, 289, 390, 425, 465, 467]), (51, [2, 6, 19, 25, 28, 55, 63, 77, 121, 154]), (52, [28, 48, 86, 116, 141, 209, 282, 292, 309, 447]), (53, [96, 238, 306, 328, 379, 423, 532, 537, 551, 559]), (54, [42, 97, 178, 198, 261, 329, 334, 435, 439, 474]), (55, [81, 129, 130, 132, 173, 189, 193, 196, 198, 248])]
Thi P 2D gaussian_laplace knn para 0.821 k=1
Thi P 2D gaussian_laplace knn para 0.661 k=3
Thi P 2D gaussian_laplace knn para 0.607 k=5
Thi P 2D gaussian_laplace 0.054
[(0, [1, 2, 3, 4, 10, 25, 53, 94, 154, 288]), (1, [1, 52, 215, 286, 407, 415, 423, 448, 467, 481]), (2, [1, 2, 3, 5, 8, 9, 11, 19, 30, 31]), (3, [2, 19, 25, 34, 53, 61, 151, 153, 416, 448]), (4, [4, 5, 6, 24, 36, 77, 107, 253, 486, 503]), (5, [1, 2, 3, 4, 21, 73, 141, 174, 277, 339]), (6, [1, 2, 3, 4, 5, 6, 8, 13, 41, 58]), (7, [1, 2, 3, 15, 77, 165, 173, 362, 378, 379]), (8, [1, 2, 3, 4, 9, 11, 14, 36, 52, 104]), (9, [2, 7, 13, 22, 55, 78, 227, 246, 351, 419]), (10, [1, 2, 3, 4, 5, 6, 7, 8, 16, 21]), (11, [1, 9, 10, 34, 48, 86, 100, 142, 146, 203]), (12, [1, 2, 3, 4, 5, 6, 9, 11, 26, 31]), (13, [1, 2, 3, 5, 9, 10, 12, 15, 21, 33]), (14, [2, 3, 4, 11, 16, 25, 28, 42, 144, 161]), (15, [1, 2, 3, 4, 5, 7, 8, 9, 21, 26]), (16, [3, 5, 11, 22, 45, 57, 241, 377, 494, 500]), (17, [1, 2, 3, 5, 9, 10, 13, 16, 19, 32]), (18, [1, 2, 4, 5, 6, 30, 92, 112, 242, 295]), (19, [1, 2, 5, 6, 10, 11, 14, 33, 91, 99]), (20, [1, 4, 29, 90, 105, 116, 119, 152, 155, 158]), (21, [1, 5, 22, 38, 73, 170, 328, 335, 457, 464]), (22, [5, 6, 29, 47, 56, 60, 206, 215, 294, 320]), (23, [1, 2, 3, 4, 10, 19, 23, 30, 36, 43]), (24, [1, 2, 3, 4, 5, 6, 7, 11, 17, 26]), (25, [1, 2, 3, 4, 5, 10, 12, 15, 17, 37]), (26, [1, 2, 3, 8, 86, 120, 140, 157, 258, 263]), (27, [14, 21, 35, 52, 67, 97, 101, 108, 162, 280]), (28, [1, 2, 3, 4, 5, 7, 8, 12, 35, 56]), (29, [1, 11, 39, 70, 99, 104, 231, 261, 307, 376]), (30, [1, 2, 3, 4, 7, 8, 12, 15, 16, 55]), (31, [1, 43, 47, 49, 149, 291, 434, 459, 502, 510]), (32, [1, 2, 3, 4, 6, 7, 22, 32, 45, 156]), (33, [1, 11, 39, 110, 111, 113, 116, 140, 153, 248]), (34, [1, 2, 3, 12, 30, 33, 125, 156, 251, 317]), (35, [1, 2, 3, 4, 5, 8, 20, 21, 38, 61]), (36, [1, 2, 3, 4, 6, 14, 27, 47, 106, 196]), (37, [1, 28, 34, 50, 148, 174, 180, 264, 290, 385]), (38, [1, 2, 3, 5, 9, 12, 15, 22, 99, 211]), (39, [1, 2, 9, 15, 17, 28, 40, 84, 158, 214]), (40, [1, 2, 3, 4, 5, 6, 8, 18, 23, 37]), (41, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]), (42, [1, 2, 3, 4, 5, 6, 7, 8, 12, 14]), (43, [1, 2, 4, 5, 7, 8, 10, 11, 18, 20]), (44, [1, 2, 3, 4, 5, 6, 7, 9, 104, 190]), (45, [1, 2, 3, 4, 6, 13, 59, 91, 220, 241]), (46, [1, 5, 9, 21, 24, 33, 109, 141, 403, 460]), (47, [1, 2, 4, 10, 24, 59, 92, 137, 143, 176]), (48, [1, 3, 6, 29, 52, 133, 228, 290, 501, 503]), (49, [1, 2, 3, 4, 5, 6, 7, 8, 11, 13]), (50, [1, 2, 3, 4, 5, 10, 13, 14, 25, 83]), (51, [3, 6, 12, 50, 62, 192, 443, 467, 498, 506]), (52, [4, 44, 45, 57, 213, 243, 367, 372, 453, 463]), (53, [1, 2, 489, 504, 512, 516, 528, 536, 539, 540]), (54, [8, 10, 33, 50, 97, 123, 162, 185, 294, 315]), (55, [1, 6, 10, 24, 52, 63, 148, 155, 197, 237])]
Thi M 1D blu knn para 0.643 k=1
Thi M 1D gaussian_laplace knn para 0.393 k=3
Thi M 1D gaussian_laplace knn para 0.339 k=5
Thi M 1D gaussian_laplace 0.135
[(0, [1, 4, 10, 13, 20, 56, 132, 235, 344, 390]), (1, [5, 66, 147, 310, 343, 381, 411, 432, 475, 481]), (2, [1, 2, 3, 9, 11, 20, 23, 33, 56, 248]), (3, [2, 16, 18, 55, 172, 173, 460, 467, 544, 551]), (4, [9, 15, 17, 20, 60, 71, 79, 162, 226, 275]), (5, [1, 3, 9, 20, 150, 255, 306, 382, 419, 425]), (6, [1, 2, 3, 4, 7, 8, 15, 67, 176, 229]), (7, [1, 10, 17, 150, 267, 275, 458, 499, 520, 522]), (8, [1, 3, 4, 5, 12, 62, 67, 109, 158, 315]), (9, [2, 29, 40, 90, 157, 183, 192, 232, 390, 443]), (10, [2, 6, 8, 12, 27, 37, 43, 62, 70, 160]), (11, [6, 8, 51, 79, 135, 237, 284, 367, 472, 511]), (12, [4, 19, 24, 32, 35, 37, 52, 71, 118, 170]), (13, [1, 2, 3, 4, 5, 13, 14, 26, 38, 78]), (14, [1, 9, 11, 13, 16, 25, 28, 46, 90, 223]), (15, [9, 10, 18, 21, 26, 30, 43, 58, 80, 81]), (16, [3, 14, 47, 62, 63, 85, 166, 188, 206, 266]), (17, [1, 2, 3, 7, 16, 21, 36, 39, 40, 162]), (18, [2, 8, 22, 28, 90, 187, 301, 375, 453, 471]), (19, [1, 5, 18, 43, 77, 131, 146, 188, 261, 313]), (20, [1, 7, 8, 13, 80, 118, 120, 158, 277, 301]), (21, [26, 37, 49, 137, 143, 221, 238, 281, 419, 434]), (22, [34, 38, 50, 52, 65, 88, 221, 226, 351, 403]), (23, [1, 2, 3, 12, 18, 45, 55, 87, 124, 190]), (24, [1, 2, 3, 4, 13, 27, 40, 61, 99, 108]), (25, [1, 2, 3, 4, 5, 11, 14, 41, 171, 211]), (26, [1, 6, 58, 80, 119, 189, 230, 371, 502, 509]), (27, [5, 105, 145, 208, 261, 391, 466, 524, 535, 536]), (28, [2, 5, 9, 11, 15, 20, 25, 30, 47, 55]), (29, [4, 134, 251, 292, 358, 369, 378, 393, 408, 409]), (30, [1, 2, 4, 14, 24, 36, 50, 91, 129, 292]), (31, [1, 38, 65, 85, 109, 146, 280, 355, 525, 531]), (32, [1, 2, 3, 5, 8, 23, 32, 289, 379, 382]), (33, [1, 45, 47, 59, 97, 134, 159, 162, 214, 225]), (34, [1, 4, 5, 27, 47, 86, 92, 105, 111, 119]), (35, [1, 3, 8, 29, 80, 297, 329, 401, 466, 521]), (36, [1, 4, 8, 15, 31, 51, 58, 121, 205, 254]), (37, [1, 4, 77, 95, 150, 173, 295, 392, 426, 468]), (38, [2, 3, 23, 25, 36, 57, 70, 102, 200, 274]), (39, [1, 4, 9, 13, 32, 64, 103, 190, 351, 385]), (40, [1, 2, 3, 4, 31, 43, 60, 109, 123, 124]), (41, [1, 2, 3, 4, 5, 6, 9, 11, 12, 13]), (42, [1, 2, 3, 4, 5, 6, 15, 57, 64, 71]), (43, [1, 2, 3, 6, 7, 10, 13, 16, 123, 138]), (44, [1, 2, 3, 4, 5, 9, 19, 41, 77, 253]), (45, [1, 10, 28, 32, 94, 126, 245, 364, 453, 477]), (46, [1, 3, 6, 23, 24, 66, 355, 471, 544, 546]), (47, [1, 2, 5, 27, 55, 82, 88, 101, 114, 309]), (48, [29, 36, 59, 251, 408, 423, 458, 547, 556, 559]), (49, [1, 2, 3, 4, 6, 15, 48, 52, 157, 249]), (50, [1, 2, 3, 4, 5, 7, 10, 14, 35, 117]), (51, [8, 16, 30, 38, 71, 137, 438, 458, 534, 538]), (52, [36, 86, 97, 112, 258, 325, 463, 464, 525, 532]), (53, [1, 72, 200, 211, 223, 231, 234, 252, 276, 360]), (54, [68, 87, 138, 263, 282, 295, 326, 341, 472, 482]), (55, [1, 9, 15, 47, 81, 87, 238, 254, 270, 409])]

In [27]:
img = images_test[0]#cv2.imread('drawing.png')
rows,cols,ch = img.shape

pts1 = np.float32([[50,50],[200,50],[50,200]])
pts2 = np.float32([[10,100],[200,50],[100,250]])

M = cv2.getAffineTransform(pts1,pts2)

dst = cv2.warpAffine(img,M,(cols,rows))

rows,cols = img.shape[:2]
rows,cols

plt.figure()
plt.imshow(normalize(img))
plt.figure()
plt.imshow(normalize(dst))

plot_gallery('Exemplo de uma imagem tabela 5 ',[img,dst],2,1,img.shape)
normalize(img)


Out[27]:
array([[[ 0.21322957,  0.1729305 ,  0.07612726],
        [ 0.1632105 ,  0.13142596,  0.0296025 ],
        [ 0.18999008,  0.15304799,  0.04678416],
        ..., 
        [ 0.14068818,  0.11194018,  0.04028382],
        [ 0.13879606,  0.11067369,  0.04139773],
        [ 0.1401236 ,  0.11165026,  0.04248112]],

       [[ 0.21574731,  0.16896315,  0.07388418],
        [ 0.16482795,  0.12950332,  0.02720684],
        [ 0.19293507,  0.15638972,  0.04885939],
        ..., 
        [ 0.14320592,  0.1142443 ,  0.04026856],
        [ 0.1404593 ,  0.11419852,  0.04234379],
        [ 0.14175631,  0.11572442,  0.04443427]],

       [[ 0.21930266,  0.17270161,  0.07663081],
        [ 0.1631342 ,  0.12832837,  0.02619974],
        [ 0.19053941,  0.15551995,  0.04818799],
        ..., 
        [ 0.14573892,  0.11480888,  0.04373236],
        [ 0.14509804,  0.11514458,  0.04530404],
        [ 0.14589151,  0.12019532,  0.04827955]],

       ..., 
       [[ 0.19645991,  0.16023499,  0.08773938],
        [ 0.15811398,  0.12747387,  0.05502403],
        [ 0.17338827,  0.14314488,  0.06593423],
        ..., 
        [ 0.02348363,  0.02529946,  0.03212024],
        [ 0.02513161,  0.02697795,  0.03089952],
        [ 0.02142367,  0.02478065,  0.03009079]],

       [[ 0.19346914,  0.1591516 ,  0.08778515],
        [ 0.15829709,  0.12755016,  0.05415427],
        [ 0.17390707,  0.14248875,  0.06390478],
        ..., 
        [ 0.0252842 ,  0.02830549,  0.03453117],
        [ 0.02482643,  0.02429236,  0.02854963],
        [ 0.02314794,  0.02473487,  0.03103685]],

       [[ 0.1930724 ,  0.1569543 ,  0.08601511],
        [ 0.15748836,  0.12594797,  0.05491722],
        [ 0.1734493 ,  0.14355688,  0.06598001],
        ..., 
        [ 0.02784771,  0.02729839,  0.02973983],
        [ 0.02475013,  0.02404822,  0.02731365],
        [ 0.02194247,  0.02446021,  0.02937362]]])

In [28]:
img = images_test[0]#cv2.imread('drawing.png')
dst = filters.gaussian_filter(img,(2,2,0), order=0)
plt.figure()
plt.imshow(normalize(img))
plt.figure()
plt.imshow(normalize(dst))

plot_gallery('Exemplo de uma imagem com embasamento',[img,dst],2,1,img.shape)


Tabela 5 image teste


In [131]:
print(datasets[-1])
np_images,target = getBaseAndTarget(datasets[-1],isTif18=True)
images_train, images_test, target_train, target_test = train_test_split(np_images, target, 
                                                                        test_size=len(set(target)), random_state=42,stratify=target)

plot_gallery('Base 4',images_test[:6],6,1,images_test[0].shape)


datasets\znovabase

Tabela 1 e tabela 3 image teste


In [132]:
print(datasets[4])
np_images,target = getBaseAndTarget(datasets[4],isTif18=True)
images_train, images_test, target_train, target_test = train_test_split(np_images, target, 
                                                                        test_size=len(set(target)), random_state=42,stratify=target)

plot_gallery('Base 2',images_test[:6],6,1,images_test[0].shape)


datasets\objects_16_bit-11

In [133]:
print(datasets[0])
np_images,target = getBaseAndTarget(datasets[0],isTif18=False)
images_train, images_test, target_train, target_test = train_test_split(np_images, target, 
                                                                        test_size=len(set(target)), random_state=42,stratify=target)

plot_gallery('Base 1',images_test[:6],6,1,images_test[0].shape)


datasets\blur_data-2

Tabela 1 fluorescent_8_bit


In [134]:
print(datasets[1])
np_images,target = getBaseAndTarget(datasets[1],isTif18=False)
images_train, images_test, target_train, target_test = train_test_split(np_images, target, 
                                                                        test_size=len(set(target)), random_state=42,stratify=target)
print(len(images_train),len(images_test))
target_test


datasets\fluorescent_8_bit-9
48 6
Out[134]:
[1, 4, 5, 3, 0, 2]

In [135]:
print(datasets[1])
np_images,target = getBaseAndTarget(datasets[1],isTif18=False)
images_train, images_test, target_train, target_test = train_test_split(np_images, target, 
                                                                        test_size=len(set(target)), random_state=42,stratify=target)

plot_gallery('Base 3',images_test[:6],6,1,images_test[0].shape)


datasets\fluorescent_8_bit-9

Teste de hipotese


In [9]:
def getHistogramPMethodResult(images_train,images_test,target_train,target_test):
    train_histoP,test_histoP = computeHistogram(images_train,images_test,getHistogramP,math.log,filters.gaussian_laplace)
    allTestRanked = calcRanks(train_histoP,target_train,test_histoP,target_test)
    k1 = calcKnn(allTestRanked,k=1)
    k3 = calcKnn(allTestRanked,k=3)
    k5 = calcKnn(allTestRanked,k=5)

    return k1,k3,k5
def getHistogramMResult(images_train,images_test,target_train,target_test):
    train_histoP,test_histoP = computeHistogram(images_train,images_test,getHistogramM,math.log,filters.gaussian_laplace)
    allTestRanked = calcRanks(train_histoP,target_train,test_histoP,target_test)
    k1 = calcKnn(allTestRanked,k=1)
    k3 = calcKnn(allTestRanked,k=3)
    k5 = calcKnn(allTestRanked,k=5)

    return k1,k3,k5

def getHistogramThipResult(images_train,images_test,target_train,target_test):
    train_histoP,test_histoP = computeHistogram(images_train,images_test,getHistogramThip,math.log,filters.gaussian_laplace)
    allTestRanked = calcRanks(train_histoP,target_train,test_histoP,target_test)
    k1 = calcKnn(allTestRanked,k=1)
    k3 = calcKnn(allTestRanked,k=3)
    k5 = calcKnn(allTestRanked,k=5)

    return k1,k3,k5

def getHistogramThipMResult(images_train,images_test,target_train,target_test):
    train_histoP,test_histoP = computeHistogram(images_train,images_test,getHistogramThipM,math.log,filters.gaussian_laplace)
    allTestRanked = calcRanks(train_histoP,target_train,test_histoP,target_test)
    k1 = calcKnn(allTestRanked,k=1)
    k3 = calcKnn(allTestRanked,k=3)
    k5 = calcKnn(allTestRanked,k=5)
    return k1,k3,k5

In [10]:
print(datasets[-1])
np_images,target = getBaseAndTarget(datasets[-1],isTif18=True)
CV = 10
pK1 = []
pK2 = []
pK3 = []

mK1 = []
mK2 = []
mK3 = []

thipk1 = []
thipk2 = []
thipk3 = []

thimk1 = []
thimk2 = []
thimk3 = []

for i in range(CV):
    images_train, images_test, target_train, target_test = train_test_split(np_images, target,test_size=len(set(target)), 
                                                                            random_state=i,stratify=target)
    k1,k2,k3 = getHistogramPMethodResult(images_train,images_test,target_train,target_test)
    pK1.append(k1)
    pK2.append(k2)
    pK3.append(k3)
    print(pK3)
    k1,k2,k3 = getHistogramMResult(images_train,images_test,target_train,target_test)
    mK1.append(k1)
    mK2.append(k2)
    mK3.append(k3)
    print(mK3)

    k1,k2,k3 = getHistogramThipResult(images_train,images_test,target_train,target_test)
    thipk1.append(k1)
    thipk2.append(k2)
    thipk3.append(k3)
    print(thipk3)

    k1,k2,k3 = getHistogramThipMResult(images_train,images_test,target_train,target_test)
    thimk1.append(k1)
    thimk2.append(k2)
    thimk3.append(k3)
    print(thimk3)


datasets\znovabase
[0.1388888888888889]
[0.1111111111111111]
[0.8055555555555556]
[0.3333333333333333]
[0.1388888888888889, 0.1388888888888889]
[0.1111111111111111, 0.05555555555555555]
[0.8055555555555556, 0.8888888888888888]
[0.3333333333333333, 0.5555555555555556]
[0.1388888888888889, 0.1388888888888889, 0.16666666666666666]
[0.1111111111111111, 0.05555555555555555, 0.0]
[0.8055555555555556, 0.8888888888888888, 0.7222222222222222]
[0.3333333333333333, 0.5555555555555556, 0.5]
[0.1388888888888889, 0.1388888888888889, 0.16666666666666666, 0.05555555555555555]
[0.1111111111111111, 0.05555555555555555, 0.0, 0.1111111111111111]
[0.8055555555555556, 0.8888888888888888, 0.7222222222222222, 0.8333333333333334]
[0.3333333333333333, 0.5555555555555556, 0.5, 0.5277777777777778]
[0.1388888888888889, 0.1388888888888889, 0.16666666666666666, 0.05555555555555555, 0.05555555555555555]
[0.1111111111111111, 0.05555555555555555, 0.0, 0.1111111111111111, 0.027777777777777776]
[0.8055555555555556, 0.8888888888888888, 0.7222222222222222, 0.8333333333333334, 0.75]
[0.3333333333333333, 0.5555555555555556, 0.5, 0.5277777777777778, 0.4722222222222222]
[0.1388888888888889, 0.1388888888888889, 0.16666666666666666, 0.05555555555555555, 0.05555555555555555, 0.1388888888888889]
[0.1111111111111111, 0.05555555555555555, 0.0, 0.1111111111111111, 0.027777777777777776, 0.08333333333333333]
[0.8055555555555556, 0.8888888888888888, 0.7222222222222222, 0.8333333333333334, 0.75, 0.7777777777777778]
[0.3333333333333333, 0.5555555555555556, 0.5, 0.5277777777777778, 0.4722222222222222, 0.5]
[0.1388888888888889, 0.1388888888888889, 0.16666666666666666, 0.05555555555555555, 0.05555555555555555, 0.1388888888888889, 0.16666666666666666]
[0.1111111111111111, 0.05555555555555555, 0.0, 0.1111111111111111, 0.027777777777777776, 0.08333333333333333, 0.05555555555555555]
[0.8055555555555556, 0.8888888888888888, 0.7222222222222222, 0.8333333333333334, 0.75, 0.7777777777777778, 0.8333333333333334]
[0.3333333333333333, 0.5555555555555556, 0.5, 0.5277777777777778, 0.4722222222222222, 0.5, 0.4166666666666667]
[0.1388888888888889, 0.1388888888888889, 0.16666666666666666, 0.05555555555555555, 0.05555555555555555, 0.1388888888888889, 0.16666666666666666, 0.2222222222222222]
[0.1111111111111111, 0.05555555555555555, 0.0, 0.1111111111111111, 0.027777777777777776, 0.08333333333333333, 0.05555555555555555, 0.08333333333333333]
[0.8055555555555556, 0.8888888888888888, 0.7222222222222222, 0.8333333333333334, 0.75, 0.7777777777777778, 0.8333333333333334, 0.7222222222222222]
[0.3333333333333333, 0.5555555555555556, 0.5, 0.5277777777777778, 0.4722222222222222, 0.5, 0.4166666666666667, 0.5]
[0.1388888888888889, 0.1388888888888889, 0.16666666666666666, 0.05555555555555555, 0.05555555555555555, 0.1388888888888889, 0.16666666666666666, 0.2222222222222222, 0.16666666666666666]
[0.1111111111111111, 0.05555555555555555, 0.0, 0.1111111111111111, 0.027777777777777776, 0.08333333333333333, 0.05555555555555555, 0.08333333333333333, 0.05555555555555555]
[0.8055555555555556, 0.8888888888888888, 0.7222222222222222, 0.8333333333333334, 0.75, 0.7777777777777778, 0.8333333333333334, 0.7222222222222222, 0.7777777777777778]
[0.3333333333333333, 0.5555555555555556, 0.5, 0.5277777777777778, 0.4722222222222222, 0.5, 0.4166666666666667, 0.5, 0.4722222222222222]
[0.1388888888888889, 0.1388888888888889, 0.16666666666666666, 0.05555555555555555, 0.05555555555555555, 0.1388888888888889, 0.16666666666666666, 0.2222222222222222, 0.16666666666666666, 0.19444444444444445]
[0.1111111111111111, 0.05555555555555555, 0.0, 0.1111111111111111, 0.027777777777777776, 0.08333333333333333, 0.05555555555555555, 0.08333333333333333, 0.05555555555555555, 0.16666666666666666]
[0.8055555555555556, 0.8888888888888888, 0.7222222222222222, 0.8333333333333334, 0.75, 0.7777777777777778, 0.8333333333333334, 0.7222222222222222, 0.7777777777777778, 0.8611111111111112]
[0.3333333333333333, 0.5555555555555556, 0.5, 0.5277777777777778, 0.4722222222222222, 0.5, 0.4166666666666667, 0.5, 0.4722222222222222, 0.4444444444444444]

In [13]:
for i,arr in enumerate([pK1,pK2,pK3,mK1,mK2,mK3,thipk1,thipk2,thipk3,thimk1,thimk2,thimk3]):
    if i%3==0:
        print(" ")
    print("mean %.3f std %.3f"%(np.mean(arr),np.std(arr)))


 
mean 0.386 std 0.103
mean 0.222 std 0.058
mean 0.144 std 0.051
 
mean 0.272 std 0.081
mean 0.158 std 0.062
mean 0.075 std 0.045
 
mean 0.947 std 0.029
mean 0.853 std 0.039
mean 0.797 std 0.054
 
mean 0.764 std 0.060
mean 0.558 std 0.047
mean 0.472 std 0.060

Exemplo de indexação table 5


In [13]:
def calcRanks1To6(train_histogram_dict,train_target,test_histogram_dict,test_target,nr=1):
    allTestRanked = []
    for test_id in test_histogram_dict:
        allDistances=[]
        for train_id in train_histogram_dict:
            d = dist.euclidean(train_histogram_dict[train_id], test_histogram_dict[test_id])
            allDistances.append((train_id,d,train_target[train_id]))
        allDistances = sorted(allDistances,key=lambda x:x[1],reverse=False)
        allTestRanked.append((test_id,[ x for i,(x,d,target) in enumerate(allDistances[:6])] ))
    return allTestRanked

In [14]:
print(datasets[-1])
np_images,target = getBaseAndTarget(datasets[-1],isTif18=True)

images_train, images_test, target_train, target_test = train_test_split(np_images, target, 
                                                                        test_size=len(set(target)), random_state=42,stratify=target)
images_train_cp = list(images_train)
images_test_cp = list(images_test)

print(len(images_train),len(images_test))


datasets\znovabase
360 36

In [17]:
train_histoP,test_histoP = computeHistogram(images_train,images_test,getHistogramP,math.log,filters.gaussian_laplace)
allTestRanked = calcRanks1To6(train_histoP,target_train,test_histoP,target_test)
testedOne = allTestRanked.pop(0)
trainImages = [image for i,image in enumerate(images_train_cp) if i  in testedOne[1]]
trainImages.append(images_test_cp[testedOne[0]])
plot_gallery('Base 5 teste Com metodo P',trainImages,6,2,trainImages[0].shape)



In [18]:
train_histoP,test_histoP = computeHistogram(images_train,images_test,getHistogramM,math.log,filters.gaussian_laplace)
allTestRanked = calcRanks1To6(train_histoP,target_train,test_histoP,target_test)
testedOne = allTestRanked.pop(0)
trainImages = [image for i,image in enumerate(images_train_cp) if i  in testedOne[1]]
trainImages.append(images_test_cp[testedOne[0]])
plot_gallery('Base 5 teste Com metodo M',trainImages,6,2,trainImages[0].shape)



In [20]:
train_histoP,test_histoP = computeHistogram(images_train,images_test,getHistogramThip,math.log,filters.gaussian_laplace)
allTestRanked = calcRanks1To6(train_histoP,target_train,test_histoP,target_test)
testedOne = allTestRanked.pop(0)
trainImages = [image for i,image in enumerate(images_train_cp) if i  in testedOne[1]]
trainImages.append(images_test_cp[testedOne[0]])
plot_gallery('Base 5 teste Com metodo thi p',trainImages,6,2,trainImages[0].shape)



In [21]:
train_histoP,test_histoP = computeHistogram(images_train,images_test,getHistogramThipM,math.log,filters.gaussian_laplace)
allTestRanked = calcRanks1To6(train_histoP,target_train,test_histoP,target_test)
testedOne = allTestRanked.pop(0)
trainImages = [image for i,image in enumerate(images_train_cp) if i  in testedOne[1]]
trainImages.append(images_test_cp[testedOne[0]])
plot_gallery('Base 5 teste Com metodo Thi m',trainImages,6,2,trainImages[0].shape)



In [ ]: