def __estimate_motion(self, imgs, win=5, eig_th=1e-4): img0 = imgs[0] img1 = imgs[1] img0_gray = cv2.cvtColor(img0, cv2.COLOR_BGR2GRAY) img1_gray = cv2.cvtColor(img1, cv2.COLOR_BGR2GRAY) w, h = np.meshgrid(range(img0.shape[1]), range(img0.shape[0])) loc0 = (np.vstack((w.flatten(), h.flatten())).T).astype('float32') flow = cv2.calcOpticalFlowFarneback(img0_gray, img1_gray, 0.5, 0, win, 3, 5, 1.2, 0) flow = (np.vstack((flow[:, :, 0].flatten(), flow[:, :, 1].flatten())).T).astype('float32') # Removing irrelevant flows minEig = cv2.cornerMinEigenVal(img0_gray, blockSize=win * 3, borderType=cv2.BORDER_REPLICATE) loc0_of = loc0[minEig.flatten() > eig_th, :] loc1_of = flow[minEig.flatten() > eig_th, :] + loc0_of # Surf-based match loc0_sf, loc1_sf = self.__calc_surf(imgs) loc0_all = np.vstack((loc0_of, loc0_sf)) loc1_all = np.vstack((loc1_of, loc1_sf)) hom = cv2.findHomography(loc0_all, loc1_all, cv2.cv.CV_RANSAC, 1)[0] gm = cv2.perspectiveTransform(np.array([loc0]), hom)[0] - loc0 lm = flow - gm gm = gm[:, 0] * 1j + gm[:, 1] lm = lm[:, 0] * 1j + lm[:, 1] lm[minEig.flatten() < eig_th] = 0 return gm, lm
def detect(self, frame, img_pyr, quality_level): grid_shape = (self.grid_rows, self.grid_cols) n_grids = self.grid_cols * self.grid_rows corners = [Corner(0, 0, 0, 0) for i in range(n_grids)] best_eigs = [] fts = [] for level in range(self.pyr_levels): scale = 2**level eigs = cv2.cornerMinEigenVal(img_pyr[0], 3) best_eigs.append(eigs.max()) kps = self.fast.detect(img_pyr[level], None) for kp in kps: x, y = kp.pt x, y = int(x), int(y) row_idx = math.floor(y * scale / self.cell_size) col_idx = math.floor(x * scale / self.cell_size) k = np.ravel_multi_index([row_idx, col_idx], grid_shape) if self.grid_occupancy[k]: continue score = eigs[y, x] if score > corners[k].score: corners[k] = Corner(x * scale, y * scale, score, level) for c in corners: if c.score > quality_level * best_eigs[c.level]: pt = np.array([c.x, c.y], dtype=np.float32 ) # cv2.calcOpticalFlowPyrLK requires float32 fts.append(Feature(frame, pt, c.level)) self.reset_grid() return fts
def main(): gdal.AllRegister() infile = auxil.select_infile() if infile: inDataset = gdal.Open(infile,GA_ReadOnly) cols = inDataset.RasterXSize rows = inDataset.RasterYSize else: return # read first image band rasterBand = inDataset.GetRasterBand(1) band = rasterBand.ReadAsArray(0,0,cols,rows)\ .astype(uint8) # corner detection, window size 7x7 result = cv.cornerMinEigenVal(band, 7) # write to disk outfile,fmt = auxil.select_outfilefmt() if outfile: driver = gdal.GetDriverByName(fmt) outDataset = driver.Create(outfile, cols,rows,1,GDT_Float32) outBand = outDataset.GetRasterBand(1) outBand.WriteArray(result,0,0) outBand.FlushCache() outDataset = None inDataset = None
def __DenseSample(self, gray_frame, prev_points,i): # Prepare usage parameters width = int(gray_frame.shape[0]/self.DENSE_SAMPLE_PARAM.MIN_DIST) height = int(gray_frame.shape[1]/self.DENSE_SAMPLE_PARAM.MIN_DIST) x_max = int(self.DENSE_SAMPLE_PARAM.MIN_DIST*width) y_max = int(self.DENSE_SAMPLE_PARAM.MIN_DIST*height) offset = int(self.DENSE_SAMPLE_PARAM.MIN_DIST/2.0) # Prepare sampling points all_points = numpy.array([[w, h] for w in range(width) for h in range(height)]) if prev_points.size>0: enable_prev_flg2 = ((prev_points[:,0] < self.x_max[i]) & (prev_points[:,1] < self.y_max[i])) prev_points_1 = (prev_points[enable_prev_flg2]/self.DENSE_SAMPLE_PARAM.MIN_DIST).astype(numpy.int16) enable_point_flg2=numpy.full((self.width_list[i],self.height_list[i]),True) enable_point_flg2[prev_points_1[:,0],prev_points_1[:,1]]=numpy.full((prev_points_1.shape[0]),False) enable_points2 = self.all_points[i][enable_point_flg2]*self.DENSE_SAMPLE_PARAM.MIN_DIST + self.offset else: enable_points2=numpy.reshape(self.all_points[i],(-1,2))*self.DENSE_SAMPLE_PARAM.MIN_DIST + self.offset eigen_mat = cv2.cornerMinEigenVal(gray_frame, self.DENSE_SAMPLE_PARAM.EIGEN_BLICK_SIZE, self.DENSE_SAMPLE_PARAM.EIGEN_APERTURE_SIZE) # Calculate the eigenvalue threshold for corner detection max_value = cv2.minMaxLoc(eigen_mat)[1] eigen_thresh = max_value*self.DENSE_SAMPLE_PARAM.QUALITY # Extract corner points enable_point_eigen = eigen_mat[enable_points2[:,0], enable_points2[:,1]] corner_eigen_flg = (enable_point_eigen > eigen_thresh) corner_points = enable_points2[corner_eigen_flg] return corner_points
def main(): gdal.AllRegister() infile = auxil.select_infile() if infile: inDataset = gdal.Open(infile, GA_ReadOnly) cols = inDataset.RasterXSize rows = inDataset.RasterYSize else: return # read first image band rasterBand = inDataset.GetRasterBand(1) band = rasterBand.ReadAsArray(0,0,cols,rows)\ .astype(uint8) # corner detection, window size 7x7 result = cv.cornerMinEigenVal(band, 7) # write to disk outfile, fmt = auxil.select_outfilefmt() if outfile: driver = gdal.GetDriverByName(fmt) outDataset = driver.Create(outfile, cols, rows, 1, GDT_Float32) outBand = outDataset.GetRasterBand(1) outBand.WriteArray(result, 0, 0) outBand.FlushCache() outDataset = None inDataset = None
def dense_sample(grey, mask, points, quality = quality, min_distance = min_distance): width = grey.shape[1] / min_distance height = grey.shape[0] / min_distance eig = np.zeros((grey.shape[0], grey.shape[1]), np.float32) cv2.cornerMinEigenVal(src = grey, blockSize = 3, dst = eig, ksize = 3) minVal, maxVal, minLoc, maxLoc = cv2.minMaxLoc(eig, mask) # remove if needed threshold = maxVal * quality counters = np.zeros(width * height, np.uint) x_max = min_distance * width y_max = min_distance * height for p in points: x = p[0] y = p[1] if x >= x_max or y >= y_max: continue x /= min_distance y /= min_distance counters[y * width + x] += 1 new_points = [] index = 0 offset = min_distance / 2 for i in range(height): for j in range(width): if counters[index] > 0: continue x = j * min_distance + offset y = i * min_distance + offset if(eig[y, x] > threshold and mask[y, x] == 255): new_points.append([x,y]); index += 1 return new_points
def main(): # Load the image image = cv.imread("box_in_scene.png") # Display the original image (colour) plt.imshow(image) plt.axis("off") plt.title("Original image") plt.show() # Parameters block = 5 aperature = 5 # Define the window (which I later reference for the slider) window = 'Harris Corner Detector' # Define threshold values (slider) thresholdValue = 50 maxThresholdValue = 100 # Convert image to grayscale image = cv.cvtColor(image, cv.COLOR_BGR2GRAY) dst = cv.cornerMinEigenVal(image, block, aperature) dstMin = np.amin(dst) dstMax = np.amax(dst) def detect_corners(val): # Make a copy of the image each time threshold slider changes image2 = np.copy(image) thresholdValue = max(val, 1) # Iterate the pixels in the image for x in range(image.shape[0]): for y in range(image.shape[1]): # Non-maximum supression if dst[x, y] > dstMin + ( dstMax - dstMin) * thresholdValue / maxThresholdValue: # Draw the circle cv.circle(image2, (y, x), 3, 255, 1) cv.imshow(window, image2) # Instantiate window cv.namedWindow(window) # Instantiate threshold slider cv.createTrackbar('Threshold:', window, thresholdValue, maxThresholdValue, detect_corners) detect_corners(thresholdValue) cv.waitKey()
def detect(self, frame, img_pyr, quality_level): grid_shape = (self.grid_rows, self.grid_cols) n_grids = self.grid_cols * self.grid_rows corners = [Corner(0, 0, 0, 0) for i in range(n_grids)] best_eigs = [] fts = [] for level in range(self.pyr_levels): scale = 2**level eigs = cv2.cornerMinEigenVal(img_pyr[0], 3) best_eigs.append(eigs.max()) kps = cv2.goodFeaturesToTrack(img_pyr[level], 1000, 0.01, 10) for kp in kps: x, y = kp[0].astype(int) row_idx = math.floor(y * scale / self.cell_size) col_idx = math.floor(x * scale / self.cell_size) k = np.ravel_multi_index([row_idx, col_idx], grid_shape) if self.grid_occupancy[k]: continue score = eigs[y, x] if score > corners[k].score: corners[k] = Corner(x * scale, y * scale, score, level) # with open('feature2.txt', 'r') as f: # for i, line in enumerate(f): # line = line.strip() # data = line.split(',') # x, y = int(data[0]), int(data[1]) # # scale = int(data[2]) # score = float(data[2]) # L = int(data[3]) # corners[i] = Corner(x, y, score, L) for c in corners: if c.score > quality_level * best_eigs[c.level]: pt = np.array([c.x, c.y], dtype=np.float32 ) # cv2.calcOpticalFlowPyrLK requires float32 fts.append(Feature(frame, pt, c.level)) self.reset_grid() return fts
def main(): options, args = getopt.getopt(sys.argv[1:], 'b:a:') b = 1 algorithm = 1 for option, value in options: if option == '-b': b = eval(value) elif option == '-a': algorithm = eval(value) gdal.AllRegister() infile = args[0] path = os.path.dirname(infile) basename = os.path.basename(infile) root, ext = os.path.splitext(basename) inDataset = gdal.Open(infile, GA_ReadOnly) cols = inDataset.RasterXSize rows = inDataset.RasterYSize rasterBand = inDataset.GetRasterBand(b) band = rasterBand.ReadAsArray(0,0,cols,rows) \ .astype(np.uint8) if algorithm == 1: # corner detection, window size 7x7 result = cv.cornerMinEigenVal(band, 7) outfile = path + '/' + root + '_corner' + ext else: # edge detection, window size 7x7 result = cv.Canny(band, 50, 150) outfile = path + '/' + root + '_canny' + ext # write to disk driver = inDataset.GetDriver() outDataset = driver.Create(outfile, cols, rows, 1, GDT_Float32) outBand = outDataset.GetRasterBand(1) outBand.WriteArray(result, 0, 0) outBand.FlushCache() outDataset = None inDataset = None print 'result written to %s' % outfile
def dense_sample(img, points, grid_spacing, quality): grid_width = img.shape[1] // grid_spacing grid_height = img.shape[0] // grid_spacing x_max = grid_width * grid_spacing y_max = grid_height * grid_spacing eig = cv2.cornerMinEigenVal(img, 3, 3) max_val = np.amax(eig) # can use minmaxloc T = quality * max_val # threshold # ensure dense coverage verify presence of tracking point at every frame -----IS THIS NEEDED??? counter = np.zeros(grid_width * grid_height) for i in points: x = np.floor(i[0]) y = np.floor(i[1]) if x >= x_max or y >= y_max: continue x = x // grid_spacing y = y // grid_spacing index = y * grid_width + x counter[int(index)] = counter[int(index)] + 1 # get densely sampled points index = 0 offset = grid_spacing // 2 dense_points = [] for i in range(grid_height): for j in range(grid_width): if counter[index] > 0: index = index + 1 continue x = (j * grid_spacing) + offset # x is width is cols y = (i * grid_spacing) + offset # y is height is rows if eig[y][x] > T: dense_points.append([x, y]) index = index + 1 dense_points_np = np.array(dense_points) return dense_points_np
# My Harris matrix -- Using cornerEigenValsAndVecs myHarris_dst = cv.cornerEigenValsAndVecs(src_gray, blockSize, apertureSize) # calculate Mc Mc = np.empty(src_gray.shape, dtype=np.float32) for i in range(src_gray.shape[0]): for j in range(src_gray.shape[1]): lambda_1 = myHarris_dst[i,j,0] lambda_2 = myHarris_dst[i,j,1] Mc[i,j] = lambda_1*lambda_2 - 0.04*pow( ( lambda_1 + lambda_2 ), 2 ) myHarris_minVal, myHarris_maxVal, _, _ = cv.minMaxLoc(Mc) # Create Window and Trackbar cv.namedWindow(myHarris_window) cv.createTrackbar('Quality Level:', myHarris_window, myHarris_qualityLevel, max_qualityLevel, myHarris_function) myHarris_function(myHarris_qualityLevel) # My Shi-Tomasi -- Using cornerMinEigenVal myShiTomasi_dst = cv.cornerMinEigenVal(src_gray, blockSize, apertureSize) myShiTomasi_minVal, myShiTomasi_maxVal, _, _ = cv.minMaxLoc(myShiTomasi_dst) # Create Window and Trackbar cv.namedWindow(myShiTomasi_window) cv.createTrackbar('Quality Level:', myShiTomasi_window, myShiTomasi_qualityLevel, max_qualityLevel, myShiTomasi_function) myShiTomasi_function(myShiTomasi_qualityLevel) cv.waitKey()
def shi_tomasi(gray, blocksize=3, ksize=3, dilate=True): dst = cv2.cornerMinEigenVal(gray, blocksize, ksize) if dilate: return cv2.dilate(dst, None) return dst
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) gray = np.float32(gray) img[eigenMin>0.02*eigenMin.max()]=[0,0,255] displayImage('dst', img) #------------------------------------------------------------------------------ #Using cv2 #https://opencv-python-tutroals.readthedocs.io/en/latest/py_tutorials/py_feature2d/py_shi_tomasi/py_shi_tomasi.html #Harris corner detection img = cv2.imread('house.png') gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) gray = np.float32(gray) harris = cv2.cornerHarris(gray,2,3,0.04) #result is dilated for marking the corners, not important harris = cv2.dilate(harris,None) # Threshold for an optimal value, it may vary depending on the image. img[harris>0.01*harris.max()]=[0,0,255] displayImage('dst', img) #Shi Tomasi detection img = cv2.imread('house.png') gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) gray = np.float32(gray) shi = cv2.cornerMinEigenVal(gray, 10) shi = cv2.dilate(shi,None) img[shi>0.01*shi.max()]=[0,0,255] displayImage('dst', img)
# # 评价标准 R = min(r1, r2) # goodFeaturesToTrack(img, maxCorners, qualityLevel, minDistance, None, mask, blockSize, useHarrisDetector, k def goodFeaturesToTrack(img, maxCorners, qualityLevel, minDistance): corners = cv2.goodFeaturesToTrack(img, maxConners, qualityLevel, minDistance) return corners # 自定义 Harris # 输出: 每一个像素的 (r1, r2) # 然后自定义 R = r1r2 - k*((r1+r2)**2) def cornerEigenValsAndVecs(img, blockSize, ksize): return cv2.cornerEigenValsAndVecs(img, blockSize, ksize) # 输出: 每一个像素的 (r1, r2) # 然后自定义 R = min(r1, r2) def cornerMinEigenVal(img, blockSize, ksize) return cv2.cornerMinEigenVal(img, blockSize, None, ksize) # 亚像素 # 所有的角点都不是一个真正准确像素点 如(100.234, 5.789) -> (100, 5) # 亚像素定位的方法 # - 插值法 # - 基于图像矩计算 # - 曲线拟合方法(高斯曲面,多项式, 椭圆曲面) def cornerSubPix(img, corners, winSize, zeroZone, criteria): corners = cv2.cornerSubPix(img, corners, winSize, zeroZone, criteria) return corners # SIFT # 特性: 旋转不变性、尺寸不变性 # 尺寸不变性可以通过尺寸空间滤波器实现(尺寸空间滤波器可以使用一些列具有不同方差的高斯卷积构成) # LOG: 高斯拉普拉斯算子
def compute_trajectory(self, video_pattern, traj_dirname, verbose=False, force_overwrite=False): u""" Calculating dense trajectories and save them as npy file """ if (force_overwrite): getoutput('rm -rf "%s"' % traj_dirname) getoutput('rm -rf %s*.csv' % traj_dirname) else: if (os.path.exists(traj_dirname)): print( 'Directory exists! If you want to overwrite the trajectory directory, change force_overwrite option to True.' ) print( 'Please make sure that force_overwrite=True will execute \'rm -rf %s\' and \'rm -rf %s/csv\'.' % (traj_dirname, traj_dirname)) return 0 getoutput('mkdir -p "%s"' % traj_dirname) imgs = vread(video_pattern) W, H = self.frame_shape w_grid, h_grid = np.meshgrid(range(W), range(H)) p_grid = np.vstack((w_grid.flatten(), h_grid.flatten())).T video_dirname = os.path.dirname(video_pattern) trajlist = [] img = cv2.resize(imgs[0], (W, H)) img_g = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY) for t in tqdm(range(len(imgs)), desc='Generating trajectories'): prev_img_g = img_g img = cv2.resize(imgs[t], (W, H)) img_g = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY) img_hsv = cv2.cvtColor(img, cv2.COLOR_RGB2HSV) flow = cv2.calcOpticalFlowFarneback(prev_img_g, img_g, None, 0.5, 3, 15, 3, 5, 1.2, 0) flow = cv2.medianBlur(flow, 5) p_dst = p_grid + flow.reshape((W * H, 2)) is_texture = cv2.cornerMinEigenVal(prev_img_g, 5) > self.eigen_th _, hom = self.__calc_gm(img_g, prev_img_g) lm = p_dst - cv2.perspectiveTransform( p_grid[np.newaxis].astype('float64'), hom)[0] lm[:, 1] *= -1 # Initializing trajectories if (np.mod(t, self.t_step) == 0): init_mask = is_texture.copy() for x in trajlist: init_mask[(x.last_loc[1] - self.step):(x.last_loc[1] + self.step), (x.last_loc[0] - self.step):(x.last_loc[0] + self.step)] = False w_grid, h_grid = np.meshgrid(range(0, W, self.step), range(0, H, self.step)) p0 = np.vstack((w_grid.flatten(), h_grid.flatten())).T p0 = p0[init_mask[p0[:, 1], p0[:, 0]], :] trajlist += [ Trajectory(loc, t, self.min_trajectory_length, self.max_trajectory_length, self.n_piece, self.frame_shape, video_dirname, traj_dirname) for loc in p0 ] # Updating trajectories is_alive = [ x.append(p_dst[x.last_loc[0] + x.last_loc[1] * W], img_hsv[x.last_loc[1], x.last_loc[0], :], lm[x.last_loc[0] + x.last_loc[1] * W], is_texture[x.last_loc[1], x.last_loc[0]]) for x in trajlist ] trajlist = [x for (x, f) in zip(trajlist, is_alive) if f is True] if (verbose): img_ = img.copy() for x in trajlist: cv2.circle(img_, (np.int(x.last_loc[0]), np.int(x.last_loc[1])), 3, (255, 0, 0)) cv2.imshow('Video', img_[:, :, ::-1]) cv2.waitKey(1) [x.finalize(x.length >= x.min_trajectory_length) for x in trajlist] if (verbose): cv2.destroyAllWindows()
# 0803.py import cv2 import numpy as np #1 src = cv2.imread('./data/CornerTest.jpg') gray = cv2.cvtColor(src, cv2.COLOR_BGR2GRAY) eigen = cv2.cornerMinEigenVal(gray, blockSize=5) print('eigen.shape=', eigen.shape) #2 T = 0.2 corners = np.argwhere(eigen > T) corners[:, [0, 1]] = corners[:, [1, 0]] # switch x, y print('len(corners ) =', len(corners)) dst = src.copy() for x, y in corners: cv2.circle(dst, (x, y), 3, (0, 0, 255), 2) cv2.imshow('dst', dst) cv2.waitKey() cv2.destroyAllWindows()
Height, Width, _ = img.shape mean = np.mean(img) var = np.var(img) std = np.std(img) threshold_value = thresh _, otsu = cv.threshold(blur, 0, 255, cv.THRESH_BINARY + cv.THRESH_OTSU) contours, _ = cv.findContours(otsu, cv.RETR_TREE, cv.CHAIN_APPROX_SIMPLE)[-2:] no_of_contours = len(contours) dst = cv.cornerMinEigenVal(gray, 2, 3, 0) total_corner_detect = len(dst) img_txt = img_name.split('.')[0] file = open('Features_of_Image_' + img_txt + '.txt', 'w') file.write('\nHeight : ' + str(Height)) file.write('\nWidth : ' + str(Width)) file.write('\nMean : ' + str(mean)) file.write('\nVariance : ' + str(var)) file.write('\nStd : ' + str(std)) file.write('\nThreshold : ' + str(threshold_value)) file.write('\nNo_of_Regions : ' + str(no_of_contours)) file.write('\nNo._of_Corner : ' + str(total_corner_detect)) file.close()
# The Sobel y-axis kernel sobelY = np.array(([-1, -2, -1], [0, 0, 0], [1, 2, 1]), dtype="float32") #Read in the image image = cv2.imread( 'C:/Users/Virginia Saurer/PycharmProjects/A1/box_in_scene.png', 0) origImage_window = 'Original Image' #show the original image in a window cv2.imshow(origImage_window, image) # Setting the parameters for the cornerMinEigenVal function blockSize = 3 apertureSize = 3 #Calculating the minimum Eigenvalue of the image minEigenValues = cv2.cornerMinEigenVal(image, blockSize, apertureSize) #Calculating the min and max Eigen values of the image minEigen, maxEigen, _, _ = cv2.minMaxLoc(minEigenValues) #Naming the 3 corner detection windows that will be shown #This window displays the original image with the corners being detected and indicated with white dots HarrisCorner_window = 'Harris Corner Detector ' #This window displays only the corners of the orginal image, indicated with white pixels that passed # the threshold test HarrisCorner_window2 = 'The Corners at T = 0.01' #This window displays the thined corners of the orginal image with non-maximum supression applied to the # pixels that passed the threshold test JustCorners_window = 'The Corners w nonMaxSupression'