Gamma校正@幂律转换

时间:2012-06-26 15:55:49

标签: opencv

我试图对图像进行一些简单的伽马校正。起初,我尝试使用Matlab,然后将其应用于opencv。但是我得到了不同的结果。下面是一些代码。代码在哪里出错?

在matlab中:

for i=1:r;
    for j=1:c;
       imout(i,j)=constant_value*power(img_double(i,j),0.04);
    end
 end

在OpenCV中:

for(int y=0; y<height; y++){
   for(int x=0; x<width; x++)
   {
        dataNew[y*stepNew+x] = constant_value*pow(dataNew[y*stepNew+x], 0.04);
   }
}

图像是无符号8位,1通道图像。我想念哪一部分?

2 个答案:

答案 0 :(得分:3)

我的猜测是你忘记将OpenCV中的图像数据缩放到区间[0,1]。在Matlab中im2double会自动为您完成此操作。 因此,对于8位图像,这样的事情应该起作用:

dataNew[y*stepNew+x] = 255 * constant_value*pow(dataNew[y*stepNew+x]/255.0, 0.04);

答案 1 :(得分:1)

"""Function gamma( ) performs gamma(power transform) 
   logt() performs logarithmic transform
   histogram_equal( ) histogram equalization transform
"""

import numpy as np
def gamma(image,gamma = 0.5):
    img_float = np.float32(image)
    max_pixel = np.max(img_float)
    #image pixel normalisation
    img_normalised = img_float/max_pixel
    #gamma correction exponent calulated
    gamma_corr = np.log(img_normalised)*gamma
    #gamma correction being applied
    gamma_corrected = np.exp(gamma_corr)*255.0
    #conversion to unsigned int 8 bit
    gamma_corrected = np.uint8(gamma_corrected)
    return gamma_corrected


def logt(image):
    img_float = np.float32(image)
    max_pixel = np.max(img_float)
    #log correction being caluclated
    log_corrected = (255.0*np.log(1+img_float))/np.log(1+max_pixel)
    #conversion to unsigned int 8 bit
    log_corrected = np.uint8(log_corrected)
    return log_correctedenter code here
def histogram_equal(image):
    img_float = np.float32(image)
    #conversion 2D array to 1D array
    img_flat = img_float.flatten()
    #histogram genreation
    hist,bins = np.histogram(img_float,256,[0,255])
    #histogram cumulative distribution
    cdf = hist.cumsum()
    #to ignore values of cdf = 0
    cdf_masked = np.ma.masked_equal(cdf,0)
    num_cdf_m = (cdf_masked - cdf_masked.min())*255
    den_cdf_m = (cdf_masked.max()-cdf_masked.min())
    cdf_masked = num_cdf_m/den_cdf_m
    cdf = np.ma.filled(cdf_masked,0)
    cdf = np.uint8(cdf)
    img_flat = np.uint8(img_flat)
    img = cdf[img_flat]
    img = np.reshape(img,img_float.shape)
    return img