<reponame>houzeyu2683/NCKUCSIEIIR


import cv2
import pandas
import numpy
from skimage.metrics import structural_similarity

# result['Dft'] += [cv2.compareHist(objective['bin'], objective['bin'], cv2.NORM_L1)]

# edge_x = cv2.Sobel(src=x, ddepth=cv2.CV_64F, dx=1, dy=1, ksize=5)


# objective['image'] = cv2.imread(item['link'])
# objective['image'] = cv2.resize(objective['image'], (256, 256))
# blurred = cv2.GaussianBlur(gray, (5, 5), 0)
# canny = cv2.Canny(blurred, 30, 150)

'''
使用顏色的方法來生成長條讀，計算彼此相似程度，
由於不同的相似度評估方法產生的數值範圍都不太一樣，
將輸出結果轉換到 0 跟 1 之間，越像就是 1 ，反之。
'''
def compare(group, by):

    cache = {'by':by, "score":[]}
    for index, item in group.iterrows():
                
        if(index==0):

            objective = {}
            objective['image'] = cv2.imread(item['link'])
            objective['image'] = cv2.GaussianBlur(objective['image'], (5, 5), 0)
            objective['image'] = cv2.resize(objective['image'], (256, 256))
            objective['image'] = cv2.cvtColor(objective['image'], cv2.COLOR_BGR2GRAY)
            objective['image'] = numpy.float32(objective['image'])
            pass

            if(cache['by']=="DFT"):

                objective['texture'] = cv2.dft(src=objective['image'], dst=None, flags=cv2.DFT_COMPLEX_OUTPUT, nonzeroRows=None)
                objective['texture'] = numpy.fft.fftshift(objective['texture'])
                objective['texture'] = 10 * numpy.log(cv2.magnitude(objective['texture'][:, :, 0], objective['texture'][:, :, 1]))
                cache['score'] += [structural_similarity(objective['texture'], objective['texture'])]
                pass

            if(cache['by']=='Sobel'):

                objective['texture'] = cv2.Sobel(src=objective['image'], ddepth=cv2.CV_64F, dx=1, dy=1, ksize=5)
                cache['score'] += [structural_similarity(objective['texture'], objective['texture'])]
                pass

            if(cache['by']=='Canny'):

                objective['texture'] = cv2.Canny(numpy.uint8(objective['image']), 30, 150)
                cache['score'] += [structural_similarity(objective['texture'], objective['texture'])]
                pass            
            
            continue
        
        else:

            sample = {}
            sample['image'] = cv2.imread(item['link'])
            sample['image'] = cv2.GaussianBlur(sample['image'], (5, 5), 0)
            sample['image'] = cv2.resize(sample['image'], (256, 256))
            sample['image'] = cv2.cvtColor(sample['image'], cv2.COLOR_BGR2GRAY)
            sample['image'] = numpy.float32(sample['image'])
            pass

            if(cache['by']=="DFT"):

                sample['texture'] = cv2.dft(src=sample['image'], dst=None, flags=cv2.DFT_COMPLEX_OUTPUT, nonzeroRows=None)
                sample['texture'] = numpy.fft.fftshift(sample['texture'])
                sample['texture'] = 10 * numpy.log(cv2.magnitude(sample['texture'][:, :, 0], sample['texture'][:, :, 1]))
                cache['score'] += [structural_similarity(objective['texture'], sample['texture'])]
                pass

            if(cache['by']=='Sobel'):

                sample['texture'] = cv2.Sobel(src=sample['image'], ddepth=cv2.CV_64F, dx=1, dy=1, ksize=5)
                cache['score'] += [structural_similarity(objective['texture'], sample['texture'])]
                pass

            if(cache['by']=='Canny'):

                sample['texture'] = cv2.Canny(numpy.uint8(sample['image']), 30, 150)
                cache['score'] += [structural_similarity(objective['texture'], sample['texture'])]
                pass     
            pass

        pass
    
    group['by'] = cache['by']
    group['score'] = cache['score']
    output = group.sort_values(["score"], ascending=False).copy()
    return(output)















