1

我尝试对图像进行一些简单的伽马校正。起初,我尝试使用 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 通道图像。我错过了哪一部分?

4

2 回答 2

4

我的猜测是您忘记将 OpenCV 中的图像数据缩放到区间 [0,1]。在 Matlabim2double中会自动为您执行此操作。因此,对于 8 位图像,这样的东西应该可以工作:

dataNew[y*stepNew+x] = 255 * constant_value*pow(dataNew[y*stepNew+x]/255.0, 0.04);
于 2012-06-26T16:20:11.610 回答
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   
于 2015-06-04T23:33:34.260 回答