Questo sito utilizza cookies, anche di terze parti, per mostrare pubblicità e servizi in linea con il tuo account. Leggi l'informativa sui cookies.
Username: Password: oppure
C/C++ - C++  Algoritmo segmentazione immagini
Forum - C/C++ - C++ Algoritmo segmentazione immagini

Avatar
peppepres (Normal User)
Newbie


Messaggi: 1
Iscritto: 20/01/2009

Segnala al moderatore
Postato alle 18:44
Martedì, 20/01/2009
Ho un algoritmo che non riesco a compilare.
Ho sabttuto un po e sto sbattendo ma non va mi da un errore relativo alle librerie , ho provato a seguire consigli gia presenti in altri topic niente. Posto il mio codice magari qualcuno puo aiutarmi!

Codice sorgente - presumibilmente C++

  1. #define _WINDOWS
  2. #include <fstream>
  3. #include <iostream>
  4. using namespace std;
  5.  
  6. // Union-Find Data Structure
  7. // See Tarjan, R. E. "Efficiency of a Good But Not Linear Set Union Algorithm," Journal of the ACM (JACM), 22(2), 215-225,  1975
  8.  
  9. class ggvUnionFind{
  10. public:
  11.         int  *rank;
  12.         int *parent;
  13.  
  14. ggvUnionFind(int n)
  15. {int k;
  16.  
  17. parent=new int[n]; rank=new int[n];
  18. for (k = 0; k < n; k++)
  19.       {parent[k]   = k;rank[k] = 0;     }
  20. }
  21.  
  22. ~ggvUnionFind()
  23. {delete [] rank; delete [] parent;}
  24.  
  25.  // Find procedures
  26. int ggvUnionFind::Find(int k)
  27. {
  28.         while (parent[k]!=k ) k=parent[k];
  29.     return k;}
  30.  
  31. // Assume x and y being roots
  32. int ggvUnionFind::UnionRoot(int x, int y)
  33. {  
  34.         if ( x == y ) return -1;
  35.  
  36.       if (rank[x] > rank[y])  
  37.       {parent[y]=x; return x;}
  38.       else                      
  39.       { parent[x]=y;if (rank[x]==rank[y]) rank[y]++;return y;}
  40. }
  41. };
  42.  
  43. // Portable pixelmap I/O
  44. //
  45.  
  46. // P6        
  47. // width height
  48. // max
  49. // rgb (in binary)
  50.  
  51. void SaveImagePPM(unsigned char * data, int w, int h, char * file)
  52. {
  53.         //ifstream IN(file, ios::in);
  54.  
  55.         //if (IN) {cerr<<"File "<<file<<" already exists. Cannot save PPM image."<<endl; IN.close();return;}
  56.  
  57.         ofstream OUT(file, ios::binary | ios::noreplace);
  58.         if (OUT){
  59.     OUT << "P6" << endl << w << ' ' << h << endl << 255 << endl;
  60.         OUT.write((char *)data, 3*w*h);
  61.         OUT.close();}
  62. }
  63.  
  64. // Load a PPM Image that does not contain comments.
  65. unsigned char * LoadImagePPM(char *ifile,  int &w, int &h)
  66. {
  67.          char dummy1=0, dummy2=0; int maxc;
  68.          char * img;
  69.         ifstream IN(ifile, ios::binary);
  70.  
  71.         IN.get(dummy1);IN.get(dummy2);
  72.         if ((dummy1!='P')&&(dummy2!='6')) {cerr<<"Not P6 PPM file"<<endl; return NULL;}
  73.  
  74.     IN >> w >> h;
  75.     IN >> maxc;
  76.     IN.get(dummy1);
  77.        
  78.     img=new unsigned char[3*w*h];
  79.         IN.read(img, 3*w*h);
  80.         IN.close();
  81.         return img;
  82. }
  83.  
  84.  
  85. #include <math.h>
  86.  
  87. #define max3(a,b,c) max(max((a),(b)),(c))
  88. #define max(A,B) ((A>B) ? (A):(B))
  89. #define min(A,B) ((A<B) ? (A):(B))
  90.  
  91.  
  92. // Region Growing Class
  93. class ggvRm
  94. {
  95. public:
  96. int w,h,n;
  97. unsigned char *raster;
  98.  
  99. double Q; // it is usually an integer representing the number of independent statistical variables
  100. double g; // number of levels in a color channel
  101. double logdelta;
  102.  
  103. // Auxilliary buffers for union-find operations
  104. int *N;
  105. float *Ravg, *Gavg, *Bavg;
  106. int *C; // the class number
  107.  
  108. // Output image
  109. int wo, ho;
  110. unsigned char *rastero;
  111.  
  112. // number of pixels that define a "small region" to be collapsed
  113. int smallregion;
  114. int borderthickness;
  115.  
  116. ggvUnionFind *UF;
  117.  
  118. inline bool MergePredicate(int reg1, int reg2);
  119. inline void ggvRm::MergeRegions(int C1, int C2);
  120.  
  121. ggvRm::ggvRm(int width, int height, unsigned char *ras);
  122. ggvRm::~ggvRm();
  123.  
  124. int Segment();
  125.  
  126. void ggvRm::OutputSegmentation();
  127. void ggvRm::MergeSmallRegion();
  128. void DrawBorder();
  129. };
  130.  
  131.  
  132. ggvRm::ggvRm(int width, int height, unsigned char *ras)
  133. {
  134. int i,j,index;
  135.  
  136. n =width*height;
  137. w=wo=width;
  138. h=ho=height;
  139.  
  140. N=new int[n];
  141. C=new int[n];
  142. Ravg=new float[n];
  143. Gavg=new float[n];
  144. Bavg=new float[n];
  145.  
  146. UF=new ggvUnionFind(n);// Disjoint sets with exactly n pixels
  147.  
  148. raster=ras;
  149. rastero=new unsigned char [wo*ho*3];
  150. //
  151. // Initialize to each pixel a leaf region in a union-find data structure
  152.  
  153. index=0;
  154. for(i=0;i<h;i++)
  155.         for(j=0;j<w;j++)
  156.                 {
  157.                 N[index]=1;
  158.                 C[index]=index;
  159.                 Ravg[index]=(float)raster[3*index];
  160.                 Gavg[index]=(float)raster[3*index+1];
  161.                 Bavg[index]=(float)raster[3*index+2];
  162.                 index++;
  163.                 }
  164.  
  165. Q=32.0; g=256.0; logdelta = 2.0*log(6.0*n);
  166. smallregion=(int)(0.001*n); // small regions are less than 0.1% of image pixels
  167. borderthickness=3; // border thickness of regions
  168. }
  169.  
  170. // Destructor
  171. ggvRm::~ggvRm()
  172. {delete UF; delete [] raster; delete [] rastero;}
  173.  
  174. inline bool ggvRm::MergePredicate(int reg1, int reg2)
  175. {
  176. double dR, dG, dB;
  177. double logreg1, logreg2;
  178. double dev1, dev2, dev;
  179.  
  180. dR=(Ravg[reg1]-Ravg[reg2]); dR*=dR;
  181. dG=(Gavg[reg1]-Gavg[reg2]); dG*=dG;
  182. dB=(Bavg[reg1]-Bavg[reg2]); dB*=dB;
  183. logreg1 = min(g,N[reg1])*log(1.0+N[reg1]);
  184. logreg2 = min(g,N[reg2])*log(1.0+N[reg2]);
  185. dev1=((g*g)/(2.0*Q*N[reg1]))*(logreg1 + logdelta);
  186. dev2=((g*g)/(2.0*Q*N[reg2]))*(logreg2 + logdelta);
  187. dev=dev1+dev2;
  188. return ( (dR<dev) && (dG<dev) && (dB<dev) );
  189. }
  190.  
  191. // A pair of pixels
  192. class ggvRmpair{
  193. public:
  194. int r1,r2;
  195. unsigned char diff;
  196.  
  197. // overload operators
  198. bool operator <  ( const ggvRmpair & rhs) {return (diff<rhs.diff);}
  199. bool operator >  (const ggvRmpair & rhs) {return (diff>rhs.diff);}
  200. };
  201.  
  202. // Sorting with buckets
  203. void BucketSort(ggvRmpair * &a, int n)
  204. {
  205. int i;
  206. int nbe[256], cnbe[256];
  207. ggvRmpair *b;
  208.  
  209. b=new ggvRmpair[n];
  210. for(i=0;i<256;i++) nbe[i]=0;
  211. // class all elements according to their family
  212. for(i=0;i<n;i++) nbe[a[i].diff]++;
  213. // cumulative histogram
  214. cnbe[0]=0;
  215. for(i=1;i<256;i++) cnbe[i]=cnbe[i-1]+nbe[i-1]; // index of first element of category i
  216.  
  217. // allocation
  218. for(i=0;i<n;i++) {b[cnbe[a[i].diff]++]=a[i];}
  219. delete [] a;
  220. a = b;
  221. }
  222.  
  223. void ggvRm::OutputSegmentation()
  224. {
  225. int i,j, index, indexb;
  226.  
  227. index=0;
  228. for(i=0;i<h;i++) // for each row
  229.         for(j=0;j<w;j++) // for each column
  230.         {
  231.         indexb=UF->Find(index); // Get the index root
  232.         // average color choice
  233.         rastero[3*index]   =(unsigned char)Ravg[indexb];
  234.         rastero[3*index+1] = (unsigned char)Gavg[indexb];
  235.     rastero[3*index+2] =(unsigned char)Bavg[indexb];
  236.         index++;
  237.         }
  238. }
  239.  
  240.  
  241. // Merge small regions
  242. void ggvRm::MergeSmallRegion()
  243. {
  244. int i,j, C1,C2, index;
  245.  
  246. index=0;
  247.  
  248. for(i=0;i<h;i++) // for each row
  249.         for(j=1;j<w;j++) // for each column
  250.         {
  251.         index=i*w+j;
  252.         C1=UF->Find(index);
  253.         C2=UF->Find(index-1);
  254.         if (C2!=C1) {if ((N[C2]<smallregion)||(N[C1]<smallregion)) MergeRegions(C1,C2);}       
  255. }
  256. }
  257.  
  258. // Draw white borders delimiting perceptual regions
  259. void ggvRm::DrawBorder()
  260. {
  261. int i, j, k, l, C1,C2, reg,index;
  262.  
  263. for(i=1;i<h;i++) // for each row
  264.         for(j=1;j<w;j++) // for each column
  265.         {
  266.         index=i*w+j;
  267.  
  268.         C1=UF->Find(index);
  269.         C2=UF->Find(index-1-w);
  270.         if (C2!=C1)
  271.         {
  272.         for(k=-borderthickness;k<=borderthickness;k++)
  273.                 for(l=-borderthickness;l<=borderthickness;l++)
  274.                 {
  275.                 index=(i+k)*w+(j+l);
  276.                 if ((index>=0)&&(index<w*h)) {
  277.                 rastero[3*index]=255;
  278.                 rastero[3*index+1]=255;
  279.                 rastero[3*index+2]=255;}
  280.                 }
  281.         }
  282.         }
  283. }
  284.  
  285. // This is the core statistical region growing segmentation algorithm
  286. inline void ggvRm::MergeRegions(int C1, int C2)
  287. {
  288. int reg,nreg;
  289. float ravg,gavg,bavg;
  290.  
  291.                         reg=UF->UnionRoot(C1,C2);
  292.                         nreg=N[C1]+N[C2];
  293.                         ravg=(N[C1]*Ravg[C1]+N[C2]*Ravg[C2])/nreg;
  294.                         gavg=(N[C1]*Gavg[C1]+N[C2]*Gavg[C2])/nreg;
  295.                         bavg=(N[C1]*Bavg[C1]+N[C2]*Bavg[C2])/nreg;
  296.                        
  297.                         N[reg]=nreg;
  298.                         Ravg[reg]=ravg;
  299.                         Gavg[reg]=gavg;
  300.                         Bavg[reg]=bavg;
  301. }
  302.  
  303. // Region-growing segmentation framework
  304. int ggvRm::Segment()
  305. {
  306. int i,j,index;
  307. int reg1,reg2;
  308. ggvRmpair * order;
  309. int npair;
  310. int cpair=0;
  311. int C1,C2;
  312.  
  313.  
  314. // Consider C4-connectivity here
  315. npair=2*(w-1)*(h-1)+(h-1)+(w-1);
  316. order=new ggvRmpair[npair];
  317.        
  318. for(i=0;i<h-1;i++)
  319. {
  320.         for(j=0;j<w-1;j++)
  321.         {
  322.         index=i*w+j;
  323.  
  324.         // C4  left
  325.         order[cpair].r1=index;
  326.         order[cpair].r2=index+1;
  327.         order[cpair].diff=max3(\
  328.                 abs(raster[3*index]-raster[3*(index+1)]),\
  329.                 abs(raster[3*index+1]-raster[3*(index+1)+1]),\
  330.                 abs(raster[3*index+2]-raster[3*(index+1)+2])\
  331.                 );
  332.         cpair++;
  333.  
  334.         // C4 below
  335.         order[cpair].r1=index;
  336.         order[cpair].r2=index+w;
  337.         order[cpair].diff=max3(\
  338.                 abs(raster[3*index]-raster[3*(index+w)]),\
  339.                 abs(raster[3*index+1]-raster[3*(index+w)+1]),\
  340.                 abs(raster[3*index+2]-raster[3*(index+w)+2])\
  341.                 );
  342.         cpair++;
  343.         }
  344. }
  345.  
  346. for(i=0;i<h-1;i++)
  347. {
  348. index=i*w+w-1;
  349. order[cpair].r1=index;
  350.         order[cpair].r2=index+w;
  351.         order[cpair].diff=max3(\
  352.                 abs(raster[3*index]-raster[3*(index+w)]),\
  353.                 abs(raster[3*index+1]-raster[3*(index+w)+1]),\
  354.                 abs(raster[3*index+2]-raster[3*(index+w)+2])\
  355.                 );
  356.         cpair++;
  357. }
  358.  
  359. for(j=0;j<w-1;j++)
  360.         {
  361.         index=(h-1)*w+j;
  362.  
  363.         order[cpair].r1=index;
  364.         order[cpair].r2=index+1;
  365.         order[cpair].diff=max3(\
  366.                 abs(raster[3*index]-raster[3*(index+1)]),\
  367.                 abs(raster[3*index+1]-raster[3*(index+1)+1]),\
  368.                 abs(raster[3*index+2]-raster[3*(index+1)+2])\
  369.                 );
  370.         cpair++;
  371.         }
  372.  
  373. BucketSort(order,npair);
  374.  
  375. // Main algorithm is here!!!
  376. for(i=0;i<npair;i++)
  377. {
  378.         reg1=order[i].r1; C1=UF->Find(reg1);
  379.         reg2=order[i].r2; C2=UF->Find(reg2);
  380.         if ((C1!=C2)&&(MergePredicate(C1,C2))) MergeRegions(C1,C2);                    
  381. }
  382.  
  383. delete [] order;
  384. return 1;
  385. }
  386.  
  387.  
  388. // Main program
  389. #include <iostream>
  390.  
  391. int main(int argc, char* argv[])
  392. {
  393.         int w, h; unsigned char *imgppm;
  394.         char filenamein[256], filenameout[256];
  395.         ggvRm * seg;
  396.  
  397.        
  398.         cout<<"Visual Computing: Geometry, Graphics, and Vision (ISBN:1-58450-427-7)"<<endl;
  399.         cout<<"Demo program\n\n"<<endl;
  400.  
  401.         cout <<"Statistical Region Growing.\n"<<endl;
  402.         if (argc==1) {cout<<argv[0]<<" srg-source.ppm [srg-output.ppm]\n(Only for PPM Images without comments. Eg., convert bmp->ppm with XnView.)"<<endl;return -1;}
  403.         if (argc>=2) sprintf(filenamein,argv[1]); else sprintf(filenamein,"srg-source.ppm");
  404.         if (argc>=3) sprintf(filenameout,argv[2]); else sprintf(filenameout,"srg-output.ppm");
  405.        
  406.         imgppm=LoadImagePPM(filenamein, w, h);
  407.         seg=new ggvRm(w,h,imgppm);
  408.         seg->Segment();
  409.         seg->MergeSmallRegion(); // optional
  410.         seg->OutputSegmentation(); // optional: write in the rastero buffer
  411.         seg->DrawBorder(); // optional
  412.         SaveImagePPM(seg->rastero,w,h,filenameout);
  413.  
  414.  
  415. cout<<"Press Return key"<<endl;
  416. char line[100];
  417. gets(line);
  418.  
  419.  
  420. return 1;
  421. }



Grazie!

Pierotofy: aggiustato il code

Ultima modifica effettuata da pierotofy il 21/01/2009 alle 6:51
PM Quote
Avatar
gioser (Normal User)
Pro


Messaggi: 111
Iscritto: 03/10/2008

Segnala al moderatore
Postato alle 0:10
Mercoledì, 21/01/2009
dovresti editare il post e mettere il codice in un blocco Code

PM Quote