Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 11th, 2012  |  syntax: None  |  size: 2.61 KB  |  hits: 14  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Strange array error in OpenCV
  2. void Noise_Reduction(CvMat* Color_mat,boolean showresult){
  3. int tv[__COLORNUM]={0,0,0,0,0,0,0,0,0};
  4. int counter;
  5.  
  6. for(int y=1;y<Color_mat->height-1;y++)
  7. {
  8.     for(int x=1;x<Color_mat->width-1;x++)
  9.     {  
  10.         for(int i=0;i<9;i++,tv[i]=0);          
  11.         tv[CV_MAT_ELEM(*Color_mat,uchar,y-1,x-1)]++;
  12.         tv[CV_MAT_ELEM(*Color_mat,uchar,y-1,x  )]++;
  13.         tv[CV_MAT_ELEM(*Color_mat,uchar,y-1,x+1)]++;
  14.         tv[CV_MAT_ELEM(*Color_mat,uchar,y  ,x-1)]++;
  15.         tv[CV_MAT_ELEM(*Color_mat,uchar,y  ,x+1)]++;
  16.         tv[CV_MAT_ELEM(*Color_mat,uchar,y+1,x-1)]++;
  17.         tv[CV_MAT_ELEM(*Color_mat,uchar,y+1,x  )]++;
  18.         tv[CV_MAT_ELEM(*Color_mat,uchar,y+1,x+1)]++;
  19.         for(int i=0;i<9;i++){
  20.         std::cout<<tv[i]<<",";}
  21.         std::cout<<endl;
  22.         int max=0;
  23.         int indx=0;
  24.         max= tv[0];
  25.         for(int i = 1; i<__COLORNUM; i++)
  26.          {
  27.              if(tv[i] > max){
  28.                     max = tv[i];
  29.                     indx=i;
  30.              }
  31.  
  32.          }
  33.  
  34.         if(max>=5)
  35.         {
  36.             counter++;
  37.             *( (uchar*)CV_MAT_ELEM_PTR( *Color_mat, y, x ))=(uchar)indx;
  38.             //std::cout<<"times:"<<counter <<endl;
  39.  
  40.         }
  41.  
  42.     }
  43. }
  44. std::cout<<"times:"<<counter <<endl;    
  45.  
  46. if(showresult){
  47.     IplImage* Noise_result = cvCreateImage(cvSize(Color_mat->width,Color_mat->height),IPL_DEPTH_8U,3);
  48.     for( int y=0; y<Noise_result->height; y++ ) {
  49.         uchar* ptr = (uchar*)(Color_mat->data.ptr + y * Color_mat->step);
  50.         for( int x=0; x<Noise_result->width; x++ ) {
  51.             switch ( *ptr) {
  52.  
  53.                           case 1 :
  54.                               cvSet2D(Noise_result,y,x,oo);
  55.                               break;
  56.  
  57.                           case 2 :
  58.                               cvSet2D(Noise_result,y,x,bb);
  59.                               break;
  60.  
  61.                           case 3 :
  62.                               cvSet2D(Noise_result,y,x,yy);
  63.                               break;
  64.  
  65.                           case 4 :
  66.                               cvSet2D(Noise_result,y,x,gg);
  67.                               break;
  68.  
  69.                           case 5 :
  70.                               cvSet2D(Noise_result,y,x,ww);
  71.                               break;
  72.  
  73.                           default :  
  74.                               cvSet2D(Noise_result,y,x,uk);
  75.                               break;
  76.             }
  77.             ptr++;
  78.         }
  79.     }
  80.  
  81.     if(showresult)
  82. {
  83.     cvNamedWindow( "Noiseresult", CV_WINDOW_FREERATIO);
  84.     cvShowImage( "Noise_result", Noise_result );
  85.     cvWaitKey(0);
  86.  
  87. }
  88.  
  89.  
  90.  
  91. }
  92.        
  93. for(int i=0;i<9;i++,tv[i]=0);
  94.        
  95. for(int i=0;i<9;tv[i]=0,i++);