Doubt on gamma correction implementation

3 次查看(过去 30 天)
Here I posted Gamma correction algorithm and matlab code....I'm little bit confused ...Are these two same ?
gammaCorrection = 1 / gamma
colour = GetPixelColour(x, y)
newRed = 255 * (Red(colour) / 255) ^ gammaCorrection
newGreen = 255 * (Green(colour) / 255) ^ gammaCorrection
newBlue = 255 * (Blue(colour) / 255) ^ gammaCorrection
PutPixelColour(x, y) = RGB(newRed, newGreen, newBlue)
%%%%%%%
gamma=5;
gammaCorrection=1/gamma;
red=I(:,:,1);
green=I(:,:,2);
blue=I(:,:,3);
newRed = 255 * ( red / 255) ^ gammaCorrection;
newGreen = 255 * (green / 255) ^ gammaCorrection;
newBlue = 255 * (blue / 255) ^ gammaCorrection;
GImage=zeros(size(I));
GImage(:,:,1)=newRed ;
GImage(:,:,2)=newGreen;
GImage(:,:,3)=newBlue

回答(1 个)

Image Analyst
Image Analyst 2020-2-2
I'd cast to double first, then divide, and put some parentheses around it, use .^ to do element-by-element processing, and cast back to uint8. Then use cat() to build the output image. Untested code (based on your second chunk):
gamma=5;
gammaCorrection=1/gamma;
red=I(:,:,1);
green=I(:,:,2);
blue=I(:,:,3);
newRed = uint8(255 * (( double(red) / 255) .^ gammaCorrection));
newGreen = uint8(255 * ((double(green) / 255) .^ gammaCorrection));
newBlue = uint8(255 * ((double(blue) / 255) .^ gammaCorrection));
GImage= cat(3, newRed, newGreen, newBlue); % Use cat() to create the new image.
  2 个评论
Sajina Rose
Sajina Rose 2020-2-3
Hi, this code produce black image as output.
DGM
DGM 2023-3-9
编辑:DGM 2023-3-9
The given code works fine.
I = imread('peppers.png'); % uint8
gamma=2.4; % something plausible
gammaCorrection=1/gamma;
red=I(:,:,1);
green=I(:,:,2);
blue=I(:,:,3);
newRed = uint8(255 * (( double(red) / 255) .^ gammaCorrection));
newGreen = uint8(255 * ((double(green) / 255) .^ gammaCorrection));
newBlue = uint8(255 * ((double(blue) / 255) .^ gammaCorrection));
GImage= cat(3, newRed, newGreen, newBlue); % Use cat() to create the new image.
imshow(GImage)
... but only so long as the input is uint8.
I = im2double(imread('peppers.png')); % double
gamma=2.4; % something plausible
gammaCorrection=1/gamma;
red=I(:,:,1);
green=I(:,:,2);
blue=I(:,:,3);
newRed = uint8(255 * (( double(red) / 255) .^ gammaCorrection));
newGreen = uint8(255 * ((double(green) / 255) .^ gammaCorrection));
newBlue = uint8(255 * ((double(blue) / 255) .^ gammaCorrection));
GImage= cat(3, newRed, newGreen, newBlue); % Use cat() to create the new image.
imshow(GImage)
That's the hazard of assuming that all image data is always uint8. A slightly better approach would be to use the appropriate tools to make the process insensitive to the input class. We still have the problem that there isn't a convenient canonical way to make the output image inherit its class and scale from the input, so we're left asserting that the output is always uint8.
inpict = imread('peppers.png');
gam = 2.4; % something plausible
outpict = im2uint8(im2double(inpict).^(1/gam));
imshow(outpict)
Better yet, you could use imadjust(). Note that nothing here is dependent on assuming either input or output class. The only requirement is that the image is of one of the typical numeric/logical classes associated with images, and that it's properly-scaled for its class.
inpict = imread('peppers.png');
gam = 2.4; % something plausible
outpict = imadjust(inpict,[0 1],[0 1],1/gam);
imshow(outpict)
But if the goal is gamma correction instead of arbitrary image adjustment, maybe it'd be better to be using something other than a simple power function. You decide. See also rgb2lin().
inpict = imread('peppers.png');
outpict = lin2rgb(inpict);
imshow(outpict)

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Convert Image Type 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by