【发布时间】:2011-12-31 22:37:09
【问题描述】:
在这个函数中,我有一个颜色矩阵(每个像素可以有一个从 0 到 9 的值),我想要做的是减少噪音。具有相同颜色的相邻像素超过 5 个的像素将更改为该颜色!我定义了一个数组 [0-9] 来保存邻居颜色的数量,例如,如果 6 个像素的值为 8,而 2 的值为 5,则数组应该是这样的 [0,0,0,0,0, 2,0,6,0,0] 但是当我打印数组时,第一个元素(数组 [0])的值是错误的,它从 8 开始,每次脉冲 8 !这是代码。 你能帮我吗?
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);
}
}
}
【问题讨论】:
标签: c++ c arrays pointers opencv