Python sklearn.preprocessing 模块,binarize() 实例源码

我们从Python开源项目中,提取了以下8个代码示例,用于说明如何使用sklearn.preprocessing.binarize()

项目:variational-autoencoder    作者:musyoku    | 项目源码 | 文件源码
def load_images(image_dir, convert_to_grayscale=True, dist="bernoulli"):
    dataset = []
    fs = os.listdir(image_dir)
    print "loading", len(fs), "images..."
    for fn in fs:
        f = open("%s/%s" % (image_dir, fn), "rb")
        if convert_to_grayscale:
            img = np.asarray(Image.open(StringIO(f.read())).convert("L"), dtype=np.float32) / 255.0
        else:
            img = np.asarray(Image.open(StringIO(f.read())).convert("RGB"), dtype=np.float32).transpose(2, 0, 1) / 255.0
        if dist == "bernoulli":
            # Sampling
            img = preprocessing.binarize(img, threshold=0.5)
            pass
        elif dist == "gaussian":
            pass
        else:
            raise Exception()
        dataset.append(img)
        f.close()
    return dataset
项目:histonets-cv    作者:sul-cidr    | 项目源码 | 文件源码
def binarize_image(image, method='li', **kwargs):
    """Binarize image using one of the available methods: 'isodata',
    'li', 'otsu', 'sauvola', and 'boolean'. Defaults to 'li'.
    Extra keyword arguments are passed in as is to the corresponding
    scikit-image thresholding function. The 'boolean' method refers to simple
    thresholding from a grey-scale image. If a 'threshold' kwarg is not passed
    to the 'boolean' method, 'li' thresholding is performed.
    For reference
    Sezgin M. and Sankur B. (2004) "Survey over Image Thresholding Techniques
    and Quantitative Performance Evaluation" Journal of Electronic Imaging,
    13(1): 146-165 DOI:10.1117/1.1631315
    """
    if image.ndim != 2:
        # image is not gray-scale
        image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    if np.unique(image).size == 2:
        # image is already binary
        return image
    boolean_threshold = kwargs.get('threshold', None)
    if method == 'boolean' and boolean_threshold:
        preprocessing.binarize(image, threshold=boolean_threshold, copy=False)
        return convert(image)
    if method not in ('sauvola', 'isodata', 'otsu', 'li'):
        method = 'li'
    thresh_func = getattr(filters.thresholding, "threshold_{}".format(method))
    threshold = thresh_func(image, **kwargs)
    # OpenCV can't write black and white images using boolean values, it needs
    # at least a 8bits 1-channel image ranged from 0 (black) to 255 (white)
    return convert(image <= threshold)
项目:FeatureHub    作者:HDI-Project    | 项目源码 | 文件源码
def hi_lo_age(dataset):
    from sklearn.preprocessing import binarize
    cutoff = 30
    return binarize(dataset["users"]["age"].values.reshape(-1,1), cutoff)
项目:FeatureHub    作者:HDI-Project    | 项目源码 | 文件源码
def g(a):
    from sklearn.preprocessing import binarize
    return f(a)
项目:FeatureHub    作者:HDI-Project    | 项目源码 | 文件源码
def test_run_isolated_from_function_from_source():
    args = [1,3,7]
    f_source = b'def f(a):\n    return a+1\n'
    f1 = featurehub.util.get_function(f_source)
    g_source = b'def f(a):\n    return a+1\n\ndef g(a):\n    from sklearn.preprocessing import binarize\n    return f(a)\n'
    g1 = featurehub.util.get_function(g_source)
    for arg in args:
        assert f1(arg) == featurehub.util.run_isolated(f1, arg)
        assert g1(arg) == featurehub.util.run_isolated(g1, arg)
项目:FeatureHub    作者:HDI-Project    | 项目源码 | 文件源码
def test_run_isolated_from_function2_from_source():
    args = [1,3,7]
    f_source = b'def f(a):\n    return a+1\n'
    f1 = featurehub.util.get_function2(f_source)
    g_source = b'def f(a):\n    return a+1\n\ndef g(a):\n    from sklearn.preprocessing import binarize\n    return f(a)\n'
    g1 = featurehub.util.get_function2(g_source)
    for arg in args:
        assert f1(arg) == featurehub.util.run_isolated(f1, arg)
        assert g1(arg) == featurehub.util.run_isolated(g1, arg)

# ------------------------------------------------------------------------------ 
# Test compute_dataset_hash
项目:variational-autoencoder    作者:musyoku    | 项目源码 | 文件源码
def load_labeled_images(image_dir, convert_to_grayscale=True, dist="bernoulli"):
    dataset = []
    labels = []
    fs = os.listdir(image_dir)
    i = 0
    for fn in fs:
        m = re.match("([0-9]+)_.+", fn)
        label = int(m.group(1))
        f = open("%s/%s" % (image_dir, fn), "rb")
        if convert_to_grayscale:
            img = np.asarray(Image.open(StringIO(f.read())).convert("L"), dtype=np.float32) / 255.0
        else:
            img = np.asarray(Image.open(StringIO(f.read())).convert("RGB"), dtype=np.float32).transpose(2, 0, 1) / 255.0
        if dist == "bernoulli":
            # Sampling
            img = preprocessing.binarize(img, threshold=0.5)
            pass
        elif dist == "gaussian":
            pass
        else:
            raise Exception()
        dataset.append(img)
        labels.append(label)
        f.close()
        i += 1
        if i % 100 == 0:
            sys.stdout.write("\rloading images...({:d} / {:d})".format(i, len(fs)))
            sys.stdout.flush()
    sys.stdout.write("\n")
    return dataset, labels
项目:histonets-cv    作者:sul-cidr    | 项目源码 | 文件源码
def skeletonize_image(image, method=None, dilation=None, binarization=None,
                      invert=False):
    """Extract a 1 pixel wide representation of image by morphologically
    thinning the white contiguous blobs (connected components).
    If image is not black and white, a binarization process is applied
    according to binarization, which can be 'sauvola', 'isodata', 'otsu',
    'li' (default, ref: binarize()).

    A process of dilation can also be applied by specifying the number
    of pixels in dilate prior to extracting the skeleton.

    The method for skeletonization can be 'medial', '3d', 'regular', or a
    'combined' version of the three. Defaults to 'regular'.
    A 'thin' operator is also available. For reference,
    see http://scikit-image.org/docs/dev/auto_examples/edges/plot_skeleton.html
    """
    # if image is all one single color, return it
    if len(np.unique(image)) == 1:
        return image
    # scikit-image needs only 0's and 1's
    mono_image = binarize_image(image, method=binarization) / 255
    if invert:
        mono_image = invert_image(mono_image)
    if dilation:
        dilation = (2 * dilation) + 1
        dilation_kernel = np.ones((dilation, dilation), np.uint8)
        mono_image = cv2.morphologyEx(mono_image, cv2.MORPH_DILATE,
                                      dilation_kernel)
    with warnings.catch_warnings(record=True):
        warnings.filterwarnings('ignore', category=UserWarning)
        if method == '3d':
            skeleton = morphology.skeletonize_3d(mono_image)
        elif method == 'medial':
            skeleton = morphology.medial_axis(mono_image,
                                              return_distance=False)
        elif method == 'thin':
            skeleton = morphology.thin(mono_image)
        elif method == 'combined':
            skeleton = (morphology.skeletonize_3d(mono_image)
                        | morphology.medial_axis(mono_image,
                                                 return_distance=False)
                        | morphology.skeletonize(mono_image))
        else:
            skeleton = morphology.skeletonize(mono_image)
    return convert(skeleton)