1. 程式人生 > 實用技巧 >Photoshop影象處理演算法—色階調整

Photoshop影象處理演算法—色階調整

前言:之前在公司做專案的用到photoshop顏色空間的一些相關方法,在此總結一下。下面原理部分是從我的總結文件裡擷取來的。需要複製的童鞋自己手寫一下~

2、程式部分

1)Matlab實驗程式。

 1 clc;clear;close all;
 2 Image=imread('Fotor_LomoOrg.bmp');
 3 figure(1);
 4 imshow(Image);
 5  
 6 R=double(Image(:,:,1));
 7 G=double(Image(:,:,2));
 8 B=double(Image(:,:,3));
 9  
10 %調整引數
11 HighLight=238
; 12 Shadow=159; 13 Midtones=0.51; 14 15 Diff=HighLight-Shadow; 16 rDiff=R-Shadow; 17 gDiff=G-Shadow; 18 bDiff=B-Shadow; 19 20 rDiff(rDiff<0)=0; 21 gDiff(gDiff<0)=0; 22 bDiff(bDiff<0)=0; 23 24 R=(rDiff/Diff).^(1/Midtones)*255; 25 G=(gDiff/Diff).^(1/Midtones)*255; 26 B=(bDiff/Diff).^(1/Midtones)*255
; 27 28 R(R>255)=255; 29 G(G>255)=255; 30 B(B>255)=255; 31 32 img(:,:,1)=uint8(R); 33 img(:,:,2)=uint8(G); 34 img(:,:,3)=uint8(B); 35 figure(2); 36 imshow(img); 37 </span>

2)C程式

 1 void LevelAdjustRGB(unsigned char *pSrc, unsigned char *pDest, int nWidth, int nHeight,int nShadow,int
nHighLight,double dMidtone) 2 { 3 //區域性變數宣告 4 int i = 0; 5 int nLength = nWidth * nHeight; 6 double dDiff = nHighLight-nShadow; 7 int *nRgbDiff = new int[3 * nLength]; 8 9 for(i = 0;i < 3 * nLength; i++) 10 { 11 nRgbDiff[i] = pSrc[i] - nShadow; 12 if(nRgbDiff[i] < 0) 13 { 14 nRgbDiff[i] = 0; 15 } 16 17 //此處必須用int作為中間變數 不能用char 會有資料截斷 18 int nTemp = static_cast<int>(pow((nRgbDiff[i] / dDiff),1 / dMidtone) * 255); 19 pDest[i] = CLIP8(nTemp); 20 } 21 //釋放空間 22 if(!nRgbDiff) 23 { 24 delete []nRgbDiff; 25 nRgbDiff = NULL; 26 } 27 }</span>

其中

#define  CLIP8(a)  (((a) & 0xFFFFFF00) ? (((a) < 0) ? 0 : 255 ) : (a))      ///<判斷8位資料範圍

3、實驗結果,同photoshop處理結果

圖1 原圖

圖2 nShadow=159 nHighLight=238 dMidtone=0.51結果