def matchSections(): pitches = cPickle.load(open('AmuInstPitches.pkl')) sections = cPickle.load(open('AmuInstSections.pkl')) filenames = cPickle.load(open('AmuFilenames.pkl')) keys = cPickle.load(open('AmuKeys.pkl')) for i in range(len(pitches)/2): pyplot.figure(i,(16,9)) newp = [] for vector in pitches[2*i]: deq = deque(vector) deq.rotate(keys[2*i+1]-keys[2*i]) l = list(deq) newp.append(l) pitches[2*i] = newp image = numpy.array(pitches[2*i]) template = numpy.array(pitches[2*i+1][sections[2*i+1][0]:sections[2*i+1][1]]) im = feature.match_template(image,template,pad_input=True) pyplot.vlines(12,0,im.shape[0],'b') for j in range(len(sections[2*i+1])-2): template = numpy.array(pitches[2*i+1][sections[2*i+1][j+1]:sections[2*i+1][j+2]]) temp = feature.match_template(image,template,pad_input=True) im = numpy.concatenate((im,temp),axis = 1) pyplot.vlines(12*j+12,0,im.shape[0],'b') ij = numpy.unravel_index(numpy.argmax(im), im.shape) x, y = ij[::-1] pyplot.imshow(im, cmap = pyplot.get_cmap('gray'), aspect = 'auto', origin = 'lower') pyplot.ylabel(os.path.basename(filenames[2*i]) + " (in beats)") pyplot.xlabel(os.path.basename(filenames[2*i+1]) + " (12 Chroma Values Each)") pyplot.title('Section Similarity') pyplot.plot(x,y,'o',markeredgecolor='r',markerfacecolor='none',markersize=10) pyplot.xlim(0,im.shape[1]-1) pyplot.ylim(0,im.shape[0]) pyplot.show() sys.exit()
def match_digit(image,templates,min_w=24,is_two_digits=True,debug=False): h,w = image.shape if is_two_digits: #first digit should be: 1,2,3,4,5 ret = [] wlimit = max(w/2,min_w) for digit in [1,2,3,4,5]: result = match_template(image[:,:wlimit], templates[digit]) max_correl = np.max(result) ret += [(max_correl,digit)] if debug: print "digit1:",digit,max_correl #second digit should be any except if first digit is 5. That case options are 0,1,2,3,4 if len(ret) > 0: ret.sort() #best digit is the last correl1,digit1 = ret[-1] else: return None,None digits = range(0,10) if digit1 != 5 else [0,1,2,3,4] ret = [] for digit in digits: result = match_template(image[:,w - wlimit:], templates[digit]) max_correl = np.max(result) ret += [(max_correl,digit)] if debug: print "digit2:",digit,max_correl if len(ret) > 0: ret.sort() #best digit is the last correl2,digit2 = ret[-1] else: return None,None return (correl1,digit1),(correl2,digit2) else: #assert h >= 20, "problem with w {0}:{1}".format(h,20) #assert w >= 18, "problem with h {0}:{1}".format(w,18) ret = [] #one digit: 6,7,8,9 for digit in [6,7,8,9]: result = match_template(image, templates[digit]) max_correl = np.max(result) if debug: print "digit1:",digit,max_correl ret += [(max_correl,digit)] if len(ret) > 0: ret.sort() #best digit is the last return ret[-1],None
def find_bulb(image, templ): """finds the terminal bulb in an image using template correlation. Finds the best location (shifted cross-correlation) between image and template return: location (x,y) and correlation value at the maximal correlation. """ image = ndimage.gaussian_filter(image, 2) #- ndimage.gaussian_filter(res, 50) cut = int(0.1*image.shape[1]) result = match_template(image, templ) xm = int(result.shape[1]/2.) res = result[:,max(0,-cut + xm):xm+cut] ij = np.unravel_index(np.argmax(res), res.shape) x0, y0 = ij[::-1] # calculate half template size t_half = int(templ.shape[0]/2.) conf = res[y0,x0] result1 = match_template(image, templ[t_half:,]) res1 = result1[:,max(0,-cut + xm):xm+cut] ij = np.unravel_index(np.argmax(res1), res1.shape) x1, y1 = ij[::-1] conf1 = res1[y1,x1] if conf1 > conf: conf = conf1 x0,y0 = x1,y1 res = res1 t_half = int(templ.shape[0]/4.) result2 = match_template(image, templ[:t_half,]) res2 = result2[:,max(0,-cut + xm):xm+cut] ij = np.unravel_index(np.argmax(res2), res2.shape) x2, y2 = ij[::-1] conf2 = res2[y2,x2] if conf2 > conf: conf = conf2 x0,y0 = x2,y2 res = res2 t_half = int(templ.shape[0])/4. x = max(0, min(x0+templ.shape[1]/2.+cut, image.shape[1]-1)) y = max(0,min(y0+t_half, image.shape[0]-1)) if conf < 0.4 or conf/np.std(res) < 2.5: conf = 0.0 return y,x, conf
def test_pad_input(): """Test `match_template` when `pad_input=True`. This test places two full templates (one with values lower than the image mean, the other higher) and two half templates, which are on the edges of the image. The two full templates should score the top (positive and negative) matches and the centers of the half templates should score 2nd. """ # Float prefactors ensure that image range is between 0 and 1 template = 0.5 * diamond(2) image = 0.5 * np.ones((9, 19)) mid = slice(2, 7) image[mid, :3] -= template[:, -3:] # half min template centered at 0 image[mid, 4:9] += template # full max template centered at 6 image[mid, -9:-4] -= template # full min template centered at 12 image[mid, -3:] += template[:, :3] # half max template centered at 18 result = match_template(image, template, pad_input=True, constant_values=image.mean()) # get the max and min results. sorted_result = np.argsort(result.flat) i, j = np.unravel_index(sorted_result[:2], result.shape) assert_equal(j, (12, 0)) i, j = np.unravel_index(sorted_result[-2:], result.shape) assert_equal(j, (18, 6))
def transform(self, X): from skimage.feature import match_template X_out = None n_templates = len(self.template) raw = self.raw for i, x in enumerate(X): if i % 1000 == 0: print i for j, template in enumerate(self.template): result = match_template(x, template, pad_input=True) if X_out is None: if raw: dtype = (X.shape[0], n_templates, result.shape[0], result.shape[1]) else: dtype = (X.shape[0], n_templates) X_out = np.empty(dtype, dtype=np.float32) if not raw: result = np.max(result) X_out[i, j] = result if raw: X_out = np.max(X_out, axis=1) return X_out
def test_normalization(): """Test that `match_template` gives the correct normalization. Normalization gives 1 for a perfect match and -1 for an inverted-match. This test adds positive and negative squares to a zero-array and matches the array with a positive template. """ n = 5 N = 20 ipos, jpos = (2, 3) ineg, jneg = (12, 11) image = np.full((N, N), 0.5) image[ipos:ipos + n, jpos:jpos + n] = 1 image[ineg:ineg + n, jneg:jneg + n] = 0 # white square with a black border template = np.zeros((n + 2, n + 2)) template[1:1 + n, 1:1 + n] = 1 result = match_template(image, template) # get the max and min results. sorted_result = np.argsort(result.flat) iflat_min = sorted_result[0] iflat_max = sorted_result[-1] min_result = np.unravel_index(iflat_min, result.shape) max_result = np.unravel_index(iflat_max, result.shape) # shift result by 1 because of template border assert np.all((np.array(min_result) + 1) == (ineg, jneg)) assert np.all((np.array(max_result) + 1) == (ipos, jpos)) assert np.allclose(result.flat[iflat_min], -1) assert np.allclose(result.flat[iflat_max], 1)
def CalculateImageShift(imgref,img): imgH,imgW = imgref.shape result = match_template(imgref,img,pad_input=True) ij = np.unravel_index(np.argmax(result),result.shape) sx,sy = ij[::-1] sx,sy = imgW/2-sx,imgH/2-sy return sx,sy
def find_peaks(self): """Identify candidate sources that match the PSF. Updates self.corrmap and self.pix. """ data = self.data psf_grid = self.psf_grid psf_size = psf_grid.psf_size grid_size = psf_grid.grid_size box_size = psf_grid.box_size # map the corrleation coefficient data_grid = blockshaped(data, grid_size, grid_size) corrmap = np.array([match_template(image, template, pad_input=True) for image, template in zip(data_grid, psf_grid.to_array())]) corrmap = unblockshaped(corrmap, *self.shape) # find peak positions in the correlation map mask = ((corrmap > self.corrmin) & (data > self.adumin) & (data < self.adumax)) pix = np.array([bbox_max(corrmap, region.bbox) for region in regionprops(label(mask, neighbors=4))]) # exclude peaks near the edges fov = 0, 0, self.shape[0], self.shape[1] d_max = np.max([box_size, psf_size])/2 pix = pix[np.array([bbox_inside(p[0], p[1], fov, d_max) for p in pix])] if len(pix) == 0: warn("Found no candidate sources.") self.corrmap = corrmap self.pix = pix
def findTemplateInImage(templatePath, imagePath, debug=False): """ Returns the position of a template in an image """ # load template template = skimage.transform.rescale(io.imread(templatePath), 0.5) if debug: plt.imshow(template) plt.title("Template") plt.show() # load image image = io.imread(imagePath, False); image = skimage.transform.rescale(image, 0.5) if debug: plt.imshow(image) plt.title("Image") plt.show() # find building position result = match_template(image, template) result = result.squeeze() ij = numpy.unravel_index(numpy.argmax(result), result.shape) x, y = ij[::-1] # add image midpoint x += int(float(len(template[:]))/2) y += int(float(len(template))/2) # re-rescale ;) return numpy.array([x*2, y*2])
def processImages(): sims = cPickle.load(open('AmuInstSimMats.pkl')) for i,sim in enumerate(sims): pyplot.figure(0,(16,9)) pyplot.imshow(sim, vmin = 0, vmax = 1, cmap = pyplot.get_cmap('gray'), aspect = 'auto', origin = 'lower') pyplot.title('Unfiltered Sim Matrix ' + str(i)) pyplot.savefig('Unfiltered Sim Matrix ' + str(i) + '.jpg') pyplot.figure(1,(16,9)) pyplot.imshow(filter.tv_denoise(numpy.array(sim,numpy.float64), weight = 1), vmin = 0, vmax = 1, cmap = pyplot.get_cmap('gray'), aspect = 'auto', origin = 'lower') pyplot.title('TV_Denoise ' + str(i)) pyplot.savefig('TV_Denoise ' + str(i) + '.jpg') pyplot.figure(2,(16,9)) pyplot.imshow(filter.threshold_adaptive(numpy.array(sim,numpy.float64),21), vmin = 0, vmax = 1, cmap = pyplot.get_cmap('gray'), aspect = 'auto', origin = 'lower') pyplot.title('Threshold_Adaptive ' + str(i)) pyplot.savefig('Threshold_Adaptive ' + str(i) + '.jpg') pyplot.figure(3,(16,9)) pyplot.imshow(ndimage.minimum_filter(numpy.array(sim,numpy.float64),size=2), vmin = 0, vmax = 1, cmap = pyplot.get_cmap('gray'), aspect = 'auto', origin = 'lower') pyplot.title('Local Minimum_Filter ' + str(i)) pyplot.savefig('Local Minimum_Filter ' + str(i) + '.jpg') pyplot.figure(4,(16,9)) template = numpy.array([[0,1,1,1,1,1,1,1],[1,0,1,1,1,1,1,1],[1,1,0,1,1,1,1,1],[1,1,1,0,1,1,1,1], [1,1,1,1,0,1,1,1],[1,1,1,1,1,0,1,1],[1,1,1,1,1,1,0,1],[1,1,1,1,1,1,1,0]]) pyplot.imshow(feature.match_template(numpy.array(sim,numpy.float64),template), vmin = 0, vmax = 1, cmap = pyplot.get_cmap('gray'), aspect = 'auto', origin = 'lower') pyplot.title('Match_Template with my own 8x8 beat diagonal template ' + str(i)) pyplot.savefig('Match_Template with my own 8x8 beat diagonal template ' + str(i) + '.jpg') sys.exit()
def test_template(): size = 100 # Float prefactors ensure that image range is between 0 and 1 image = np.full((400, 400), 0.5) target = 0.1 * (np.tri(size) + np.tri(size)[::-1]) target_positions = [(50, 50), (200, 200)] for x, y in target_positions: image[x:x + size, y:y + size] = target np.random.seed(1) image += 0.1 * np.random.uniform(size=(400, 400)) result = match_template(image, target) delta = 5 positions = peak_local_max(result, min_distance=delta) if len(positions) > 2: # Keep the two maximum peaks. intensities = result[tuple(positions.T)] i_maxsort = np.argsort(intensities)[::-1] positions = positions[i_maxsort][:2] # Sort so that order matches `target_positions`. positions = positions[np.argsort(positions[:, 0])] for xy_target, xy in zip(target_positions, positions): assert_almost_equal(xy, xy_target)
def run(self, im, skin_thresh=[-1,1], n_peaks=3): ''' im : color image ''' im_skin = im self.im_skin = im_skin skin_match_c = match_template(im_skin, self.template, pad_input=True)*(im>0) self.skin_match = skin_match_c # cv2.matchTemplate(im_skin, self.template, cv2.cv.CV_TM_SQDIFF_NORMED) # imshow(cv2.matchTemplate(im_skin.astype(np.float32), self.template.astype(np.float32), cv2.cv.CV_TM_CCOEFF_NORMED)) # Display Predictions - Color Based matching optima = peak_local_max(skin_match_c, min_distance=20, num_peaks=n_peaks, exclude_border=False) # Visualize if len(optima) > 0: optima_values = skin_match_c[optima[:,0], optima[:,1]] optima_thresh = np.max(optima_values) / 2 optima = optima.tolist() for i,o in enumerate(optima): if optima_values[i] < optima_thresh: optima.pop(i) break self.markers = optima return self.markers
def align_converge(y_LR,size=64): """iterate until offsets converge""" (h,w) = y_LR.shape # split image y_L = y_LR[:,:w/2] y_R = y_LR[:,w/2:] (h,w) = y_L.shape s = size / 2 # now find n offsets rand = RandomState(0) prev_dx, prev_dy = 0, 0 series = [] while True: # at a random locations in y_L y = rand.randint(h/4,h*3/4) x = rand.randint(w/4,w*3/4) it = y_L[y:y+s,x:x+s] # take an s x s chunk there tm = match_template(y_R,it) # match it against y_R ry, rx = maximum_position(tm) # max value is location series += [((y-ry), (x-rx))] # accumulatea print series n = len(series) if n % 2 == 0: # take the median dy, dx = np.median(np.asarray(series),axis=0).astype(int) if n > 100 or (abs(dy-prev_dy) == 0 and abs(dx-prev_dx) == 0): return dy, dx prev_dy, prev_dx = dy, dx
def detect_start_end_times(pattern_wav, recording_wav, sr, overlap): """Find matches for the start/end pattern within the recorded audio""" # Compute the STFT of the recordings specgram1 = numpy.array(stft.spectrogram(pattern_wav, overlap=overlap)) specgram2 = numpy.array(stft.spectrogram(recording_wav, overlap=overlap)) # Restrict the spectrum to the frequency band occupied by the start/end pattern pattern = abs(specgram1[7:16,:]) recording = abs(specgram2[7:16,:]) # Search for matches of the pattern in the input recording and return a confidence score # for each time position of the input recording confidence = match_template(recording, pattern) # Search for peaks in the confidence score, and choose the two highest peaks # Minimum distance between consecutive peaks is set to 1 second peaks = peakutils.indexes(confidence[0], thres=0, min_dist=seconds_to_samples(1, overlap, sr)) peaks = sorted(peaks, key=lambda p: -confidence[0,p])[:2] #TODO: throw errors instead of printing, if necessary if len(peaks) < 1: print "Could not detect a starting beep!" elif len(peaks) < 2: print "Could only detect one starting beep!" else: start, end = sorted(peaks) print "Initial beep detected at " + "%.3f" % samples_to_seconds(start, overlap, sr) + " seconds." print "Final beep detected at " + "%.3f" % samples_to_seconds(end, overlap, sr) + " seconds." return samples_to_seconds(start, overlap, sr), samples_to_seconds(end, overlap, sr)
def test_bounding_values(): image = img_as_float(data.page()) template = np.zeros((3, 3)) template[1, 1] = 1 result = match_template(img_as_float(data.page()), template) print(result.max()) assert result.max() < 1 + 1e-7 assert result.min() > -1 - 1e-7
def _template_matching_shift(self, im1, im2, template): index = [] for im in [im1, im2]: match = match_template(im, template) index.append(np.unravel_index(np.argmax(match), match.shape)) index = np.array(index) shift = index[1] - index[0] return shift
def test_padding_reflect(): template = diamond(2) image = np.zeros((10, 10)) image[2:7, :3] = template[:, -3:] result = match_template(image, template, pad_input=True, mode='reflect') assert_equal(np.unravel_index(result.argmax(), result.shape), (4, 0))
def _process(self): """Finds the Suns and the fiducials.""" # Perform a coarse search for Suns coarse_image = self.image[::10, ::10] coarse_match = match_template(coarse_image, template_sun[::10, ::10], pad_input=True) coarse_peaks = peak_local_max(coarse_match, threshold_abs=0.9, num_peaks=3) fine_peaks = [] strength = [] fiducials = [] for coarse_peak in coarse_peaks: # For each coarse detection, do a detection at the full resolution if coarse_peak[0] < 11 or coarse_peak[0] > 84 or coarse_peak[1] < 11 or coarse_peak[1] > 116: break sub_image = self.image[coarse_peak[0] * 10 - 110:coarse_peak[0] * 10 + 111, coarse_peak[1] * 10 - 110:coarse_peak[1] * 10 + 111] match = match_template(sub_image, template_sun, pad_input=True) peak = peak_local_max(match, threshold_abs=0.9, num_peaks=1) if len(peak) > 0: peak = peak[0] peak_r, peak_c = parapeak(match[peak[0] - 1:peak[0] + 2, peak[1] - 1:peak[1] + 2]) peak += coarse_peak * 10 - 110 fine_peaks.append((peak[0] + peak_r, peak[1] + peak_c)) #FIXME: need a more robust estimate of the strength of each peak strength.append(self.image[peak[0], peak[1]]) # Find fiducials near the center of the Sun match = match_template(self.image[peak[0]-60:peak[0]+61, peak[1]-60:peak[1]+61], template_fiducial, pad_input=True) fids = peak_local_max(match, threshold_abs=0.8) for fid in fids: fid_r, fid_c = parapeak(match[fid[0] - 1:fid[0] + 2, fid[1] - 1:fid[1] + 2]) fid += peak - 60 fiducials.append((fid[0] + fid_r, fid[1] + fid_c)) # Sort the peaks in order of decreasing strength fine_peaks = [peak for (strength, peak) in sorted(zip(strength, fine_peaks), reverse=True)] return fine_peaks, fiducials
def detect_keypatch(img, template): simg = feature.match_template(img, template, pad_input=True) simg = simg.clip(0, simg.max()) rel_thr = 0.75 peaks = feature.peak_local_max(simg, num_peaks=1, threshold_abs=rel_thr*(simg.max()-simg.min()), exclude_border=False) ht, wt = template.shape for i in range(len(peaks)): peaks[i] = [peaks[i][1]-wt/2, peaks[i][0]-ht/2] return peaks
def xcorr(h1,h2): #print(h1.shape) h1_ = flattn(h1) #print(h1_) h2_ = flattn(h2) val = (match_template(np.array([h1_]),np.array([h2_]))[0][0])*1000 if val < 0 : return 1000 + val else: return 1000 - val
def _speckleDisplacementSingleCore_method2(image, image_ref, halfsubwidth, halfTemplateSize, stride, verbose): ''' see http://scikit-image.org/docs/dev/auto_examples/plot_template.html ''' from skimage.feature import match_template irange = np.arange(halfsubwidth, image.shape[0] - halfsubwidth + 1, stride) jrange = np.arange(halfsubwidth, image.shape[1] - halfsubwidth + 1, stride) pbar = tqdm(total=np.size(irange)) # progress bar sx = np.ones(image.shape) * NAN sy = np.ones(image.shape) * NAN error = np.ones(image.shape) * NAN for (i, j) in itertools.product(irange, jrange): interrogation_window = image_ref[i - halfTemplateSize: \ i + halfTemplateSize+ 1, j - halfTemplateSize: \ j + halfTemplateSize + 1] sub_image = image[i - halfsubwidth:i + halfsubwidth + 1, j - halfsubwidth:j + halfsubwidth + 1] result = match_template(sub_image, interrogation_window) shift_y, shift_x = np.unravel_index(np.argmax(result), result.shape) shift_x -= halfsubwidth - halfTemplateSize shift_y -= halfsubwidth - halfTemplateSize error_ij = 1.0 - np.max(result) sx[i, j] = shift_x sy[i, j] = shift_y error[i, j] = error_ij if j == jrange[-1]: pbar.update() # update progress bar print(" ") return (sx[halfsubwidth:-halfsubwidth:stride, halfsubwidth:-halfsubwidth:stride], sy[halfsubwidth:-halfsubwidth:stride, halfsubwidth:-halfsubwidth:stride], error[halfsubwidth:-halfsubwidth:stride, halfsubwidth:-halfsubwidth:stride], stride)
def knn_custom_match(av,bv): agray = av[:4224].reshape((66,64)) bgray = bv[:4224].reshape((66,64)) acen = av[-2:] bcen = bv[-2:] res = feature.match_template(agray,bgray) dis = res.max() if dis > 0.9: return 1- dis cen = acen cenimg = com_get_patch(agray,cen) sres = feature.match_template(bgray,cenimg) sdis = sres.max() if sdis > dis: dis = sdis return 1 - dis
def test_3d_pad_input(): np.random.seed(1) template = np.random.rand(3, 3, 3) image = np.zeros((12, 12, 12)) image[3:6, 5:8, 4:7] = template result = match_template(image, template, pad_input=True) assert_equal(result.shape, (12, 12, 12)) assert_equal(np.unravel_index(result.argmax(), result.shape), (4, 6, 5))
def test_3d(): np.random.seed(1) template = np.random.rand(3, 3, 3) image = np.zeros((12, 12, 12)) image[3:6, 5:8, 4:7] = template result = match_template(image, template) assert_equal(result.shape, (10, 10, 10)) assert_equal(np.unravel_index(result.argmax(), result.shape), (3, 5, 4))
def match_sub_multiple(image, image_sub): result = match_template(image, image_sub) peaks = peak_local_max(result, min_distance=50, threshold_rel=0.85) h_sub, w_sub = image_sub.shape peaks[:, 0] += h_sub // 2 peaks[:, 1] += w_sub // 2 return peaks
def correlate_frames_other_size(frame_small, frame_big): """ Correlates other sizes small and big frame and finds offset between the two ----------------------- :param frame_small: Previous frame :param frame_big: New frame to correlate with :return: offset: the offset between the two frames """ corr = match_template(frame_big, frame_small) offset = -np.transpose(np.asarray(np.where(corr == np.amax(corr))))[0] return offset
def template_matching(image,kernel_radius, gRatio): r = gRatio*kernel_radius y,x = np.ogrid[-kernel_radius: kernel_radius+1, -kernel_radius: kernel_radius+1] D = x**2+y**2 <= kernel_radius**2 d = x**2+y**2 <= r**2 template = (D-d)*255 result = match_template(image, template) return [template, result]
def fraction_match(label, training_dir, image_data): fraction = 0 for i in range(10): image_dir = os.path.join(training_dir, label, label + '_' + str(i) + '.jpg') image_sample = imread(image_dir, as_grey=True) image_sample = image_sample < threshold_otsu(image_sample) match_fraction = match_template(image_data, image_sample) fraction += (match_fraction[0, 0] / 10) return fraction
def match(image,template,debug=False): if debug: print image.shape,template.shape result = match_template(image, template) #find i,j with maxvalue maxrow = np.argmax(result,axis=1) if debug: print maxrow maxcol = np.argmax(result,axis=0) if debug: print maxcol #maxcorrel = result[maxrow,maxcol] return np.max(result)
def making_matching_temp(): for i in range(274): index = 5 * i image = cv2.imread('Frame_' + str(index) + '.png', 0) image = image[750:750 + 1385 + 5, 1400:1400 + 2400 + 5] # window frame = cv2.imread('crop.png', 0) # template coin = frame result = match_template(image, coin, pad_input=True) #added the pad_input bool io.imshow(result, cmap='gray') io.imsave('m_' + str(index) + '.png', result) plt.show()
def getperson(img, tem): # 获取小人的位置 img = img[:, :, :3] # 去掉RGBA中的A img[:200, :, :] = (0, 0, 0) # 去掉顶部 img[1100:, :, :] = (0, 0, 0) # 去掉底部 # 找到与小人颜色相近的像素 grey = filt_color(img) # 模板匹配寻找小人的头部 res = feature.match_template(grey, tem) pos = np.unravel_index(np.argmax(res), res.shape) pos = pos - delta return pos
def yard_match(image, yard, view, img_f): yard_match_template_folder = base_folder + '/images_processed/pattern_match_template/{0}2/{1}'.format(view, yard) template_files = os.listdir(yard_match_template_folder) print(template_files) for yard_f in template_files: image_yard = io.imread(yard_match_template_folder + '/' + yard_f) image_yard = rgb2gray(image_yard) io.imshow(image_yard) io.show() ############################ #print('shape', np.shape(image)) result = match_template(image, image_yard) #print(result) ij = np.unravel_index(np.argmax(result), result.shape) x, y = ij[::-1] #print('xy', x, y) fig = plt.figure(figsize=(12, 8)) #fig = plt.figure() ax1 = plt.subplot(1, 3, 1) ax2 = plt.subplot(1, 3, 2) #ax3 = plt.subplot(1, 3, 3, sharex=ax2, sharey=ax2) ax3 = plt.subplot(1, 3, 3) ax1.imshow(image_yard, cmap=plt.cm.gray) ax1.set_axis_off() ax1.set_title('template') ax2.imshow(image, cmap=plt.cm.gray) ax2.set_axis_off() ax2.set_title('image') # highlight matched region h_sub, w_sub = image_yard.shape #print('hw', h_sub, w_sub) rect = plt.Rectangle((x, y), w_sub, h_sub, edgecolor='r', facecolor='none') ax2.add_patch(rect) ax2.plot(x, y, marker='v', color="green") ax3.imshow(result) ax3.set_axis_off() ax3.set_title('`match_template`\nresult') # highlight matched region ax3.autoscale(False) ax3.plot(x, y, 'o', markeredgecolor='r', markerfacecolor='none', markersize=10) plt.show()
def ring_search(target_image, template_list, threshold = 0.7, diameter = 8, show_rings = True): '''search for rings in the target_image (array) based on the templates in template_list (array) threhsold sets the sensibility of the algorithm to find a match, 0.7 by default show_rings: if True shows the raw_image with the rings found; diameter sets the diameter of those rings''' # create empty list to fill with ring coordenates all_ring_coord = [] # loop over different templates for template in template_list: # get template height, width; find center of the template; # use a counter clockwise rotation holding the center for different angles (h, w) = template.shape[:2] temp_center = (w / 2, h / 2) angles = [0, 90, 180, 270] for angle in angles: rotation = cv2.getRotationMatrix2D(temp_center, angle, 1) rotated_template = cv2.warpAffine(template, rotation, (h, w)) # use match_template function from skimage and peak_local_max find_rings = match_template(target_image, rotated_template, pad_input = True) coordinates = peak_local_max(find_rings, num_peaks = 200, threshold_abs = threshold, min_distance = diameter, exclude_border = True, indices = True) all_ring_coord.append([list(rings) for rings in coordinates]) # unpack sublists into a single list of coordenates all_ring_coord = [j for i in all_ring_coord for j in i] # filter overlaping coordenates (i.e they correspond to the same ring) all_ring_coord = coord_filtering(all_ring_coord, diameter) if show_rings == True: fig, ax = plt.subplots(figsize=(5,5), dpi = 120, constrained_layout = True) ax.imshow(target_image, cmap = 'gray') for x, y in all_ring_coord: circle = plt.Circle((y, x), diameter, color = 'blue', linewidth = 1.2, fill = False) ax.add_patch(circle) ax.set_title("{} rings found".format(len(all_ring_coord))) return all_ring_coord
def test_no_nans(): """Test that `match_template` doesn't return NaN values. When image values are only slightly different, floating-point errors can cause a subtraction inside of a square root to go negative (without an explicit check that was added to `match_template`). """ np.random.seed(1) image = 0.5 + 1e-9 * np.random.normal(size=(20, 20)) template = np.ones((6, 6)) template[:3, :] = 0 result = match_template(image, template) assert not np.any(np.isnan(result))
def match_texture_patch(new_texture_patch,previous_texture_patch): newShape = np.shape(new_texture_patch) previousShape = np.shape(previous_texture_patch) shapeCenterX = int(previousShape[0] / 2) shapeCenterY = int(previousShape[1] / 2) corr = match_template(previous_texture_patch, new_texture_patch, pad_input=True) loc = tuple((np.where(corr == np.max(corr))[0][0], np.where(corr == np.max(corr))[1][0])) # print("newShape:"+str(newShape)+" previousShape:"+str(previousShape)+" loc:"+str(loc)) # print("newlocation is x:"+str(loc[1]-shapeCenterX)+" y:"+str(loc[0]-shapeCenterY)) return tuple((loc[0] - shapeCenterY, loc[1] - shapeCenterX))
def get_locations(reader, template): locations = [] for i in range(40, 350): im = reader.get_data(i) image = skimage.color.rgb2gray(im) result = match_template(image, template) locations.append(argmax(result)) index = np.unravel_index(locations, result.shape) return index
def show_best_match(image,template): result = match_template(image, template) ij = np.unravel_index(np.argmax(result), result.shape) z, y,x = ij[::-1] width_temp,height_temp,depth=template.shape # print(x,y,z) ax = plt.subplot(111) ax.imshow(image) # print(x,y) # print(width_temp,height_temp,depth) rect = plt.Rectangle((y,x), height_temp,width_temp, edgecolor='r',facecolor="None") ax.add_patch(rect) plt.show()
def get_xcorr(image, template, verbose=False): try: cc = match_template(image, template, pad_input=False) if verbose: plot.multi_imshow(image, template, cc, titles=['image', 'template', 'cc']) return cc except ValueError: logging.error( f"Error in cc_template. Image shape {image.shape}, template shape {template.shape }" )
def get_number(im): im = gaussian(im, sigma=2) scores = { i: np.max(match_template(im, tmpl)) for i, tmpl in TEMPLATES.items() } scores = sorted(scores.items(), key=lambda x: x[1], reverse=True) number, score = scores[0] # special case to prevent 5 from being classified as 15 if number == 15: if score <= 0.8: return 5 return number
def find_number(tmpl, im, ax): result = match_template(im, tmpl) ij = np.unravel_index(np.argmax(result), result.shape) x, y = ij[::-1] ax.imshow(im, cmap=plt.cm.gray) ax.set_axis_off() # highlight matched region h, w = tmpl.shape rect = plt.Rectangle((x, y), w, h, edgecolor='r', facecolor='none') ax.add_patch(rect) offset = (x + w // 2) - (im.shape[1] // 2) return np.max(result), np.abs(offset)
def findImage(mainImage, template): # Read both images temp_main = mpimg.imread(mainImage) temp_small = mpimg.imread(template) # Convert both images into grayscale main = rgb2gray(temp_main) small = rgb2gray(temp_small) # Create an array of those pixel values main_data = np.array(main, dtype=None) template_data = np.array(small, dtype=None) # Print out both original images in greyscale show_images(0, main_data, "Main Image w/ greyscale") show_images(1, template_data, "Template image w/ greyscale") # Apply the match template function result = match_template(main_data, template_data, pad_input=True) # Variables to hold index values, size of half of template temp_row = int(len(template_data) / 2) temp_col = int(len(template_data[0]) / 2) rows = 0 cols = 0 temp = result[0][0] i = 0 # Loop through main image and find highest correlation value as center while (i <= int(len(result)) - 1): k = 0 while (k <= int(len(result)) - 1): if (result[i][k] > temp): temp = result[i][k] rows = i cols = k k += 1 i += 1 # Assign index values on main image to know area of template beg_row = rows - temp_row beg_col = cols - temp_col rows = rows + temp_row cols = cols + temp_col # Black out the image from the main image with index values main_data[beg_row:rows, beg_col:cols] = 0 # Plot out final image with black square where template is found show_images(2, main_data, "Main image w/ template removed") plt.show()
def match_corner(image, template): result = match_template(image, template, pad_input=True) # plt.imshow(result) # plt.show() result[ result > 1] = 0 # there are some weird artefacts in the nodata area! # peaks = peak_local_max(result, min_distance=min(template.shape)//2) # scores = [result[y,x] for (y,x) in peaks] # scores.sort(reverse=True) # print(scores) ij = np.unravel_index(np.argmax(result), result.shape) x, y = ij[::-1] corr_coef = result[y, x] return (x, y)
def auto_calib_elevation_offset(cloud_mask_to_fit_on, cloud_mask_to_be_fitted): mask_for_template = cloud_mask_to_fit_on x_size, y_size = mask_for_template.shape x_half = int(x_size / 2) y_half = int(y_size / 2) size = 200 template = mask_for_template[x_half - size:x_half + size, y_half - size:y_half + size] result = match_template(cloud_mask_to_be_fitted, template) return result, template
def template_ncc(img_path, template_path): img = cv2.imread(img_path, 0) template = cv2.imread(template_path, 0) result = match_template(img, template, pad_input=True) fig = plt.figure(figsize=(10, 5)) ax = plt.subplot(1, 1, 1, ylabel="Pixel location in Y direction", xlabel="Pixel location in X direction") ax.imshow(result, cmap=plt.cm.gray) ax.autoscale(False) plt.show(block=False)
def _parseLevel(gray, template): # find location of level resultImage = match_template(gray, template) ij = np.unravel_index(np.argmax(resultImage), resultImage.shape) x, y = ij[::-1] # extract level from image height, width = gray.shape levelarea = gray[y-int(0.09*width):y+int(0.01*width),x-int(0.03*width):x+int(0.13*width)] level = pytesseract.image_to_string(levelarea) print("Found text -> {" + level + "}\n") reqLevel = [int(s.strip('.')) for s in level.split() if (s.strip('.')).isdigit()] return reqLevel
def check_image_if_contain(img, feature_list): """ 利用match_template判定图像中是否包含特征列表中的内容 :param img: :param feature_list: :return return_list: """ result_list = [] for each_feature in feature_list: res = match_template(img, each_feature) _, max_val, _, max_loc = cv2.minMaxLoc(res) result_list.append([1 if max_val > 0.99 else 0, max_val]) return result_list
def secondalgorithm(self): ''' Algorithm for computing registration by features using segmentBloodVessel. ''' # Perform Segmentation. binaryBL = self.segmentBloodVessel(self.bl) binaryFU = self.segmentBloodVessel(self.fu) # Preprocess the images for template matching. grey_bl = color.rgb2gray(self.bl) roibl = grey_bl[50:1300, 50:1300] roibl = filters.gaussian(roibl) grey_fu = color.rgb2gray(self.fu) roifu = grey_fu roifu = filters.gaussian(roifu) # Loop to find the rotation. found = None for angle in np.linspace(-10.0, 10.0, 20)[::-1]: print(angle) rotated = tf.rotate(roibl, angle) result = feature.match_template(roifu, rotated) (_, maxVal, _, maxLoc) = cv2.minMaxLoc(result) if found is None or maxVal > found[0]: found = (maxVal, maxLoc, angle) print(found) (_, maxLoc, angle) = found rotated_seg = tf.rotate(binaryFU, angle=angle) #shifts, error, phasediff = feature.register_translation(self.bl, test) shifts, error, phasediff = feature.register_translation( rotated_seg, binaryBL) shifts = np.flip(shifts) tform = np.zeros((3, 3), dtype=float) np.fill_diagonal(tform, 1.0) tform[0:shifts.shape[0], 2] = shifts.T transformed = tf.warp(self.fu, tform) plt.figure(1) plt.imshow(self.bl) plt.imshow(transformed, cmap='Greens', alpha=0.5) plt.show()
def match(self): self.matchElem=self.matcher.combo.currentIndex() for i in arange(self.projections-1): img=self.data[self.matchElem,i,:,:] img1=ones([img.shape[0]*2,img.shape[1]*2])*self.data[self.matchElem,i,:10,:10].mean() img1[img.shape[0]/2:img.shape[0]*3/2,img.shape[1]/2:img.shape[1]*3/2]=img img2=self.data[self.matchElem,i+1,:,:] result=match_template(img1,img2) result=np.where(result==np.max(result)) self.yshift[i+1]=result[0][0]-img.shape[0]/2 self.xshift[i+1]=result[1][0]-img.shape[1]/2 print self.xshift[i+1], self.yshift[i+1] self.data[:,i+1,:,:]=np.roll(self.data[:,i+1,:,:], self.xshift[i+1], axis=2) self.data[:,i+1,:,:]=np.roll(self.data[:,i+1,:,:], self.yshift[i+1], axis=1)
def morphological_threshold(im, white_background, mean_radius, min_feature_size, small, mask): im_mod = np.array(im, dtype='float64') if white_background: im_mod = np.abs(im_mod - np.max(im_mod)) # subtract the mean before running match_template # not sure this works quite right im_mod = im_mod - np.mean(im_mod) # set large areas of background to zero using the mask #im_mod = im_mod * mask if small: template_matrix = np.pad(disk(max(2, int(mean_radius))), pad_width=max(2, int(mean_radius)), mode='constant', constant_values=0) else: template_matrix = np.pad(disk(int(mean_radius / 4)), pad_width=int(mean_radius), mode='constant', constant_values=0) matched_im = match_template(im_mod, template=template_matrix, pad_input=True) thresh = threshold_isodata(matched_im) matched_im_bin = matched_im > thresh matched_im_bin *= mask distance = ndimage.distance_transform_edt(matched_im_bin) # dilate the distance map to merge close peaks (merges multiple peaks in one particle) distance = ndimage.grey_dilation(distance, size=int(min_feature_size)) local_maxi = peak_local_max(distance, indices=False, min_distance=int(min_feature_size)) markers = ndimage.label(local_maxi)[0] with warnings.catch_warnings(): warnings.simplefilter('ignore') labels_th = watershed(-distance, markers, mask=matched_im_bin) return labels_th, matched_im_bin
def match_all_window(image,templates,min_w=24,is_two_digits=True,debug=False): h,w = image.shape if debug: print "match_all_window:h,w",h,w,min_w,is_two_digits wlimit = max(w/2,min_w) ret = np.zeros((54-6+1,2)) for window in range(6,55): if window <= 9 and not is_two_digits: corr = np.max(match_template(image[:,:wlimit], templates[window])) ret[window-6,0] += corr ret[window-6,1] += 1 if debug: print "match_all_window:window",window,corr elif window > 9 and is_two_digits: sw = str(window) d1 = int(sw[0]) d2 = int(sw[1]) r1 = np.max(match_template(image[:,:wlimit], templates[d1])) r2 = np.max(match_template(image[:,w - wlimit:], templates[d2])) #normalize correlations h1,w1 = templates[d1].shape h2,w2 = templates[d2].shape weight1 = h1*w1/float(h1*w1 + h2*w2) weight2 = h2*w2/float(h1*w1 + h2*w2) if debug: print "match_all_window:window",window,r1,r2,h1,w1,h2,w2,weight1,weight2,(weight1*r1 + weight2*r2) #ret[window-6,0] += (weight1*r1 + weight2*r2) ret[window-6,0] += (r1 + r2)*0.5 ret[window-6,1] += 1 return ret
def find_best_template(edges, imsave): """Find the best exemplar of a probe in the image. We use template matching with a hollow disk (annulus), and return the best match in the image.""" template = make_stage1_template() stage1_match = match_template(edges.image_array, template, pad_input=True) imsave('stage1_match.png', stage1_match) cmax = np.max(stage1_match) px, py = zip(*np.where(stage1_match == cmax))[0] tr = 4 better_template = edges.image_array[px-tr:px+tr,py-tr:py+tr] imsave('better_template.png', better_template) return better_template
def findCCMatches(self,seg,sg,thr): """ Cross-correlation. Takes a segment and looks for others that match it to within thr. match_template computes fast normalised cross-correlation """ from skimage.feature import match_template # seg and sg have the same $y$ size, so the result of match_template is 1D #m = match_template(sg,seg) matches = np.squeeze(match_template(sg, seg)) import peakutils md = np.shape(seg)[0]/2 threshold = thr*np.max(matches) indices = peakutils.indexes(matches, thres=threshold, min_dist=md) return indices
def cmp_corrlation_images(Afile,Bfile,r=None): Aimage = io.imread(Afile,as_grey = True) Bimage = io.imread(Bfile,as_grey = True) result = match_template(Bimage,Aimage) result = np.round(result,3) row,col = np.where(result == result.max()) return result.max()
def debug_in_oneimage_centerblock(Afile,Bfile): Aimage = io.imread(Afile,as_grey = True) Bimage = io.imread(Bfile,as_grey = True) Acenter = Aimage[3:63,3:63] #Acenter = Aimage[13:53,13:53] result = match_template(Bimage,Acenter) result = np.round(result,3) row,col = np.where(result == result.max()) print (result.max())
def fraction_match(self, label, training_dir, image_data): fraction = 0 extensions = {".jpg", ".png"} image_dir = os.path.join(training_dir, label) image_files = [ f for f in os.listdir(image_dir) if (f.endswith(ext) for ext in extensions) ] for image_file in image_files: image_sample = imread(os.path.join(image_dir, image_file), as_gray=True) image_sample = image_sample < threshold_otsu(image_sample) match_fraction = match_template(image_data, image_sample) fraction += (match_fraction[0, 0] / len(image_files)) return fraction