- Strange array error in OpenCV
- void Noise_Reduction(CvMat* Color_mat,boolean showresult){
- int tv[__COLORNUM]={0,0,0,0,0,0,0,0,0};
- int counter;
- for(int y=1;y<Color_mat->height-1;y++)
- {
- for(int x=1;x<Color_mat->width-1;x++)
- {
- for(int i=0;i<9;i++,tv[i]=0);
- tv[CV_MAT_ELEM(*Color_mat,uchar,y-1,x-1)]++;
- tv[CV_MAT_ELEM(*Color_mat,uchar,y-1,x )]++;
- tv[CV_MAT_ELEM(*Color_mat,uchar,y-1,x+1)]++;
- tv[CV_MAT_ELEM(*Color_mat,uchar,y ,x-1)]++;
- tv[CV_MAT_ELEM(*Color_mat,uchar,y ,x+1)]++;
- tv[CV_MAT_ELEM(*Color_mat,uchar,y+1,x-1)]++;
- tv[CV_MAT_ELEM(*Color_mat,uchar,y+1,x )]++;
- tv[CV_MAT_ELEM(*Color_mat,uchar,y+1,x+1)]++;
- for(int i=0;i<9;i++){
- std::cout<<tv[i]<<",";}
- std::cout<<endl;
- int max=0;
- int indx=0;
- max= tv[0];
- for(int i = 1; i<__COLORNUM; i++)
- {
- if(tv[i] > max){
- max = tv[i];
- indx=i;
- }
- }
- if(max>=5)
- {
- counter++;
- *( (uchar*)CV_MAT_ELEM_PTR( *Color_mat, y, x ))=(uchar)indx;
- //std::cout<<"times:"<<counter <<endl;
- }
- }
- }
- std::cout<<"times:"<<counter <<endl;
- if(showresult){
- IplImage* Noise_result = cvCreateImage(cvSize(Color_mat->width,Color_mat->height),IPL_DEPTH_8U,3);
- for( int y=0; y<Noise_result->height; y++ ) {
- uchar* ptr = (uchar*)(Color_mat->data.ptr + y * Color_mat->step);
- for( int x=0; x<Noise_result->width; x++ ) {
- switch ( *ptr) {
- case 1 :
- cvSet2D(Noise_result,y,x,oo);
- break;
- case 2 :
- cvSet2D(Noise_result,y,x,bb);
- break;
- case 3 :
- cvSet2D(Noise_result,y,x,yy);
- break;
- case 4 :
- cvSet2D(Noise_result,y,x,gg);
- break;
- case 5 :
- cvSet2D(Noise_result,y,x,ww);
- break;
- default :
- cvSet2D(Noise_result,y,x,uk);
- break;
- }
- ptr++;
- }
- }
- if(showresult)
- {
- cvNamedWindow( "Noiseresult", CV_WINDOW_FREERATIO);
- cvShowImage( "Noise_result", Noise_result );
- cvWaitKey(0);
- }
- }
- for(int i=0;i<9;i++,tv[i]=0);
- for(int i=0;i<9;tv[i]=0,i++);