示例#1
0
    def compute(self, image):
        """
        Compute the LBP features. These features are returned as histograms of 
        LBPs.
        """
        try:
            lbp = local_binary_pattern(image, self.npoints_, self.radius_, self.method_)
            hist, _ = np.histogram(lbp, normed=True, bins=self.nhbins_, range=(0, self.nhbins_))
        except:
            print("Error in LBPDescriptor.compute()")

        return hist
示例#2
0
    def compute(self, image):
        """
        Compute the LBP features. These features are returned as histograms of 
        LBPs.
        """
        try:
            lbp = local_binary_pattern(image, self.npoints_, self.radius_,
                                       self.method_)
            hist, _ = np.histogram(lbp,
                                   normed=True,
                                   bins=self.nhbins_,
                                   range=(0, self.nhbins_))
        except:
            print("Error in LBPDescriptor.compute()")

        return hist
示例#3
0
def feature_build(img):
	from skimage.feature.texture import greycoprops, greycomatrix, local_binary_pattern
	from skimage.color import rgb2gray
	img = np.asarray(rgb2gray(img.numpy()), dtype=np.uint8)
	mat = greycomatrix(img, [1, 2], [0, np.pi/2], levels=4, normed=True, symmetric=True)
	features = []
	if (True):
		features.append(greycoprops(mat, 'contrast'))
		features.append(greycoprops(mat, 'dissimilarity'))
		features.append(greycoprops(mat, 'homogeneity'))
		#features.append(greycoprops(mat, 'energy'))
		#features.append(greycoprops(mat, 'correlation'))
		features = np.concatenate(features)
	else:
		radius = 2
		features = local_binary_pattern(img, 8*radius, radius, method='default') #'ror', 'uniform', 'var'
	feature = features.flatten()
	return torch.tensor(feature).float()
示例#4
0
def extract_lbp(image, radius=1.5):
    '''
    Extract LBP features.
    '''
    image = Image(data=image.data)
    image.convert_to_gray()
    image.equalize_clahe()

    n_points = 8 * radius
    lbp = local_binary_pattern(image.data, n_points, radius)

    n_bins = lbp.max() + 1
    hist, _ = np.histogram(lbp, normed=True, bins=n_bins, range=(0, n_bins))
    hist, _, _ = z_norm_by_feature(hist)
    plt.plot(hist)
    plt.show()

    return lbp
    def compute_LBP_rgb(im, mask):
        """
        Given a segmented image and a segmentation mask compute LBP and return the corresponding normalized histogram
        with 256 bins

        :param im: an np array NxMx3 of a segmented image
        :param mask: a np array NxMx3 of the segmentation mask
        :return: a np array corresponding to the lbp normalized histogram with 256 bins
        """
        im_copy = im.copy()
        mask_copy = mask.copy()
        im_copy = FeatureExtractor.rgb2grayscale(im_copy)
        P, R = 8, 1
        dim = 2 ** P
        mask_copy = mask_copy[:, :, 0].astype(bool)
        codes = local_binary_pattern(im_copy, P, R, method="ror")
        # hist, _ = np.histogram(codes[mask], bins=np.arange(dim + 1), range=(0, dim))
        hist, _ = np.histogram(codes[mask_copy], bins=256, range=(0, 255))
        norm_hist = hist / np.sum(hist)
        return norm_hist
