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 に答える