def func(gam, prevgamma, prevdiff, sampling, rhatnorm): while(gam < 10): r_gam = tgamma(2/gam) * tgamma(2/gam) / (tgamma(1/gam) * tgamma(3/gam)) diff = abs(r_gam - rhatnorm) if(diff > prevdiff): break prevdiff = diff prevgamma = gam gam += sampling gamma_best = prevgamma return gamma_best
def compute_features(img): scalenum = 2 feat = [] # make a copy of the image im_original = img.copy() # scale the images twice for itr_scale in range(scalenum): im = im_original.copy() # normalize the image im = im / 255.0 # calculating MSCN coefficients mu = cv2.GaussianBlur(im, (7, 7), 1.166) mu_sq = mu * mu sigma = cv2.GaussianBlur(im*im, (7, 7), 1.166) sigma = (sigma - mu_sq)**0.5 # structdis is the MSCN image structdis = im - mu structdis /= (sigma + 1.0/255) # calculate best fitted parameters from MSCN image best_fit_params = AGGDfit(structdis) # unwrap the best fit parameters lsigma_best = best_fit_params[0] rsigma_best = best_fit_params[1] gamma_best = best_fit_params[2] # append the best fit parameters for MSCN image feat.append(gamma_best) feat.append((lsigma_best*lsigma_best + rsigma_best*rsigma_best)/2) # shifting indices for creating pair-wise products shifts = [[0,1], [1,0], [1,1], [-1,1]] # H V D1 D2 for itr_shift in range(1, len(shifts) + 1): OrigArr = structdis reqshift = shifts[itr_shift-1] # shifting index # create transformation matrix for warpAffine function M = np.float32([[1, 0, reqshift[1]], [0, 1, reqshift[0]]]) ShiftArr = cv2.warpAffine(OrigArr, M, (structdis.shape[1], structdis.shape[0])) Shifted_new_structdis = ShiftArr Shifted_new_structdis = Shifted_new_structdis * structdis # shifted_new_structdis is the pairwise product # best fit the pairwise product best_fit_params = AGGDfit(Shifted_new_structdis) lsigma_best = best_fit_params[0] rsigma_best = best_fit_params[1] gamma_best = best_fit_params[2] constant = m.pow(tgamma(1/gamma_best), 0.5)/m.pow(tgamma(3/gamma_best), 0.5) meanparam = (rsigma_best - lsigma_best) * (tgamma(2/gamma_best)/tgamma(1/gamma_best)) * constant # append the best fit calculated parameters feat.append(gamma_best) # gamma best feat.append(meanparam) # mean shape feat.append(m.pow(lsigma_best, 2)) # left variance square feat.append(m.pow(rsigma_best, 2)) # right variance square # resize the image on next iteration im_original = cv2.resize(im_original, (0,0), fx=0.5, fy=0.5, interpolation=cv2.INTER_CUBIC) return feat
def gamma(cls, arg): assert arg > 0.0 return tgamma(arg)