首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Gamma校正@幂律变换

Gamma校正@幂律变换
EN

Stack Overflow用户
提问于 2012-06-26 23:55:49
回答 2查看 5.2K关注 0票数 1

我试着对图像做一些简单的伽马校正。首先,我尝试使用Matlab,然后将其应用于opencv。但是我得到了不同的结果。下面是一些代码。代码哪里出了问题?

在matlab中:

代码语言:javascript
复制
for i=1:r;
    for j=1:c;
       imout(i,j)=constant_value*power(img_double(i,j),0.04);
    end
 end

在OpenCV中:

代码语言:javascript
复制
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通道图像。我错过了哪部分?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-06-27 00:20:12

我的猜测是,您忘记了将OpenCV中的图像数据缩放到0,1的间隔。在Matlab中,im2double会自动为您执行此操作。因此,对于一个8位的图像,应该可以这样做:

代码语言:javascript
复制
dataNew[y*stepNew+x] = 255 * constant_value*pow(dataNew[y*stepNew+x]/255.0, 0.04);
票数 4
EN

Stack Overflow用户

发布于 2015-06-05 07:33:34

代码语言:javascript
复制
"""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   
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11211260

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档