def detect_irregular_letter(letter): letter = prepare_image_for_correlation(letter) letter_i = prepare_image_for_correlation( Image.open(SAVED_LETTERS_DIR + 'i.png')) letter_q = prepare_image_for_correlation( Image.open(SAVED_LETTERS_DIR + 'q.png')) letter_n = prepare_image_for_correlation( Image.open(SAVED_LETTERS_DIR + 'n.png')) letter_enie = prepare_image_for_correlation( Image.open(SAVED_LETTERS_DIR + 'enie.png')) candidates = {} candidates['I'] = letter_i candidates['Q'] = letter_q candidates['N'] = letter_n candidates['Ñ'] = letter_enie current_candidate = None max_correlation = 0 for candidate_letter, candidate_letter_array in candidates.iteritems(): correlation = correlate2d(letter, candidate_letter_array, mode='same').max() if correlation > max_correlation: max_correlation = correlation current_candidate = candidate_letter return current_candidate
def estimateCoarseShift(): from scipy import floor global testImage, refImage, xshiftVar, yshiftVar global sampleBoxVar text = sampleBoxVar.get() x0, y0, x1, y1 = text.split() sampleBox = tuple([int(x0), int(y0), int(x1), int(y1)]) print "sampleBox=", sampleBox refSmallImage = refImage.crop(sampleBox).convert('L') testSmallImage = testImage.crop(sampleBox).convert('L') # refSmallImage.show(); testSmallImage.show() xsize, ysize = refSmallImage.size # Convert to numpy arrays and do cross correlation. refData = numpy.array(refSmallImage.getdata()) refData.shape = refSmallImage.size testData = numpy.array(testSmallImage.getdata()) testData.shape = testSmallImage.size try: # Requires Scipy. corrData = correlate2d(refData, testData) # print "corrData=", corrData ibig, jbig, corrbig = locateMaximum(corrData) # Might it be better to shift by a fraction of a pixel? # This is possible by doing bilinear interpolation, say. print "Max correlation=", corrbig, " at i=", ibig, "j=", jbig xshift, yshift = xsize - 1 - ibig, ysize - 1 - jbig # xshift, yshift = ibig - xsize/2 + 1, jbig - ysize/2 + 1 except: xshift, yshift = 0, 0 xshiftVar.set(xshift) yshiftVar.set(yshift) return
def corrsim(img, tgt): """ A cross correlation image similarity metric Parameters: img - the input image tgt - the target image for comparison Returns a scalar metric for the similarity between images """ # Normalize images normi = normalize(img) normt = normalize(tgt) # Cross correlation xcor = np.max(correlate2d(normi, normt, mode='same')) # Autocorrelations icor = np.max(correlate2d(normi, normi, mode='same')) tcor = np.max(correlate2d(normt, normt, mode='same')) # Similarity metric return xcor / np.sqrt(icor * tcor)
def calc_gradients(self): """ Calculate the gradients for the kernels of this layer. """ self.gradients = np.zeros((self.num_prev_maps, self.num_maps, self.kernel_size, self.kernel_size)) for fm_idx in range(self.num_maps): for prev_fm_idx in range(self.num_prev_maps): prev_fm_output = self.inputs[prev_fm_idx] fm_delta = self.deltas[fm_idx] kernel = self.weights[prev_fm_idx, fm_idx] # the gradient is the product of the delta and the activation. # However, here all pixels influenced by a weight have to be # considered. fm_gradient = nputils.rot180(signal.correlate2d(prev_fm_output, fm_delta, mode='valid')) self.gradients[prev_fm_idx, fm_idx] = fm_gradient
def correlate2d(self): """So f*****g slow :return: float """ import scipy as sp from scipy.misc import imread from scipy.signal.signaltools import correlate2d def get(path): data = imread(path) data = sp.inner(data, [299, 587, 114]) / 1000.0 return (data - data.mean()) / data.std() value = correlate2d(get(self.image_a_path), get(self.image_b_path)).max() return value
def correlate2d(self): """ : So f*****g slow : return: float """ import scipy as sp from scipy.misc import imread from scipy.signal.signaltools import correlate2d def get(path): data = imread(path) data = sp.inner(data, [299, 587, 114]) / 1000.0 return (data - data.mean()) / data.std() value = correlate2d(get(self.image_a_path), get(self.image_b_path)).max() return value
def domain_time_compute(): """ time a spatial domain convolution for 10-by-10 x 20-by-20 matrices """ if domain_time_compute.K is None: AS = 10 BS = 40 a = scipy.ones((AS, AS)) b = scipy.ones((BS, BS)) mintime = 0.5 k = 0 t1 = time.clock() for i in range(100): c = correlate2d(a, b, mode='same') t2 = time.clock() t_total = (t2 - t1) / 100 # convolution time = K*prod(size(a))*prod(size(b)) # t_total = K*AS*AS*BS*BS = 40000*K domain_time_compute.K = t_total / (AS * AS * BS * BS) return domain_time_compute.K
def domain_time_compute(): """ time a spatial domain convolution for 10-by-10 x 20-by-20 matrices """ if domain_time_compute.K is None: AS = 10 BS = 40 a = scipy.ones((AS,AS)) b = scipy.ones((BS,BS)) mintime = 0.5 k = 0 t1 = time.clock() for i in range(100): c = correlate2d(a,b,mode='same') t2 = time.clock() t_total = (t2-t1)/100 # convolution time = K*prod(size(a))*prod(size(b)) # t_total = K*AS*AS*BS*BS = 40000*K domain_time_compute.K = t_total/(AS*AS*BS*BS) return domain_time_compute.K
def backpropagate(self, error): """ Backpropagation based on given error. :param error: Error for this layer (backpropagated error) :return Error of the previous layer """ if self.outputs is None: raise ValueError("Feedforward has to be performed before backpropagating!") out_size = np.shape(self.outputs[0]) # has the same size as the input for each previous feature map backprop_error = np.zeros((self.num_prev_maps, out_size[0] + self.kernel_size - 1, out_size[1] + self.kernel_size - 1)) self.deltas = np.zeros((self.num_maps, out_size[0], out_size[1])) # calculate deltas for this layer for fm_idx in range(self.num_maps): fm_error = error[fm_idx] # calculate deltas for feature map # supposing that the derivation function takes the function value as # input derived_input = self.deriv_activation_func(self.outputs[fm_idx]) self.deltas[fm_idx] = fm_error * derived_input # calculate errors for previous layer's feature maps: cross-correlate # each feature map's delta with the connection's kernel, the sum over # all these correlations (actually only those that have a connection to # the previous feature map, here: fully connected) is the delta for the # feature map in the previous layer for prev_fm_idx in range(self.num_prev_maps): for fm_idx in range(self.num_maps): # correlate delta with kernel using 'full' mode, to obtain the # error for the feature map in the previous layer kernel = self.weights[prev_fm_idx, fm_idx] # 'full' mode pads the input on all sides with zeros increasing # the overall size of the input by kernel_size-1 in both # dimensions ( (kernel_size-1)/2 on each side) fm_error = signal.correlate2d(self.deltas[fm_idx], kernel, mode='full') backprop_error[prev_fm_idx] += fm_error return backprop_error
def detect_irregular_letter(letter): letter = prepare_image_for_correlation(letter) letter_i = prepare_image_for_correlation(Image.open(SAVED_LETTERS_DIR+'i.png')) letter_q = prepare_image_for_correlation(Image.open(SAVED_LETTERS_DIR+'q.png')) letter_n = prepare_image_for_correlation(Image.open(SAVED_LETTERS_DIR+'n.png')) letter_enie = prepare_image_for_correlation(Image.open(SAVED_LETTERS_DIR+'enie.png')) candidates = {} candidates['I'] = letter_i candidates['Q'] = letter_q candidates['N'] = letter_n candidates['Ñ'] = letter_enie current_candidate = None max_correlation = 0 for candidate_letter,candidate_letter_array in candidates.iteritems(): correlation = correlate2d(letter, candidate_letter_array, mode='same').max() if correlation > max_correlation: max_correlation = correlation current_candidate = candidate_letter return current_candidate
def soc(res): for i in range(len(res)): soccers.append(correlate2d(res[0], res[i], mode='same')) return soccers
def compare_data(d1, d2): return correlate2d(d1, d2, mode='same').max()
def corr_score(array1, array2): return correlate2d(array1, array2, mode="same").max()