00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00030 #include <stdio.h>
00031 #include <stdlib.h>
00032 #include <string.h>
00033 #include <math.h>
00034 #include "rgbcubeIPOL.h"
00035 #include "colorfilteringIPOL.h"
00036 #include "miscIPOL.h"
00037 #include "mdmath.h"
00038
00047 int count_RGBcolors(unsigned char *imR, unsigned char *imG,
00048 unsigned char *imB, int w, int h)
00049 {
00050 int iR, iG, nRGB;
00051
00052
00053
00054 unsigned char ***check=new unsigned char**[256];
00055 for (iR=0; iR < 256; iR++) {
00056 check[iR]=new unsigned char*[256];
00057 for (iG=0; iG < 256; iG++) {
00058 check[iR][iG]=new unsigned char[256];
00059 memset(check[iR][iG], 0, 256);
00060 }
00061 }
00062
00063
00064 nRGB=0;
00065 for (int n=0; n < w*h; n++) {
00066 if (!check[imR[n]][imG[n]][imB[n]]) {
00067 check[imR[n]][imG[n]][imB[n]]=1;
00068 nRGB++;
00069 }
00070 }
00071
00072
00073 for (iR=0; iR < 256; iR++) {
00074 for (iG=0; iG < 256; iG++) {
00075 delete[] check[iR][iG];
00076 }
00077 delete[] check[iR];
00078 }
00079 delete[] check;
00080
00081 return nRGB;
00082 }
00083
00110 void PCAviews(unsigned char *imR, unsigned char *imG, unsigned char *imB,
00111 int w, int h,
00112 unsigned char *refR, unsigned char *refG, unsigned char *refB,
00113 int wref, int href,
00114 unsigned char *im12R, unsigned char *im12G, unsigned char *im12B,
00115 unsigned char *im13R, unsigned char *im13G, unsigned char *im13B,
00116 unsigned char *im23R, unsigned char *im23G,
00117 unsigned char *im23B,
00118 int wout, int hout)
00119 {
00120
00121 float Rcm, Gcm, Bcm, vp1, vp2, vp3, vc1[3], vc2[3], vc3[3];
00122
00123 if ((refR == NULL) || (refG == NULL) || (refB == NULL)) {
00124 refR=imR; refG=imG; refB=imB; wref=w; href=h;
00125 }
00126
00127 compute_RGB_PCA(refR, refG, refB, wref*href, Rcm, Gcm, Bcm, vp1, vp2, vp3,
00128 vc1, vc2, vc3);
00129
00130 float M[3];
00131 M[0]=Rcm;
00132 M[1]=Gcm;
00133 M[2]=Bcm;
00134 projectRGBcube(imR, imG, imB, w*h, NULL, im12R, im12G, im12B,
00135 wout, hout, M, vc1, vc2);
00136 projectRGBcube(imR, imG, imB, w*h, NULL, im13R, im13G, im13B,
00137 wout, hout, M, vc1, vc3);
00138 projectRGBcube(imR, imG, imB, w*h, NULL, im23R, im23G, im23B,
00139 wout, hout, M, vc2, vc3);
00140 }
00141
00142
00163 void PCAviewsB(unsigned char *imR, unsigned char *imG, unsigned char *imB,
00164 int w, int h,
00165 unsigned char *refR, unsigned char *refG, unsigned char *refB,
00166 int wref, int href,
00167 unsigned char *im123R, unsigned char *im123G,
00168 unsigned char *im123B,
00169 int wout, int hout)
00170 {
00171
00172 unsigned char *im12R=new unsigned char[wout*hout];
00173 unsigned char *im12G=new unsigned char[wout*hout];
00174 unsigned char *im12B=new unsigned char[wout*hout];
00175 unsigned char *im13R=new unsigned char[wout*hout];
00176 unsigned char *im13G=new unsigned char[wout*hout];
00177 unsigned char *im13B=new unsigned char[wout*hout];
00178 unsigned char *im23R=new unsigned char[wout*hout];
00179 unsigned char *im23G=new unsigned char[wout*hout];
00180 unsigned char *im23B=new unsigned char[wout*hout];
00181
00182 PCAviews(imR, imG, imB, w, h, refR, refG, refB, wref, href,
00183 im12R, im12G, im12B, im13R, im13G, im13B, im23R, im23G, im23B,
00184 wout, hout);
00185
00186
00187 for (int j=0; j < hout; j++) {
00188 memcpy(im123R+3*j*wout, im12R+j*wout, wout);
00189 memcpy(im123R+3*j*wout+wout, im13R+j*wout, wout);
00190 memcpy(im123R+3*j*wout+2*wout, im23R+j*wout, wout);
00191 memcpy(im123G+3*j*wout, im12G+j*wout, wout);
00192 memcpy(im123G+3*j*wout+wout, im13G+j*wout, wout);
00193 memcpy(im123G+3*j*wout+2*wout, im23G+j*wout, wout);
00194 memcpy(im123B+3*j*wout, im12B+j*wout, wout);
00195 memcpy(im123B+3*j*wout+wout, im13B+j*wout, wout);
00196 memcpy(im123B+3*j*wout+2*wout, im23B+j*wout, wout);
00197 }
00198
00199 delete[] im12R;
00200 delete[] im12G;
00201 delete[] im12B;
00202 delete[] im13R;
00203 delete[] im13G;
00204 delete[] im13B;
00205 delete[] im23R;
00206 delete[] im23G;
00207 delete[] im23B;
00208 }
00209
00247 void get_densityRGB_PCA(unsigned char *R, unsigned char *G, unsigned char *B,
00248 unsigned char *Rref, unsigned char *Gref,
00249 unsigned char *Bref,
00250 int w, int h,
00251 unsigned char *h12r, unsigned char *h12g,
00252 unsigned char *h12b,
00253 unsigned char *h13r, unsigned char *h13g,
00254 unsigned char *h13b,
00255 unsigned char *h23r, unsigned char *h23g,
00256 unsigned char *h23b,
00257 int wout, int hout, int rdst, float hdst,
00258 unsigned char cumdsty,
00259 unsigned char *Idsty, unsigned char logscale)
00260 {
00261 float ***dsts;
00262 float Rcm, Gcm, Bcm, vp1, vp2, vp3, vc1[3], vc2[3], vc3[3];
00263 float maxdst, maxhistdst;
00264
00265
00266 dsts=RGBdensities(R, G, B, w, h, rdst, hdst, maxdst);
00267
00268
00269 if (!Rref || !Gref || !Bref) { Rref=R; Gref=G; Bref=B; }
00270 compute_RGB_PCA(Rref, Gref, Bref, w*h, Rcm, Gcm, Bcm,
00271 vp1, vp2, vp3, vc1, vc2, vc3);
00272
00273 float M[3];
00274 M[0]=Rcm;
00275 M[1]=Gcm;
00276 M[2]=Bcm;
00277
00278 if (cumdsty) {
00279
00280 maxhistdst=0;
00281 projectRGBdensities(R, G, B, w*h, h12r, h12g, h12b, wout, hout,
00282 M, vc1, vc2, dsts, maxhistdst, logscale);
00283 projectRGBdensities(R, G, B, w*h, h13r, h13g, h13b, wout, hout,
00284 M, vc1, vc3, dsts, maxhistdst, logscale);
00285 projectRGBdensities(R, G, B, w*h, h23r, h23g, h23b, wout, hout,
00286 M, vc2, vc3, dsts, maxhistdst, logscale);
00287
00288 projectRGBdensities(R, G, B, w*h, h12r, h12g, h12b, wout, hout,
00289 M, vc1, vc2, dsts, maxhistdst, logscale);
00290 projectRGBdensities(R, G, B, w*h, h13r, h13g, h13b, wout, hout,
00291 M, vc1, vc3, dsts, maxhistdst, logscale);
00292 } else {
00293 unsigned char *I=NULL;
00294 if (!Idsty) {
00295
00296 I=new unsigned char[w*h];
00297 for (int n=0; n < w*h; n++) {
00298 if (logscale) {
00299 I[n]=(int) (255.0*log(dsts[R[n]][G[n]][B[n]])/log(maxdst));
00300 } else {
00301 I[n]=(int) (255.0*dsts[R[n]][G[n]][B[n]]/maxdst);
00302 }
00303 }
00304 } else I=Idsty;
00305
00306 projectRGBcube(R, G, B, w*h, I, h12r, h12g, h12b, wout, hout,
00307 M, vc1, vc2);
00308 projectRGBcube(R, G, B, w*h, I, h13r, h13g, h13b, wout, hout,
00309 M, vc1, vc3);
00310 projectRGBcube(R, G, B, w*h, I, h23r, h23g, h23b, wout, hout,
00311 M, vc2, vc3);
00312
00313 if (!Idsty) delete[] I;
00314 }
00315
00316
00317 delete_RGBdensities(dsts);
00318
00319 }
00320
00321
00322
00355 void PCAviews_density(unsigned char *imR, unsigned char *imG,
00356 unsigned char *imB,
00357 int w, int h,
00358 unsigned char *refR, unsigned char *refG,
00359 unsigned char *refB,
00360 unsigned char *im12R, unsigned char *im12G,
00361 unsigned char *im12B,
00362 unsigned char *im13R, unsigned char *im13G,
00363 unsigned char *im13B,
00364 unsigned char *im23R, unsigned char *im23G,
00365 unsigned char *im23B,
00366 int wout, int hout, int rdst, unsigned char cumdsty,
00367 unsigned char *Idsty)
00368 {
00369 unsigned char logscale=1;
00370
00371 float hdst;
00372
00373 hdst=(float) rdst/(float) sqrt(2.0);
00374
00375 get_densityRGB_PCA(imR, imG, imB, refR, refG, refB, w, h,
00376 im12R, im12G, im12B, im13R, im13G, im13B,
00377 im23R, im23G, im23B,
00378 wout, hout, rdst, hdst, cumdsty, Idsty, logscale);
00379 }
00380
00381
00409 void PCAviews_densityB(unsigned char *imR, unsigned char *imG,
00410 unsigned char *imB,
00411 int w, int h,
00412 unsigned char *refR, unsigned char *refG,
00413 unsigned char *refB,
00414 unsigned char *im123R, unsigned char *im123G,
00415 unsigned char *im123B,
00416 int wout, int hout, int rdst, unsigned char cumdsty,
00417 unsigned char *Idsty)
00418 {
00419
00420 unsigned char *im12R=new unsigned char[wout*hout];
00421 unsigned char *im12G=new unsigned char[wout*hout];
00422 unsigned char *im12B=new unsigned char[wout*hout];
00423 unsigned char *im13R=new unsigned char[wout*hout];
00424 unsigned char *im13G=new unsigned char[wout*hout];
00425 unsigned char *im13B=new unsigned char[wout*hout];
00426 unsigned char *im23R=new unsigned char[wout*hout];
00427 unsigned char *im23G=new unsigned char[wout*hout];
00428 unsigned char *im23B=new unsigned char[wout*hout];
00429
00430 PCAviews_density(imR, imG, imB, w, h, refR, refG, refB,
00431 im12R, im12G, im12B, im13R, im13G, im13B,
00432 im23R, im23G, im23B,
00433 wout, hout, rdst, cumdsty, Idsty);
00434
00435
00436 for (int j=0; j < hout; j++) {
00437 memcpy(im123R+3*j*wout, im12R+j*wout, wout);
00438 memcpy(im123R+3*j*wout+wout, im13R+j*wout, wout);
00439 memcpy(im123R+3*j*wout+2*wout, im23R+j*wout, wout);
00440 memcpy(im123G+3*j*wout, im12G+j*wout, wout);
00441 memcpy(im123G+3*j*wout+wout, im13G+j*wout, wout);
00442 memcpy(im123G+3*j*wout+2*wout, im23G+j*wout, wout);
00443 memcpy(im123B+3*j*wout, im12B+j*wout, wout);
00444 memcpy(im123B+3*j*wout+wout, im13B+j*wout, wout);
00445 memcpy(im123B+3*j*wout+2*wout, im23B+j*wout, wout);
00446 }
00447
00448 delete[] im12R;
00449 delete[] im12G;
00450 delete[] im12B;
00451 delete[] im13R;
00452 delete[] im13G;
00453 delete[] im13B;
00454 delete[] im23R;
00455 delete[] im23G;
00456 delete[] im23B;
00457 }
00458
00459
00460
00461
00487 int filtercolor(unsigned char *inR, unsigned char *inG, unsigned char *inB,
00488 unsigned char *outR, unsigned char *outG, unsigned char *outB,
00489 int w, int h, int type, float eitmax, int r, int rsim)
00490 {
00491 int kneigh;
00492 kneigh=10;
00493 unsigned char ignore000=1;
00494
00495 struct paramColorFilter *param;
00496 param=new_colorfiltering_parameters(r, rsim, kneigh, eitmax,
00497 (unsigned char) type, ignore000);
00498
00499 int it=colorfiltering(inR, inG, inB, outR, outG, outB, w, h, param);
00500
00501 delete_colorfiltering_parameters(param);
00502 return it;
00503 }
00504
00505
00517 void mergeimages(unsigned char *in1R, unsigned char *in1G, unsigned char *in1B,
00518 unsigned char *in2R, unsigned char *in2G, unsigned char *in2B,
00519 unsigned char *outR, unsigned char *outG, unsigned char *outB,
00520 int w, int h)
00521 {
00522 memcpy(outR, in1R, w*h);
00523 memcpy(outG, in1G, w*h);
00524 memcpy(outB, in1B, w*h);
00525 for (int n=0; n < w*h; n++) {
00526 if ((in1R[n] == 0) && (in1G[n] == 0) && (in1B[n] == 0)) {
00527 outR[n]=in2R[n];
00528 outG[n]=in2G[n];
00529 outB[n]=in2B[n];
00530 }
00531 }
00532 }
00533
00568 void densityImage(unsigned char *R, unsigned char *G, unsigned char *B,
00569 int w, int h, unsigned char *I, int rdst,
00570 unsigned char logscale)
00571 {
00572 float maxdst;
00573 float ***dsts;
00574
00575 float hdst=(float) rdst/(float) sqrt(2.0);
00576 dsts=RGBdensities(R, G, B, w, h, rdst, hdst, maxdst);
00577 for (int n=0; n < w*h; n++) {
00578 if (logscale) {
00579 I[n]=(int) (255.0*log(dsts[R[n]][G[n]][B[n]])/log(maxdst));
00580 } else {
00581 I[n]=(int) (255.0*dsts[R[n]][G[n]][B[n]]/maxdst);
00582 }
00583 }
00584 delete_RGBdensities(dsts);
00585 }
00586
00587
00588
00589
00604 int RGBviews_sequence(unsigned char *imR, unsigned char *imG,
00605 unsigned char *imB,
00606 int win, int hin,
00607 unsigned char ***viewsR, unsigned char ***viewsG,
00608 unsigned char ***viewsB,
00609 int wview, int hview,
00610 unsigned char displayDensity)
00611 {
00612 unsigned char **viewR, **viewG, **viewB;
00613 int nviews=0;
00614 int idview;
00615
00616
00617
00618
00619
00620
00621
00622
00623
00624
00625
00626
00627
00628
00629
00630
00631
00632
00633
00634 float R;
00635 float A[3], O[3], B[3], C[3], D[3], E[3], F[3], G[3], H[3];
00636 A[0]=255.0f; A[1]=255.0f; A[2]=255.0f;
00637 O[0]=127.5f; O[1]=127.5f; O[2]=127.5f;
00638 R=(float) sqrt((A[0]-O[0])*(A[0]-O[0])+(A[1]-O[1])*(A[1]-O[1])+(A[2]-O[2])*(A[2]-O[2]));
00639 B[0]=O[0]; B[1]=O[1]; B[2]=O[2]+R;
00640 C[0]=0.0f; C[1]=255.0f; C[2]=255.0f;
00641 D[0]=0.0f; D[1]=255.0f; D[2]=0.0f;
00642 E[0]=O[0]; E[1]=O[1]; E[2]=O[2]-R;
00643 F[0]=0.0f; F[1]=0.0f; F[2]=0.0f;
00644 G[0]=0.0f; G[1]=0.0f; G[2]=255.0f;
00645 H[0]=255.0f; H[1]=0.0f; H[2]=255.0f;
00646
00647
00648 int nsamples1;
00649
00650 int nsamples2;
00651
00652 nsamples1=6;
00653 nsamples2=7;
00654
00655
00656 nviews=(nsamples1-1)*4+(nsamples2-1)+1;
00657 printf("%i views\n", nviews);
00658
00659
00660 viewR=new unsigned char*[nviews];
00661 viewG=new unsigned char*[nviews];
00662 viewB=new unsigned char*[nviews];
00663 for (int n=0; n < nviews; n++) {
00664 viewR[n]=new unsigned char[wview*hview];
00665 viewG[n]=new unsigned char[wview*hview];
00666 viewB[n]=new unsigned char[wview*hview];
00667 }
00668
00669
00670 unsigned char *I=NULL;
00671 if (displayDensity) {
00672 float maxdst;
00673 float ***dsts;
00674 int rdst=5;
00675
00676 float hdst=(float) rdst/(float) sqrt(2.0);
00677 dsts=RGBdensities(imR, imG, imB, win, hin, rdst, hdst, maxdst);
00678 I=new unsigned char[win*hin];
00679 for (int n=0; n < win*hin; n++) {
00680
00681 I[n]=(int) (255.0*log(dsts[imR[n]][imG[n]][imB[n]])/log(maxdst));
00682 }
00683 delete_RGBdensities(dsts);
00684 }
00685
00686
00687 idview=0;
00688 float **P;
00689 float w[3];
00690 float u[3];
00691 float v[3];
00692
00693
00694
00695
00696
00697 P=sample_arclength(A, B, O, nsamples1);
00698
00699 u[0]=C[0]-H[0];
00700 u[1]=C[1]-H[1];
00701 u[2]=C[2]-H[2];
00702 normalize_vector(u);
00703 for (int n=0; n < nsamples1-1; n++) {
00704 w[0]=O[0]-P[n][0];
00705 w[1]=O[1]-P[n][1];
00706 w[2]=O[2]-P[n][2];
00707 normalize_vector(w);
00708 vectorial_product(w, u, v);
00709 normalize_vector(v);
00710 projectRGBcubeB(imR, imG, imB, win*hin, I,
00711 viewR[idview], viewG[idview], viewB[idview],
00712 wview, hview, P[n], u, v);
00713 idview++;
00714 }
00715
00716 delete_list3D(P, nsamples1);
00717
00718
00719 P=sample_arclength(B, C, O, nsamples1);
00720
00721 v[0]=A[0]-G[0];
00722 v[1]=A[1]-G[1];
00723 v[2]=A[2]-G[2];
00724 normalize_vector(v);
00725 for (int n=0; n < nsamples1-1; n++) {
00726 w[0]=O[0]-P[n][0];
00727 w[1]=O[1]-P[n][1];
00728 w[2]=O[2]-P[n][2];
00729 normalize_vector(w);
00730 vectorial_product(v, w, u);
00731 normalize_vector(u);
00732 projectRGBcubeB(imR, imG, imB, win*hin, I,
00733 viewR[idview], viewG[idview], viewB[idview],
00734 wview, hview, P[n], u, v);
00735 idview++;
00736 }
00737
00738 delete_list3D(P, nsamples1);
00739
00740
00741 P=sample_arclength(C, D, O, nsamples2);
00742
00743
00744 for (int n=0; n < nsamples2-1; n++) {
00745 w[0]=O[0]-P[n][0];
00746 w[1]=O[1]-P[n][1];
00747 w[2]=O[2]-P[n][2];
00748 normalize_vector(w);
00749 vectorial_product(v, w, u);
00750 normalize_vector(u);
00751 projectRGBcubeB(imR, imG, imB, win*hin, I,
00752 viewR[idview], viewG[idview], viewB[idview],
00753 wview, hview, P[n], u, v);
00754 idview++;
00755 }
00756
00757 delete_list3D(P, nsamples2);
00758
00759
00760 P=sample_arclength(D, E, O, nsamples1);
00761
00762
00763 for (int n=0; n < nsamples1-1; n++) {
00764 w[0]=O[0]-P[n][0];
00765 w[1]=O[1]-P[n][1];
00766 w[2]=O[2]-P[n][2];
00767 normalize_vector(w);
00768 vectorial_product(v, w, u);
00769 normalize_vector(u);
00770 projectRGBcubeB(imR, imG, imB, win*hin, I,
00771 viewR[idview], viewG[idview], viewB[idview],
00772 wview, hview, P[n], u, v);
00773 idview++;
00774 }
00775
00776 delete_list3D(P, nsamples1);
00777
00778
00779 P=sample_arclength(E, F, O, nsamples1);
00780
00781 u[0]=H[0]-C[0];
00782 u[1]=H[1]-C[1];
00783 u[2]=H[2]-C[2];
00784 normalize_vector(u);
00785 for (int n=0; n < nsamples1; n++) {
00786 w[0]=O[0]-P[n][0];
00787 w[1]=O[1]-P[n][1];
00788 w[2]=O[2]-P[n][2];
00789 normalize_vector(w);
00790 vectorial_product(w, u, v);
00791 normalize_vector(v);
00792 projectRGBcubeB(imR, imG, imB, win*hin, I,
00793 viewR[idview], viewG[idview], viewB[idview],
00794 wview, hview, P[n], u, v);
00795 idview++;
00796 }
00797
00798 delete_list3D(P, nsamples1);
00799
00800
00801 *viewsR=viewR;
00802 *viewsG=viewG;
00803 *viewsB=viewB;
00804 if (I) delete[] I;
00805
00806 return nviews;
00807 }
00808
00809
00819 int RGBviews_sequence_params(const char *paramsfile)
00820 {
00821 int nviews=0;
00822 int idview;
00823
00824
00825
00826
00827
00828
00829
00830
00831
00832
00833
00834
00835
00836
00837
00838
00839
00840
00841
00842 float R;
00843 float A[3], O[3], B[3], C[3], D[3], E[3], F[3], G[3], H[3];
00844 A[0]=255.0f; A[1]=255.0f; A[2]=255.0f;
00845 O[0]=127.5f; O[1]=127.5f; O[2]=127.5f;
00846 R=(float) sqrt((A[0]-O[0])*(A[0]-O[0])+(A[1]-O[1])*(A[1]-O[1])+(A[2]-O[2])*(A[2]-O[2]));
00847 B[0]=O[0]; B[1]=O[1]; B[2]=O[2]+R;
00848 C[0]=0.0f; C[1]=255.0f; C[2]=255.0f;
00849 D[0]=0.0f; D[1]=255.0f; D[2]=0.0f;
00850 E[0]=O[0]; E[1]=O[1]; E[2]=O[2]-R;
00851 F[0]=0.0f; F[1]=0.0f; F[2]=0.0f;
00852 G[0]=0.0f; G[1]=0.0f; G[2]=255.0f;
00853 H[0]=255.0f; H[1]=0.0f; H[2]=255.0f;
00854
00855
00856 int nsamples1;
00857
00858 int nsamples2;
00859
00860
00861 nsamples1=6;
00862 nsamples2=7;
00863
00864
00865
00866 nviews=(nsamples1-1)*4+(nsamples2-1)+1;
00867 printf("%i views\n", nviews);
00868
00869
00870 FILE *params=fopen(paramsfile, "w");
00871 fprintf(params, "%i\n", nviews);
00872
00873
00874 idview=0;
00875 float **P;
00876 float w[3];
00877 float u[3];
00878 float v[3];
00879
00880
00881
00882
00883
00884 P=sample_arclength(A, B, O, nsamples1);
00885
00886 u[0]=C[0]-H[0];
00887 u[1]=C[1]-H[1];
00888 u[2]=C[2]-H[2];
00889 normalize_vector(u);
00890 for (int n=0; n < nsamples1-1; n++) {
00891 w[0]=O[0]-P[n][0];
00892 w[1]=O[1]-P[n][1];
00893 w[2]=O[2]-P[n][2];
00894 normalize_vector(w);
00895 vectorial_product(w, u, v);
00896 normalize_vector(v);
00897
00898 fprintf(params, "%f %f %f %f %f %f %f %f %f\n",
00899 P[n][0], P[n][1], P[n][2], u[0], u[1], u[2], v[0], v[1], v[2]);
00900 idview++;
00901 }
00902
00903 delete_list3D(P, nsamples1);
00904
00905
00906 P=sample_arclength(B, C, O, nsamples1);
00907
00908 v[0]=A[0]-G[0];
00909 v[1]=A[1]-G[1];
00910 v[2]=A[2]-G[2];
00911 normalize_vector(v);
00912 for (int n=0; n < nsamples1-1; n++) {
00913 w[0]=O[0]-P[n][0];
00914 w[1]=O[1]-P[n][1];
00915 w[2]=O[2]-P[n][2];
00916 normalize_vector(w);
00917 vectorial_product(v, w, u);
00918 normalize_vector(u);
00919
00920 fprintf(params, "%f %f %f %f %f %f %f %f %f\n",
00921 P[n][0], P[n][1], P[n][2], u[0], u[1], u[2], v[0], v[1], v[2]);
00922 idview++;
00923 }
00924
00925 delete_list3D(P, nsamples1);
00926
00927
00928 P=sample_arclength(C, D, O, nsamples2);
00929
00930
00931 for (int n=0; n < nsamples2-1; n++) {
00932 w[0]=O[0]-P[n][0];
00933 w[1]=O[1]-P[n][1];
00934 w[2]=O[2]-P[n][2];
00935 normalize_vector(w);
00936 vectorial_product(v, w, u);
00937 normalize_vector(u);
00938
00939 fprintf(params, "%f %f %f %f %f %f %f %f %f\n",
00940 P[n][0], P[n][1], P[n][2], u[0], u[1], u[2], v[0], v[1], v[2]);
00941 idview++;
00942 }
00943
00944 delete_list3D(P, nsamples2);
00945
00946
00947 P=sample_arclength(D, E, O, nsamples1);
00948
00949
00950 for (int n=0; n < nsamples1-1; n++) {
00951 w[0]=O[0]-P[n][0];
00952 w[1]=O[1]-P[n][1];
00953 w[2]=O[2]-P[n][2];
00954 normalize_vector(w);
00955 vectorial_product(v, w, u);
00956 normalize_vector(u);
00957
00958 fprintf(params, "%f %f %f %f %f %f %f %f %f\n",
00959 P[n][0], P[n][1], P[n][2], u[0], u[1], u[2], v[0], v[1], v[2]);
00960 idview++;
00961 }
00962
00963 delete_list3D(P, nsamples1);
00964
00965
00966 P=sample_arclength(E, F, O, nsamples1);
00967
00968 u[0]=H[0]-C[0];
00969 u[1]=H[1]-C[1];
00970 u[2]=H[2]-C[2];
00971 normalize_vector(u);
00972 for (int n=0; n < nsamples1; n++) {
00973 w[0]=O[0]-P[n][0];
00974 w[1]=O[1]-P[n][1];
00975 w[2]=O[2]-P[n][2];
00976 normalize_vector(w);
00977 vectorial_product(w, u, v);
00978 normalize_vector(v);
00979
00980 fprintf(params, "%f %f %f %f %f %f %f %f %f\n",
00981 P[n][0], P[n][1], P[n][2], u[0], u[1], u[2], v[0], v[1], v[2]);
00982 idview++;
00983 }
00984
00985 delete_list3D(P, nsamples1);
00986
00987 fclose(params);
00988 return nviews;
00989 }
00990
00998 void delete_views_sequence(unsigned char **viewR, unsigned char **viewG,
00999 unsigned char **viewB, int nviews)
01000 {
01001 for (int n=0; n < nviews; n++) {
01002 delete[] viewR[n];
01003 delete[] viewG[n];
01004 delete[] viewB[n];
01005 }
01006 delete[] viewR;
01007 delete[] viewG;
01008 delete[] viewB;
01009 }
01010
01011
01025 void compute_RGB_PCA(unsigned char *R, unsigned char *G, unsigned char *B,
01026 int npoints,
01027 float &Rcm, float &Gcm, float &Bcm,
01028 float &vp1, float &vp2, float &vp3,
01029 float vc1[3], float vc2[3], float vc3[3])
01030 {
01031 int n;
01032 double mean[3], C[3][3], VecPs[3][3], ValPs[3];
01033
01034
01035 for (int i=0; i < 3; i++) mean[i]=0;
01036 for (int i=0; i < 3; i++)
01037 for (int j=0; j < 3; j++) C[i][j]=0;
01038 for (n=0; n < npoints; n++) {
01039 mean[0]+=(double) R[n];
01040 mean[1]+=(double) G[n];
01041 mean[2]+=(double) B[n];
01042 C[0][0]+=((double) R[n])*((double) R[n]);
01043 C[0][1]+=((double) R[n])*((double) G[n]);
01044 C[0][2]+=((double) R[n])*((double) B[n]);
01045 C[1][1]+=((double) G[n])*((double) G[n]);
01046 C[1][2]+=((double) G[n])*((double) B[n]);
01047 C[2][2]+=((double) B[n])*((double) B[n]);
01048 }
01049 for (int i=0; i < 3; i++) mean[i]/=((double) npoints);
01050 for (int i=0; i < 3; i++)
01051 for (int j=i; j < 3; j++)
01052 C[i][j]=(C[i][j]/(double) npoints)-mean[i]*mean[j];
01053 for (int i=0; i < 3; i++)
01054 for (int j=0; j < i; j++) C[i][j]=C[j][i];
01055
01056
01057
01058 compute_pca3(C, ValPs, VecPs);
01059
01060 Rcm=(float) mean[0];
01061 Gcm=(float) mean[1];
01062 Bcm=(float) mean[2];
01063
01064
01065 vc1[0]=(float) VecPs[0][0];
01066 vc1[1]=(float) VecPs[1][0];
01067 vc1[2]=(float) VecPs[2][0];
01068 vc2[0]=(float) VecPs[0][1];
01069 vc2[1]=(float) VecPs[1][1];
01070 vc2[2]=(float) VecPs[2][1];
01071 vc3[0]=(float) VecPs[0][2];
01072 vc3[1]=(float) VecPs[1][2];
01073 vc3[2]=(float) VecPs[2][2];
01074 normalize_vector(vc1);
01075 normalize_vector(vc2);
01076 normalize_vector(vc3);
01077 vp1=(float) ValPs[0];
01078 vp2=(float) ValPs[1];
01079 vp3=(float) ValPs[2];
01080 }
01081
01090 void cube_edges(float **Rout, float **Gout, float **Bout, int &npoints)
01091 {
01092 float s, disc=0.2f;
01093 int ndisc=(int) (1/disc);
01094 int npointsmax=12*256*(ndisc+1);
01095
01096
01097 float *R=new float[npointsmax];
01098 float *G=new float[npointsmax];
01099 float *B=new float[npointsmax];
01100
01101 npoints=0;
01102 for (s=0; s <= 255; s+=disc) {
01103 R[npoints]=0; G[npoints]=0; B[npoints]=s; npoints++; }
01104 for (s=0; s <= 255; s+=disc) {
01105 R[npoints]=255; G[npoints]=0; B[npoints]=s; npoints++; }
01106 for (s=0; s <= 255; s+=disc) {
01107 R[npoints]=0; G[npoints]=255; B[npoints]=s; npoints++; }
01108 for (s=0; s <= 255; s+=disc) {
01109 R[npoints]=255; G[npoints]=255; B[npoints]=s; npoints++; }
01110 for (s=0; s <= 255; s+=disc) {
01111 R[npoints]=0; G[npoints]=s; B[npoints]=0; npoints++; }
01112 for (s=0; s <= 255; s+=disc) {
01113 R[npoints]=0; G[npoints]=s; B[npoints]=255; npoints++; }
01114 for (s=0; s <= 255; s+=disc) {
01115 R[npoints]=255; G[npoints]=s; B[npoints]=0; npoints++; }
01116 for (s=0; s <= 255; s+=disc) {
01117 R[npoints]=255; G[npoints]=s; B[npoints]=255; npoints++; }
01118 for (s=0; s <= 255; s+=disc) {
01119 R[npoints]=s; G[npoints]=0; B[npoints]=0; npoints++; }
01120 for (s=0; s <= 255; s+=disc) {
01121 R[npoints]=s; G[npoints]=255; B[npoints]=0; npoints++; }
01122 for (s=0; s <= 255; s+=disc) {
01123 R[npoints]=s; G[npoints]=0; B[npoints]=255; npoints++; }
01124 for (s=0; s <= 255; s+=disc) {
01125 R[npoints]=s; G[npoints]=255; B[npoints]=255; npoints++; }
01126
01127 *Rout=R; *Gout=G; *Bout=B;
01128 }
01129
01130
01131
01132
01133
01147 void project_point(float P[3], float PP[3], float M[3], float u[3], float &d,
01148 unsigned char option)
01149 {
01150 if (option == 1) {
01151 float dp[3], lambda;
01152
01153 dp[0]=P[0]-M[0];
01154 dp[1]=P[1]-M[1];
01155 dp[2]=P[2]-M[2];
01156 lambda=scalar_product(dp, u);
01157 PP[0]=M[0]+lambda*u[0];
01158 PP[1]=M[1]+lambda*u[1];
01159 PP[2]=M[2]+lambda*u[2];
01160
01161 dp[0]=PP[0]-P[0];
01162 dp[1]=PP[1]-P[1];
01163 dp[2]=PP[2]-P[2];
01164 d=vector_norm(dp);
01165 } else {
01166
01167 float lambda;
01168 lambda=scalar_product(u, M)-scalar_product(u, P);
01169 PP[0]=P[0]+lambda*u[0];
01170 PP[1]=P[1]+lambda*u[1];
01171 PP[2]=P[2]+lambda*u[2];
01172 d=lambda;
01173 }
01174 }
01175
01176
01192 void projectRGBcube(unsigned char *R, unsigned char *G, unsigned char *B,
01193 int npoints, unsigned char *I,
01194 unsigned char *Rout, unsigned char *Gout,
01195 unsigned char *Bout,
01196 int wout, int hout, float M[3], float u[3], float v[3])
01197 {
01198 float **cplane;
01199 float w[3], P[3], PP[3], PPP[3], d;
01200 float xmin, ymin, xmax, ymax, zmin, zmax, wx, wy, wmax, *dplane;
01201 int x, y, sout, npointsB;
01202 float *RB, *GB, *BB;
01203
01204
01205 RB=GB=BB=NULL;
01206 npointsB=0;
01207 cube_edges(&RB, &GB, &BB, npointsB);
01208
01209
01210 cplane=new float*[npoints+npointsB];
01211 for (int n=0; n < npoints+npointsB; n++) cplane[n]=new float[3];
01212
01213
01214 vectorial_product(u, v, w);
01215 normalize_vector(w);
01216
01217
01218 for (int n=0; n < npoints+npointsB; n++) {
01219 if (n < npoints) {
01220 P[0]=(float) R[n]; P[1]=(float) G[n]; P[2]=(float) B[n];
01221 } else {
01222 P[0]=(float) RB[n-npoints];
01223 P[1]=(float) GB[n-npoints];
01224 P[2]=(float) BB[n-npoints];
01225 }
01226
01227 project_point(P, PP, M, w, d, 2);
01228 PPP[0]=PP[0]-M[0]; PPP[1]=PP[1]-M[1]; PPP[2]=PP[2]-M[2];
01229 cplane[n][0]=scalar_product(PPP, u);
01230 cplane[n][1]=scalar_product(PPP, v);
01231 cplane[n][2]=d;
01232 }
01233
01234
01235
01236 xmin=xmax=cplane[0][0];
01237 ymin=ymax=cplane[0][1];
01238 zmin=zmax=cplane[0][2];
01239 for (int n=1; n < npoints+npointsB; n++) {
01240 if (cplane[n][0] < xmin) xmin=cplane[n][0];
01241 if (cplane[n][0] > xmax) xmax=cplane[n][0];
01242 if (cplane[n][1] < ymin) ymin=cplane[n][1];
01243 if (cplane[n][1] > ymax) ymax=cplane[n][1];
01244 if (cplane[n][2] < zmin) zmin=cplane[n][2];
01245 if (cplane[n][2] > zmax) zmax=cplane[n][2];
01246 }
01247 wx=xmax-xmin;
01248 wy=ymax-ymin;
01249 wmax=(wx > wy)?(wx):(wy);
01250
01251
01252 sout=(wx > wy)?(wout):(hout);
01253
01254
01255
01256
01257 dplane=new float[wout*hout];
01258 for (int n=0; n < wout*hout; n++) dplane[n]=-1;
01259 memset(Rout, 255, wout*hout);
01260 memset(Gout, 255, wout*hout);
01261 memset(Bout, 255, wout*hout);
01262 for (int n=0; n < npoints+npointsB; n++) {
01263
01264 x=(int) ((cplane[n][0]-xmin)*((float) sout-1)/wmax + 0.5f);
01265 y=(int) ((cplane[n][1]-ymin)*((float) sout-1)/wmax + 0.5f);
01266 if ((x < wout) && (y < hout)) {
01267
01268
01269
01270 if (cplane[n][2]-zmin > dplane[x+y*wout]) {
01271 if (n < npoints) {
01272 if (!I) {
01273 Rout[x+y*wout]=R[n];
01274 Gout[x+y*wout]=G[n];
01275 Bout[x+y*wout]=B[n];
01276 } else {
01277 Rout[x+y*wout]=I[n];
01278 Gout[x+y*wout]=I[n];
01279 Bout[x+y*wout]=I[n];
01280 }
01281
01282 if (x+1 < wout) {
01283 if (!I) {
01284 Rout[x+1+y*wout]=R[n];
01285 Gout[x+1+y*wout]=G[n];
01286 Bout[x+1+y*wout]=B[n];
01287 } else {
01288 Rout[x+1+y*wout]=I[n];
01289 Gout[x+1+y*wout]=I[n];
01290 Bout[x+1+y*wout]=I[n];
01291 }
01292 }
01293 if (y+1 < hout) {
01294 if (!I) {
01295 Rout[x+(y+1)*wout]=R[n];
01296 Gout[x+(y+1)*wout]=G[n];
01297 Bout[x+(y+1)*wout]=B[n];
01298 } else {
01299 Rout[x+(y+1)*wout]=I[n];
01300 Gout[x+(y+1)*wout]=I[n];
01301 Bout[x+(y+1)*wout]=I[n];
01302 }
01303 }
01304 if ((x+1 < wout) && (y+1 < hout)) {
01305 if (!I) {
01306 Rout[x+1+(y+1)*wout]=R[n];
01307 Gout[x+1+(y+1)*wout]=G[n];
01308 Bout[x+1+(y+1)*wout]=B[n];
01309 } else {
01310 Rout[x+1+(y+1)*wout]=I[n];
01311 Gout[x+1+(y+1)*wout]=I[n];
01312 Bout[x+1+(y+1)*wout]=I[n];
01313 }
01314 }
01315
01316 } else {
01317 Rout[x+y*wout]=(int) (RB[n-npoints]+0.5f);
01318 Gout[x+y*wout]=(int) (GB[n-npoints]+0.5f);
01319 Bout[x+y*wout]=(int) (BB[n-npoints]+0.5f);
01320 }
01321
01322
01323 dplane[x+y*wout]=cplane[n][2]-zmin;
01324 }
01325 }
01326 }
01327
01328 for (int n=0; n < npoints+npointsB; n++) delete[] cplane[n];
01329 delete[] cplane;
01330 delete[] dplane;
01331
01332 if (RB) delete[] RB;
01333 if (GB) delete[] GB;
01334 if (BB) delete[] BB;
01335 }
01336
01337
01355 void projectRGBcubeB(unsigned char *R, unsigned char *G, unsigned char *B,
01356 int npoints, unsigned char *I,
01357 unsigned char *Rout, unsigned char *Gout,
01358 unsigned char *Bout,
01359 int wout, int hout,
01360 float M[3], float u[3], float v[3])
01361 {
01362 float **cplane;
01363 float w[3], P[3], PP[3], PPP[3], d;
01364 float zmin, zmax, *dplane;
01365 int x, y, smax, npointsB;
01366 float *RB, *GB, *BB;
01367
01368
01369 RB=GB=BB=NULL;
01370 npointsB=0;
01371 cube_edges(&RB, &GB, &BB, npointsB);
01372
01373
01374 cplane=new float*[npoints+npointsB];
01375 for (int n=0; n < npoints+npointsB; n++) cplane[n]=new float[3];
01376
01377
01378 vectorial_product(u, v, w);
01379 normalize_vector(w);
01380
01381
01382 for (int n=0; n < npoints+npointsB; n++) {
01383 if (n < npoints) {
01384 P[0]=(float) R[n]; P[1]=(float) G[n]; P[2]=(float) B[n];
01385 } else {
01386 P[0]=(float) RB[n-npoints];
01387 P[1]=(float) GB[n-npoints];
01388 P[2]=(float) BB[n-npoints];
01389 }
01390
01391 project_point(P, PP, M, w, d, 2);
01392 PPP[0]=PP[0]-M[0]; PPP[1]=PP[1]-M[1]; PPP[2]=PP[2]-M[2];
01393 cplane[n][0]=scalar_product(PPP, u);
01394 cplane[n][1]=scalar_product(PPP, v);
01395 cplane[n][2]=d;
01396 }
01397
01398
01399
01400 zmin=zmax=cplane[0][2];
01401 for (int n=1; n < npoints+npointsB; n++) {
01402 if (cplane[n][2] < zmin) zmin=cplane[n][2];
01403 if (cplane[n][2] > zmax) zmax=cplane[n][2];
01404 }
01405
01406
01407 smax=255.0*(float)sqrt((float) 3);
01408
01409
01410
01411
01412 dplane=new float[wout*hout];
01413 for (int n=0; n < wout*hout; n++) dplane[n]=-1;
01414 memset(Rout, 255, wout*hout);
01415 memset(Gout, 255, wout*hout);
01416 memset(Bout, 255, wout*hout);
01417 for (int n=0; n < npoints+npointsB; n++) {
01418
01419 x=(int) ((cplane[n][0]*((float) wout-1)/smax)+wout/2);
01420 y=(int) ((cplane[n][1]*((float) hout-1)/smax)+hout/2);
01421 if ((x < wout) && (y < hout) && (x >= 0) && (y >= 0)) {
01422
01423
01424
01425 if (cplane[n][2]-zmin > dplane[x+y*wout]) {
01426 if (n < npoints) {
01427 if (!I) {
01428 Rout[x+y*wout]=R[n];
01429 Gout[x+y*wout]=G[n];
01430 Bout[x+y*wout]=B[n];
01431 } else {
01432 Rout[x+y*wout]=I[n];
01433 Gout[x+y*wout]=I[n];
01434 Bout[x+y*wout]=I[n];
01435 }
01436
01437 if (x+1 < wout) {
01438 if (!I) {
01439 Rout[x+1+y*wout]=R[n];
01440 Gout[x+1+y*wout]=G[n];
01441 Bout[x+1+y*wout]=B[n];
01442 } else {
01443 Rout[x+1+y*wout]=I[n];
01444 Gout[x+1+y*wout]=I[n];
01445 Bout[x+1+y*wout]=I[n];
01446 }
01447 }
01448 if (y+1 < hout) {
01449 if (!I) {
01450 Rout[x+(y+1)*wout]=R[n];
01451 Gout[x+(y+1)*wout]=G[n];
01452 Bout[x+(y+1)*wout]=B[n];
01453 } else {
01454 Rout[x+(y+1)*wout]=I[n];
01455 Gout[x+(y+1)*wout]=I[n];
01456 Bout[x+(y+1)*wout]=I[n];
01457 }
01458 }
01459 if ((x+1 < wout) && (y+1 < hout)) {
01460 if (!I) {
01461 Rout[x+1+(y+1)*wout]=R[n];
01462 Gout[x+1+(y+1)*wout]=G[n];
01463 Bout[x+1+(y+1)*wout]=B[n];
01464 } else {
01465 Rout[x+1+(y+1)*wout]=I[n];
01466 Gout[x+1+(y+1)*wout]=I[n];
01467 Bout[x+1+(y+1)*wout]=I[n];
01468 }
01469 }
01470
01471 } else {
01472 Rout[x+y*wout]=(int) (RB[n-npoints]+0.5f);
01473 Gout[x+y*wout]=(int) (GB[n-npoints]+0.5f);
01474 Bout[x+y*wout]=(int) (BB[n-npoints]+0.5f);
01475 }
01476
01477
01478 dplane[x+y*wout]=cplane[n][2]-zmin;
01479 }
01480 }
01481 }
01482
01483 for (int n=0; n < npoints+npointsB; n++) delete[] cplane[n];
01484 delete[] cplane;
01485 delete[] dplane;
01486
01487 if (RB) delete[] RB;
01488 if (GB) delete[] GB;
01489 if (BB) delete[] BB;
01490 }
01491
01492
01493
01494
01515 void projectRGBdensities(unsigned char *R, unsigned char *G, unsigned char *B,
01516 int npoints,
01517 unsigned char *Rout, unsigned char *Gout,
01518 unsigned char *Bout,
01519 int wout, int hout,
01520 float M[3], float u[3], float v[3],
01521 float ***dsty, float &maxdsty,
01522 unsigned char logscale)
01523 {
01524 float **cplane;
01525 float w[3], P[3], PP[3], d;
01526 float xmin, ymin, xmax, ymax, zmin, zmax, wx, wy, wmax, *dplane, hv, a;
01527 int x, y, sout, npointsB;
01528 float *RB, *GB, *BB;
01529 float *cumdsty;
01530
01531
01532 RB=GB=BB=NULL;
01533 npointsB=0;
01534 cube_edges(&RB, &GB, &BB, npointsB);
01535
01536
01537 cplane=new float*[npoints+npointsB];
01538 for (int n=0; n < npoints+npointsB; n++) cplane[n]=new float[3];
01539
01540
01541 vectorial_product(u, v, w);
01542 normalize_vector(w);
01543
01544
01545 for (int n=0; n < npoints+npointsB; n++) {
01546 if (n < npoints) {
01547 P[0]=(float) R[n]; P[1]=(float) G[n]; P[2]=(float) B[n];
01548 } else {
01549 P[0]=(float) RB[n-npoints];
01550 P[1]=(float) GB[n-npoints];
01551 P[2]=(float) BB[n-npoints];
01552 }
01553
01554 project_point(P, PP, M, w, d, 2);
01555 cplane[n][0]=scalar_product(PP, u);
01556 cplane[n][1]=scalar_product(PP, v);
01557 cplane[n][2]=d;
01558 }
01559
01560
01561
01562 xmin=xmax=cplane[0][0];
01563 ymin=ymax=cplane[0][1];
01564 zmin=zmax=cplane[0][2];
01565 for (int n=1; n < npoints+npointsB; n++) {
01566 if (cplane[n][0] < xmin) xmin=cplane[n][0];
01567 if (cplane[n][0] > xmax) xmax=cplane[n][0];
01568 if (cplane[n][1] < ymin) ymin=cplane[n][1];
01569 if (cplane[n][1] > ymax) ymax=cplane[n][1];
01570 if (cplane[n][2] < zmin) zmin=cplane[n][2];
01571 if (cplane[n][2] > zmax) zmax=cplane[n][2];
01572 }
01573 wx=xmax-xmin;
01574 wy=ymax-ymin;
01575 wmax=(wx > wy)?(wx):(wy);
01576
01577
01578 sout=(wx > wy)?(wout):(hout);
01579
01580
01581 cumdsty=new float[wout*hout];
01582 memset(cumdsty, 0, wout*hout*sizeof(float));
01583 for (int n=0; n < npoints; n++) {
01584
01585 x=(int) ((cplane[n][0]-xmin)*((float) sout-1)/wmax + 0.5f);
01586 y=(int) ((cplane[n][1]-ymin)*((float) sout-1)/wmax + 0.5f);
01587 if ((x < wout) && (y < hout)) {
01588
01589
01590 cumdsty[x+y*wout]+=dsty[R[n]][G[n]][B[n]];
01591
01592 if (x+1 < wout) cumdsty[x+1+y*wout]+=dsty[R[n]][G[n]][B[n]];
01593 if (y+1 < wout) cumdsty[x+(y+1)*wout]+=dsty[R[n]][G[n]][B[n]];
01594 if ((x+1 < wout) && (y+1 < hout))
01595 cumdsty[x+1+(y+1)*wout]+=dsty[R[n]][G[n]][B[n]];
01596 }
01597 }
01598 for (int n=0; n < wout*hout; n++) {
01599 if (cumdsty[n] > maxdsty) maxdsty=cumdsty[n];
01600 }
01601
01602
01603 dplane=new float[wout*hout];
01604 for (int n=0; n < wout*hout; n++) dplane[n]=-1;
01605 memset(Rout, 255, wout*hout);
01606 memset(Gout, 255, wout*hout);
01607 memset(Bout, 255, wout*hout);
01608
01609
01610 for (int n=0; n < wout*hout; n++) {
01611 if (cumdsty[n] > 0) {
01612 hv=(float) cumdsty[n];
01613 if (logscale) {
01614 hv=(float) log((double) hv);
01615 a=hv*255.0f/(float) log((double) maxdsty);
01616 } else a=hv*255.0f/maxdsty;
01617 Rout[n]=(unsigned char) (a+0.5f);
01618 Gout[n]=(unsigned char) (a+0.5f);
01619 Bout[n]=(unsigned char) (a+0.5f);
01620 }
01621 }
01622
01623
01624 for (int n=0; n < npoints; n++) {
01625
01626 x=(int) ((cplane[n][0]-xmin)*((float) sout-1)/wmax + 0.5f);
01627 y=(int) ((cplane[n][1]-ymin)*((float) sout-1)/wmax + 0.5f);
01628 if ((x < wout) && (y < hout)) {
01629
01630
01631 if (cplane[n][2]-zmin > dplane[x+y*wout])
01632 dplane[x+y*wout]=cplane[n][2]-zmin;
01633 }
01634 }
01635 for (int n=npoints; n < npoints+npointsB; n++) {
01636
01637 x=(int) ((cplane[n][0]-xmin)*((float) sout-1)/wmax + 0.5f);
01638 y=(int) ((cplane[n][1]-ymin)*((float) sout-1)/wmax + 0.5f);
01639 if ((x < wout) && (y < hout)) {
01640
01641
01642
01643 if (cplane[n][2]-zmin > dplane[x+y*wout]) {
01644 Rout[x+y*wout]=(int) (RB[n-npoints]+0.5f);
01645 Gout[x+y*wout]=(int) (GB[n-npoints]+0.5f);
01646 Bout[x+y*wout]=(int) (BB[n-npoints]+0.5f);
01647 dplane[x+y*wout]=cplane[n][2]-zmin;
01648 }
01649 }
01650 }
01651
01652 for (int n=0; n < npoints+npointsB; n++) delete[] cplane[n];
01653 delete[] cplane;
01654
01655 if (RB) delete[] RB;
01656 if (GB) delete[] GB;
01657 if (BB) delete[] BB;
01658 delete[] cumdsty;
01659 delete[] dplane;
01660 }
01661
01676 float diffRMSE(unsigned char *R1, unsigned char *G1, unsigned char *B1,
01677 unsigned char *R2, unsigned char *G2, unsigned char *B2,
01678 int w, int h, unsigned char use_voxel, float &dmean)
01679 {
01680
01681 unsigned char ***usedRGB=NULL;
01682 if (use_voxel) {
01683 usedRGB=new unsigned char**[256];
01684 for (int ir=0; ir < 256; ir++) {
01685 usedRGB[ir]=new unsigned char*[256];
01686 for (int ig=0; ig < 256; ig++) {
01687 usedRGB[ir][ig]=new unsigned char[256];
01688 memset(usedRGB[ir][ig], 0, 256);
01689 }
01690 }
01691 }
01692
01693 dmean=0;
01694 float mse=0, rmse;
01695 float dR, dG, dB;
01696 int npix=0;
01697 for (int n=0; n < w*h; n++) {
01698 if (!use_voxel || (use_voxel && (usedRGB[R1[n]][G1[n]][B1[n]] == 0))) {
01699
01700
01701 dR=(float) (R1[n]-R2[n]);
01702 dG=(float) (G1[n]-G2[n]);
01703 dB=(float) (B1[n]-B2[n]);
01704 mse+=(dR*dR+dG*dG+dB*dB);
01705 dmean+=sqrt(dR*dR+dG*dG+dB*dB);
01706 npix++;
01707 }
01708 if (use_voxel) usedRGB[R1[n]][G1[n]][B1[n]]=1;
01709 }
01710
01711 mse/=(float) npix;
01712 rmse=sqrt(mse);
01713 dmean/=(float) npix;
01714
01715
01716
01717 if (use_voxel) {
01718 for (int ir=0; ir < 256; ir++) {
01719 for (int ig=0; ig < 256; ig++) delete[] usedRGB[ir][ig];
01720 delete[] usedRGB[ir];
01721 }
01722 delete[] usedRGB;
01723 }
01724 return rmse;
01725 }
01726
01727
01739 void RGBcube2PLY(unsigned char *R, unsigned char *G, unsigned char *B,
01740 int npoints, unsigned char *I, const char *nameout)
01741 {
01742 float *RB, *GB, *BB;
01743 int npointsB;
01744 FILE *out;
01745
01746
01747 RB=GB=BB=NULL;
01748 npointsB=0;
01749 cube_edges(&RB, &GB, &BB, npointsB);
01750
01751 out=fopen(nameout, "w");
01752 fprintf(out, "ply\n");
01753 fprintf(out, "format ascii 1.0\n");
01754 fprintf(out, "comment source: http://www.ipol.im/pub/demo/blm_color_dimensional_filtering/\n");
01755 fprintf(out, "element vertex %i\n", npoints+npointsB);
01756 fprintf(out, "property float x\n");
01757 fprintf(out, "property float y\n");
01758 fprintf(out, "property float z\n");
01759 fprintf(out, "property uchar red\n");
01760 fprintf(out, "property uchar green\n");
01761 fprintf(out, "property uchar blue\n");
01762 fprintf(out, "end_header\n");
01763 float x, y, z;
01764 int ir, ig, ib;
01765 for (int n=0; n < npointsB; n++) {
01766
01767
01768 x=((float) RB[n]-127.5f)/255.0;
01769 y=((float) GB[n]-127.5f)/255.0;
01770 z=((float) BB[n]-127.5f)/255.0;
01771 ir=(int) RB[n];
01772 ig=(int) GB[n];
01773 ib=(int) BB[n];
01774 fprintf(out, "%f %f %f %i %i %i\n", x, y, z, ir, ig, ib);
01775 }
01776 for (int n=0; n < npoints; n++) {
01777
01778
01779 x=((float) R[n]-127.5f)/255.0;
01780 y=((float) G[n]-127.5f)/255.0;
01781 z=((float) B[n]-127.5f)/255.0;
01782 ir=(I)?((int) I[n]):((int) R[n]);
01783 ig=(I)?((int) I[n]):((int) G[n]);
01784 ib=(I)?((int) I[n]):((int) B[n]);
01785 fprintf(out, "%f %f %f %i %i %i\n", x, y, z, ir, ig, ib);
01786 }
01787
01788 fclose(out);
01789
01790 if (RB) delete[] RB;
01791 if (GB) delete[] GB;
01792 if (BB) delete[] BB;
01793 }
01794
01806 void RGBcube2VRML2(unsigned char *R, unsigned char *G, unsigned char *B,
01807 int npoints, unsigned char *I, const char *nameout)
01808 {
01809 float *RB, *GB, *BB;
01810 int npointsB;
01811 FILE *out;
01812
01813
01814 RB=GB=BB=NULL;
01815 npointsB=0;
01816 cube_edges(&RB, &GB, &BB, npointsB);
01817
01818 out=fopen(nameout, "w");
01819 fprintf(out, "#VRML V2.0 utf8\n");
01820 fprintf(out, "#Source: http://www.ipol.im/pub/demo/blm_color_dimensional_filtering/\n");
01821 fprintf(out, "#Author: J.L. Lisani (joseluis.lisani@uib.es)\n");
01822 fprintf(out, "#Color points (includig RGB cube edges)\n");
01823 fprintf(out, "Shape {\n");
01824 fprintf(out, " geometry PointSet {\n");
01825 fprintf(out, " coord Coordinate {\n");
01826 fprintf(out, " point [\n");
01827 float x, y, z;
01828 for (int n=0; n < npointsB; n++) {
01829
01830
01831 x=((float) RB[n]-127.5f)/255.0;
01832 y=((float) GB[n]-127.5f)/255.0;
01833 z=((float) BB[n]-127.5f)/255.0;
01834 fprintf(out, " %f %f %f,\n", x, y, z);
01835 }
01836 for (int n=0; n < npoints; n++) {
01837
01838
01839 x=((float) R[n]-127.5f)/255.0;
01840 y=((float) G[n]-127.5f)/255.0;
01841 z=((float) B[n]-127.5f)/255.0;
01842 fprintf(out, " %f %f %f,\n", x, y, z);
01843 }
01844 fprintf(out, " ]\n");
01845 fprintf(out, " }\n");
01846 fprintf(out, " color Color {\n");
01847 fprintf(out, " color [\n");
01848 float fr, fg, fb;
01849 for (int n=0; n < npointsB; n++) {
01850 fr=(float) RB[n]/255.0f;
01851 fg=(float) GB[n]/255.0f;
01852 fb=(float) BB[n]/255.0f;
01853 fprintf(out, " %f %f %f,\n", fr, fg, fb);
01854 }
01855 for (int n=0; n < npoints; n++) {
01856 fr=(I)?((float) I[n]/255.0f):((float) R[n]/255.0f);
01857 fg=(I)?((float) I[n]/255.0f):((float) G[n]/255.0f);
01858 fb=(I)?((float) I[n]/255.0f):((float) B[n]/255.0f);
01859 fprintf(out, " %f %f %f,\n", fr, fg, fb);
01860 }
01861 fprintf(out, " ]\n");
01862 fprintf(out, " }\n");
01863 fprintf(out, " }\n");
01864 fprintf(out, "}\n");
01865
01866 fclose(out);
01867
01868 if (RB) delete[] RB;
01869 if (GB) delete[] GB;
01870 if (BB) delete[] BB;
01871 }