Reputation: 1963
I am trying to blur an image.
I am using a kernel called k[][3]= { 1, .01, 1, .01, 0, .01, 1, .01, 1};
The code is as follows:
void blur(IplImage *notgray)
{
IplImage *img = cvCreateImage(cvGetSize(notgray), notgray->depth, 1);
cvCvtColor(notgray, img, CV_RGB2GRAY);
int rows=img->height,cols=img->width,row,col,i,j,ki,kj;
uchar* temp_ptr=0 ;
float sum,k[][3]= { 1, .01, 1,
.01, 0, .01,
1, .01, 1};
for( row = 0; row < rows; ++row)
{
sum=0;
for ( col = 0; col < cols; ++col)
{
temp_ptr = &((uchar*)(img->imageData + (img->widthStep*row)))[col];
for( j=-1,ki=0; j<=1;j++,ki++)
{
for( i=-1,kj=0; i<=1;i++,kj++)
{
int x2=col+i;
int y2=row+j;
if ( x2>=0 && x2<img->width && y2>=0 && y2<img->height)
{
sum=sum+k[ki][kj];
if (sum<0) sum=0; else if (sum>255) sum=255;
temp_ptr[0] =(uchar) sum;
}
}
}
}
}
}
I know the output really depends on my algorithm, but looking at the code and output i would request some guidance what i should do further to solve my problem.
Upvotes: 0
Views: 218
Reputation: 9244
You set sum = 0; at the wrong place. Since it is only reset at the start of each row, each pixel will be whiter and whiter and...
Upvotes: 1