【问题标题】:How to implement histogram equalization for images in tensorflow?如何实现张量流中图像的直方图均衡化?
【发布时间】:2017-08-07 16:42:48
【问题描述】:

我是深度学习和 Tensorflow 新手。

我正在尝试修改 cifar10 tensorflow 教程,以便将其与面部输入图像一起使用。

如何计算直方图均衡化?

是否可以包装类似于Histogram equalization of grayscale images with NumPy 中的解决方案?

【问题讨论】:

    标签: python image-processing tensorflow


    【解决方案1】:

    对于灰度uint8 图像,您可以使用以下内容:

    def tf_equalize_histogram(image):
        values_range = tf.constant([0., 255.], dtype = tf.float32)
        histogram = tf.histogram_fixed_width(tf.to_float(image), values_range, 256)
        cdf = tf.cumsum(histogram)
        cdf_min = cdf[tf.reduce_min(tf.where(tf.greater(cdf, 0)))]
    
        img_shape = tf.shape(image)
        pix_cnt = img_shape[-3] * img_shape[-2]
        px_map = tf.round(tf.to_float(cdf - cdf_min) * 255. / tf.to_float(pix_cnt - 1))
        px_map = tf.cast(px_map, tf.uint8)
    
        eq_hist = tf.expand_dims(tf.gather_nd(px_map, tf.cast(image, tf.int32)), 2)
        return eq_hist
    

    测试用:

    import tensorflow as tf
    import numpy as np
    import cv2
    
    image_ph = tf.placeholder(tf.uint8, shape = [None, None, 1])
    image_eq_hist = tf_equalize_histogram(image_ph)
    
    image = cv2.imread("./test_gs.png", 0)
    image = np.reshape(image, (image.shape[0], image.shape[1], 1))
    with tf.Session() as sess:
        sess.run(tf.global_variables_initializer())
        image_eq_hist_ = sess.run(image_eq_hist, feed_dict = {image_ph : image})
    
    cv2.imshow("eq_cv", cv2.equalizeHist(image))
    cv2.imshow("eq", image_eq_hist_)
    cv2.waitKey()
    

    【讨论】:

    • 好答案,为我工作! @ale-6 应该接受这个作为 IMO 的答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-27
    • 1970-01-01
    • 1970-01-01
    • 2013-11-20
    • 2010-12-23
    • 2020-02-11
    相关资源
    最近更新 更多