Photoshop image processing algorithm-saturation adjustment

Source: Internet
Author: User

Preface: Some relevant methods of Photoshop color space used in the company to do the project, summarize here. The following principles are partially intercepted from my summary document. Need to copy the children's shoes to write their own ~



2. Program part

1) MATLAB experimental program.

<span style= "FONT-SIZE:18PX;" >clc;clear all;close all;image=imread (' fotor_lomoorg.bmp '); image=double (Image); R=image (:,:, 1); G=image (:,:, 2); B=image (:,:, 3); [Row, col] = size (R); R_new=r; g_new=g; b_new=b;%%%% Increment, saturation adjustment increment ( -100,100) Photoshop range increment=-50;% converted to adjustment ratio increment=increment/100;%        Use the HSL pattern to derive the color s and lfor i=1:row for J=1:col Rgbmax=max (R (i,j), Max (G (i,j), B (I,J)));        Rgbmin=min (R (i,j), Min (G (i,j), B (I,J)));           Delta= (rgbmax-rgbmin)/255;        if (delta==0)% if delta=0, then saturation s=0, so can not adjust saturation continue;        End value = (Rgbmax + rgbmin)/255;                                L=VALUE/2;        %lightness if (l<0.5)% calculates the saturation S S=delta/value according to the luminosity L;        else S =delta/(2-value); End% specific saturation adjustment, Increment for saturation increment if (increment>=0) if ((increment+s) >=1) alpha=            S else alpha=1-increment;            End Alpha=1/alpha-1;          R_new (I,J) = R (i,j) + (R (i,j)-L * 255) * ALPHA;          G_new (I,J) = g (i,j) + (g (i,j)-L * 255) * ALPHA;        B_new (I,J) = B (i,j) + (b (i,j)-L * 255) * ALPHA;          else alpha=increment;          R_new (i,j) = l*255 + (R (i,j)-L * 255) * (1+ALPHA);          G_new (i,j) = l*255 + (G (i,j)-L * 255) * (1+ALPHA);         B_new (i,j) = l*255 + (B (i,j)-L * 255) * (1+ALPHA); End EndEnd Image_new (:,:, 1) =r_new;image_new (:,:, 2) =g_new;image_new (:,:, 3) =b_new;imshow (image/255); figure, Imshow ( image_new/255); </span><span style= "font-weight:bold; font-size:18px; " ></span>

2) C program, where only the key processing section, has turned the image into an array to deal with

<span style= "FONT-SIZE:18PX;" >void Saturationadjustrgb (unsigned char *psrc, unsigned char *pdest, int nwidth, int nheight,int nparameter) {//local variable declaration i NT i = 0;int t = 0;int nlength = nheight * nwidth;//parameter handling double dpercent= static_cast< double > (nparameter)/100;//r GB Color Channel Declaration unsigned char *imgr = new unsigned char[nlength];unsigned char *imgg = new unsigned char[nlength];unsigned char *i MgB = new unsigned char[nlength];//local variable declaration unsigned char rgbmax;unsigned char rgbmin;double ddelta;double dvalue;double D L;double ds;double dalpha;//separate RGB channel for (i = 0; i < nlength; i++) {t = 3 * I;imgb[i] = psrc[t];imgg[i] = psrc[t + 1];i Mgr[i] = psrc[t + 2];} for (int i = 0; i < nlength; i++) {Rgbmax = max (max (Imgr[i], imgg[i), Imgb[i]), rgbmin = min (min (imgr[i], imgg[i]),  Imgb[i]);dD Elta = static_cast<double> (rgbmax-rgbmin)/255;dvalue = static_cast<double> (RgbMax + rgbMin)/ 255;//If the pixel is gray not processed if (0 = = Ddelta) {continue;} Calculate luminosity by formula L [0,1]DL = DValue/2;//calculates saturation by Formula S [0,1]if (DL < 0.5) {DS = Ddelta/dvalue;} Else{ds = Ddelta/(2-dvalue);} For saturation adjustment if (dpercent >= 0) {if (dpercent + DS >= 1) {dalpha = ds;} Else{dalpha = 1-dpercent;} Dalpha = 1/dalpha-1;imgb[i] = Imgb[i] + (Imgb[i]-DL * 255) * Dalpha;imgg[i] = Imgg[i] + (Imgg[i]-DL * 255) * d Alpha;imgr[i] = Imgr[i] + (Imgr[i]-DL * 255) * DALPHA;} Else{dalpha = Dpercent;imgb[i] = DL * 255 + (Imgb[i]-DL * 255) * (1 + dalpha); Imgg[i] = DL * 255 + (Imgg[i]-DL * 255 ) * (1 + dalpha); Imgr[i] = DL * 255 + (Imgr[i]-DL * 255) * (1 + dalpha);}} Get results for (i = 0; i < nlength; i++) {t = 3 * i;pdest[t] = Imgb[i];pD est[t + 1] = Imgg[i];pD est[t + 2] = Imgr[i];} Free memory if (!imgr) {delete []imgr;imgr = NULL;} if (!imgg) {delete []IMGG;IMGG = NULL;} if (!IMGB) {delete []IMGB;IMGB = NULL;}} </span>

3, experimental results, and Photoshop processing results consistent


Figure 1 Original


Figure 2 The result of a parameter of 50


Figure 2 The result of a parameter of-50

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Photoshop image processing algorithm-saturation adjustment

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.