The five--adaptive migration algorithm for color migration

Source: Internet
Author: User
Tags cos sin

The color space is a three-dimensional linear space, usually using red, green and Blue (RGB) as the base of the color space, but the three primary colors do not intuitively measure the hue, saturation and brightness (HSV), in order to reflect the different characteristics of the color space, people summed up a lot of color space. The three components of the LMS color space proposed by Smith, respectively, represent the long, medium, and short excitation spectra. The light sensor of the cone cells in the human retina is the most sensitive to the wavelength. In this sense, we convert the RGB image representation in the computer into a LMS representation based on the more sensitive wavelength of the human eye. In fact, the different color space is isomorphic, so there must be a transformation matrix between them, in fact, Reinhard and so on to give the RGB and LMS color transformation, after the transformation of the LMS value of the distribution domain and the RGB value of the distribution domain, or relatively divergent, in order to make the LMS data points more amass, Reinhard and so on the LMS to do a logarithmic transformation l=logl,m=logm,s=logs to replace the LMS value. Hu Guofei, the correlation matrix of LMS is obtained based on this.

The correlation coefficients of L and M,m and s,l and S are rlm,rms,rls respectively. Experimental results of a large number of image samples show that the correlation matrix is usually not a three diagonal matrix, or even a diagonal-dominated matrix, which means that the LMS three components have a serious correlation. Then, by using eigenvalue analysis and principal element analysis method to get the composite linear transformation matrix, a series of linear transformations of translation rotation and ratio are adopted to the LMS value, so that the color component of each pixel in the image is basically eliminated. According to the matrix theory, it is easy to decompose the eigenvalue of the correlation matrix, find the eigenvalue λ1,λ2,λ3 and the eigenvector e1,e2,e3, and memorize the feature matrix as

Then the load factor matrix is obtained, then the transition matrix of the base vector of the LMS to the lαβ color space can be obtained (which is the inverse of the eigenvalue matrix), and this c is the required composite transformation matrix.

For each image to be processed, there are different transition matrix C. The values of the lαβ color space are calculated by using the adaptive processing of color images and shape images. Instead of using a fixed c to process all the images. Although this algorithm pays a little price in the computation time, the adaptive method accords with the color distribution of the image itself. In addition, processing all the pixels of an image increases the accuracy of the statistics, but for computational speed and storage resources, you can also sample at intervals to reduce the amount of data processed, without compromising the overall effect.

——————————————————————————————————————-——————————————————————

Using some of the functions, most of the others refer to the code I wrote before about the Reinhard algorithm.

