def getSURFFeatures2(image, threshold, levels): image = cv2.GaussianBlur(image, (3, 3), 0) images = [] zero0 = np.zeros(np.shape(image)) zero1 = np.zeros(np.shape(image)) # zero2=removepadd(zero1,40) # zero3=removepadd(zero1,40) images.append(zero1) maxs = [0] for i in range(1, levels): temp = hessian_matrix_det(image, sigma=i) # temp=removepadd(temp,40) cv2.imwrite(str(i) + ".jpg", temp * 255) images.append(temp) maxs.append(np.max(temp) / 2) images.append(zero0) maxs.append(0) images = np.array(images) row, col = np.shape(images[0]) finalCoord = [] for i in range(1, row - 1): for j in range(1, col - 1): for k in range(1, len(images) - 1): img = images[k] point = img[i][j] if (img[i][j] > min(threshold, maxs[k])): grid = images[k - 1:k + 2, i - 1:i + 2, j - 1:j + 2] if (np.where(grid >= point)[0].shape[0] == 1): finalCoord.append([i, j, k]) return np.array(finalCoord)
def getSURFFeatures(image, threshold, levels): image = cv2.GaussianBlur(image, (3, 3), 0) images = [] for i in range(1, levels): temp = hessian_matrix_det(image, sigma=i) temp = removepadd(temp, 40) cv2.imwrite(str(i) + ".jpg", temp * 255) images.append(temp) row, col = np.shape(images[0]) finalCoord = [] for i in range(1, row - 1, 5): for j in range(1, col - 1, 6): maxPt = -1 coord = [-1, -1, -1] for k in range(1, levels): img = images[k - 1] box = img[i - 1:i + 2, j - 1:j + 2] val = np.amax(box) if (val > maxPt): maxPt = val x, y = np.unravel_index(box.argmax(), box.shape) coord = [x - 1 + i + 20, y - 1 + j + 20, k] img = np.array(images)[:, i - 1:i + 2, j - 1:j + 2] if (maxPt > threshold): finalCoord.append(coord) return np.array(finalCoord)
def hessian(img): scales = [] s = 2 k = math.pow(2,0.25) for i in range(12): image = hessian_matrix_det(img, sigma=1.2*i) # image = hessian_matrix_det(img, sigma=math.pow(k,i)*s) Hrr, Hrc, Hcc = hessian_matrix(integral_image(img), sigma=math.pow(k,i)*s, order='rc') det = Hrr*Hcc - np.power((0.9*Hrc),2) scales.append(image) keypts = keypt(scales) return keypts
def get_surf_feat(sigmas, image): integral_img = integral_image(image) # integral_image = np.float(integral_img) surf_keypts = [] for idx, sigma in enumerate(sigmas): det_o_hess = feature.hessian_matrix_det(image, sigma=sigma) lmax_pts = feature.peak_local_max(det_o_hess, num_peaks=50) for iidx in range(lmax_pts.shape[0]): surf_keypts.append((lmax_pts[iidx, 0], lmax_pts[iidx, 1], sigma)) return surf_keypts
def get_feature_vector_database(img): integral_img = integral_image(img) features = [] for sig in (sigmas): determinant_hessian = hessian_matrix_det(img, sigma=sig) coordinates = peak_local_max(determinant_hessian, num_peaks=50) for i in range(coordinates.shape[0]): features.append([coordinates[i][0], coordinates[i][1], sig]) return features
def test_hessian_matrix_det_3d(im3d): D = hessian_matrix_det(im3d) D0 = D[D.shape[0] // 2] row_center, col_center = np.array(D0.shape) // 2 # testing in 3D is hard. We test this by showing that you get the # expected flat-then-low-then-high 2nd derivative response in a circle # around the midplane of the sphere. circles = [draw.circle_perimeter(row_center, col_center, r, shape=D0.shape) for r in range(1, D0.shape[1] // 2 - 1)] response = np.array([np.mean(D0[c]) for c in circles]) lowest = np.argmin(response) highest = np.argmax(response) assert lowest < highest assert response[lowest] < 0 assert response[highest] > 0
def read_image(self, image_name, size=None): options = self.get_options() if size: im = np.array(Image.open(image_name).convert("L").resize(size)) else: im = np.array(Image.open(image_name).convert("L")) options["image"] = im feature = hessian_matrix_det(**options) # plt.imshow(feature) # plt.show() return feature.reshape((1, feature.shape[0] * feature.shape[1]))[0]
def find_feature_vector(image): # Integral Images feature_vector = [] integrated_img = integral_image(image) S = [1, 1.5, 2, 2.5, 3, 3.5, 4, 4.5, 5] for s in S: hess_matrix = hessian_matrix_det(image, sigma=s) potential_keypoints = peak_local_max(hess_matrix, num_peaks=50) for p in potential_keypoints: point = [(float)(s), (float)(p[0]), (float)(p[1])] feature_vector.append(point) refined_keypoints = remove_overlapping_blobs(feature_vector) return list(feature_vector)
def surf(img, is_test=False): itgl_img = integral_image(img) threshold = 0.0002 num_octaves = 4 octaves = np.array([[9, 15, 21, 27], [15, 27, 39, 51], [27, 51, 75, 99], [51, 99, 147, 195]]) filter_maps = [0] * 16 (h, w) = itgl_img.shape[:2] step = 1 sigma_list = np.zeros(octaves.shape[0] * octaves.shape[1]) for i in range(octaves.shape[0]): for j in range(octaves.shape[1]): cur_filter = octaves[i, j] sigma_list[i * octaves.shape[1] + j] = cur_filter * (1.2 / 9) filter_maps[i * octaves.shape[1] + j] = hessian_matrix_det( img.astype(np.float64), sigma_list[i * octaves.shape[1] + j]) filtered_imgs = np.stack(filter_maps, axis=-1) extremas = find_extrema(filtered_imgs, img) em = extremas.astype(np.float64) sigmas_of_peaks = sigma_list[extremas[:, -1]] sigmas_of_peaks = np.expand_dims(sigmas_of_peaks, axis=1) em = np.hstack([em[:, :-1], sigmas_of_peaks]) overlap = 0.2 blobs = _prune_blobs(em, overlap) # print (blobs.shape) if is_test: fig, ax = plt.subplots() nh, nw = img.shape count = 0 ax.imshow(img, interpolation='nearest', cmap="gray") for blob in blobs: y, x, r = blob c = plt.Circle((x, y), r * 1.414, color='red', linewidth=1.5, fill=False) ax.add_patch(c) ax.plot() plt.show() #descriptor = get_descriptor(blobs, itgl_img) return blobs
def find_blobs_hessian(img): size = 20 img0 = np.zeros((img.shape[0]+ 2*size, img.shape[1]+ 2*size)) img0[size:size+img.shape[0], size:size+img.shape[1]] = img blobs = blob_doh(img0, max_sigma=10, num_sigma=10) true_blobs = [] for y0, x0, b0 in blobs: p0 = util.neighbour(img0, y0, x0, 1) y, x = np.unravel_index(p0.argmax(), p0.shape) y0 += y - 1 x0 += x - 1 p0 = util.neighbour(img0, y0, x0, 2*b0) if True: # method 1 (hessian diagonal) hs0, hs1, hs2 = hessian_matrix(p0, 0.5*b0) q0 = 0.5 *(hs0 + hs2) if b0 >= 2 \ and b0 <= 8 \ and q0[2*b0,2*b0] > 0.95*util.neighbour(q0,2*b0,2*b0,b0).max(): true_blobs.append([y0,x0,b0]) else: # method 2 (hessian determinant) q0 = hessian_matrix_det(p0, b0) print b0, q0[2*b0, 2*b0], util.neighbour(q0, 2*b0,2*b0,b0).max() if b0 >= 2 \ and b0 <= 8 \ and q0[2*b0,2*b0] > 0.8*util.neighbour(q0,2*b0,2*b0,b0).max(): true_blobs.append([y0, x0, b0]) if len(true_blobs) > 0: true_blobs = np.array(true_blobs) - [size, size, 0] if len(blobs) > 0: blobs = blobs - [size, size, 0] return true_blobs, blobs
def get_surf_vector(image): #Preprocess Image feature = [] image = cv2.resize(image, DIM, interpolation = cv2.INTER_AREA) gray_temp = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY).astype('float') image = cv2.normalize(gray_temp, None, 0, 1, cv2.NORM_MINMAX) #Get Interal/Summation Representation image = integral_image(image) for scale in scale_values: #Get Hessian Determinant and Get Local Maximums det = hessian_matrix_det(image, scale) blobs = peak_local_max(det) #We Need to Add the Scale Information to the feature for blob in blobs: x_coordinate = blob[0] y_coordinate = blob[1] point = [float(x_coordinate), float(y_coordinate), scale] feature.append(point) return feature
def get_hessian_det(data, sigma=None): if sigma is None: sigma = np.ones(data.ndim) dets = hessian_matrix_det(data, sigma) return dets
keypointlist = [] images_path = [] for mm in range(1,11): strk = "images" + str(mm) + ".pickle" strm = "imagesname" + str(mm) + ".pickle" img = pickle.load(open(strk,"rb")) imgname = pickle.load(open(strm,"rb")) for ind in range(len(imgname)): print(imgname[ind]) im = cv2.resize(img[ind], (int(np.shape(img[ind])[1]/4), int(np.shape(img[ind])[0]/4) ), interpolation = cv2.INTER_AREA) im1 = deepcopy(im) img[ind] = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY) img[ind] = img[ind]/255 arr = [] for i in range(len(sigmalist)): conv = hessian_matrix_det(img[ind], int(sigmalist[i])) arr.append(conv) t = sorted(np.reshape(arr,(np.shape(arr)[0]*np.shape(arr)[1]*np.shape(arr)[2])))[::-1][6000] im, blobs = nonmaxsuppr(sigmalist, im1, arr, t) blobs = blob._prune_blobs(np.array(blobs),0.6) keypointlist.append(blobs) images_path.append(imgname[ind]) for x in blobs: i = x[0] j = x[1] r = x[2] cv2.circle(im, (j, i), r, (0,0,255), 1) strr = "./surfblob/" + imgname[ind].split("\\")[1] cv2.imwrite(strr,im) stop = timeit.default_timer() print((stop - start)/len(keypointlist))
def test_hessian_matrix_det(): image = np.zeros((5, 5)) image[2, 2] = 1 det = hessian_matrix_det(image, 5) assert_almost_equal(det, 0, decimal=3)
def time_hessian_matrix_det(self): result = feature.hessian_matrix_det(self.image, 4)
glcm=feature.greycomatrix(r+g+b, [1],[0,np.pi/4,np.pi/2,3*np.pi/4,np.pi], levels=3*256)# greycghg=feature.greycoprops(glcm, prop='homogeneity')/(256*256*9)#size1*5 greycgcl=feature.greycoprops(glcm, prop='correlation')/(256*256*9) greycgeg=feature.greycoprops(glcm, prop='energy')/(256*256*9) greycgasm=feature.greycoprops(glcm, prop='ASM')/(256*256*9) greycgctt=feature.greycoprops(glcm, prop='contrast')/(256*256*9) lbp=feature.local_binary_pattern(imgrey, 8, np.pi/4)#size同图片 plm=feature.peak_local_max(imgrey, min_distance=1)#多个坐标对 st=feature.structure_tensor(imgrey, sigma=1, mode='constant', cval=0)#三个同图片一样大的矩阵 ste=feature.structure_tensor_eigvals(st[0],st[1],st[2])#两个个同图片一样大的矩阵 # hmf=feature.hessian_matrix(image, sigma=1, mode='constant', # cval=0, order=None)#6个三通道的原图大小矩阵 hmd=feature.hessian_matrix_det(imgrey, sigma=1)#原图大小矩阵 # hme=feature.hessian_matrix_eigvals(hmf, Hxy=None, Hyy=None) si=feature.shape_index(imgrey, sigma=1, mode='constant', cval=0)#原图大小矩阵 # ckr=feature.corner_kitchen_rosenfeld(image, mode='constant', cval=0) ##原图大小矩阵 三通道 # ch=feature.corner_harris(imgrey, method='k', k=0.05, eps=1e-06, sigma=1)#原图大小矩阵 # cht=feature.corner_shi_tomasi(imgrey, sigma=1)#原图大小矩阵 # cfs=feature.corner_foerstner(imgrey, sigma=1)#2个 #原图大小矩阵 # csb=feature.corner_subpix(image, ch, window_size=11, alpha=0.99) cps=feature.corner_peaks(imgrey, min_distance=1, threshold_abs=None, threshold_rel=0.1, exclude_border=True, indices=True, footprint=None, labels=None)#一堆坐标值 # cmr=feature.corner_moravec(imgrey, window_size=1)#原图大小矩阵 # cft=feature.corner_fast(imgrey, n=12, threshold=0.15)#原图大小矩阵 corners = feature.corner_peaks(feature.corner_fast(imgrey, 9), min_distance=1)#一堆坐标 corts=feature.corner_orientations(imgrey, corners, octagon(3, 2))#一维矩阵长度不定 # mtem=feature.match_template(image, template, pad_input=False,
from skimage.color import rgb2gray from skimage.transform import integral_image as integral_image import matplotlib.pyplot as plt from skimage.feature import hessian_matrix_det from skimage.feature import hessian_matrix, peak_local_max # In[129]: for m in range(len(images)): image = io.imread("images/" + images[m]) image = rgb2gray(image) image = integral_image(image) scales = np.linspace(1, 8, 20) hessians = [] for s in scales: h = hessian_matrix_det(image, s) hessians.append(h) h_cuboid = np.dstack(hessians) blobs = peak_local_max(h_cuboid) d = [blobs[i] for i in range(1, len(blobs), 500)] a = image Dict = [] for i in d: Dict.append((i[0].item(), i[1].item(), i[2].item())) D = {images[m]: Dict} with open(images[m][:-4] + 'surf.json', 'w') as fp: json.dump(D, fp) print("Saved", images[m][:-4] + "surf.json")
file = path + name image = cv2.imread(file, 0) # In[11]: image = image / 255 # In[13]: sizes = list(range(2, 11)) # In[45]: g_images = [] for i in sizes: temp = hessian_matrix_det(image, i) g_images.append(temp) # In[46]: scale_space = np.zeros((9, image.shape[0], image.shape[1]), dtype=np.float64) for i in range(9): scale_space[i] = g_images[i] # In[47]: d = [] for i in range(9): s = set() Rmax = generic_filter(g_images[i], np.max, footprint=np.ones((3, 3)))