def search(self, image1_pixels, x_start, y_start, image2_pixels): self.reset_search() block_size = self.block_size pass_step = self.pass_step subimage_1_pixels = ImageConverter.sub_pixels(image1_pixels, x_start, y_start, x_start+block_size, y_start+block_size) p = pass_step ds = p/2 s = 1 xs = x_start ys = y_start xs_min = -1 ys_min = -1 best_local_MAD = 10000 best_global_MAD = 10000 best_x = -1 best_y = -1 klog("Check block from %d, %d" %(x_start, y_start)) while ds >= 1: for x in [xs, xs+ds, xs-ds]: for y in [ys, ys+ds, ys-ds]: if not ImageComparator.is_valid_coordinate(x, y, block_size, image2_pixels): continue MAD = self.calculate_MAD(subimage_1_pixels, image2_pixels, x, y, x+block_size, y+block_size) if MAD < best_local_MAD: best_local_MAD = MAD xs_min = x ys_min = y #Check if the local MAD is the best global MAD if MAD < best_global_MAD: best_global_MAD = MAD best_x = x best_y = y s += 1 ds -= 1 xs = xs_min ys = ys_min print "-" return best_x, best_y, best_global_MAD, self._MAD_checks_count
def search(self, image1_pixels, x_start, y_start, image2_pixels): self.reset_search() block_size = self.block_size pass_step = self.pass_step #subimage_1 = image1.copy(x_start, y_start, block_size, block_size) subimage_1_pixels = ImageConverter.sub_pixels(image1_pixels, x_start, y_start, x_start+block_size, y_start+block_size) p = pass_step ds = math.pow(2, math.floor( math.log(p,2))-1 ) s = 1 xs = x_start ys = y_start xs_min = -1 ys_min = -1 best_local_MAD = 10000 best_global_MAD = 10000 best_x = -1 best_y = -1 while ds >= 1: if ds != 1: for (x,y) in [(xs, ys), (xs-ds,ys), (xs+ds, ys), (xs, ys+ds), (xs,ys-ds)]: if not ImageComparator.is_valid_coordinate(x, y, block_size, image2_pixels): continue MAD = self.calculate_MAD(subimage_1_pixels, image2_pixels, x, y, x+block_size, y+block_size) if MAD < best_local_MAD: best_local_MAD = MAD xs_min = x ys_min = y #Check if the local MAD is the best global MAD if MAD < best_global_MAD: best_global_MAD = MAD best_x = x best_y = y s += 1 #Check if the xs_min and ys_min are the central point of the fives checked if xs_min == xs and ys_min == ys: ds /= 2 xs = xs_min ys = ys_min else: #ds==1 last step for x in [xs, xs+ds, xs-ds]: for y in [ys, ys+ds, ys-ds]: #TODO: CODE HERE IS COPY-PASTED!! THIS SUCKS! secondo me va bene! if not ImageComparator.is_valid_coordinate(x, y, block_size, image2_pixels): continue MAD = self.calculate_MAD(subimage_1_pixels, image2_pixels, x, y, x+block_size, y+block_size) if MAD < best_local_MAD: best_local_MAD = MAD xs_min = x ys_min = y #Check if the local MAD is the best global MAD if MAD < best_global_MAD: best_global_MAD = MAD best_x = x best_y = y ds = 0 s += 1 print "ds: %d" %ds return best_x, best_y, best_global_MAD, self._MAD_checks_count
def search(self, image1_pixels, x_start, y_start, image2_pixels): self.reset_search() block_size = self.block_size pass_step = self.pass_step subimage_1_pixels = ImageConverter.sub_pixels(image1_pixels, x_start, y_start, x_start+block_size, y_start+block_size) p = pass_step ds = p/2+1 s = 1 xs = x_start ys = y_start best_local_MAD = 10000 xs_min = -1 ys_min = -1 best_local_a_MAD = 10000 xs_a_min = -1 ys_a_min = -1 best_global_MAD = 10000 best_x = -1 best_y = -1 #klog("Check block from %d, %d" %(x_start, y_start)) while True: for (x,y) in [ (xs,ys), (xs-ds,ys), (xs+ds,ys) ]: if not ImageComparator.is_valid_coordinate(x, y, block_size, image2_pixels): continue MAD = self.calculate_MAD(subimage_1_pixels, image2_pixels, x, y, x+block_size, y+block_size) if MAD < best_local_a_MAD: best_local_a_MAD = MAD xs_a_min = x ys_a_min = y #Check if the local MAD is the best global MAD if MAD < best_global_MAD: best_global_MAD = MAD best_x = x best_y = y for (x,y) in [ (xs_a_min,ys_a_min), (xs_a_min,ys_a_min-ds), (xs_a_min, ys_a_min+ds) ]: if not ImageComparator.is_valid_coordinate(x, y, block_size, image2_pixels): continue #Calculate the MAD MAD = self.calculate_MAD(subimage_1_pixels, image2_pixels, x, y, x+block_size, y+block_size) if MAD < best_local_MAD: best_local_MAD = MAD xs_min = x ys_min = y #Check if the local MAD is the best global MAD if MAD < best_global_MAD: best_global_MAD = MAD best_x = x best_y = y if ds == 1: break s += 1 ds = math.ceil( ds/2 ) # klog("ds: %d" %(ds)) xs = xs_min ys = ys_min best_local_MAD = 10000 #reset best_local_a_MAD = 10000 #reset #klog("-") #klog("MADs check count: %d" %MAD_checks_count) return best_x, best_y, best_global_MAD, self._MAD_checks_count
def search(self, image1_pixels, x_start, y_start, image2_pixels): self.reset_search() block_size = self.block_size margin_size = self.margin_size best_MAD = 1000000 best_x = None best_y = None subimage_1_pixels = ImageConverter.sub_pixels(image1_pixels, x_start, y_start, x_start+block_size, y_start+block_size) #Start with the center if ImageComparator.is_valid_coordinate(x_start, y_start, block_size, image2_pixels): MAD = self.calculate_MAD(subimage_1_pixels, image2_pixels, x_start, y_start, x_start+block_size, y_start+block_size) if MAD < best_MAD: #klog("Best MAD found: %f, at (%f,%f)" % (MAD, px, py)) best_MAD = MAD best_x = x_start best_y = y_start if best_MAD == 0: return best_x, best_y, best_MAD, self._MAD_checks_count for py in range(y_start-margin_size, y_start+margin_size): if py < 0: continue #il blocco esce in su dall'immagine, avanza con il prossimo py incrementato #CHECK!! if not ImageComparator.is_valid_coordinate(0, py, block_size, image2_pixels): break #il blocco esce in giu dall'immagine, esci for px in range(x_start-margin_size, x_start+margin_size): if px < 0: continue #il blocco esce a sinistra dall'immagine, avanza con il prossimo px incrementato #CHECK!! if not ImageComparator.is_valid_coordinate(px, py, block_size, image2_pixels): break #il blocco esce in giu dall'immagine, esci #if px+block_size > image2.width(): # break #il blocco esce a destra dall'immagine, esci #klog("Valuating block (%f,%f)" %(px, py)) MAD = self.calculate_MAD(subimage_1_pixels, image2_pixels, px, py, px+block_size, py+block_size) if MAD < best_MAD: #klog("Best MAD found: %f, at (%f,%f)" % (MAD, px, py)) best_MAD = MAD best_x = px best_y = py if best_MAD == 0: return best_x, best_y, best_MAD, self._MAD_checks_count return best_x, best_y, best_MAD, self._MAD_checks_count