def logistic_lbp_diff(image1, image2, gamma=1e-3, points=4, radius=2): """ Generate local binary patterns (LBP) for each of the images by taking the average across the color components (assumed to be axis = 0), and then generating a LBP using a given number of points and comparing at a given radius away from the center of the image. The distance between the LBPs is regressed. gamma, points, and radius defaults chosen purely hearuistically to provide decent dynamic range as seen in image_handling_tb.ipynb, figure 14. """ return logistic_vector_dist( local_binary_pattern(np.average(image1, axis=0), points, radius), local_binary_pattern(np.average(image2, axis=0), points, radius), gamma)
def fit(self, modality, ground_truth=None, cat=None): """Compute the LBP images in the three-ortogonal planes. Parameters ---------- modality : object of type TemporalModality The modality object of interest. ground-truth : object of type GTModality or None The ground-truth of GTModality. If None, the whole data will be considered. cat : str or None String corresponding at the ground-truth of interest. Cannot be None if ground-truth is not None. Return ------ self : object Return self. """ super(LBPExtraction, self).fit(modality=modality, ground_truth=ground_truth, cat=cat) # Fix the z axis lbp_z = np.zeros(modality.data_.shape) for z in range(modality.data_.shape[2]): lbp_z[:, :, z] = local_binary_pattern(modality.data_[:, :, z], self.p, self.r, method=self.kind) # Fix the x axis lbp_x = np.zeros(modality.data_.shape) for x in range(modality.data_.shape[1]): lbp_x[:, x, :] = local_binary_pattern(modality.data_[:, x, :], self.p, self.r, method=self.kind) # Fix the y axis lbp_y = np.zeros(modality.data_.shape) for y in range(modality.data_.shape[0]): lbp_y[y, :, :] = local_binary_pattern(modality.data_[y, :, :], self.p, self.r, method=self.kind) self.data_ = np.array((lbp_z, lbp_x, lbp_y)) return self
def get_features(directory): features = [] for fn in iglob("%s/*.png" % directory): image = color.rgb2gray(io.imread(fn)) lbp_image = feature.local_binary_pattern(image, LBP_POINTS, LBP_RADIUS, "uniform") features.append(get_histogram_feature(lbp_image)) return features
def __init__(self, clf): ''' Classifies and scores eyes based on whether they are open or closed. INPUTS: clf - a classifier the following are defined by the clf: n_size - a float, the size of the LBP n_circ_sym - number of the LBP divisions to use n_splt - how many times to symmetrically subdivide the image trim - the number of pixels to trim off the the borders of the returned LBP image. scaler - a standard scaler ''' self.clf = clf self._n_size = clf.n_size self._n_circ_sym = clf.n_circ_sym self._n_split = clf.n_splits self._trim = clf.trim self._resize = clf.img_size self.scaler = clf.scaler self.f_lbp = lambda x: feature.local_binary_pattern(x, self._n_circ_sym, self._n_size).astype(int)
def k_means_classifier(image): n_clusters = 8 # blur and take local maxima blur_image = gaussian(image, sigma=8) blur_image = ndi.maximum_filter(blur_image, size=3) # get texture features feats = local_binary_pattern(blur_image, P=40, R=5, method="uniform") feats_r = feats.reshape(-1, 1) # cluster the texture features km = k_means(n_clusters=n_clusters, batch_size=500) clus = km.fit(feats_r) # copy relevant attributes labels = clus.labels_ clusters = clus.cluster_centers_ # reshape label arrays labels = labels.reshape(blur_image.shape[0], blur_image.shape[1]) # segment shadow img = blur_image.ravel() shadow_seg = img.copy() for i in range(0, n_clusters): # set up array of pixel indices matching cluster mask = np.nonzero((labels.ravel() == i) == True)[0] if len(mask) > 0: thresh = threshold_otsu(img[mask]) shadow_seg[mask] = shadow_seg[mask] < thresh shadow_seg = shadow_seg.reshape(*image.shape) return shadow_seg
def LBP(self, img, save=False, parms=None, subtract=False): """Get the LBP image (reference: http://goo.gl/aeADZd) @param img: image array Keyword arguments: save -- True to save the image parms -- [points, radius] (default: None) subtract -- True to subtract values to pts (default: False) """ from skimage.feature import local_binary_pattern if self.is_rgb(img): img = self.gray(img) if parms is None: pts = int(img.shape[0]*img.shape[1]*0.0003) radius = min(img.shape[0], img.shape[1])*0.015 else: pts = parms[0] radius = parms[1] lbp = local_binary_pattern(img, pts, radius, method='uniform') if subtract: lbp = np.abs(lbp - pts) if save: self.pl.plot_matrix(lbp, fname='lbp_cm.png', show_text=False, show_axis=False, norm=False) return lbp
def calculateLBP(self): paramList = list() with open(self.paramTxt) as f: for line in f: paramList.append(int(line.strip())) print(paramList) for image in self.trainDict.iterkeys(): print(image) img = cv2.imread(image) imgGray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # radius = 3 # noPoints = 8 * radius radius = paramList[0] noPoints = paramList[1] * radius print(radius) print(noPoints) lbpImage = local_binary_pattern(imgGray, noPoints, radius, method='uniform') # Calculate the histogram x = itemfreq(lbpImage.ravel()) # normalize the histogram hist = x[:, 1] / sum(x[:, 1]) # hist = cv2.calcHist(lbp, [0], None, [256], [0, 256]) # cv2.normalize(hist,hist) # hist = hist.flatten() self.addrImg.append(image) self.lbpHistogram.append(hist) self.tagNo.append(self.trainDict.get(image)) joblib.dump((self.addrImg, self.lbpHistogram, self.tagNo), "lbp.pkl", compress=3)
def lbf(infile): result = [] label = [] ix = 0 for here, i in enumerate(open(infile).readlines()): ix +=1 imgpath, l = i.split(',') if os.path.exists(imgpath): im = cv2.imread(imgpath) im = cv2.resize(im, (200, 200)) im_gray = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY) radius = 3 no_points = 8 * radius lbp = local_binary_pattern(im_gray, no_points, radius, method='uniform') x = itemfreq(lbp.ravel()) hist = x[:, 1]/sum(x[:, 1]) result.append(hist) if "FE" in l: label.append(1) else: label.append(-1) print len(result) print len(label)
def get_lbp(img): # settings for LBP radius = 4 n_points = 8 * radius METHOD = 'uniform' lbp = local_binary_pattern(color.rgb2gray(img), n_points, radius, METHOD) return lbp
def testLBP (format, formatMask, path, output) : dataset = pd.read_csv(path) idxCls = dataset['idx'] # cnts = dataset['Cnt'] fnList = dataset['path'] # out = open(output, 'w') lbps = list(map(lambda x: local_binary_pattern(cv2.bitwise_and(imread(format.format(x)),imread(formatMask.format(x))), lbpP, lbpR, lbpMethod), fnList)) histograms = list(map(lambda x: np.histogram(x, bins=range(int(np.max(lbps)) + 1))[0], lbps)) distances = prw.pairwise_distances(histograms, metric='l1') np.fill_diagonal(distances, math.inf) guessedClasses = np.apply_along_axis(lambda x: np.argmin(x), 1, distances) scores = np.apply_along_axis(lambda x: np.min(x), 1, distances) correct = list(map(lambda i: idxCls[guessedClasses[i]] == idxCls[i], range(0, np.alen(idxCls)))) # out.write(str(np.average(correct))) # fpr, tpr, thresholds = roc_curve(correct, scores, pos_label=1) # pyplot.plot(tpr, fpr) # pyplot.show() with open(output + 'lbp_distances.csv', 'w', newline='') as fp: a = csv.writer(fp, delimiter=',') a.writerows(distances) with open(output + 'lbp_guessedClasses.csv', 'w', newline='') as fp: a = csv.writer(fp, delimiter=',') a.writerow(guessedClasses) with open(output + 'lbp_correct.csv', 'w', newline='') as fp: a = csv.writer(fp, delimiter=',') a.writerow(correct) with open(output + 'lbp_real.csv', 'w', newline='') as fp: a = csv.writer(fp, delimiter=',') a.writerow(idxCls)
def pca_hash(fxy,pca): lbp = local_binary_pattern(fxy,3,24) #print(len(flattn(lbp))) #print(np.array([flattn(lbp)]).shape) res = pca.transform(np.array([flattn(lbp)])) #print(res.shape) return res[0]
def train_lbp(): train_dir = "/home/gast/ImageHashing/face_train/" suffix = ".png" size = 200 train = [] for fn in sorted(os.listdir(train_dir)): if fn.endswith(suffix): rgb = Image.load(train_dir + fn) gray = Image.rgb2gray(rgb) resized = Image.resize(gray,size) img = Image.gray2real(resized) lbps = local_binary_pattern(img,3,24) #print(flatten(lbps)) #print(len(flatten(lbps))) train.append(flatten(lbps)) #print(lbps) pca = PCA(n_components=100) #print(len(train)) #print(len(train[0])) train_np = np.array(train) #print(train_np.shape) pca.fit(train_np) return pca
def limpa_imagem(img_cinza): #binariza a imagem em escala de cinza img_bin_cinza = np.where(img_cinza < np.mean(img_cinza), 0, 255) # aplica lbp sobre a imagem em escala de cinza # lbp foi aplicado para evitar perda de informacao em regioes # proximas as regioes escuras (provaveis celulas) lbp_img = local_binary_pattern(img_cinza, 24, 3, method='uniform') # aplica efeito de blurring sobre a imagem resultante do lbp blur_img = gaussian(lbp_img,sigma=6) img_bin_blur = np.where(blur_img < np.mean(blur_img), 0, 255) # junta as duas regiões definidas pela binarizacao da imagem em escala # de cinza e a binarizacao do blurring mascara = np.copy(img_bin_cinza) for (a,b), valor in np.ndenumerate(img_bin_blur): if valor == 0: mascara[a][b] = 0 # aplica a mascara obtida sobre a imagem original (em escala de cinza) # para delimitar melhor as regiões que não fornecerao informacoes (regioes # totalmente brancas) img_limpa = np.copy(img_cinza) for (a,b), valor in np.ndenumerate(mascara): if valor == 255: img_limpa[a][b] = 255 return (img_limpa)
def extract(self, image): features = np.array([]) vec = [] if 'raw' in self.features: vec = image.flatten() features = np.append(features, vec) vec = [] if 'textons' in self.features: import gen_histogram as tx vec = np.array(tx.histogram(image, self.centers)) features = np.append(features, vec) vec = [] if 'hog' in self.features: vec = hog(image, cells_per_block=(3, 3)) vec = np.append(vec, hog(image, cells_per_block=(4, 4))) vec = np.append(vec, hog(image, cells_per_block=(1, 1))) vec = np.append(vec, hog(image, cells_per_block=(2, 2))) features = np.append(features, vec) vec = [] if 'lbp' in self.features: vec = local_binary_pattern(image, 24, 3).flatten() features = np.append(features, vec) vec = [] if 'daisy' in self.features: vec = daisy(image).flatten() features = np.append(features, vec) return features
def ExtractLBPFeatures(img): # This function extracts LBP features from a given image radius = 3 n_points = 8 * radius lbp_feature = local_binary_pattern(image,n_points,radius) return lbp_feature
def predictImg(img): img = transform.resize(img, (100, 150)) lbp = feature.local_binary_pattern(img, n_points, radius, METHOD) lbp = lbp.flatten() k = dt.predict(lbp) return tags[k[0]]
def retrieve_LBP_feature_histogram(image_path): try: # Read feature directly from file image_feature_path = image_path + FEATURE_EXTENSION if os.path.isfile(image_feature_path): LBP_feature_histogram = np.genfromtxt(image_feature_path, delimiter=",") return LBP_feature_histogram # Define LBP parameters radius = 5 n_points = 8 bins_num = pow(2, n_points) LBP_value_range = (0, pow(2, n_points) - 1) # Retrieve feature assert os.path.isfile(image_path) image_content_in_gray = imread(image_path, as_grey=True) image_content_in_gray = img_as_ubyte(image_content_in_gray) LBP_feature = local_binary_pattern(image_content_in_gray, n_points, radius) LBP_feature_histogram, _ = np.histogram(LBP_feature, bins=bins_num, range=LBP_value_range, density=True) # Save feature to file assert LBP_feature_histogram is not None np.savetxt(image_feature_path, LBP_feature_histogram, delimiter=",") return LBP_feature_histogram except: print("Unable to retrieve LBP feature histogram in %s." % (os.path.basename(image_path))) return None
def trainSys(): global a global tags i = 0 print "Starting training..." directory = "/home/leroy/Desktop/ATI - Mini/datasets/" for key, ta in tags.iteritems(): folder = directory + ta + '/' for file in os.listdir(folder): file2 = folder + file img = io.imread(file2, as_grey = True) img = transform.resize(img, (100, 150)) lbp = feature.local_binary_pattern(img, n_points, radius, METHOD) lbp = lbp.flatten() a[i] = lbp i = i + 1 input_images_tags = numpy.ones(20) input_images_tags2 = numpy.empty(20) input_images_tags2.fill(2) input_images_tags3 = numpy.empty(20) input_images_tags3.fill(3) input_images_tags4 = numpy.empty(20) input_images_tags4.fill(4) #input_images_tags3 = numpy.concatenate([input_images_tags3, input_images_tags4]) input_images_tags2 = numpy.concatenate([input_images_tags2, input_images_tags3]) input_images_tags = numpy.concatenate([input_images_tags, input_images_tags2]) dt.fit(a, input_images_tags) print "Training Successfully Completed!"
def lbp(self, image, label_bboxes, axes, mins, maxs): rawbbox = image ccbboxobject, passed, ccbboxexcl = label_bboxes #FIXME: there is a mess about which of the lbp features are computed (obj, excl or incl) import skimage.feature as ft P=8 R=1 lbp_total = np.zeros(passed.shape) for iz in range(maxs.z - mins.z): #an lbp image bboxkey = [slice(None)] * 3 bboxkey[axes.z] = iz bboxkey = tuple(bboxkey) lbp_total[bboxkey] = ft.local_binary_pattern(rawbbox[bboxkey], P, R, "uniform") #extract relevant parts lbp_incl = lbp_total[passed] lbp_excl = lbp_total[ccbboxexcl.astype(bool)] lbp_obj = lbp_total[ccbboxobject.astype(bool)] lbp_hist_incl, _ = np.histogram(lbp_incl, normed=True, bins=(P + 2), range=(0, P + 2)) lbp_hist_excl, _ = np.histogram(lbp_excl, normed=True, bins=(P + 2), range=(0, P + 2)) lbp_hist_obj, _ = np.histogram(lbp_obj, normed=True, bins=(P + 2), range=(0, P + 2)) result = {} result["lbp_incl"] = lbp_hist_incl result["lbp_excl"] = lbp_hist_excl result["lbp"] = lbp_hist_obj return result
def lbp_extract(self, image): """ 利用LBP,对给定的图片提取特征向量。使用前必须先初始化特征提取器。 Parameters ---------- image : 二维numpy数组 灰度图的二维numpy数组。 Returns ------- 一维numpy数组 图片的特征向量。 """ assert self.red, "self.red should be initial!" height, width = image.shape w = width // 2 h = height // 3 feature = np.array([]) for i in range(2): for j in range(3): cell = image[h*j:h*(j+1), w*i:w*(i+1)] lbp = local_binary_pattern(cell, 8, 1) histogram = np.zeros(256) for pattern in lbp.ravel(): histogram[int(pattern)] += 1 histogram = (histogram - histogram.mean()) / histogram.std() feature = np.hstack((feature, histogram)) feature = self.red.transform(feature.reshape(1, -1)) return feature.ravel()
def feature_extractor(image): METHOD = 'uniform' radius = 3 n_points = 8 * radius image = preprocessing(image) lbp = local_binary_pattern(image, n_points, radius, METHOD) return lbp.flatten()
def get_lbp(face_array): radius = 3 n_points = 8 * radius lbp_array = [] for face in face_array: lbp = local_binary_pattern(face.reshape(32, 32), n_points, radius, 'uniform') lbp_array.append(np.ravel(lbp)) return np.asarray(lbp_array)
def test_nri_uniform(self): lbp = local_binary_pattern(self.image, 8, 1, 'nri_uniform') ref = np.array([[ 0, 54, 0, 57, 12, 57], [34, 0, 58, 58, 3, 22], [58, 57, 15, 50, 0, 47], [10, 3, 40, 42, 35, 0], [57, 7, 57, 58, 0, 56], [ 9, 58, 0, 57, 7, 14]]) np.testing.assert_array_almost_equal(lbp, ref)
def lbp_histogram2(gray, radius, no_points): lbp = local_binary_pattern(gray, no_points, radius, method='uniform') (hist, _) = np.histogram(lbp.ravel(), bins=np.arange(0, no_points + 3), range=(0, no_points + 2)) hist = hist.astype("float") hist /= (hist.sum() + 1e-7) #print hist.size return hist
def compute(self, image): lbp = feature.local_binary_pattern(image, self.num_of_points, self.radius, method = "uniform") #Calculating and normalizing the histogram x = itemfreq(lbp.ravel()) hist = x[:, 1]/sum(x[:, 1]) return hist
def test_ror(self): lbp = local_binary_pattern(self.image, 8, 1, 'ror') ref = np.array([[ 0, 127, 0, 255, 3, 255], [ 31, 0, 5, 51, 1, 7], [119, 255, 3, 127, 0, 63], [ 3, 1, 31, 63, 31, 0], [255, 1, 255, 95, 0, 127], [ 3, 5, 0, 255, 1, 3]]) np.testing.assert_array_equal(lbp, ref)
def test_default(self): lbp = local_binary_pattern(self.image, 8, 1, 'default') ref = np.array([[ 0, 251, 0, 255, 96, 255], [143, 0, 20, 153, 64, 56], [238, 255, 12, 191, 0, 252], [129, 64., 62, 159, 199, 0], [255, 4, 255, 175, 0, 254], [ 3, 5, 0, 255, 4, 24]]) np.testing.assert_array_equal(lbp, ref)
def test_uniform(self): lbp = local_binary_pattern(self.image, 8, 1, 'uniform') ref = np.array([[0, 7, 0, 8, 2, 8], [5, 0, 9, 9, 1, 3], [9, 8, 2, 7, 0, 6], [2, 1, 5, 6, 5, 0], [8, 1, 8, 9, 0, 7], [2, 9, 0, 8, 1, 2]]) np.testing.assert_array_equal(lbp, ref)
def real_hash_w(fxy): lbp = local_binary_pattern(fxy,3,24) bls = blocks(lbp,10,10) hists = [] for i in xrange(1,9): for j in xrange(1,9): h,be = np.histogram(bls[i][j],10) hists.append(h) return hists
def real_hash(fxy): lbp = local_binary_pattern(fxy,3,24,method='nri_uniform') bls = blocks(lbp, 10, 10) hists = [] for i in xrange(0,10): for j in xrange(0,10): h,be = np.histogram(bls[i][j],10) hists.append(h) return hists
def generateLBP(self, csvName, p, eps=1e-7): trainPaths = list(paths.list_images(p)) trainLabels = [] trainImgs = [] #because of mem size we need to partition the writing cont = 0 written = 0 size = len(trainPaths) vet = [] for path in trainPaths: str = path.split("/") #print(str[4]) label = 1 if "dog" in str[4] else 0 img = rgb2gray(mpimg.imread(path)) lbp = ft.local_binary_pattern(img, self.P, self.R, self.METHOD) (hist, _) = np.histogram(lbp.ravel(), bins=np.arange(0, self.R + 3), range=(0, self.R + 2)) hist = hist.astype("float") hist /= (hist.sum() + eps) #print(hist) aux = list(hist) #tere = np.append(tere, label) aux.append(label) #print(aux) #nova linha vet.append(aux) #print("adfas") #print(aux) cont = cont + 1 if cont > 2000: df = pd.DataFrame(vet) if written == 0: #print(vet) df.to_csv(csvName, index=None, header=False) elif written > 0: with open(csvName, 'a') as f: df.to_csv(f, index=None, header=False) #print("nova linha") #print(len(vet)) #time.sleep(5) cont = 0 written = written + 1 vet = [] print("%.2f concluído" % ((2000 * written) / size))
# GLCM grey = cv.cvtColor(image, cv.COLOR_BGR2GRAY) GLCM = greycomatrix(grey, GLCM_dis, GLCM_ang, GLCM_level) f2 = [] for prop in { 'contrast', 'dissimilarity', 'homogeneity', 'energy', 'correlation', 'ASM' }: temp = greycoprops(GLCM, prop) f2.append(np.mean(temp)) # print("f2:", np.mean(f2)) f1 = np.append(f1, f2) # LBP LBP = local_binary_pattern(grey, LBP_points, LBP_radius, 'default') LBP = LBP.astype(np.uint8) # print(LBP.dtype) hist_grey1 = cv.calcHist([LBP], [0], None, [8], [0.0, 255.0]) f3 = hist_grey1.flatten() # print("f3:", np.mean(f3)) f1 = np.append(f1, f3) X.append(f1) Y.append(i) # X是特征向量集、y是物品类别集 X = np.array(X) Y = np.array(Y) # 测试过程
method_lbp=['default', 'ror', 'uniform', 'var'] cap=cv2.VideoCapture(0) numPoints=24 radius=3 id_method_lbp=0 while True: ret, frame=cap.read() image=cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) lbp=feature.local_binary_pattern(image, numPoints, radius, method=method_lbp[id_method_lbp]) cv2.rectangle(frame, (0, 0), (frame.shape[1], 30), (100, 100, 100), cv2.FILLED) txt="[q] Quit [o|l]numPoints:{:d} [i|k]rayon:{:d} [m] Methode: {}".format(numPoints, radius, method_lbp[id_method_lbp]) cv2.putText(frame, txt, (20, 20), cv2.FONT_HERSHEY_PLAIN, 1, (255, 255, 255), 1) cv2.imshow("Image", frame) cv2.imshow("LBP", lbp/np.max(lbp)) key=cv2.waitKey(1)&0xFF if key==ord('m'): id_method_lbp=(id_method_lbp+1)%len(method_lbp) if key==ord('i'): radius=radius+1 if key==ord('k'): radius=max(3, radius-1)
normed=True, bins=n_bins, range=(0, n_bins)) score = kullback_leibler_divergence(hist, ref_hist) if score < best_score: best_score = score best_name = name return best_name brick = data.load('brick.png') grass = data.load('grass.png') wall = data.load('rough-wall.png') refs = { 'brick': local_binary_pattern(brick, n_points, radius, METHOD), 'grass': local_binary_pattern(grass, n_points, radius, METHOD), 'wall': local_binary_pattern(wall, n_points, radius, METHOD) } # classify rotated textures print('Rotated images matched against references using LBP:') print('original: brick, rotated: 30deg, match result: ', match(refs, rotate(brick, angle=30, resize=False))) print('original: brick, rotated: 70deg, match result: ', match(refs, rotate(brick, angle=70, resize=False))) print('original: grass, rotated: 145deg, match result: ', match(refs, rotate(grass, angle=145, resize=False))) # plot histograms of LBP of textures fig, ((ax1, ax2, ax3), (ax4, ax5, ax6)) = plt.subplots(nrows=2,
hsv_mask = np.zeros(gray.shape, np.uint8) # these ind values are the indices of the last detected hough circle within gray hsv_mask[ind] = gray[ind] _, thresh = cv2.threshold(hsv_mask, 1, 255, cv2.THRESH_BINARY) contours = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) x, y, w, h = cv2.boundingRect(contours[0]) crop = gray[y:y + h, x:x + w] radius = 3 # Number of points to be considered as neighbourers no_points = 8 * radius # Uniform LBP is used lbp = local_binary_pattern(crop, no_points, radius, method='uniform') # Calculate the histogram x = itemfreq(lbp.ravel()) # Normalize the histogram hist = x[:, 1] / sum(x[:, 1]) plt.plot(hist) cv2.imshow("Sample", crop) cv2.waitKey(0) cv2.destroyAllWindows()
def lbp(dataset, n_direction, r): for image in dataset: yield local_binary_pattern(image, n_direction, r)
#plot font size plt.rcParams['font.size'] = 9 # settings for LBP METHOD = 'uniform' radius = 5 n_points = 10 * radius #load an image img = io.imread('test_im.jpg') #extract a band image = img[:, :, 0] #compute LBP for the image lbp = local_binary_pattern(image, n_points, radius, METHOD) # plot histograms of LBP of textures fig, (ax_img, ax_hist) = plt.subplots(nrows=2, ncols=3, figsize=(9, 6)) plt.gray() #plot LBP distributions titles = ('edge', 'flat', 'corner') w = width = radius - 1 edge_labels = range(n_points // 2 - w, n_points // 2 + w + 1) flat_labels = list(range(0, w + 1)) + list(range(n_points - w, n_points + 2)) i_14 = n_points // 4 # 1/4th of the histogram i_34 = 3 * (n_points // 4) # 3/4th of the histogram corner_labels = (list(range(i_14 - w, i_14 + w + 1)) + list(range(i_34 - w, i_34 + w + 1)))
def test_nri_uniform(self): lbp = local_binary_pattern(self.image, 8, 1, 'nri_uniform') ref = np.array([[0, 54, 0, 57, 12, 57], [34, 0, 58, 58, 3, 22], [58, 57, 15, 50, 0, 47], [10, 3, 40, 42, 35, 0], [57, 7, 57, 58, 0, 56], [9, 58, 0, 57, 7, 14]]) np.testing.assert_array_almost_equal(lbp, ref)
def test_ror(self): lbp = local_binary_pattern(self.image, 8, 1, 'ror') ref = np.array([[0, 127, 0, 255, 3, 255], [31, 0, 5, 51, 1, 7], [119, 255, 3, 127, 0, 63], [3, 1, 31, 63, 31, 0], [255, 1, 255, 95, 0, 127], [3, 5, 0, 255, 1, 3]]) np.testing.assert_array_equal(lbp, ref)
def predict_CelebA_Spoof(path, threshold): index = 0 RightNums_CNN = 0 rightNums_lbp = 0 rightNums = 0 FA = 0 FR = 0 TP = 0 live_samples_nums = 0 spoof_samples_nums = 0 total_samples = 0 hist = np.zeros((256 * 3)) lbp_live_error = [] lbp_1 = [] lbp_0 = [] for live_or_spoof in ['live', 'spoof']: folderPath = path + '\\' + live_or_spoof imagesName_list = os.listdir(folderPath) if live_or_spoof == 'live': live_samples_nums = len(imagesName_list) else: spoof_samples_nums = len(imagesName_list) total_samples = live_samples_nums + spoof_samples_nums for idx in range(len(imagesName_list)): predict = '' filePath = folderPath + '\\' + imagesName_list[idx] predict_shuffleNet_ = model.predict(filePath) if predict_shuffleNet_[0]['score'] > threshold and predict_shuffleNet_[0]['category'] == 'live': predict_shuffleNet = 'live' else: predict_shuffleNet = 'spoof' if predict_shuffleNet_[0]['score'] > threshold and predict_shuffleNet_[0]['category'] == 'spoof': predict_shuffleNet = 'spoof' else: predict_shuffleNet = 'live' img = cv2.cvtColor(cv2.imread(filePath), cv2.COLOR_BGR2RGB) faces = detector(img, 1) if len(faces) == 0: if predict_shuffleNet == 'spoof': predict = 'spoof' else: predict = 'live' # predict = 'spoof' index = index + 1 if predict == live_or_spoof: rightNums = rightNums + 1 elif predict == 'live' and live_or_spoof == 'spoof': FA = FA + 1 elif predict == 'spoof' and live_or_spoof == 'live': FR = FR + 1 if predict == live_or_spoof == 'live': TP = TP + 1 continue left = faces[0].left() right = faces[0].right() top = faces[0].top() bottom = faces[0].bottom() if left < 0 or right < 0 or top < 0 or bottom < 0: if predict_shuffleNet == 'spoof': predict = 'spoof' else: predict = 'live' index = index + 1 if predict == live_or_spoof: rightNums = rightNums + 1 elif predict == 'live' and live_or_spoof == 'spoof': FA = FA + 1 elif predict == 'spoof' and live_or_spoof == 'live': FR = FR + 1 if predict == live_or_spoof == 'live': TP = TP + 1 continue img_face = img[top:bottom, left:right] img_face_torch = trans(img_face) * 255 img_face_hsv = cv2.cvtColor(img_face_torch.numpy().transpose((1, 2, 0)), cv2.COLOR_RGB2HSV) for j, face_img_channel in enumerate(cv2.split(img_face_hsv)): lbp = local_binary_pattern(face_img_channel, 8, 1) max_bins = int(lbp.max() + 1) histogram, _ = np.histogram(lbp, density=True, bins=max_bins, range=(0, max_bins)) hist[256 * j:256 * (j + 1)] = histogram predict_lbp = classifier_predict.predict(hist.reshape(1, -1)) # predict_lbp_value = classifier_predict.decision_function(hist.reshape(1, -1)) if predict_shuffleNet == 'live' and predict_lbp == 0: predict = 'live' elif predict_shuffleNet == 'spoof' and predict_lbp == 1: predict = 'spoof' elif predict_shuffleNet == 'live' and predict_lbp == 1 and classifier_predict.decision_function( hist.reshape(1, -1)) > 1.23: predict = 'spoof' elif predict_shuffleNet == 'live' and predict_lbp == 1 and classifier_predict.decision_function( hist.reshape(1, -1)) < 1.23: predict = 'live' elif predict_shuffleNet == 'spoof' and predict_lbp == 0 and classifier_predict.decision_function( hist.reshape(1, -1)) < -0.227: predict = 'live' elif predict_shuffleNet == 'spoof' and predict_lbp == 0 and classifier_predict.decision_function( hist.reshape(1, -1)) > -0.227: predict = 'spoof' if predict == live_or_spoof: rightNums = rightNums + 1 elif predict == 'live' and live_or_spoof == 'spoof': # print(classifier_predict.decision_function(hist.reshape(1, -1))) FA = FA + 1 elif predict == 'spoof' and live_or_spoof == 'live': # lbp_live_error.append(classifier_predict.decision_function(hist.reshape(1, -1))) # print(classifier_predict.decision_function(hist.reshape(1, -1))) FR = FR + 1 if predict == live_or_spoof == 'live': TP = TP + 1 index = index + 1 # print(index, '/', total_samples, predict_lbp, predict_shuffleNet, predict, # 'acc:{:.5f}'.format(rightNums / index), # 'rightNums:', rightNums, 'FA:', FA, 'FR:', FR) if predict_lbp == 1: lbp_1.append(classifier_predict.decision_function(hist.reshape(1, -1))) elif predict_lbp == 0: lbp_0.append(classifier_predict.decision_function(hist.reshape(1, -1))) lbp_1.sort() lbp_0.sort() if len(lbp_1) % 2 == 0: print(lbp_1[int(len(lbp_1)/2) + 1]) else: print(lbp_1[int(len(lbp_1)/2)]) if len(lbp_0) % 2 == 0: print(lbp_0[int(len(lbp_0)/2) + 1]) else: print(lbp_0[int(len(lbp_0)/2)]) print('threshold:', threshold) print('acc:', rightNums / total_samples) print('FAR:', FA / spoof_samples_nums, 'FRR:', FR / live_samples_nums) print('Recall:', TP / live_samples_nums)
from skimage import feature import matplotlib.pyplot as plt import numpy as np import cv2 import glob numPoints=8 radius=3 textures=['gazon', 'gravier', 'bois'] for texture in textures: print(">>>", "{}.jpg".format(texture)) image=cv2.imread("{}.jpg".format(texture), 0) if image is None: quit("Probleme image...") lbp=feature.local_binary_pattern(image, numPoints, radius, method='default') hist_ref, _=np.histogram(lbp, bins=2**numPoints, range=(0, 2**numPoints)) cv2.imshow("Image", image) cv2.imshow("LBP", lbp) plt.plot(hist_ref) plt.show() key=cv2.waitKey(1)&0xFF if key==ord('q'): quit()
def main(): hight = int(2334 / 1) #read the data vote = 0 num = 0 f = open("Result.txt", "w") tf = open("Time.txt", "w") extension = '.JPG' #extension of yhe image path = 'G:\\college\\4th_year\\Pattern_Recognition\\Project\\handwritting\\submitted_code\\data' #the path of folder data directory = os.listdir(path) directory.sort(key=sortKeyFunc) # directory=sorted(directory,key=lambda x: int(os.path)) for foldername in directory: timebefore = time.time() training_features = [] labels = [] for t in range(1, 4): for i in range(1, 3): training_image = cv2.imread(path + '\\' + foldername + '\\' + str(t) + '\\' + str(i) + extension) #GUSSIAN FILTER kernel = np.ones((5, 5), np.float32) / 25 training_image = image_cropping.crop_image(training_image) #print("after cropping",training_image.shape) training_image = cv2.filter2D(training_image, -1, kernel) #GRAY LEVEL and croping #training_image=cv2.cvtColor(training_image,cv2.COLOR_RGB2GRAY) labels.append(t) print(foldername + '\\' + str(t) + '\\' + str(i)) lbp2 = local_binary_pattern(training_image, 16, 1, method='default') (hist, _) = np.histogram(lbp2[np.where(lbp2 < 175)], density=True, bins=256, range=(0, 256)) eps = 1e-7 # normalize the histogram hist = hist.astype("float") hist /= (hist.sum() + eps) training_features.append(hist) test_image = cv2.imread(path + '\\' + foldername + '\\' + 'test' + extension) #GUSSIAN FILTER kernel = np.ones((5, 5), np.float32) / 25 test_image = image_cropping.crop_image(test_image) test_image = cv2.filter2D(test_image, -1, kernel) # GRAY LEVEL and croping #test_image = cv2.cvtColor(test_image, cv2.COLOR_RGB2GRAY) #lbp for the testing set :V lbp2 = local_binary_pattern(test_image, 16, 1, method='default') (hist, _) = np.histogram(lbp2[np.where(lbp2 < 175)], density=True, bins=256, range=(0, 256)) eps = 1e-7 # normalize the histogram hist = hist.astype("float") hist /= (hist.sum() + eps) test_features = [] test_features.append(hist) #send data to knn result = svm_knn.myknn(training_features, labels, test_features) print("folder ", foldername) print(result) tf.write(str(time.time() - timebefore)) tf.write('\n') f.write(str(result)) f.write('\n') tf.close() f.close()
def process_wrapper(self, **kwargs): """ Local binary pattern threshold: Gray scale and rotation invariant LBP (Local Binary Patterns). LBP is an invariant descriptor that can be used for texture classification. Real time: False Keyword Arguments (in parentheses, argument name): * Activate tool (enabled): Toggle whether or not tool is active * Channel (channel): * Number of circularly symmetric neighbor (P): Number of circularly symmetric neighbor set points (quantization of the angular space) * Radius of circle (R): Radius of circle (spatial resolution of the operator) * Method to determine the pattern (method): * Transformation applied to output (transformation): * Cut x%% lower values (lower_cut): Increase to smooth low frequency textures regions and add detail to high frequencies * Cut x%% upper values (upper_cut): Increase to smooth high frequency textures regions and add detail to low frequencies * Postprocessing option (post_processing): * Threshold min value (min_t): * Threshold max value (max_t): * Median filter size (odd values only) (median_filter_size): * Morphology operator (morph_op): * Kernel size (kernel_size): * Kernel shape (kernel_shape): * Iterations (proc_times): * Select pseudo color map (color_map): """ wrapper = self.init_wrapper(**kwargs) if wrapper is None: return False res = False try: if self.get_value_of("enabled") == 1: c = ipc.resize_image( wrapper.get_channel( wrapper.current_image, self.get_value_of("channel") ), target_rect=RectangleRegion(width=800, height=600), keep_aspect_ratio=True, output_as_bgr=False, ) c = local_binary_pattern( image=c, P=self.get_value_of("P", scale_factor=wrapper.scale_factor), R=self.get_value_of("R", scale_factor=wrapper.scale_factor), method=self.get_value_of("method"), ) # Transform ct = self.get_value_of(f"transformation") if ct == "sigmoid": c = np.interp(c, (c.min(), c.max()), (0, 5)) c = expit(c) elif ct == "log": c = np.log(c + 1) elif ct == "logit": c = np.interp(c, (c.min(), c.max()), (0.5, 0.99)) c = logit(c) elif ct == "arcsin": c = np.interp(c, (c.min(), c.max()), (0, 1)) c = np.arcsin(c) elif ct == "sqrt": c = np.interp(c, (c.min(), c.max()), (0, 1)) c = np.sqrt(c) # Cut lower_cut = self.get_value_of("lower_cut") if lower_cut > 0: c[c < np.max(c) * (lower_cut / 100)] = 0 upper_cut = self.get_value_of("upper_cut") if upper_cut > 0: upper_cut = np.max(c) * ((100 - upper_cut) / 100) c[c > upper_cut] = upper_cut c = self.to_uint8(c) # Post processing pp = self.get_value_of("post_processing") if pp == "threshold": median_filter_size = self.get_value_of("median_filter_size") median_filter_size = ( 0 if median_filter_size == 1 else ipc.ensure_odd(median_filter_size) ) c, _ = wrapper.get_mask( src_img=c, channel=None, min_t=self.get_value_of("min_t"), max_t=self.get_value_of("max_t"), median_filter_size=median_filter_size, ) self.result = self.apply_morphology_from_params(c) elif pp == "false_color": color_map = self.get_value_of("color_map") _, color_map = color_map.split("_") self.result = cv2.applyColorMap(c, int(color_map)) else: self.result = c # Store wrapper.store_image(self.result, "local_binary_pattern") res = True else: wrapper.store_image(wrapper.current_image, "current_image") res = True except Exception as e: res = False logger.error(f'Failed to process {self. name}: "{repr(e)}"') else: pass finally: return res
from skimage.feature import local_binary_pattern from skimage import data from skimage.color import label2rgb import numpy as np # settings for LBP radius = 3 n_points = 8 * radius # Get three different images to test the algorithm with brick = data.load('brick.png') grass = data.load('grass.png') wall = data.load('rough-wall.png') # Calculate the LBP features for all the three images brick_lbp = local_binary_pattern(brick, 16, 2, 'uniform') grass_lbp = local_binary_pattern(grass, 16, 2, 'uniform') wall_lbp = local_binary_pattern(wall, 16, 2, 'uniform') # Next we will augment these images by rotating the images by 22 degrees brick_rot = rotate(brick, angle = 22, resize = False) grass_rot = rotate(grass, angle = 22, resize = False) wall_rot = rotate(wall, angle = 22, resize = False) # Let us calculate the LBP features for all the rotated images brick_rot_lbp = local_binary_pattern(brick_rot, 16, 2, 'uniform') grass_rot_lbp = local_binary_pattern(grass_rot, 16, 2, 'uniform') wall_rot_lbp = local_binary_pattern(wall_rot, 16, 2, 'uniform') # We will pick any one image say brick image and try to find # its best match among the rotated images
radius = [1, 2, 4, 8] n_points = [8 * r for r in radius] METHOD = 'uniform' # initialize arrays lbp_train = [] lbp_val = [] # calculate lbp features multi-scale print('Extracting LBP features for X_trian') for img in x_train: lbp_feat = [] # concatenate multiple scales of LBP radius and points for rind, r in enumerate(radius): img = img[:, :, [2, 1, 0]] img_gray = rgb2gray(img) lbp = local_binary_pattern(img_gray, n_points[rind], r, METHOD) n_bins = int(lbp.max() + 1) lbp_hist, _ = np.histogram(lbp, density=True, bins=n_bins, range=(0, n_bins)) lbp_feat.extend(lbp_hist) # extract LBP for HSV hue channel for rind, r in enumerate(radius): img_hsv = rgb2hsv(img) lbp = local_binary_pattern(img_hsv[:, :, 0], n_points[rind], r, METHOD) n_bins = int(lbp.max() + 1) lbp_hist, _ = np.histogram(lbp, density=True, bins=n_bins, range=(0, n_bins))
def lbp(frame): frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) frame = local_binary_pattern(frame, 24, 3, method='default') return frame
image[circle] = 1, 1, 0 return image #img = img_as_float(astronaut()[::2, ::2]) #img = io.imread('test_im.jpg') img = io.imread('../test_ims/test_2_0126_subset.jpg') redband = img[:, :, 0] radius = 9 n_points = 12 * radius lbp = local_binary_pattern(redband, n_points, radius, 'uniform') img[:, :, 0] = lbp #more from: https://vcansimplify.wordpress.com/2014/07/06/scikit-image-rag-introduction/ #slic segmentation labels1 = slic(img, n_segments=100, compactness=5) labels1 = labels1 + 1 regions1 = regionprops(labels1) labels1_rgb = color.label2rgb(labels1, img, kind='avg') show_img(labels1_rgb) label1_rgb = segmentation.mark_boundaries(labels1_rgb, labels1, (0, 0, 0)) show_img(label1_rgb)
def get_lbp_img(self, gimg): lbp = feature.local_binary_pattern(gimg, self.number_points, self.radius, method="ror") return lbp
}[n] X = [] for arquivo in paths.list_images('images'): imagem = cv2.imread(arquivo) altura, largura, _ = imagem.shape classe = math.floor(int(re.sub("\D", "", arquivo.split("\\")[1]))/100) print("classe =", classe) cinza = cv2.cvtColor(imagem, cv2.COLOR_BGR2GRAY) rgb = cv2.cvtColor(imagem, cv2.COLOR_BGR2RGB) r_histograma = cv2.calcHist([rgb], [0], None, [256], [0, 256])/(altura*largura) g_histograma = cv2.calcHist([rgb], [1], None, [256], [0, 256])/(altura*largura) b_histograma = cv2.calcHist([rgb], [2], None, [256], [0, 256])/(altura*largura) lbp = feature.local_binary_pattern(cinza, 59, 1, method="uniform") (lbp_histograma, _) = np.histogram(lbp.ravel(), bins=59, range=(0, 59)) lbp_histograma = lbp_histograma.astype("float") lbp_histograma /= (lbp_histograma.sum()) X_image = [r_histograma, g_histograma, b_histograma, lbp_histograma] X_image_aux = [] for aux in X_image: X_image_aux = np.append(X_image_aux, np.ravel(aux)) X_image = [i for i in X_image_aux] X_image.append(label(classe)) X.append(X_image) df = pd.DataFrame(X)
def extract(input_folder, feature_name, feature_folder): X = [] if feature_name == "vgg19": base_model = VGG19(weights='imagenet') save_file_name = 'vgg19_features' model = Model(inputs=base_model.input, outputs=base_model.get_layer('fc2').output) for filename in glob.glob(os.path.join(input_folder, '*.jpg')): img = image.load_img(filename, target_size=(224, 224)) x = image.img_to_array(img) x = np.expand_dims(x, axis=0) x = vgg19_pi(x) features = model.predict(x) X.append(features) elif feature_name == "vgg16": base_model = VGG16(weights='imagenet') save_file_name = 'vgg16_features' model = Model(inputs=base_model.input, outputs=base_model.get_layer('fc2').output) for filename in glob.glob(os.path.join(input_folder, '*.jpg')): img = image.load_img(filename, target_size=(224, 224)) x = image.img_to_array(img) x = np.expand_dims(x, axis=0) x = vgg16_pi(x) features = model.predict(x) X.append(features) elif feature_name == 'inception': base_model = InceptionV3(weights='imagenet') save_file_name = 'inceptionV3_features' model = Model(inputs=base_model.input, outputs=base_model.get_layer('avg_pool').output) for filename in glob.glob(os.path.join(input_folder, '*.jpg')): print(filename) img = image.load_img(filename, target_size=(299, 299)) x = image.img_to_array(img) x = np.expand_dims(x, axis=0) x = iv3_pi(x) features = model.predict(x) X.append(features) elif feature_name == 'xception': base_model = Xception(weights='imagenet') save_file_name = 'xception_features' model = Model(inputs=base_model.input, outputs=base_model.get_layer('avg_pool').output) for filename in glob.glob(os.path.join(input_folder, '*.jpg')): img = image.load_img(filename, target_size=(299, 299)) x = image.img_to_array(img) x = np.expand_dims(x, axis=0) x = xc_pi(x) features = model.predict(x) X.append(features) elif feature_name == 'resnet50': base_model = ResNet50(weights='imagenet') save_file_name = 'resnet50_features' model = Model(inputs=base_model.input, outputs=base_model.get_layer('avg_pool').output) for filename in glob.glob(os.path.join(input_folder, '*.jpg')): img = image.load_img(filename, target_size=(224, 224)) x = image.img_to_array(img) x = np.expand_dims(x, axis=0) x = preprocess_input(x) avg_pool_feature = model.predict(x) X.append(avg_pool_feature) elif feature_name == 'lbp': save_file_name = 'lbp_features' for filename in glob.glob(os.path.join(input_folder, '*.jpg')): filename = imread(filename, as_grey=True) lbt_image = local_binary_pattern(filename, P=24, R=3, method='uniform') (lbt_hist, _) = np.histogram(lbt_image.ravel(), bins=int(lbt_image.max() + 1), range=(0, 24 + 2)) X.append(lbt_hist) elif feature_name == 'hog': save_file_name = 'hog_features' for filename in glob.glob(os.path.join(input_folder, '*.jpg')): filename = imread(filename, as_grey=True) lbt_image = hog(filename, block_norm='L2') X.append(lbt_image) else: print('Not support model ' + feature_name) if X: X = np.array(X) X = np.squeeze(X) save_file = os.path.join(feature_folder, save_file_name + '.npy') #if feature_name == 'lbp' or feature_name == 'hog': X = X.T if not os.path.exists(feature_folder): os.makedirs(feature_folder) else: if os.path.isfile(save_file): os.remove(save_file) np.save(save_file, X) else: print('No input received') return X
indeces_per_levels = [[], [], [], [], []] image = cv2.imread(image_path) image_number = int(LJY_utils.extract_filename_from_path(image_path)) grayimage = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY) boundary_mask = grayimage > 30 grayimage = np.float32(grayimage) dst = cv2.cornerHarris(grayimage, 2, 3, 0.04) point_list = np.column_stack(np.where(dst > 0.01 * dst.max())) object_center = np.int32(np.mean(point_list, axis=0)) # Threshold for an optimal value, it may vary depending on the image. LAB_image = cv2.cvtColor(image, cv2.COLOR_RGB2LAB) _, hog_image = hog(cv2.cvtColor(image, cv2.COLOR_RGB2GRAY), block_norm='L2-Hys', visualise=True) LBP_feature = local_binary_pattern(cv2.cvtColor(image, cv2.COLOR_BGR2GRAY), 8 * 3, 3, method='uniform') LM_features = [] for filter_number in range(0, 48): # filtering LM_feature = cv2.filter2D(cv2.cvtColor(image, code=cv2.COLOR_RGB2GRAY), -1, kernel=LM_filter_bank[:, :, filter_number]) LM_features.append(LM_feature) for superpixel_level in range(0, 5): #1~5 level segments... segments = slic(image, n_segments=50*(superpixel_level+1), compactness=50, convert2lab=True) for i in range(np.min(segments), np.max(segments)+1): # if image belongs to a segment and is not a boundary, the mask of the corresponding index is true. superpixel_mask = (segments[:] == i) # superpixel && boundary.
lbp = np.float32(feature.local_binary_pattern(subImage, 8, 2, method='nri_uniform')) #lbp_bins = int(lbp.max()) #histogram = np.histogram(lbp.ravel(), bins=lbp_bins, range=(0, lbp_bins), density=True) _, histogram = np.unique(lbp, return_counts=True) histogram = np.true_divide(histogram, np.max(histogram)) concatenatedHistograms.extend(histogram) ''' # Para a abordagem de Close-up, descomentar este bloco. lbp = np.float32( feature.local_binary_pattern( dataset[i][box[0]:box[0] + box[2] + 1, box[1]:box[1] + box[3] + 1], 8, 2, method='nri_uniform')) _, histogram = np.unique(lbp, return_counts=True) histogram = np.true_divide(histogram, np.max(histogram)) concatenatedHistograms.extend(histogram) ''' # Para a abordagem com descritores dos olhos, descomentar este bloco. left_eye_lbp = np.float32(feature.local_binary_pattern(dataset[i][l_eye[0] - 50:l_eye[0] + 50,l_eye[1] - 50:l_eye[1] + 50], 8, 2, method='default')) right_eye_lbp = np.float32(feature.local_binary_pattern(dataset[i][r_eye[0] - 50:r_eye[0] + 50,r_eye[1] - 50:r_eye[1] + 50], 8, 2, method='default')) le_bins = int(left_eye_lbp.max()) re_bins = int(right_eye_lbp.max()) le_histogram = np.histogram(left_eye_lbp.ravel(), bins=le_bins, range=(0, le_bins), density=True)
imgfilename = os.listdir(testpath + "\\" + img) print(img) for doc in tqdm(imgfilename): test_images.append(testpath + "\\" + img + "\\" + doc) test_labels.append(flag) print('\n Extract Features from testing Files...') for test_image in tqdm(test_images): im = cv2.imread(test_image) #conver im_gray = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY) radius = 3 #number of points to be considered as neighbourers no_points = 8 * radius #uniform LBP is used lbp = local_binary_pattern(im_gray, no_points, radius, method='uniform') hist = getfeatures(lbp) #Append histogram to test_data test_data.append(hist) if 'category_list' not in globals(): with open('categorylist.pkl', 'rb') as fid: category_list = pickle.load(fid) if 'knmodel' not in globals(): with open('knmodel.pkl', 'rb') as fid: knmodel = pickle.load(fid) #if 'svmmodel' not in globals(): # with open('svmmodel.pkl', 'rb') as fid: # svmmodel = pickle.load(fid) knmodel_pridiction = [] #svm_pridiction=[]
def get_lbp(image): return local_binary_pattern(np.copy(image), 24, 3, 'ror')
def generate_lbp_features(filename): radius = 3 n_points = 10 * radius image_arr = io.imread(filename, as_grey=True) return local_binary_pattern(image_arr, P=n_points, R=radius)
def extract_LBP_QM_button(): global flag flag = 1 # All files will be selected from the folder files = os.listdir(imfoldername) print(files) labels = [] n = len(files) for f in files: newString = f.split('.', 2)[0] labels.append(newString) print(labels) v0 = [] v1 = [] v2 = [] v3 = [] v4 = [] v5 = [] v6 = [] v7 = [] v8 = [] v9 = [] for img in glob.glob(imfolder_path.get() + "\*.JPG"): image = cv2.imread(img, 0) features = feature.local_binary_pattern(image, 8, 1, method="default") (hist, _) = np.histogram(features.ravel(), bins=np.arange(0, 8 + 3), range=(0, 8 + 2)) hist = hist.astype("float") hist = hist / (hist.sum() + 1e-7) print(hist) # LBPfeatures.append(features) v0.append(hist[0]) v1.append(hist[1]) v2.append(hist[2]) v3.append(hist[3]) v4.append(hist[4]) v5.append(hist[5]) v6.append(hist[6]) v7.append(hist[7]) v8.append(hist[8]) v9.append(hist[9]) print(v0) print(v1) print(v2) print(v3) print(v4) print(v5) print(v6) print(v7) print(v8) print(v9) # global workbook workbook = xlsxwriter.Workbook('Test_LPB.xlsx') worksheet = workbook.add_worksheet() worksheet.write(0, 0, "Label") worksheet.write(0, 1, "v0") worksheet.write(0, 2, "v1") worksheet.write(0, 3, "v2") worksheet.write(0, 4, "v3") worksheet.write(0, 5, "v4") worksheet.write(0, 6, "v5") worksheet.write(0, 7, "v6") worksheet.write(0, 8, "v7") worksheet.write(0, 9, "v8") worksheet.write(0, 10, "v9") worksheetRow = 1 worksheetCol = 0 for i in range(len(labels)): worksheet.write(worksheetRow, worksheetCol, labels[i]) worksheet.write(worksheetRow, worksheetCol + 1, v0[i]) worksheet.write(worksheetRow, worksheetCol + 2, v1[i]) worksheet.write(worksheetRow, worksheetCol + 3, v2[i]) worksheet.write(worksheetRow, worksheetCol + 4, v3[i]) worksheet.write(worksheetRow, worksheetCol + 5, v4[i]) worksheet.write(worksheetRow, worksheetCol + 6, v5[i]) worksheet.write(worksheetRow, worksheetCol + 7, v6[i]) worksheet.write(worksheetRow, worksheetCol + 8, v7[i]) worksheet.write(worksheetRow, worksheetCol + 9, v8[i]) worksheet.write(worksheetRow, worksheetCol + 10, v9[i]) worksheetRow += 1 workbook.close() state_Label.set("LDP Extraction Complete")
def convert_coding(file_dir): pv_mask_image, pv_mhd_image = read_from_dir(file_dir) [x_min, x_max, y_min, y_max] = get_boundingbox(pv_mask_image) roi_image = pv_mhd_image[x_min:x_max, y_min:y_max] after_conding = local_binary_pattern(roi_image, 8, 3, 'uniform') return after_conding
cv2.destroyAllWindows() cv2.imshow("Sobely", sobely.astype('uint8') * 255) cv2.waitKey(0) cv2.destroyAllWindows() sobelx = sobely.astype('uint8') * 255 sobely = sobely.astype('uint8') * 255 sobel = sobelx + sobely cv2.imshow("Sobel", sobel) cv2.waitKey(0) cv2.destroyAllWindows() img_arr = np.array(sobel) #img_arr = img_arr[:,:,0] print(img_arr.shape) feat_lbp = local_binary_pattern(img_arr, 8, 1, 'uniform') #Radius = 1, No. of neighbours = 8 feat_lbp = np.uint8((feat_lbp / feat_lbp.max()) * 255) #Converting to unit8 lbp_img = PIL.Image.fromarray(feat_lbp) #Conversion from array to PIL image plt.imshow(lbp_img, cmap='gray') #Displaying LBP lbp_arr = np.array(lbp_img) cv2.imshow("LBP", lbp_arr) cv2.waitKey(0) cv2.destroyAllWindows() # Energy and Entropy of LBP feature lbp_hist, _ = np.histogram(feat_lbp, 8) lbp_hist = np.array(lbp_hist, dtype=float) lbp_prob = np.divide(lbp_hist, np.sum(lbp_hist)) lbp_energy = np.sum(lbp_prob**2) lbp_entropy = -np.sum(np.multiply(lbp_prob, np.log2(lbp_prob))) print('LBP energy = ' + str(lbp_energy))
def create_binary_pattern(img, p, r): print('[INFO] Computing local binary pattern features.') lbp = feature.local_binary_pattern(img, p, r) return (lbp - np.min(lbp)) / (np.max(lbp) - np.min(lbp)) * 255