# # import cv2
# # import pandas
# # import numpy


# # '''
# # '''
# def compare(group):

#     result = {
#         "Dft":[],
#         "B":[]
#     }
#     for index, item in group.iterrows():

#         if(index==0):

#             objective = {}
#             objective['image'] = cv2.imread(item['link'])
#             objective['image'] = cv2.cvtColor(objective['image'], cv2.COLOR_BGR2GRAY)
#             objective['image'] = numpy.float32(objective['image'])
#             objective['texture'] = cv2.dft(src=objective['image'], dst=None, flags=cv2.DFT_COMPLEX_OUTPUT, nonzeroRows=None)
#             objective['texture'] = numpy.fft.fftshift(objective['texture'])
#             objective['texture'] = 10 * numpy.log(cv2.magnitude(objective['texture'][:, :, 0], objective['texture'][:, :, 1]))
#             objective['bin'] = cv2.calcHist([objective['texture']], [0], None, [128], [0, 256])
#             objective['bin'] = objective['bin']  / objective['bin'] .sum()
#             result['Dft'] += [cv2.compareHist(objective['bin'], objective['bin'], cv2.NORM_L1)]
#             continue
        
#         else:

#             sample = {}
#             sample['image'] = cv2.imread(item['link'])
#             sample['image'] = cv2.cvtColor(sample['image'], cv2.COLOR_BGR2GRAY)
#             sample['image'] = numpy.float32(sample['image'])
#             sample['texture'] = cv2.dft(src=sample['image'], dst=None, flags=cv2.DFT_COMPLEX_OUTPUT, nonzeroRows=None)
#             sample['texture'] = numpy.fft.fftshift(sample['texture'])
#             sample['texture'] = 10 * numpy.log(cv2.magnitude(sample['texture'][:, :, 0], sample['texture'][:, :, 1]))
#             sample['bin'] = cv2.calcHist([sample['texture']], [0], None, [128], [0, 256])
#             sample['bin'] = sample['bin']  / sample['bin'] .sum()
#             result['Dft'] += [cv2.compareHist(objective['bin'], sample['bin'], cv2.NORM_L1)]
#             pass

#         pass
    
#     # e ^ x / sum(e ^ x)
#     result = pandas.DataFrame(result)
#     output = pandas.concat([group, result], axis=1)
#     # output['L1'] = numpy.exp(output['L1']) / sum(numpy.exp(output['L1']))
#     return(output)

import cv2
import pandas
import numpy
from skimage.metrics import structural_similarity

# result['Dft'] += [cv2.compareHist(objective['bin'], objective['bin'], cv2.NORM_L1)]

# edge_x = cv2.Sobel(src=x, ddepth=cv2.CV_64F, dx=1, dy=1, ksize=5)


# objective['image'] = cv2.imread(item['link'])
# objective['image'] = cv2.resize(objective['image'], (256, 256))
# blurred = cv2.GaussianBlur(gray, (5, 5), 0)
# canny = cv2.Canny(blurred, 30, 150)

