def main(): filename = getFileName() cap = cv2.VideoCapture(filename) ret, im = cap.read() im = im.astype('float32') prev_gray = cv2.cvtColor(im,cv2.COLOR_BGR2GRAY) move = None points = [] while True: ret, im = cap.read() if ret is False: break im = im.astype('float32') im = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY) p = cv2.phaseCorrelate(im,prev_gray) prev_gray = im # P is movement form top left = (0,0), so need to invert all numbers if move is None: move = (-p[0], -p[1]) else: move = ( move[0] - p[0], move[1] - p[1]) print move points.append(move) #print 'Total move: ', move plt.figure(1) line = plt.plot(points) plt.legend(line, ('Horizontal', 'Vertical'), 'best') plt.xlabel('Frame') plt.ylabel('Movement') plt.title('Phase Correlation Movement') xp = [] yp = [] for p in points: xp.append(p[1]) yp.append(p[0]) plt.figure(2) line = plt.plot(yp,xp) plt.xlabel('Horizontal Movement') plt.ylabel('Vertical Movement') plt.title('Phase Correlation Movement, x-y plot') plt.annotate('Start', xy=(0, 0), xytext=(-20, 400),arrowprops=dict(facecolor='black', shrink=0.05)) plt.show()
def phase_cor_f(self, img0, img1, vis): start = time.time() # img0, img1 = self.prev_gray, frame_gray img0 = np.float32(img0) # convert first into float32 img1 = np.float32(img1) # convert second into float32 h,w = img0.shape cX, cY = w//2, h//2 prev_polar = cv.linearPolar(img0,(cX, cY), min(cX, cY), 0) cur_polar = cv.linearPolar(img1,(cX, cY), min(cX, cY), 0) #what is df? (dx, dy), df = cv.phaseCorrelate(img0,img1) # now calculate the phase correlation self.X1 += dx self.Y1 += dy textp(vis, 'X, Y: {:.2f} {:.2f}'.format(self.X1, self.Y1),(20, 200)) (sx, sy), sf = cv.phaseCorrelate(prev_polar, cur_polar) rotation = -sy / h * 360; self.rot += rotation textp(vis, 'rot: %d' % self.rot,(20, 250)) end = time.time() print('time elapsed: ',end-start) return vis
def rotation_scale_stabilization(img1, img2, rows, cols, print_result): """ Perform rotation and scale stabilization using phase correlation on two log polar images. :param img1_polar: :param img2_polar: :param img2_to_stabilized: :param rows: :param cols: :param print_result: :return: """ img1_gray = ImageProcess.to_gray(img1) img1_polar = ImageProcess.to_log_polar(img1_gray) img2_gray = ImageProcess.to_gray(img2) img2_polar = ImageProcess.to_log_polar(img2_gray) (log_polar_cx, log_polar_cy), _ = cv2.phaseCorrelate(np.float32(img2_polar), np.float32(img1_polar)) rotation, scale = ImageProcess.__scale_rotation( log_polar_cy, log_polar_cx, rows, cols) print_result['scale'] = scale print_result['rotation'] = rotation centre = (cols // 2, rows // 2) transformation_matrix = cv2.getRotationMatrix2D( centre, rotation, scale) flags = cv2.INTER_LINEAR | cv2.WARP_INVERSE_MAP result_image = cv2.warpAffine(img2, transformation_matrix, dsize=(cols, rows), flags=flags) return result_image, print_result
def main(): # parse args. parser = argparse.ArgumentParser() parser.add_argument('path_to_src', nargs=None) parser.add_argument('path_to_dst', nargs=None) args = parser.parse_args() # read images src_im = cv2.imread(args.path_to_src) dst_im = cv2.imread(args.path_to_dst) src_im = cv2.cvtColor(src_im, cv2.COLOR_BGR2GRAY) dst_im = cv2.cvtColor(dst_im, cv2.COLOR_BGR2GRAY) print(np.sum(src_im)) # src_im = np.float32(src_im) # dst_im = np.float32(dst_im) src_im = cv2.Laplacian(src_im, cv2.CV_32F) dst_im = cv2.Laplacian(dst_im, cv2.CV_32F) ret = cv2.phaseCorrelate(src_im, dst_im) print(ret)
def findMaximalFit(self, current_image, previous_image, blob_position): """ Function try find best fit blobs from preview image to current. If max_range=None - function try find in all image. :param previous_image: :param current_image: image to fit :param blob_position: position blob to fit :return: """ current_image = current_image.astype(float) mask_with_blob = np.zeros((self.image_rows, self.image_cols)) mask_to_clear = np.zeros((self.blob_size, self.blob_size)) mask_with_blob[blob_position[0]:blob_position[0] + self.blob_size, blob_position[1]:blob_position[1] + self.blob_size] = \ previous_image[blob_position[0]:blob_position[0] + self.blob_size, blob_position[1]:blob_position[1] + self.blob_size] corr = cv.phaseCorrelate(mask_with_blob, current_image) # clear mask mask_with_blob[blob_position[0]:blob_position[0] + self.blob_size, blob_position[1]:blob_position[1] + self.blob_size] = mask_to_clear """
def shift_stabilization(img1, img2, rows, cols, print_result=None): """ Perform shift stabilization on two images using phase correlation with hanning window :param img1 source image :param img2: target image :param rows: rows of result image :param cols: columns of result image :param print_result: gathered information during stabilization :return: result_image: stabilized (shifted) image print_result: collected information during shift stabilization """ img1_gray = ImageProcess.to_gray(img1) img2_gray = ImageProcess.to_gray(img2) hanning = cv2.createHanningWindow((cols, rows), cv2.CV_32F) (cx, cy), _ = cv2.phaseCorrelate(np.float32(img2_gray), np.float32(img1_gray)) # (cx, cy) = (round(cx, 2), round(cy, 2)) M = np.float32([[1, 0, cx], [0, 1, cy]]) print_result['x'] = cx print_result['y'] = cy t_form = transform.EuclideanTransform(translation=(cx, cy)) result_image = transform.warp(img2, t_form) return img_as_ubyte(result_image), print_result
def stabilise(self): # Reference: https://docs.opencv.org/2.4/modules/imgproc/doc/motion_analysis_and_object_tracking.html#phasecorrelate for frameNo in range(len(self.frames) - 1): firstFrame = self.frames[frameNo] secondFrame = self.frames[frameNo + 1] gFirstFrame = cv2.cvtColor(firstFrame, cv2.COLOR_BGR2GRAY) gSecondFrame = cv2.cvtColor(secondFrame, cv2.COLOR_BGR2GRAY) gFirstFrame = cv2.medianBlur(gFirstFrame, 9) gSecondFrame = cv2.medianBlur(gSecondFrame, 9) threshold, _ = cv2.threshold(src = gFirstFrame, thresh = 0, maxval = 255, type = cv2.THRESH_BINARY | cv2.THRESH_OTSU) gFirstFrame = cv2.Canny(image = gFirstFrame, threshold1 = 0.5 * threshold, threshold2 = threshold) threshold, _ = cv2.threshold(src = gSecondFrame, thresh = 0, maxval = 255, type = cv2.THRESH_BINARY | cv2.THRESH_OTSU) gSecondFrame = cv2.Canny(image = gSecondFrame, threshold1 = 0.5 * threshold, threshold2 = threshold) # Convert to CV_32FC1 gFirstFrame32 = np.float32(gFirstFrame) gSecondFrame32 = np.float32(gSecondFrame) (xDif, yDif), _ = cv2.phaseCorrelate(src1 = gSecondFrame32, src2 = gFirstFrame32) # Reference: https://docs.opencv.org/3.2.0/da/d6e/tutorial_py_geometric_transformations.html translationMatrix = np.float32([[1, 0, xDif], [0, 1, yDif]]) newFrame = cv2.warpAffine(secondFrame, translationMatrix, (self.width, self.height)) self.frames[frameNo + 1] = newFrame if frameNo % 100 == 0: print int(float(frameNo) / len(self.frames) * 100.0), '%'
def raw2phasecorr(arr_list,clip=0): #cv import cv2 cx = 0.0 cy = 0.0 stb_arr_list=[] prev_frame = arr_list[0] prev_image = np.float32(restoration.denoise_tv_chambolle(prev_frame.astype('uint16'), weight=0.1, multichannel=True)) #ref for frame in arr_list: image = np.float32(restoration.denoise_tv_chambolle(frame.astype('uint16'), weight=0.1, multichannel=True)) # TODO: set window around phase correlation dp = cv2.phaseCorrelate(prev_image, image) cx = cx - dp[0] cy = cy - dp[1] xform = np.float32([[1, 0, cx], [0, 1, cy]]) stable_image = cv2.warpAffine(frame.astype('float32'), xform, dsize=(image.shape[1], image.shape[0])) prev_image = image #clip sides ht,wd=np.shape(stable_image) # clip=0.125 #0.25 lt=int(wd*clip) rt=int(wd-wd*clip) up=int(ht*clip) dw=int(ht-ht*clip) stable_image_clipped=stable_image[up:dw,lt:rt] stb_arr_list.append(stable_image_clipped) return stb_arr_list
def phasecorr(imlist,imlist2=None,clip=0): #cv [rowini,rowend,colini,colend] import cv2 cx = 0.0 cy = 0.0 imlist_stb=[] if imlist2!=None: imlist2_stb=[] imi=0 im_prev = imlist[0] im_denoised_prev = np.float32(restoration.denoise_tv_chambolle(im_prev.astype('uint16'), weight=0.1, multichannel=True)) #ref for im in imlist: im_denoised = np.float32(restoration.denoise_tv_chambolle(im.astype('uint16'), weight=0.1, multichannel=True)) # TODO: set window around phase correlation dp = cv2.phaseCorrelate(im_denoised_prev, im_denoised) cx = cx - dp[0] cy = cy - dp[1] xform = np.float32([[1, 0, cx], [0, 1, cy]]) im_stb = cv2.warpAffine(im.astype('float32'), xform, dsize=(im_denoised.shape[1], im_denoised.shape[0])) imlist_stb.append(imclipper(im_stb,clip)) if imlist2!=None: im2=imlist2[imi] im2_stb=cv2.warpAffine(im2.astype('float32'), xform, dsize=(im_denoised.shape[1], im_denoised.shape[0])) imlist2_stb.append(imclipper(im2_stb,clip)) im_denoised_prev = im_denoised imi+=1 if imlist2!=None: return imlist_stb,imlist2_stb else: return imlist_stb
def raw2phasecorr(arr_list, clip=0): #cv cx = 0.0 cy = 0.0 stb_arr_list = [] prev_frame = arr_list[0] prev_image = np.float32( restoration.denoise_tv_chambolle(prev_frame.astype('uint16'), weight=0.1, multichannel=True)) #ref for frame in arr_list: image = np.float32( restoration.denoise_tv_chambolle(frame.astype('uint16'), weight=0.1, multichannel=True)) # TODO: set window around phase correlation dp = cv2.phaseCorrelate(prev_image, image) cx = cx - dp[0] cy = cy - dp[1] xform = np.float32([[1, 0, cx], [0, 1, cy]]) stable_image = cv2.warpAffine(frame.astype('float32'), xform, dsize=(image.shape[1], image.shape[0])) prev_image = image #clip sides ht, wd = np.shape(stable_image) # clip=0.125 #0.25 lt = int(wd * clip) rt = int(wd - wd * clip) up = int(ht * clip) dw = int(ht - ht * clip) stable_image_clipped = stable_image[up:dw, lt:rt] stb_arr_list.append(stable_image_clipped) return stb_arr_list
def rel_pos_estimate(self, method="phase_corr", stack=None, rel_to_top_left=True): if not stack: stack = self.stack rel_pos = [] if method == "phase_corr": for i in range(1, len(self.stack)): rel_pos.append( cv2.phaseCorrelate(self.stack[i - 1], self.stack[i])) if rel_to_top_left: #chain relative positions from the centermost and find the position closest to the mean pos = [[0., 0.]] * len(rel_pos) for i, dx, dy in enumerate(rel_pos[1:], 1): pos[i][0] = pos[i - 1][0] + rel_pos[i][0] pos[i][1] = pos[i - 1][1] + rel_pos[i][1] mean = [0., 0.] for i in range(len(pos)): mean[0] += pos[i][0] mean[1] += pos[i][1] mean[0] /= len(pos) mean[1] /= len(pos) dists = [(x - mean[0])**2 + (y - mean[1])**2 for x, y in pos] idx = dists.index(min(dists)) half_side = self.stack_side / 2 return [(half_side + mean[0] - x, half_side + mean[1] - y) for x, y in pos] else: return rel_pos
def estimation_translation(self): rows, cols = self.img_r.shape img_r = cv2.copyMakeBorder(self.img_r, rows, rows, cols, cols, cv2.BORDER_CONSTANT, value=0) img_s = cv2.copyMakeBorder(self.img_s, rows, rows, cols, cols, cv2.BORDER_CONSTANT, value=0) img_r = np.float32(img_r) img_s = np.float32(img_s) [x0, y0], _ = cv2.phaseCorrelate(img_r, img_s) if abs(x0) > rows or abs(y0) > cols: x0, y0 = np.nan, np.nan self.tx = x0 self.ty = y0
def phaseCorrelate(self): # Reference: https://docs.opencv.org/2.4/modules/imgproc/doc/motion_analysis_and_object_tracking.html#phasecorrelate for frameNo in range(len(self.frames) - 1): firstFrame = self.frames[frameNo] secondFrame = self.frames[frameNo + 1] gFirstFrame = cv2.cvtColor(firstFrame, cv2.COLOR_BGR2GRAY) gSecondFrame = cv2.cvtColor(secondFrame, cv2.COLOR_BGR2GRAY) gFirstFrame = cv2.medianBlur(gFirstFrame, 9) gSecondFrame = cv2.medianBlur(gSecondFrame, 9) threshold, _ = cv2.threshold(src = gFirstFrame, thresh = 0, maxval = 255, type = cv2.THRESH_BINARY | cv2.THRESH_OTSU) gFirstFrame = cv2.Canny(image = gFirstFrame, threshold1 = 0.5 * threshold, threshold2 = threshold) threshold, _ = cv2.threshold(src = gSecondFrame, thresh = 0, maxval = 255, type = cv2.THRESH_BINARY | cv2.THRESH_OTSU) gSecondFrame = cv2.Canny(image = gSecondFrame, threshold1 = 0.5 * threshold, threshold2 = threshold) # Convert to CV_32FC1 gFirstFrame32 = np.float32(gFirstFrame) gSecondFrame32 = np.float32(gSecondFrame) (xDif, yDif), _ = cv2.phaseCorrelate(src1 = gFirstFrame32, src2 = gSecondFrame32) xDif = int(round(-xDif)) yDif = int(round(-yDif)) # Image has shifted if xDif != 0 or yDif != 0: newFrame = np.zeros((self.height, self.width, 3), np.uint8) if xDif < 0: startOldX = - xDif endOldX = self.width startNewX = 0 endNewX = self.width + xDif else: startOldX = 0 endOldX = self.width - xDif startNewX = xDif endNewX = self.width if yDif < 0: startOldY = - yDif endOldY = self.height startNewY = 0 endNewY = self.height + yDif else: startOldY = 0 endOldY = self.height - yDif startNewY = yDif endNewY = self.height print xDif, yDif newFrame[startNewY : endNewY, startNewX : endNewX] = secondFrame[startOldY : endOldY, startOldX : endOldX] else: newFrame = secondFrame.copy() self.frames[frameNo + 1] = newFrame if frameNo % 100 == 0: print int(float(frameNo) / len(self.frames) * 100.0), '%'
def alignPhase(root, colour_template, im1_p, im2_p, transform): # Read the images to be aligned im1 = cv2.imread(im1_p, cv2.IMREAD_COLOR) im2 = cv2.imread(im2_p, cv2.IMREAD_COLOR) im2 = match_histogram_colour(im2, colour_template) if im2.shape[0] > im1.shape[0]: im2 = im2[:im1.shape[0], :, :] else: im1 = im1[:im2.shape[0], :, :] if im2.shape[1] > im1.shape[1]: im2 = im2[:, :im1.shape[1], :] else: im1 = im1[:, :im2.shape[1], :] # Convert images to grayscale im1_gray = cv2.cvtColor(im1, cv2.COLOR_BGR2GRAY) im2_gray = cv2.cvtColor(im2, cv2.COLOR_BGR2GRAY) im1_float = np.float64(im1_gray) / 255.0 im2_float = np.float64(im2_gray) / 255.0 translation, some_other_var = cv2.phaseCorrelate(im1_float, im2_float) M = np.float32([[1, 0, -translation[0]], [0, 1, -translation[1]]]) im2_aligned = cv2.warpAffine(im2, M, (im1.shape[1], im1.shape[0])) im2_fn = os.path.join(root, os.path.basename(im2_p)) cv2.imwrite(im2_fn, im2_aligned) logger.info("Done Phase") del im1, im2, im2_aligned, im1_float, im2_float return im2_fn
def find_pairwise_shift(self, img1: Image, img2: Image, overlap: int, mode: str) -> int: """ Finds size of the img2 """ if mode == 'row': if overlap >= self._default_image_shape[1]: return 0 img1_overlap = img1.shape[1] - overlap img2_overlap = overlap part1 = cv.normalize(img1[:, img1_overlap:], None, 0, 1, cv.NORM_MINMAX, cv.CV_32F) part2 = cv.normalize(img2[:, :img2_overlap], None, 0, 1, cv.NORM_MINMAX, cv.CV_32F) if part1 is None or part2 is None: return 0 shift, error = cv.phaseCorrelate(part1, part2) hor_shift = shift[0] if hor_shift < 0: pairwise_shift = self._default_image_shape[1] - (overlap + hor_shift) else: pairwise_shift = self._default_image_shape[1] - hor_shift elif mode == 'col': if overlap >= self._default_image_shape[0]: return 0 img1_overlap = img1.shape[0] - overlap img2_overlap = overlap part1 = cv.normalize(img1[img1_overlap:, :], None, 0, 1, cv.NORM_MINMAX, cv.CV_32F) part2 = cv.normalize(img2[:img2_overlap, :], None, 0, 1, cv.NORM_MINMAX, cv.CV_32F) if part1 is None or part2 is None: return 0 shift, error = cv.phaseCorrelate(part1, part2) ver_shift = shift[1] if ver_shift < 0: pairwise_shift = self._default_image_shape[0] - (overlap + ver_shift) else: pairwise_shift = self._default_image_shape[0] - ver_shift return pairwise_shift
def phase_correlation(img_match, img_ref): """ openCV phase correction wrapper, as one of the align_func to perform motion correction. Open CV phaseCorrelate function returns (x_offset, y_offset). This wrapper switches the order of result and returns (height_offset, width_offset) to be more consistent with numpy indexing convention. :param img_match: the matching image :param img_ref: the reference image :return: rigid_transform coordinates of alignment (height_offset, width_offset) """ if cv2.__version__[0] == '2': x_offset, y_offset = cv2.phaseCorrelate(img_match.astype(np.float32), img_ref.astype(np.float32)) elif cv2.__version__[0] == '3' or cv2.__version__[0] == '4': (x_offset, y_offset), _ = cv2.phaseCorrelate(img_match.astype(np.float32), img_ref.astype(np.float32)) else: raise EnvironmentError('Do not understand opencv version.') return [y_offset, x_offset]
def ripoc(srcImg1, srcImg2, logpolarMagnitude=40, plotFig=False): grayImg1 = np.float64(cv2.cvtColor(srcImg1, cv2.COLOR_BGRA2GRAY)) / 255.0 grayImg2 = np.float64(cv2.cvtColor(srcImg2, cv2.COLOR_BGRA2GRAY)) / 255.0 center = (grayImg1.shape[0] / 2.0, grayImg1.shape[1] / 2.0) # lpImg1 = cv2.logPolar(grayImg1, center, logpolarMagnitude, cv2.INTER_CUBIC + cv2.WARP_FILL_OUTLIERS) lpImg1 = _logPolar(grayImg1, center, logpolarMagnitude) center = (grayImg2.shape[0] / 2.0, grayImg2.shape[1] / 2.0) # lpImg2 = cv2.logPolar(grayImg2, center, logpolarMagnitude, cv2.INTER_CUBIC + cv2.WARP_FILL_OUTLIERS) lpImg2 = _logPolar(grayImg2, center, logpolarMagnitude) if plotFig: figLP, (figLPLeft, figLPRight) = plt.subplots(ncols=2) figLPLeft.imshow(lpImg1, cmap="gray") figLPRight.imshow(lpImg2, cmap="gray") plt.show(figLP) hann = cv2.createHanningWindow(lpImg1.shape, cv2.CV_64F) ret, resp = cv2.phaseCorrelate(lpImg1, lpImg2, hann) # angle = -((ret[1] + 0.5) * 360.0 / float(lpImg1.shape[0]) ) # scale = np.exp(ret[0] / logpolarMagnitude) angle = ret[1] * 360.0 / float(lpImg1.shape[0]) scale = np.exp(ret[0] / logpolarMagnitude) # print("ret = " + str(ret) + "\nresponse = " + str(resp)) # print("angle = " + str(angle) + "\nscale = " + str(scale)) matTrans = cv2.getRotationMatrix2D(center, angle, scale) imgTrans = cv2.warpAffine(srcImg1, matTrans, srcImg1.shape[0:2], flags=cv2.INTER_LINEAR) if plotFig: figDst, (figDstLeft, figDstRight) = plt.subplots(ncols=2) figDstLeft.imshow(imgTrans) figDstRight.imshow(srcImg2) plt.show(figDst) subtractImg = cv2.subtract(cv2.cvtColor(imgTrans, cv2.COLOR_BGRA2RGB), cv2.cvtColor(srcImg2, cv2.COLOR_BGRA2RGB)) plt.imshow(subtractImg) plt.show() grayTrans = np.float64(cv2.cvtColor(imgTrans, cv2.COLOR_BGRA2GRAY)) / 255.0 ret, resp = cv2.phaseCorrelate(grayTrans, grayImg2, hann) return angle, scale, ret, resp
def main(): if mode is "phase": print("Image1,Image2,PixelOffsetX,PixelOffsetY,OffsetX,OffsetY") elif mode is "template": print("Image,Template,PixelOffsetX,PixelOffsetY,OffsetX,OffsetY") filenames = [ f for f in os.listdir(imageDirectory) if os.path.isfile(os.path.join(imageDirectory, f)) ] processedFiles = list() for file in filenames: try: fullFilename = os.path.join(imageDirectory, file) splitFilename = os.path.splitext(os.path.basename(file)) idLetter = splitFilename[0][-1] complementaryFilename = splitFilename[0][:-1] + opposite[ idLetter] + splitFilename[1] complementaryFile = os.path.join(imageDirectory, complementaryFilename) if file in processedFiles or complementaryFilename in processedFiles: continue # Don't run if we've already processed this file. else: processedFiles.append(file) processedFiles.append(complementaryFilename) if mode is "phase": # Read the images and convert them to a numpy array image1 = cv2.imread(fullFilename, 0) image2 = cv2.imread(complementaryFile, 0) image1 = np.float64(image1) image2 = np.float64(image2) # Phase correlate result = cv2.phaseCorrelate(image1, image2) offsetPix = (abs(result[0]), abs(result[1])) elif mode is "template": image1 = cv2.imread(fullFilename) templateFull = cv2.imread(complementaryFile) height, width, channels = templateFull.shape intervalH = height / 4 intervalW = width / 4 template = templateFull[intervalH - 1:intervalH * 3 - 1, intervalW - 1:intervalW * 3 - 1] result = cv2.matchTemplate(image1, template, cv2.TM_CCOEFF_NORMED) min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(result) offsetPix = (abs(max_loc[0] - intervalW - 1), abs(max_loc[1] - intervalH - 1)) offsetNm = (abs(offsetPix[0] * pixelSizeNanometers), abs(offsetPix[1] * pixelSizeNanometers)) print "%s,%s,%s,%s,%s,%s" % (file, complementaryFilename, offsetPix[0], offsetPix[1], offsetNm[0], offsetNm[1]) except: # We failed to process a file, so we won't go there again. processedFiles.append(file) return
def cam_vel(self, frame): ''' dx, dy = cv2.spatialGradient(frame) IxIy = np.append(dx.reshape((dx.size,1)), dy.reshape((dy.size,1)), axis = 1) It = (frame - self.prev_frame).reshape((frame.size, 1)) mat = np.dot(IxIy.T, IxIy) vel = np.dot(np.linalg.inv(mat), np.dot(IxIy.T, It)) ''' vel = cv2.phaseCorrelate(self.prev_frame/255., frame/255.) return (vel[0][0], vel[0][1])
def alignDFT(im0, im1, red): im0r, im1r = np.array(img_as_ubyte(skimage.transform.rescale(im0, 1 / red)), dtype='float32'), np.array(img_as_ubyte( skimage.transform.rescale(im1, 1 / red)), dtype='float32') results = cv2.phaseCorrelate(im0r, im1r) warp_matrix = np.array([[1., 0., red * (-float(results[0][0]))], [0., 1., red * (-float(results[0][1]))]]) stde = float(results[1]) return np.copy(warp_matrix), np.copy(stde)
def main(): # 2枚の画像をグレースケールで取得 im1 = cv2.imread("test1.png", 0) im2 = cv2.imread("test2.png", 0) # 画像データをfloat32型に型変換 im1 = np.float32(im1) im2 = np.float32(im2) # 位相相関を計算して2枚の画像のズレを求める dx, dy = cv2.phaseCorrelate(im1, im2) print(u"x方向のズレ:" + str(dx)) print(u"y方向のズレ:" + str(dy))
def calculateOffsetForPhaseCorrleateIncre(self, images): ''' Stitch two images :param images: [imageA, imageB] :param registrateMethod: list: :param fuseMethod: :param direction: stitching direction :return: ''' (imageA, imageB) = images offset = [0, 0] status = False maxI = (np.floor(0.5 / self.roiRatio) + 1).astype(int)+ 1 iniDirection = self.direction localDirection = iniDirection for i in range(1, maxI): # self.printAndWrite(" i=" + str(i) + " and maxI="+str(maxI)) while(True): # get the roi region of images # self.printAndWrite(" localDirection=" + str(localDirection)) roiImageA = self.getROIRegionForIncreMethod(imageA, direction=localDirection, order="first", searchRatio = i * self.roiRatio) roiImageB = self.getROIRegionForIncreMethod(imageB, direction=localDirection, order="second", searchRatio = i * self.roiRatio) # hann = cv2.createHanningWindow(winSize=(roiImageA.shape[1], roiImageA.shape[0]), type=5) # (offsetTemp, response) = cv2.phaseCorrelate(np.float32(roiImageA), np.float32(roiImageB), window=hann) (offsetTemp, response) = cv2.phaseCorrelate(np.float64(roiImageA), np.float64(roiImageB)) offset[0] = np.int(offsetTemp[1]) offset[1] = np.int(offsetTemp[0]) # self.printAndWrite("offset: " + str(offset)) # self.printAndWrite("respnse: " + str(response)) if response > self.phaseResponseThreshold: status = True if status == True: break else: localDirection = self.directionIncrease(localDirection) if localDirection == iniDirection: break if status == True: if localDirection == 1: offset[0] = offset[0] + imageA.shape[0] - int(i * self.roiRatio * imageA.shape[0]) elif localDirection == 2: offset[1] = offset[1] + imageA.shape[1] - int(i * self.roiRatio * imageA.shape[1]) elif localDirection == 3: offset[0] = offset[0] - (imageB.shape[0] - int(i * self.roiRatio * imageB.shape[0])) elif localDirection == 4: offset[1] = offset[1] - (imageB.shape[1] - int(i * self.roiRatio * imageB.shape[1])) self.direction = localDirection break if status == False: return (status, " The two images can not match") elif status == True: self.printAndWrite(" The offset of stitching: dx is " + str(offset[0]) + " dy is " + str(offset[1])) return (status, offset)
def unshake(src, sample): """ Compensate camera shaking. :rtype : np.ndarray """ f0 = cv2.cvtColor(np.float32(sample), cv2.COLOR_BGR2GRAY) f = np.float32(cv2.cvtColor(np.float32(src), cv2.COLOR_BGR2GRAY)) (dx, dy) = cv2.phaseCorrelate(f, f0) (h, w, _) = src.shape return cv2.warpAffine(src, M=np.asarray([[1, 0, dx], [0, 1, dy]]), dsize=(w, h), borderMode=cv2.BORDER_TRANSPARENT)
def run(self): # This method runs in a separate thread global px global f_refer global pixel while not self.terminated: # Wait for an image to be written to the stream if self.event.wait(1): try: self.stream.seek(0) # Read the image and do some processing on it #Image.open(self.stream) #frame=self.stream.array data = np.fromstring(self.stream.getvalue(), dtype=np.uint8) frame = cv2.imdecode(data, 1) gray_image = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) [M, N] = find_centroid(gray_image, 75) chopped_image = gray_image[(M - 159):(M + 160), (N - 159):(N + 160)] [x, y, r] = get_ROI(chopped_image) x_G = M - 160 + x y_G = N - 160 + y current = gray_image[(x_G - r + 1):(x_G + r), (y_G - r + 1):(y_G + r)] f_current = np.float32(current) f_refer = f_current [fa, fb] = cv2.phaseCorrelate(f_current, f_refer) px = math.sqrt(math.pow(fa, 2) + math.pow(fb, 2)) if px > 2: pixel.append(px) else: pixel.append(0) plt.plot(pixel) #... # Set done to True if you want the script to terminate # at some point #self.owner.done=True finally: # Reset the stream and event self.stream.seek(0) self.stream.truncate() self.event.clear() # Return ourselves to the available pool with self.owner.lock: self.owner.pool.append(self)
def register_FLS_image(polar_image1, cartesian_image1, polar_image2, cartesian_image2): polar_image1 = copy.deepcopy(np.float32(polar_image1)) polar_image2 = copy.deepcopy(np.float32(polar_image2)) cartesian_image1 = copy.deepcopy(np.float32(cartesian_image1)) cartesian_image2 = copy.deepcopy(np.float32(cartesian_image2)) polar_image1 = cv2.GaussianBlur(polar_image1, (3, 3), 0) polar_image2 = cv2.GaussianBlur(polar_image2, (3, 3), 0) cartesian_image1 = cv2.GaussianBlur(cartesian_image1, (3, 3), 0) cartesian_image2 = cv2.GaussianBlur(cartesian_image2, (3, 3), 0) (polar_dx, _), _ = cv2.phaseCorrelate(polar_image1, polar_image2) theta = polar_dx / 10. #[degree] rows, cols = cartesian_image1.shape m_def = np.float32([[1, 0, cols / 2], [0, 1, 0]]) #回転した時に情報が消えないように範囲を広げる cartesian_image1 = cv2.warpAffine(cartesian_image1, m_def, (cols*2, rows*2)) cartesian_image2 = cv2.warpAffine(cartesian_image2, m_def, (cols*2, rows*2)) rotate_matrix = cv2.getRotationMatrix2D((cols, rows), theta, 1) rotated_cartesian_image2 = cv2.warpAffine(cartesian_image2, rotate_matrix, (cols*2, rows*2)) (dx, dy), _ = cv2.phaseCorrelate(cartesian_image1, rotated_cartesian_image2) return theta, dx, dy
def phase_correlation(src_1, src_2): rows, cols = src_1.shape src_ph = cv2.copyMakeBorder(src_1, rows, rows, cols, cols, cv2.BORDER_CONSTANT, value=0) dst_ph = cv2.copyMakeBorder(src_2, rows, rows, cols, cols, cv2.BORDER_CONSTANT, value=0) src_1_pc = np.float32(src_ph) src_2_pc = np.float32(dst_ph) [x0, y0], _ = cv2.phaseCorrelate(src_1_pc, src_2_pc) if abs(x0) > rows or abs(y0) > cols: return np.nan, np.nan else: return x0, y0
def estimation_translation(img_r, img_s): rows, cols = img_r.shape # img_r = cv2.copyMakeBorder(img_r, rows, rows, cols, cols, cv2.BORDER_CONSTANT, value=0) # img_s = cv2.copyMakeBorder(img_s, rows, rows, cols, cols, cv2.BORDER_CONSTANT, value=0) img_r = np.float32(img_r) img_s = np.float32(img_s) [x0, y0], _ = cv2.phaseCorrelate(img_r, img_s) if abs(x0) > rows or abs(y0) > cols: return np.nan, np.nan else: return x0, y0
def imageProc(fname): capture = cv2.VideoCapture(fname) #initialize variables for HSV limits and blur radius: blurRad = 5 #image blur radius #load camera #print capture.get(cv.CV_CAP_PROP_FPS) frsize = (int(capture.get(cv.CV_CAP_PROP_FRAME_WIDTH)), int(capture.get(cv.CV_CAP_PROP_FRAME_HEIGHT))) vidWriter = cv2.VideoWriter("stabilized.wmv", cv.CV_FOURCC('M', 'J', 'P', 'G'), capture.get(cv.CV_CAP_PROP_FPS), frsize) print vidWriter first = True shift = np.array([0, 0]) while True: ret, img1 = capture.read() if ret: #read the camera image: if first: first = False else: imgNew = cv2.cvtColor(img1, cv2.COLOR_BGR2GRAY) imgLast = cv2.cvtColor(img2, cv2.COLOR_BGR2GRAY) #process frames #perform stabilization reet = cv2.phaseCorrelate(np.float32(imgNew), np.float32(imgLast)) #lowpass filter alpha = 0.33333 shift[0] = alpha * reet[0] + (1 - alpha) * shift[0] shift[1] = alpha * reet[1] + (1 - alpha) * shift[1] #shift = reet #shift the new image M = np.float32([[1, 0, shift[0]], [0, 1, shift[1]]]) cols, rows = np.size(img1, axis=1), np.size(img1, axis=0) img3 = cv2.warpAffine(img2, M, (cols, rows)) #export image vidWriter.write(img3) img2 = np.copy(img1) else: break if vidWriter.isOpened(): vidWriter.release()
def _get_obs_frames(self): if self.phase_correlate: shift, conf = cv2.phaseCorrelate(self.grayscale_frames[0], self.grayscale_frames[2]) if np.linalg.norm(shift) > 0.25: # print(f"shift: {shift}, confidence: {conf}") shift = tuple(reversed(shift)) + (0, ) scipy.ndimage.shift(self.frames[0], shift=shift, output=self.aligned) # self._plot(self.aligned, self.frames[2]) return (self.aligned, self.frames[2]) return (self.frames[0], self.frames[2])
def write(self, data): img = np.reshape(np.fromstring(data, dtype=np.uint8), (240, 320, 3)) # cv2.imshow("img", img) # cv2.waitKey(1) if self.first: self.first = False self.first_img = cv2.cvtColor(np.float32(img), cv2.COLOR_RGB2GRAY) self.prev_img = deepcopy(self.first_img) self.prev_time = rospy.get_time() else: curr_img = cv2.cvtColor(np.float32(img), cv2.COLOR_RGB2GRAY) curr_time = rospy.get_time() corr_first = cv2.phaseCorrelate(self.first_img, curr_img) corr_int = cv2.phaseCorrelate(self.prev_img, curr_img) self.prev_img = curr_img print 'first', corr_first print 'int', corr_int if corr_first[1] > self.threshold: # not lost print "first" self.pos = [ corr_first[0][0] * self.z / 320., corr_first[0][1] * self.z / 240. ] else: print "integrated" self.pos[0] += corr_int[0][0] * self.z / 320. self.pos[1] += corr_int[0][1] * self.z / 240. self.lr_err.err = self.pos[0] self.fb_err.err = self.pos[1] mode = Mode() mode.x_i += self.lr_pid.step(self.lr_err.err, self.prev_time - curr_time) mode.y_i += self.fb_pid.step(self.fb_err.err, self.prev_time - curr_time) self.pospub.publish(mode) prev_time = curr_time
def findDrift(imArray): Gsize = 1 res = [(0,0)] x, y = 0, 0 karnel = np.ones([Gsize,Gsize]) karnel = 1.0*karnel/sum(karnel) im1 = np.float32(cv2.GaussianBlur(a16a8(imArray[0]), (Gsize,Gsize), 0)) for i in range(1,len(imArray)): im2 = np.float32(cv2.GaussianBlur(a16a8(imArray[i]), (Gsize,Gsize), 0)) xt, yt= cv2.phaseCorrelate(im1,im2)[0] if np.sqrt(xt**2+yt**2)>50: xt, yt =0., 0. x-=xt y-=yt res.append((x,y)) im1=im2 return res
def imageProc(fname): capture = cv2.VideoCapture(fname) #initialize variables for HSV limits and blur radius: blurRad = 5#image blur radius #load camera #print capture.get(cv.CV_CAP_PROP_FPS) frsize = (int(capture.get(cv.CV_CAP_PROP_FRAME_WIDTH)),int(capture.get(cv.CV_CAP_PROP_FRAME_HEIGHT))) vidWriter = cv2.VideoWriter("stabilized.wmv",cv.CV_FOURCC('M','J','P','G'),capture.get(cv.CV_CAP_PROP_FPS),frsize) print vidWriter first = True shift = np.array([0,0]) while True: ret,img1 = capture.read() if ret: #read the camera image: if first: first = False else: imgNew = cv2.cvtColor(img1,cv2.COLOR_BGR2GRAY) imgLast = cv2.cvtColor(img2,cv2.COLOR_BGR2GRAY) #process frames #perform stabilization reet = cv2.phaseCorrelate(np.float32(imgNew),np.float32(imgLast)) #lowpass filter alpha = 0.33333 shift[0] = alpha*reet[0] + (1-alpha)*shift[0] shift[1] = alpha*reet[1] + (1-alpha)*shift[1] #shift = reet #shift the new image M = np.float32([[1,0,shift[0]],[0,1,shift[1]]]) cols,rows = np.size(img1,axis=1),np.size(img1,axis=0) img3 = cv2.warpAffine(img2,M,(cols,rows)) #export image vidWriter.write(img3) img2 = np.copy(img1) else: break if vidWriter.isOpened(): vidWriter.release()
def estimate(self, src: np.ndarray, dst: np.ndarray): """ Parameters ---------- src: np.ndarray uint8, needed for opencv operations dst: np.ndarray uint8, needed for opencv operations """ if src.shape != dst.shape: src, dst = pad_to_match(src, dst) (dx, dy), _ = cv2.phaseCorrelate(src1=dst.astype('float32'), src2=src.astype('float32')) self.matrix = np.eye(3).astype('float32') self.matrix[0, 2] = dx self.matrix[1, 2] = dy
def stabilise(self): print 'Stabilising Video' for frameNo in range(len(self.frames) - 1): firstFrame = self.frames[frameNo] secondFrame = self.frames[frameNo + 1] gFirstFrame = cv2.cvtColor(firstFrame, cv2.COLOR_BGR2GRAY) gSecondFrame = cv2.cvtColor(secondFrame, cv2.COLOR_BGR2GRAY) gFirstFrame = cv2.medianBlur(gFirstFrame, 9) gSecondFrame = cv2.medianBlur(gSecondFrame, 9) threshold, _ = cv2.threshold(src=gFirstFrame, thresh=0, maxval=255, type=cv2.THRESH_BINARY | cv2.THRESH_OTSU) gFirstFrame = cv2.Canny(image=gFirstFrame, threshold1=0.5 * threshold, threshold2=threshold) threshold, _ = cv2.threshold(src=gSecondFrame, thresh=0, maxval=255, type=cv2.THRESH_BINARY | cv2.THRESH_OTSU) gSecondFrame = cv2.Canny(image=gSecondFrame, threshold1=0.5 * threshold, threshold2=threshold) # Convert frames to CV_32FC1 gFirstFrame32 = np.float32(gFirstFrame) gSecondFrame32 = np.float32(gSecondFrame) # Find the translation between the two frames (xDif, yDif), _ = cv2.phaseCorrelate(src1=gSecondFrame32, src2=gFirstFrame32) translationMatrix = np.float32([[1, 0, xDif], [0, 1, yDif]]) newFrame = cv2.warpAffine(secondFrame, translationMatrix, (self.width, self.height)) self.frames[frameNo + 1] = newFrame
def calcPhaseCorrShift(fimg1, fimg2): frm1=cv2.imread(fimg1, 0) frm2=cv2.imread(fimg2, 0) frm1=cv2.resize(frm1.astype(np.float).copy(), (int(frm1.shape[1]/kdif), int(frm1.shape[0]/kdif))) frm2=cv2.resize(frm2.astype(np.float).copy(), (int(frm2.shape[1]/kdif), int(frm2.shape[0]/kdif))) frm1_nrm=cv2.normalize(frm1.copy(), None, 0,255, cv2.NORM_MINMAX, cv2.CV_8U) frm2_nrm=cv2.normalize(frm2.copy(), None, 0,255, cv2.NORM_MINMAX, cv2.CV_8U) hann=cv2.createHanningWindow((frm1.shape[1], frm1.shape[0]), cv2.CV_64F) # shift = cv2.phaseCorrelate(frm1, frm2, hann) dxy=shift[0] frm2_nrm_shift=np.roll(frm2_nrm, int(math.floor(-dxy[0])), 1) frm2_nrm_shift=np.roll(frm2_nrm_shift, int(math.floor(-dxy[1])), 0) tmp=np.zeros((frm1.shape[0], frm1.shape[1], 3), np.uint8) tmp[:,:,2]=frm1_nrm tmp[:,:,1]=frm2_nrm_shift tmp[:,:,0]=0 # tmp[:,:,1]=frm2_nrm cv2.imshow("frame #2 shift", cv2.resize(tmp, (tmp.shape[1]/1, tmp.shape[0]/1))) return dxy
def add_img(self, img, visualize=True): assert img.shape == self._img_ref.shape img = img.astype('float32') / 255.0 rst = [] for row, c0, c1 in self._roi_list: dx, dy = cv2.phaseCorrelate( self._img_ref[row:row+self.WIN_HEIGHT, c0:c1], img[row:row+self.WIN_HEIGHT, c0:c1]) rst.append((dx, dy)) if visualize: x, y = map(np.array, zip(*rst)) phase = np.arctan2(x, y) amph = np.sqrt(x ** 2 + y ** 2) plt.figure() plt.subplot(2, 1, 1) plt.plot(self._roi_row, amph) plt.subplot(2, 1, 2) plt.plot(self._roi_row, phase) return rst
new=conv.convert2(0.0,0.0,-difx,dify) new32=new.astype(np.float32) dd,dt=cv2.phaseCorrelate(inc,new32); print dd,dt new=conv.convert2(-pr.dx*dd[0],pr.dy*dd[1],-difx,dify) new32=new.astype(np.float32) dd,dt=cv2.phaseCorrelate(inc,new32); print dd,dt ''' print("*** Phase Only Correlation ***") for band in [1,2,3,4]: print ' Band '+str(band)+':' sat.read_band(band) new=conv.convert2(0.0,0.0,0.0,0.0) new32=new.astype(np.float32) dd,dt=cv2.phaseCorrelate(inc,new32); print dd,dt new=conv.convert2(-pr.dx*dd[0],pr.dy*dd[1],0.0,0.0) new32=new.astype(np.float32) dd,dt=cv2.phaseCorrelate(inc,new32); print dd,dt new=conv.convert2(0.0,0.0,-difx,dify) new32=new.astype(np.float32) dd,dt=cv2.phaseCorrelate(inc,new32); print dd,dt new=conv.convert2(-pr.dx*dd[0],pr.dy*dd[1],-difx,dify) new32=new.astype(np.float32) dd,dt=cv2.phaseCorrelate(inc,new32); print dd,dt #pr.write_tif('../'+fnew+'/band'+str(band)+'.tif',new,1) #ut.gwrite(sat,fnew) exit()
#sat.display('sat4',600,600) #demx=cv2.resize(dem,(600,600)) #cv2.imshow('dem',demx/np.max(demx)) dem[dem<0.0]=0.0 inc=ut.incident(dem,sat) #incx=cv2.resize(inc,(600,600)) #cv2.imshow('incx',incx/np.max(incx)) #cv2.destroyWindow('incx') #new32=new.astype(np.float32) #dd,dt=cv2.phaseCorrelate(inc,new32) #new=conv.convert(-pr.dx*dd[0],pr.dy*dd[1]) #new32=new.astype(np.float32) #cv2.phaseCorrelate(inc,new32) print("*** Phase Only Correlation ***") for band in [1,2,3,4,5,6,7,9]: sat.read_band(band) conv=ut.convert(sat,xmax,ymax) new=conv.convert(0.0,0.0) new32=new.astype(np.float32) print ' Band '+str(band)+':' #print cv2.phaseCorrelate(inc,new32) dd,dt=cv2.phaseCorrelate(inc,new32) print -pr.dx*dd[0],pr.dy*dd[1],dt exit()
# If webcam read successful, loop indefinitely while retval: # Define the frame which the webcam will show frame_show = frame # Convert frame to grayscale to make phase comparison # http://docs.opencv.org/2.4/modules/imgproc/doc/miscellaneous_transformations.html#cvtcolor frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) # Convert frames into floating point f_frame = np.float32(frame) f_previous_frame = np.float32(previous_frame) # Run a phase correlation # http://docs.opencv.org/2.4/modules/imgproc/doc/motion_analysis_and_object_tracking.html#phasecorrelate (dx,dy) = cv2.phaseCorrelate(f_frame,f_previous_frame) # Determine motion from the phase correlation if abs(dx) > MOTION_THRESHOLD and abs(dy) > MOTION_THRESHOLD: # Write some text onto the frame # http://docs.opencv.org/2.4/modules/core/doc/drawing_functions.html#puttext font_typeface = FONT_FACES[5] font_scale = 2 font_color = (0,0,255) font_weight = 5 x = 0 y = 50 cv2.putText(frame_show, "Motion!", (x,y), font_typeface, font_scale, font_color, font_weight) # Show the image on the screen
name = sys.argv[1] nout = sys.argv[2] except: print("Error, introduce nombre de los ficheros de entrada y salida") sys.exit() cap = cv2.VideoCapture(name) fps = cap.get(cv2.cv.CV_CAP_PROP_FPS) size = (int(cap.get( cv.CV_CAP_PROP_FRAME_WIDTH)),int(cap.get(cv.CV_CAP_PROP_FRAME_HEIGHT))) ret, old_frame = cap.read() old_gray = cv2.cvtColor(old_frame, cv2.COLOR_BGR2GRAY) ret = True frames = [] while(ret): ret,frame = cap.read() if ret: frame_gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) h, w = old_frame.shape[:2] old_gray = np.float32(old_gray) frame_gray = np.float32(frame_gray) dx, dy = cv2.phaseCorrelate(old_gray, frame_gray) M = np.array([[1, 0, dx],[0, 1, dy]]) old_frame = cv2.warpAffine(frame, M, (w,h), flags=cv2.INTER_LINEAR + cv2.WARP_INVERSE_MAP) frames.append(old_frame) old_gray = cv2.cvtColor(old_frame, cv2.COLOR_BGR2GRAY) writer = cv2.VideoWriter(nout, cv.CV_FOURCC('M','J','P','G'), fps, size) for frm in frames: writer.write(frm) cap.release()
def phaseCorrelation(input, input_pre): input_32 = np.float32(input) input_pre_32 = np.float32(input_pre) dx, dy = cv2.phaseCorrelate(input_32,input_pre_32) return dx, dy
# fnfrm1='/home/ar/data/UAV_Drone_Project/VID_20150324_012829/image-01-013.jpeg' # fnfrm2='/home/ar/data/UAV_Drone_Project/VID_20150324_012829/image-01-014.jpeg' xy_frm1=np.array([825, 618]) xy_frm2=np.array([1147, 675]) if __name__=='__main__': frm1=cv2.imread(fnfrm1, 0) frm2=cv2.imread(fnfrm2, 0) frm1=frm1.astype(np.float) frm2=frm2.astype(np.float) hann=cv2.createHanningWindow((frm1.shape[1], frm1.shape[0]), cv2.CV_64F) hann2=cv2.createHanningWindow((100,100), cv2.CV_64F) hann2_nrm=cv2.normalize(hann2, None, 0,255, cv2.NORM_MINMAX, cv2.CV_8U) cv2.imshow("F**K", hann2_nrm) shift = cv2.phaseCorrelate(frm1, frm2, hann) dxy=shift[0] print shift print xy_frm2-xy_frm1 hann_nrm=cv2.normalize(hann, None, 0,255, cv2.NORM_MINMAX, cv2.CV_8U) frm1_nrm=cv2.normalize(frm1, None, 0,255, cv2.NORM_MINMAX, cv2.CV_8U) frm2_nrm=cv2.normalize(frm2, None, 0,255, cv2.NORM_MINMAX, cv2.CV_8U) cv2.imshow("hanning", hann_nrm) cv2.imshow("frame #1", frm1_nrm) # cv2.imshow("frame #2", frm2_nrm) frm2_nrm_shift=np.roll(frm2_nrm, int(math.floor(-dxy[0])), 1) frm2_nrm_shift=np.roll(frm2_nrm_shift, int(math.floor(-dxy[1])), 0) tmp=np.zeros((frm1.shape[0], frm1.shape[1], 3), np.uint8) tmp[:,:,2]=frm1_nrm tmp[:,:,0]=frm2_nrm_shift tmp[:,:,1]=0