有没有办法使用OpenCV均衡每个样本图像16位的直方图?

时间:2014-02-12 23:53:27

标签: c++ opencv image-processing histogram

我正在处理16位/样本图像 是否有(简单)方法来执行此类图像的直方图均衡(转换为8bps不是一种选择)?

4 个答案:

答案 0 :(得分:4)

OpenCV中的

equalizeHist只需要8位数据。

但OpenCV中的图像标准化不限于8位数据。请参阅其描述here。在您的情况下,对函数的调用应如下所示:

normalize(src_image, dst_image, 0, 65535, NORM_MINMAX);

如果您正在尝试提高图像的对比度,请首先尝试规范化,并且只有在这不起作用时才尝试均衡。归一化更快,破坏性更小。

请参阅:http://answers.opencv.org/question/3176/improve-contrast-of-a-16u-image/

答案 1 :(得分:2)

到目前为止,OpenCV equalizeHist仅支持8位图像。我已经基于OpenCV实现here创建了16位直方图均衡函数here

void equalizeHist16Bit(const cv::Mat &_src, cv::Mat &_dst)
{
    _dst = _src.clone();

    const int hist_sz = 65536;
    int *hist = new int[hist_sz] {};
    int *lut = new int[hist_sz] {};

    for (int y = 0; y < _src.rows; y++)
        for (int x = 0; x < _src.cols; x++)
            hist[(int)_src.at<unsigned short int>(y, x)]++;

    auto i = 0;
    while (!hist[i]) ++i;

    auto total = (int)_src.total();
    if (hist[i] == total) 
    {
        _dst.setTo(i);
        return;
    }

    float scale = (hist_sz - 1.f) / (total - hist[i]);
    auto sum = 0;

    for (lut[i++] = 0; i < hist_sz; ++i) 
    {
        sum += hist[i];
        lut[i] = cv::saturate_cast<ushort>(sum * scale);
    }

    for (int y = 0; y < _src.rows; y++)
        for (int x = 0; x < _src.cols; x++)
        {
            _dst.at<unsigned short int>(y, x) = lut[(int)_src.at<unsigned short int>(y, x)];
        }
}

答案 2 :(得分:0)

#Simple implementation in python 
#Reference: https://github.com/torywalker/histogram-equalizer/blob/master/HistogramEqualization.ipynb

import cv2
import numpy as np
import matplotlib.pyplot as plt
img_tif=cv2.imread("scan.tif",cv2.IMREAD_ANYDEPTH)
img = np.asarray(img_tif)
flat = img.flatten()
hist = get_histogram(flat,65536)
#plt.plot(hist)

cs = cumsum(hist)
# re-normalize cumsum values to be between 0-255

# numerator & denomenator
nj = (cs - cs.min()) * 65535
N = cs.max() - cs.min()

# re-normalize the cdf
cs = nj / N
cs = cs.astype('uint16')
img_new = cs[flat]
#plt.hist(img_new, bins=65536)
#plt.show(block=True)
img_new = np.reshape(img_new, img.shape)
cv2.imwrite("contrast.tif",img_new)

答案 3 :(得分:0)

Python的简单实现
参考:https://github.com/torywalker/histogram-equalizer/blob/master/HistogramEqualization.ipynb

import cv2
import numpy as np
import matplotlib.pyplot as plt
img_tif=cv2.imread("scan_before threthold_873.tif",cv2.IMREAD_ANYDEPTH)
img = np.asarray(img_tif)
flat = img.flatten()
hist = get_histogram(flat,65536)
#plt.plot(hist)
#
cs = cumsum(hist)
# re-normalize cumsum values to be between 0-255

# numerator & denomenator
nj = (cs - cs.min()) * 65535
N = cs.max() - cs.min()

# re-normalize the cdf
cs = nj / N
cs = cs.astype('uint16')
img_new = cs[flat]
#plt.hist(img_new, bins=65536)
#plt.show(block=True)
img_new = np.reshape(img_new, img.shape)
cv2.imwrite("contrast.tif",img_new)