(img, lmax=5)
return lmaxStr + maxValStr + asstr
Create spherical harmonic transform (SHT) hash preview.
Create spherical harmonic transform (SHT) hash preview.
Create spherical harmonic transform ( SHT ) hash preview .
def img2shtHash(img, lmax=5):
    '''
    Create spherical harmonic transform (SHT) hash preview.
    '''
    def encodeFloat(f, maxVal):
        return np.maximum(0, np.minimum(2 * maxVal, np.round(np.sign(f) * np.sqrt(np.abs(f)) * maxVal + maxVal))).astype(int)

    def encodeCoeff(r, g, b, maxVal):
        quantR = encodeFloat(r / maxVal, 9)
        quantG = encodeFloat(g / maxVal, 9)
        quantB = encodeFloat(b / maxVal, 9)
        return quantR * 19 ** 2 + quantG * 19 + quantB

    b83chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz#$%*+,-.:;=?@[]^_{|}~"

    def b83encode(vals, length):
        result = ""
        for val in vals:
            for i in range(1, length + 1):
                result += b83chars[int(val // (83 ** (length - i))) % 83]
        return result

    # Calculate SHT coefficients
    r = pysh.expand.SHExpandDH(img[..., 0], sampling=2, lmax_calc=lmax)
    g = pysh.expand.SHExpandDH(img[..., 1], sampling=2, lmax_calc=lmax)
    b = pysh.expand.SHExpandDH(img[..., 2], sampling=2, lmax_calc=lmax)

    # Remove values above diagonal for both sine and cosine components
    # Also remove first row and column for sine component
    # These values are always zero
    r = np.append(r[0][np.tril_indices(lmax + 1)], r[1, 1:, 1:][np.tril_indices(lmax)])
    g = np.append(g[0][np.tril_indices(lmax + 1)], g[1, 1:, 1:][np.tril_indices(lmax)])
    b = np.append(b[0][np.tril_indices(lmax + 1)], b[1, 1:, 1:][np.tril_indices(lmax)])

    # Encode as string
    maxVal = np.max([np.max(r), np.max(b), np.max(g)])
    vals = encodeCoeff(r, g, b, maxVal).flatten()
    asstr = b83encode(vals, 2)
    lmaxStr = b83encode([lmax], 1)
    maxValStr = b83encode(encodeFloat([2 * maxVal / 255 - 1], 41), 1)
    return lmaxStr + maxValStr + asstr
def img2shtHash ( img , lmax = 5 ) : def encodeFloat ( f , maxVal ) : return np . maximum ( 0 , np . minimum ( 2 * maxVal , np . round ( np . sign ( f ) * np . sqrt ( np . abs ( f ) ) * maxVal + maxVal ) ) ) . astype ( int ) def encodeCoeff ( r , g , b , maxVal ) : quantR = encodeFloat ( r / maxVal , 9 ) quantG = encodeFloat ( g / maxVal , 9 ) quantB = encodeFloat ( b / maxVal , 9 ) return quantR * 19 ** 2 + quantG * 19 + quantB b83chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz#$%*+,-.:;=?@[]^_{|}~" def b83encode ( vals , length ) : result = "" for val in vals : for i in range ( 1 , length + 1 ) : result += b83chars [ int ( val // ( 83 ** ( length - i ) ) ) % 83 ] return result # Calculate SHT coefficients r = pysh . expand . SHExpandDH ( img [ ... , 0 ] , sampling = 2 , lmax_calc = lmax ) g = pysh . expand . SHExpandDH ( img [ ... , 1 ] , sampling = 2 , lmax_calc = lmax ) b = pysh . expand . SHExpandDH ( img [ ... , 2 ] , sampling = 2 , lmax_calc = lmax ) # Remove values above diagonal for both sine and cosine components # Also remove first row and column for sine component # These values are always zero r = np . append ( r [ 0 ] [ np . tril_indices ( lmax + 1 ) ] , r [ 1 , 1 : , 1 : ] [ np . tril_indices ( lmax ) ] ) g = np . append ( g [ 0 ] [ np . tril_indices ( lmax + 1 ) ] , g [ 1 , 1 : , 1 : ] [ np . tril_indices ( lmax ) ] ) b = np . append ( b [ 0 ] [ np . tril_indices ( lmax + 1 ) ] , b [ 1 , 1 : , 1 : ] [ np . tril_indices ( lmax ) ] ) # Encode as string maxVal = np . max ( [ np . max ( r ) , np . max ( b ) , np . max ( g ) ] ) vals = encodeCoeff ( r , g , b , maxVal ) . flatten ( ) asstr = b83encode ( vals , 2 ) lmaxStr = b83encode ( [ lmax ] , 1 ) maxValStr = b83encode ( encodeFloat ( [ 2 * maxVal / 255 - 1 ] , 41 ) , 1 ) return lmaxStr + maxValStr + asstr