您的位置:首页 > 运维架构

Photoshop图像处理算法实现—明度调整

2015-08-27 20:15 681 查看
前言:之前在公司做项目的用到photoshop颜色空间的一些相关方法,在此总结一下。下面原理部分是从我的总结文档里截取来的。需要复制的童鞋自己手写一下~



2、程序部分

1)Matlab实验程序。

clc;clear;close all;
Image=imread('IMG_0950_cut.jpg');
figure(1);
imshow(Image);

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

%输入调整参数value [-100,100]之间,与photoshop一致
value=-50;

%放缩到[-255,255]之间  对应物理意义的
value=value*255/100;

if(value>=0)
    R = R + (255 - R) * value / 255;
    G = G + (255 - G) * value / 255;
    B = B + (255 - B) * value / 255;
else
    R = R + R * value / 255;
    G = G + G * value / 255;
    B = B + B * value / 255;
end

img(:,:,1)=uint8(R);
img(:,:,2)=uint8(G);
img(:,:,3)=uint8(B);
figure(2);
imshow(img);


2)C程序,此处只贴上关键处理部分,已经把图像变成了数组来处理。

void BrightAdjustRGB(unsigned char *pSrc, unsigned char *pDest, int nWidth, int nHeight,int nParameter)
{

	//参数范围有效性判断
	if (nParameter < -100 || nParameter > 100)
	    return;

	//局部变量声明
	int i = 0; 
	int t = 0;

	int nLength = nWidth * nHeight;

	//缩放明度调整参数
	nParameter = nParameter * 255 / 100;

	//得到结果
	if(nParameter >= 0)
	{
    	for (i = 0; i < nLength; i++)
    	{
	       t = i * 3;

		    pDest[t] = pSrc[t] + (255 - pSrc[t]) * nParameter / 255;
	    	pDest[t + 1] = pSrc[t + 1]+(255 - pSrc[t + 1]) * nParameter / 255;
	     	pDest[t + 2] = pSrc[t + 2]+(255 - pSrc[t + 2]) * nParameter / 255;
	     }
	}
	else
	{
	    for (i = 0; i < nLength; i++)
    	{
	        t = i * 3;

		    pDest[t] = pSrc[t] + pSrc[t] * nParameter / 255;
	    	pDest[t + 1] = pSrc[t + 1] + pSrc[t + 1] * nParameter / 255;
	     	pDest[t + 2] = pSrc[t + 2] + pSrc[t + 2] * nParameter / 255;
	     }
	}

}


3、实验结果,与photoshop处理结果一致



图1 原图



图2 参数为-50结果



图3 参数为50结果
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: