2012-06-26 6 views
0

이미지에서 간단한 감마 보정을 시도했습니다. 처음에는 Matlab을 사용하여 시도한 다음 opencv에 적용했습니다. 그러나 나는 다른 결과를 얻는다. 아래 코드 중 일부입니다. 코드가 어디에서 잘못 되었습니까? 매트랩감마 보정 @ 전력 법 변환

:

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

OpenCV의 물 :

화상 부호없는 8 비트, 1 개 채널 영상이다
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); 
    } 
} 

. 내가 놓친 부분은?

답변

3

내 생각 엔 OpenCV의 이미지 데이터를 구간 [0,1]로 스케일링하는 것을 잊었을 것입니다. Matlab im2double에서 자동으로이 작업을 수행합니다. 따라서이 같은 8 비트 이미지 뭔가 작업을해야합니다 위해 :

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

왜 상수 값 앞에 거기에 255? 내부 부서를 수행 한 후 255로 스케일링하는 것입니까? – Mzk

+0

그것이 정확히 무엇을위한 것입니다. – sietschie

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