示例#6
0
def getHistogramPeaks(path):
    if not os.path.exists(path):
        print(path, "does not exist")
        return None
    
    print "Handling", os.path.basename(path)
    
    vidcap = cv2.VideoCapture(path)
    framenum = 0
    peaks = 0
    peaks16 = 0
    peaks8 = 0
    lbppeaks = 0
    peakfreq = [0 for _ in range(34)]
    peakfreq16 = [0 for _ in range(18)]
    peakfreq8 = [0 for _ in range(10)]
    lbppeakfreq = [0 for _ in range(28)]
    lasthist = [0 for _ in range(32)]
    lasthist16 = [0 for _ in range(16)]
    lasthist8 = [0 for _ in range(8)]
    histdiffs = []
    histdiffs16 = []
    histdiffs8 = []
    
    success, image = vidcap.read()
    
    while success:
        grey = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
        blurred = cv2.medianBlur(grey, 3)
        
        histogram = cv2.calcHist([blurred], [0], None, [32], [0, 256])
        cv2.normalize(histogram, histogram)
        hist16 = cv2.calcHist([blurred], [0], None, [16], [0, 256])
        cv2.normalize(hist16, hist16)
        hist8 = cv2.calcHist([blurred], [0], None, [8], [0, 256])
        cv2.normalize(hist8, hist8)
        histarray = np.asarray(histogram).ravel()
        hist16 = np.asarray(hist16).ravel()
        hist8 = np.asarray(hist8).ravel()
        
        if framenum % 5 == 0:
            radius = 3
            no_points = 8 * radius
            
            lbp = local_binary_pattern(grey, no_points, radius, method='uniform')
            cv2.normalize(lbp, lbp)
            lbpfreq = itemfreq(lbp.ravel())
            lbphist = lbpfreq[:, 1]/sum(lbpfreq[:, 1])
            lbphist = np.asarray(lbphist).ravel()
            lbphist = np.insert(lbphist, 0, 0)
            lbphist = np.append(lbphist, 0)
            currentlbppeaks = peakutils.indexes(lbphist, 0.3 * (28 / float(26)), 1) # change dist back to 2 if it worsens
            for val in currentlbppeaks:
                lbppeakfreq[val] += 1
                lbppeaks += len(currentlbppeaks)
        
        minbins = [0 for _ in range(32)]
        for idx, hbin in enumerate(histarray):
            minbins[idx] = min(hbin, lasthist[idx])
            
        minbins16 = [0 for _ in range(16)]
        for idx, hbin in enumerate(hist16):
            minbins16[idx] = min(hbin, lasthist16[idx])
            
        minbins8 = [0 for _ in range(8)]
        for idx, hbin in enumerate(hist8):
            minbins8[idx] = min(hbin, lasthist8[idx])
            
        histarray = np.insert(histarray, 0, 0)
        histarray = np.append(histarray, 0)
        hist16 = np.insert(hist16, 0, 0)
        hist16 = np.append(hist16, 0)
        hist8 = np.insert(hist8, 0, 0)
        hist8 = np.append(hist8, 0)
        
        currentpeaks = peakutils.indexes(histarray, 0.3 * (34 / float(32)), 1)
        curpeaks16 = peakutils.indexes(hist16, 0.3 * (18 / float(16)), 1)
        curpeaks8 = peakutils.indexes(hist8, 0.3 * (10 / float(8)), 1)
        
        for val in currentpeaks:
            peakfreq[val] += 1
            
        for val in curpeaks16:
            peakfreq16[val] += 1
            
        for val in curpeaks8:
            peakfreq8[val] += 1
            
        histdiffs.append(1 - (sum(minbins) / float(sum(histarray))))
        histdiffs16.append(1 - (sum(minbins16) / float(sum(hist16))))
        histdiffs8.append(1 - (sum(minbins8) / float(sum(hist8))))
        
        peaks += len(currentpeaks)
        peaks16 += len(curpeaks16)
        peaks8 += len(curpeaks8)
        
        lasthist = histarray
        lasthist16 = hist16
        lasthist8 = hist8
        framenum += 1
        
        success, image = vidcap.read()
    
    bias = 0
    bias16 = 0
    bias8 = 0
    lbpbias = 0
    if sum(peakfreq) != 0:
        #bias = (sum(heapq.nlargest(2, peakfreq)) / float(sum(peakfreq)))
        bias = np.var(peakfreq)
        bias16 = np.var(peakfreq16)
        bias8 = np.var(peakfreq8)
    if sum(lbppeakfreq) != 0:
        #lbpbias = max(lbppeakfreq) / float(sum(lbppeakfreq))
        lbpbias = np.var(lbppeakfreq)
        
    meandiff = np.mean(histdiffs)
    meandiff16 = np.mean(histdiffs16)
    meandiff8 = np.mean(histdiffs8)
    lbpmax = np.argmax(lbppeakfreq)
    
    print(peaks, "histogram peaks (32 bins),", lbppeaks, "lbp peaks")
    print(meandiff, "mean diff")
    
    return peaks, peaks16, peaks8, lbppeaks, lbpmax, meandiff, meandiff16, meandiff8, bias, bias16, bias8, lbpbias
