matlab實現調整圖片的對比度

god 小文發表於2020-04-27
%src為rgb影像,Contrast為調節的對比度值,調節範圍為【-100100】
function img = ContrastAdjust(src,Contrast)

Image=double(src);

R=Image(:,:,1);
G=Image(:,:,2);
B=Image(:,:,3);

Average=127;
%調整引數[-100,100]


Contrast=Contrast/100*255;
Percent=Contrast/255;
if(Contrast>0)
    R = Average + (R - Average) * 1 / (1 - Percent) ;
    G = Average + (G - Average) * 1 / (1 - Percent) ;
    B = Average + (B - Average) * 1 / (1 - Percent) ;
else
    R= Average + (R - Average) * (1 + Percent);
    G= Average + (G - Average) * (1 + Percent);
    B= Average + (B - Average) * (1 + Percent);
    
end

img(:,:,1)=R;
img(:,:,2)=G;
img(:,:,3)=B;
end

相關文章