1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
| #include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "cv.h"
#include "highgui.h"
double f(double t) {
if(t > 0.008856)
return pow(t, 1/3.0);
else if (t <= 0.008856)
return 7.787*t + 16/116;
}
double genererL(double y) {
if(y > 0.008856)
return 116*pow(y, 1/3.0) -16 ;
else if(y <= 0.008856)
return 903.3*y;
}
int main(int argc, char *argv[])
{
IplImage *img=NULL, *dst=NULL;
char *raster, *rasterLAB;
int w,h,nCh;
int i,j;
img = cvLoadImage("fruits.jpg",CV_LOAD_IMAGE_COLOR);
dst = cvCreateImage( cvGetSize(img), IPL_DEPTH_8U, 3);
cvCvtColor(img, dst, CV_BGR2Lab);
/* Lecture des informations sur l'image */
raster = img->imageData;
rasterLAB = dst->imageData;
w = img->width;
h = img->height;
nCh = img->nChannels;
uchar R, G, B;
uchar LAB_L, LAB_a, LAB_b;
/* Image couleur */
for(i=0;i<h;i++)
for(j=0;j<w;j++) {
// convert sRGB (R,G,B) to linear-rgb (r,g,b)
R = *(raster+i*w*nCh+j*nCh);
G = *(raster+i*w*nCh+j*nCh+1);
B = *(raster+i*w*nCh+j*nCh+2);
// convert to XYZ (assuming sRGB was D65)
uchar X = R*0.412453 + G*0.357580 + B*0.180423;
uchar Y = R*0.212671 + G*0.715160 + B*0.072169;
uchar Z = R*0.019334 + G*0.119193 + B*0.950227;
// Rescale X/Y/Z relative to white point D65
double Xr = 0.950456, Yr = 1.0, Zr = 1.088754;
double xr = X/Xr;
double yr = Y/Yr;
double zr = Z/Zr;
LAB_L = genererL(yr);
LAB_a = 500*(f(xr)-f(yr));
LAB_b = 200*(f(yr)-f(zr));
if(j==0 && i==0) {
printf("\n%d %d %d\n",R, G, B);
printf("\n%d %d %d\n",LAB_L, LAB_a, LAB_b);
}
*(rasterLAB+i*dst->width*dst->nChannels+j*dst->nChannels) = LAB_L;
*(rasterLAB+i*dst->width*dst->nChannels+j*dst->nChannels+1) = LAB_a;
*(rasterLAB+i*dst->width*dst->nChannels+j*dst->nChannels+2) = LAB_b;
}
/* Création de la fenêtre Windows */
cvNamedWindow( "Source", CV_WINDOW_AUTOSIZE );
cvShowImage( "Source", img );
cvNamedWindow("Destination", CV_WINDOW_AUTOSIZE );
cvShowImage( "Destination", dst );
/* Position de la fenêtre */
cvMoveWindow("Source", 100,100);
cvWaitKey(0);
/* Libérer la mémoire de la structure image et la fenêtre */
cvReleaseImage( &img );
cvReleaseImage( &dst );
cvDestroyWindow( "Source" );
cvDestroyWindow( "Destination" );
return 0;
} |
Partager