示例#7
0
            cv2.IMREAD_GRAYSCALE)
        # get mask and convert them to boolean
        pleuraMask = cv2.imread(
            inputDir + "/boundary_masks/" + boundaryDataSet + "/pleura/" +
            imageName, cv2.IMREAD_GRAYSCALE) > 0
        nonPleuraMask = cv2.imread(
            inputDir + "/boundary_masks/" + boundaryDataSet + "/non_pleura/" +
            imageName, cv2.IMREAD_GRAYSCALE) > 0

        # local Binary Pattern (LBP)
        radius = 3
        nPoints = 8 * radius

        # Compute LBP fro the whole input image
        lbp = local_binary_pattern(inputImage,
                                   nPoints,
                                   radius,
                                   method='uniform')
        nBins = int(lbp.max() + 1)

        # split masks into tiles
        pleuraTiles, positions = SplitImage(
            pleuraMask,
            tile_size)  # get positions just one here because it is the same
        nonPleuraTiles, _ = SplitImage(nonPleuraMask, tile_size)
        lbpTiles, _ = SplitImage(lbp, tile_size)

        pleuraDataset = ComputeLBPHistograms(positions, lbpTiles, pleuraTiles,
                                             1)
        nonPleuraDataset = ComputeLBPHistograms(positions, lbpTiles,
                                                nonPleuraTiles, -1)
def syn_graph_met(m_img, segments, lambda_coff, dist_hist=False):
    image = m_img

    # init graph by first method, by color distance metric between superpixels.
    row = image.shape[0]
    col = image.shape[1]
    #     print(row, col)
    segmentsLabel = []
    for i in range(row):
        for j in range(col):
            l = segments[i, j]
            if l not in segmentsLabel:
                segmentsLabel.append(l)

    position = []
    ave_position = []
    flatten_position = []

    for i in segmentsLabel:
        pixel_position = []
        flatten_pos = []
        for m in range(row):
            for n in range(col):
                if segments[m, n] == i:
                    pixel_position.append([m, n])
                    flatten_pos.append(m * col + n)

        position.append(pixel_position)
        flatten_position.append(flatten_pos)

        pixel_position = np.asarray(pixel_position)
        ave_position.append(
            (sum(pixel_position) / len(pixel_position)).tolist())

    # generate average color value and red, green, blue color values
    average = []
    red_average = []
    green_average = []
    blue_average = []
    for i in range(len(position)):
        val = 0
        red_val = 0
        green_val = 0
        blue_val = 0
        for j in position[i]:
            [m, n] = j
            val += 0.299 * image[m, n, 0] + 0.587 * image[
                m, n, 1] + 0.114 * image[m, n, 2]
            red_val += image[m, n, 0]
            green_val += image[m, n, 1]
            blue_val += image[m, n, 2]
            # val += image[m, n]
        average.append(val / len(position[i]))
        red_average.append(red_val / len(position[i]))
        green_average.append(green_val / len(position[i]))
        blue_average.append(blue_val / len(position[i]))

    # distance metric: by average value
    # average = []
    # for i in range(len(position)):
    #     val = 0
    #     for j in position[i]:
    #         [m, n] = j
    #         val += 0.299*image[m, n, 0] + 0.587*image[m, n, 1] + 0.114*image[m, n, 2]
    #         # val += image[m, n]
    #     average.append(val/len(position[i]))

    # length = len(position)
    # graph = np.zeros((length, length))
    # for i in range(length):
    #     for j in range(length):
    #         graph[i, j] = abs(average[i] - average[j]) ** 2

    graph_time = time.time()

    # fully connected
    sigma = 255.0
    length = len(position)
    graph = np.zeros((length, length))

    # settings for LBP
    radius = 2
    n_points = 8 * radius
    METHOD = 'uniform'

    img, lbp = [], []
    for i in range(3):
        c_img = image[:, :, i]
        c_lbp = local_binary_pattern(c_img, n_points, radius, METHOD)
        img.append(c_img)
        lbp.append(c_lbp)

    for i in range(length):
        for j in range(length):
            if not dist_hist:
                diff = abs(red_average[i] - red_average[j]) + abs(
                    green_average[i] -
                    green_average[j]) + abs(blue_average[i] - blue_average[j])
