Reputation: 175
in this function i have a matrix of colors(each pixel can has a value from 0 to 9) and what i want to do is to reduce noise. pixels that have, more than 5 neighbor, with the same color are changed to that color! i define an array[0-9] to hold the number of neighbors color for example if 6 pixel has the value of 8 and 2 has the value of 5 the array should be like this [0,0,0,0,0,2,0,6,0,0] but when i print the array the value of first element( array[0]) is wrong ,it starts with 8 and each time pulse by 8 ! here is the code. can you help me pls ?
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);
}
}
}
Upvotes: 0
Views: 136
Reputation: 9877
for(int i=0;i<9;i++,tv[i]=0);
That line looks fishy (it will never set tv[0] to 0), don't you mean this instead:
for(int i=0;i<9;tv[i]=0,i++);
(Or even better: memset(tv, 0, sizeof(tv));)
Upvotes: 3