'''
使用顏色的方法來生成長條讀，計算彼此相似程度，
由於不同的相似度評估方法產生的數值範圍都不太一樣，
將輸出結果轉換到 0 跟 1 之間，越像就是 1 ，反之。
'''
def compare(group, by):

    cache = {'by':by, "score":[]}
    for index, item in group.iterrows():
                
        if(index==0):

            objective = {}
            objective['image'] = cv2.imread(item['link'])
            objective['image'] = cv2.GaussianBlur(objective['image'], (5, 5), 0)
            objective['image'] = cv2.resize(objective['image'], (256, 256))
            objective['image'] = cv2.cvtColor(objective['image'], cv2.COLOR_BGR2GRAY)
            objective['image'] = numpy.float32(objective['image'])
            pass

            if(cache['by']=="DFT"):

                objective['texture'] = cv2.dft(src=objective['image'], dst=None, flags=cv2.DFT_COMPLEX_OUTPUT, nonzeroRows=None)
                objective['texture'] = numpy.fft.fftshift(objective['texture'])
                objective['texture'] = 10 * numpy.log(cv2.magnitude(objective['texture'][:, :, 0], objective['texture'][:, :, 1]))
                cache['score'] += [structural_similarity(objective['texture'], objective['texture'])]
                pass

            if(cache['by']=='Sobel'):

                objective['texture'] = cv2.Sobel(src=objective['image'], ddepth=cv2.CV_64F, dx=1, dy=1, ksize=5)
                cache['score'] += [structural_similarity(objective['texture'], objective['texture'])]
                pass

            if(cache['by']=='Canny'):

                objective['texture'] = cv2.Canny(numpy.uint8(objective['image']), 30, 150)
                cache['score'] += [structural_similarity(objective['texture'], objective['texture'])]
                pass            
            
            continue
        
        else:

            sample = {}
            sample['image'] = cv2.imread(item['link'])
            sample['image'] = cv2.GaussianBlur(sample['image'], (5, 5), 0)
            sample['image'] = cv2.resize(sample['image'], (256, 256))
            sample['image'] = cv2.cvtColor(sample['image'], cv2.COLOR_BGR2GRAY)
            sample['image'] = numpy.float32(sample['image'])
            pass

            if(cache['by']=="DFT"):

                sample['texture'] = cv2.dft(src=sample['image'], dst=None, flags=cv2.DFT_COMPLEX_OUTPUT, nonzeroRows=None)
                sample['texture'] = numpy.fft.fftshift(sample['texture'])
                sample['texture'] = 10 * numpy.log(cv2.magnitude(sample['texture'][:, :, 0], sample['texture'][:, :, 1]))
                cache['score'] += [structural_similarity(objective['texture'], sample['texture'])]
                pass

            if(cache['by']=='Sobel'):

                sample['texture'] = cv2.Sobel(src=sample['image'], ddepth=cv2.CV_64F, dx=1, dy=1, ksize=5)
                cache['score'] += [structural_similarity(objective['texture'], sample['texture'])]
                pass

            if(cache['by']=='Canny'):

                sample['texture'] = cv2.Canny(numpy.uint8(sample['image']), 30, 150)
                cache['score'] += [structural_similarity(objective['texture'], sample['texture'])]
                pass     
            pass

        pass
    
    group['by'] = cache['by']
    group['score'] = cache['score']
    output = group.sort_values(["score"], ascending=False).copy()
    return(output)















# # import cv2
# # import pandas
# # import numpy


# # '''
# # '''
# def compare(group):

#     result = {
#         "Dft":[],
#         "B":[]
#     }
#     for index, item in group.iterrows():

#         if(index==0):

#             objective = {}
#             objective['image'] = cv2.imread(item['link'])
#             objective['image'] = cv2.cvtColor(objective['image'], cv2.COLOR_BGR2GRAY)
#             objective['image'] = numpy.float32(objective['image'])
#             objective['texture'] = cv2.dft(src=objective['image'], dst=None, flags=cv2.DFT_COMPLEX_OUTPUT, nonzeroRows=None)
#             objective['texture'] = numpy.fft.fftshift(objective['texture'])
#             objective['texture'] = 10 * numpy.log(cv2.magnitude(objective['texture'][:, :, 0], objective['texture'][:, :, 1]))
#             objective['bin'] = cv2.calcHist([objective['texture']], [0], None, [128], [0, 256])
#             objective['bin'] = objective['bin']  / objective['bin'] .sum()
#             result['Dft'] += [cv2.compareHist(objective['bin'], objective['bin'], cv2.NORM_L1)]
#             continue
        
#         else:

