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
댓글 수: 0
답변 (1개)
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
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 Center 및 File Exchange에서 Read, Write, and Modify Image에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!