Matrix for eigenvalues and eigenvectors bool Jacbicor (double *pmatrix,int nDim, double *pdblvects, double *pdbeigenvalues, double dbeps,int nJt) {   for (int i = 0; i < NDim; i + +) {Pdblvects[i*ndim+i] = 1.0f;   for (int j = 0; J < NDim; J + +) {if (i! = j) pdblvects[i*ndim+j]=0.0f;     }} int ncount = 0;  Iteration count while (1) {//Find the largest element on PMatrix non-diagonal double Dbmax = pmatrix[1];  int nrow = 0;  int ncol = 1; for (int i = 0; i < NDim; i + +)//line {for (int j = 0; J < NDim; J + +)//column {double d = fabs (Pmatri   X[I*NDIM+J]);     if ((i!=j) && (d> Dbmax)) {Dbmax = D;     Nrow = i;   Ncol = j;    }}} if (Dbmax < dbeps)//precision meets requirements break;  if (ncount > NJt)//The number of iterations exceeds the limit break;  ncount++;  Double Dbapp = Pmatrix[nrow*ndim+nrow];  Double DBAPQ = Pmatrix[nrow*ndim+ncol];  Double dbaqq = Pmatrix[ncol*ndim+ncol];  Calculate rotation angle Double dbangle = 0.5*atan2 ( -2*dbapq,dbaqq-dbapp);  Double Dbsintheta = sin (dbangle);  Double dbcostheta = cos (dbangle); DoubLe Dbsin2theta = sin (2*dbangle);  Double dbcos2theta = cos (2*dbangle);  Pmatrix[nrow*ndim+nrow] = Dbapp*dbcostheta*dbcostheta + Dbaqq*dbsintheta*dbsintheta + 2*dbApq*dbCosTheta*dbSinTheta;  Pmatrix[ncol*ndim+ncol] = Dbapp*dbsintheta*dbsintheta + Dbaqq*dbcostheta*dbcostheta-2*dbapq*dbcostheta*dbsintheta;  Pmatrix[nrow*ndim+ncol] = 0.5* (dbaqq-dbapp) *dbsin2theta + Dbapq*dbcos2theta;  Pmatrix[ncol*ndim+nrow] = Pmatrix[nrow*ndim+ncol];  for (int i = 0; i < NDim; i + +) {if ((I!=ncol) && (I!=nrow)) {int u = I*ndim + nrow;  p int w = I*ndim + ncol;   Q Dbmax = Pmatrix[u];   pmatrix[u]= Pmatrix[w]*dbsintheta + Dbmax*dbcostheta;   Pmatrix[w]= Pmatrix[w]*dbcostheta-dbmax*dbsintheta;  }} for (int j = 0; J < NDim; J + +) {if ((J!=ncol) && (J!=nrow)) {int u = Nrow*ndim + j;  p int w = Ncol*ndim + j;   Q Dbmax = Pmatrix[u];   pmatrix[u]= Pmatrix[w]*dbsintheta + Dbmax*dbcostheta;   Pmatrix[w]= Pmatrix[w]*dbcostheta-dbmax*dbsintheta;  } }//Calculate eigenvectors for (int i = 0; i < NDim; i + +) {int u = I*ndim + nrow;      p int w = I*ndim + ncol;   Q Dbmax = Pdblvects[u];   Pdblvects[u] = Pdblvects[w]*dbsintheta + Dbmax*dbcostheta;   PDBLVECTS[W] = Pdblvects[w]*dbcostheta-dbmax*dbsintheta;   }} for (int i = 0; i < NDim; i + +) pdbeigenvalues[i] = Pmatrix[i*ndim+i];  Set the positive sign for (int i = 0; i < NDim; i + +) {double Dsumvec = 0;  for (int j = 0; J < NDim; J + +) Dsumvec + = Pdblvects[j * NDim + i];  if (dsumvec<0) {for (int j = 0;j < NDim; j + +) Pdblvects[j * nDim + i] *=-1; }} return 1;}

//matrix inverse Void Inversematrix (double a[3][3],double b[3][3],int n) {int i,j,k,m=2*n;double mik,temp;double a[3][6]={0};for (i=0;i<n        ; i++) {for (j=0;j<n;j++) {if (i==j) b[i][j]=1.0;elseb[i][j]=0.0;}}  Initialize B=efor (i=0;i<n;i++) for (j=0;j<n;j++) a[i][j]=a[i][j];  Copy A to A to avoid changing the value of a for (i=0;i<n;i++) for (j=n;j<m;j++) a[i][j]=b[i][j-n]; Copy B to a, augmented matrix for (k=1;k<=n-1;k++) {for (i=k+1;i<=n;i++) {mik=a[i-1][k-1]/a[k-1][k-1];for (j=k+1;j<=m;j++) {A [I-1]        [J-1]-=mik*a[k-1][j-1];}}        Order Gaussian elimination The lower left corner is zero for (i=1;i<=n;i++) {temp=a[i-1][i-1];for (j=1;j<=m;j++) {a[i-1][j-1]/=temp;}} Normalized for (k=n-1;k>=1;k--) {for (i=k;i>=1;i--) {mik=a[i-1][k];for (j=k+1;j<=m;j++) {a[i-1][j-1]-=mik*a[k][        J-1];}}}  Inverse Gaussian elimination of the augmented matrix to the left of the unit matrix for (i=0;i<n;i++) for (j=0;j<n;j++) b[i][j]=a[i][j+n]; Remove the inverse result for (i=0;i<n;i++) for (j=0;j<n;j++) if (Fabs (B[i][j]) <0.0001) b[i][j]=0.0;} 

——————————————————————————————————————-——————————————————————

References: Hu Guofei, Mobile, Peng. Adaptive color Migration [J]. Chinese Journal of Computer Science, 2004,27 (9): 1245-1249.

The five--adaptive migration algorithm for color migration

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.