前言:之前在公司做项目的用到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结果