#             sample = {}
#             sample['image'] = cv2.imread(item['link'])
#             sample['image'] = cv2.cvtColor(sample['image'], cv2.COLOR_BGR2GRAY)
#             sample['image'] = numpy.float32(sample['image'])
#             sample['texture'] = cv2.dft(src=sample['image'], dst=None, flags=cv2.DFT_COMPLEX_OUTPUT, nonzeroRows=None)
#             sample['texture'] = numpy.fft.fftshift(sample['texture'])
#             sample['texture'] = 10 * numpy.log(cv2.magnitude(sample['texture'][:, :, 0], sample['texture'][:, :, 1]))
#             sample['bin'] = cv2.calcHist([sample['texture']], [0], None, [128], [0, 256])
#             sample['bin'] = sample['bin']  / sample['bin'] .sum()
#             result['Dft'] += [cv2.compareHist(objective['bin'], sample['bin'], cv2.NORM_L1)]
#             pass

#         pass
    
#     # e ^ x / sum(e ^ x)
#     result = pandas.DataFrame(result)
#     output = pandas.concat([group, result], axis=1)
#     # output['L1'] = numpy.exp(output['L1']) / sum(numpy.exp(output['L1']))
#     return(output)
# result['Dft'] += [cv2.compareHist(objective['bin'], objective['bin'], cv2.NORM_L1)] # edge_x = cv2.Sobel(src=x, ddepth=cv2.CV_64F, dx=1, dy=1, ksize=5) # objective['image'] = cv2.imread(item['link']) # objective['image'] = cv2.resize(objective['image'], (256, 256)) # blurred = cv2.GaussianBlur(gray, (5, 5), 0) # canny = cv2.Canny(blurred, 30, 150) 使用顏色的方法來生成長條讀，計算彼此相似程度， 由於不同的相似度評估方法產生的數值範圍都不太一樣， 將輸出結果轉換到 0 跟 1 之間，越像就是 1 ，反之。 # # import cv2 # # import pandas # # import numpy # # ''' # # ''' # def compare(group): #     result = { #         "Dft":[], #         "B":[] #     } #     for index, item in group.iterrows(): #         if(index==0): #             objective = {} #             objective['image'] = cv2.imread(item['link']) #             objective['image'] = cv2.cvtColor(objective['image'], cv2.COLOR_BGR2GRAY) #             objective['image'] = numpy.float32(objective['image']) #             objective['texture'] = cv2.dft(src=objective['image'], dst=None, flags=cv2.DFT_COMPLEX_OUTPUT, nonzeroRows=None) #             objective['texture'] = numpy.fft.fftshift(objective['texture']) #             objective['texture'] = 10 * numpy.log(cv2.magnitude(objective['texture'][:, :, 0], objective['texture'][:, :, 1])) #             objective['bin'] = cv2.calcHist([objective['texture']], [0], None, [128], [0, 256]) #             objective['bin'] = objective['bin']  / objective['bin'] .sum() #             result['Dft'] += [cv2.compareHist(objective['bin'], objective['bin'], cv2.NORM_L1)] #             continue #         else: #             sample = {} #             sample['image'] = cv2.imread(item['link']) #             sample['image'] = cv2.cvtColor(sample['image'], cv2.COLOR_BGR2GRAY) #             sample['image'] = numpy.float32(sample['image']) #             sample['texture'] = cv2.dft(src=sample['image'], dst=None, flags=cv2.DFT_COMPLEX_OUTPUT, nonzeroRows=None) #             sample['texture'] = numpy.fft.fftshift(sample['texture']) #             sample['texture'] = 10 * numpy.log(cv2.magnitude(sample['texture'][:, :, 0], sample['texture'][:, :, 1])) #             sample['bin'] = cv2.calcHist([sample['texture']], [0], None, [128], [0, 256]) #             sample['bin'] = sample['bin']  / sample['bin'] .sum() #             result['Dft'] += [cv2.compareHist(objective['bin'], sample['bin'], cv2.NORM_L1)] #             pass #         pass #     # e ^ x / sum(e ^ x) #     result = pandas.DataFrame(result) #     output = pandas.concat([group, result], axis=1) #     # output['L1'] = numpy.exp(output['L1']) / sum(numpy.exp(output['L1'])) #     return(output)