#                 if lambda_coff:
#                     dist = LA.norm(np.asarray(ave_position[i]) - np.asarray(ave_position[j]))
#                     diff = diff + lambda_coff * dist
            else:
                # reads an input image, color mode
                hist1 = hist(flatten_position[i], img, lbp)
                hist2 = hist(flatten_position[j], img, lbp)

                diff = abs(distance.cityblock(hist1, hist2))

            graph[i, j] = diff
            # graph[i, j] = math.e ** (-(diff ** 2) / sigma)


#     print('graph construction time: ', time.time() - graph_time)

# matrix eigen-decomposition, scipy.sparse.linalg
    vals, vectors = np.linalg.eigh(graph)
    vals, vectors = np.real(vals), np.real(vectors)
    index1, index2, index3 = np.argsort(vals)[0], np.argsort(
        vals)[1], np.argsort(vals)[2]
    # index1, index2, index3 = np.argsort(vals)[::-1][0], np.argsort(vals)[::-1][1], np.argsort(vals)[::-1][2]
    ev1, ev2, ev3 = vectors[:, index1], vectors[:, index2], vectors[:, index3]

    return vectors[:, index1], graph
示例#9
0
文件: LBP.py 项目: ivarvb/LHA
    def process(self, arg):
        imageName = arg["imageName"]
        inputdir = arg["inputdir"]
        #boundaryDataSet = arg["boundaryDataSet"]
        targetSet = arg["targetSet"]
        imagedir = arg["imagedir"]
        masksdir = arg["masksdir"]
        maskstilesdir = arg["maskstilesdir"]
        tile_size = arg["parameters"]["tile_size"]
        radius = arg["parameters"]["radius"]
        tilepercentage = arg["tilepercentage"]

        #df = arg["df"]

        print(imageName)

        fimage = inputdir + '/' + imagedir + '/' + targetSet + '/' + imageName
        fimagmask_pleura_path = inputdir + "/" + masksdir + "/pleura/"
        fimagmask_nonpleura_path = inputdir + "/" + masksdir + "/non_pleura/"
        """
        fimage = inputdir + imagedir+'/images_cleaned/' + targetSet + '/' + imageName        
        fimagmask_pleura_path = inputdir + "/boundary_masks/" + boundaryDataSet + "/pleura/"
        fimagmask_nonpleura_path = inputdir + "/boundary_masks/" + boundaryDataSet + "/non_pleura/"
        """

        #print(fimagmask_pleura_path + imageName)
        inputImage = cv2.imread(fimage, cv2.IMREAD_GRAYSCALE)
        pleuraMask = cv2.imread(fimagmask_pleura_path + imageName,
                                cv2.IMREAD_GRAYSCALE) > 0
        nonPleuraMask = cv2.imread(fimagmask_nonpleura_path + imageName,
                                   cv2.IMREAD_GRAYSCALE) > 0

        # local Binary Pattern (LBP)
        #radius = 3
        nPoints = 8 * radius

        # Compute LBP fro the whole input image
        lbp = local_binary_pattern(inputImage,
                                   nPoints,
                                   radius,
                                   method='uniform')
        nBins = int(lbp.max() + 1)

        # split masks into tiles
        pleuraTiles, positions = Util.splitImage(
            pleuraMask,
            tile_size)  # get positions just one here because it is the same
        nonPleuraTiles, _ = Util.splitImage(nonPleuraMask, tile_size)
        lbpTiles, _ = Util.splitImage(lbp, tile_size)

        icc = 1
        pleuraDataset = self.computeLBPHistograms(icc, imageName, positions,
                                                  lbpTiles, pleuraTiles, nBins,
                                                  "pleura", tilepercentage)
        icc = len(pleuraDataset) + 1
        nonPleuraDataset = self.computeLBPHistograms(icc, imageName, positions,
                                                     lbpTiles, nonPleuraTiles,
                                                     nBins, "nopleura",
                                                     tilepercentage)

        return pleuraDataset + nonPleuraDataset