1

Here is my code:

    int Factor=3,offset=0,k,l,p,q;
    IplImage * image = cvCreateImage(cvSize(img->width, img->height),img->depth, img->nChannels);
     cvCopy (img, image, 0);
    long double mean=0,nTemp=0,c,sum=0,n=0,s=0,d=0;
    int i=0,j=0,krow,kcol;
    kernel[0][0]=kernel[0][2]=kernel[2][0]=kernel[2][2]=0;
    kernel[0][1]=kernel[1][0]=kernel[1][2]=kernel[2][1]=1;
    kernel[1][1]=-4;
    uchar* temp_ptr=0 ;
    int rows=image->height,cols=image->width,row,col;

     //calculate the mean of image and deviation 

    for ( row = 1; row < rows - 2; row++ )
    {
        for ( col = 1; col < cols - 2; col++ )
        {
            nTemp = 0.0;
                for (p=0, krow = -1 ; p < 3; krow++,p++)
                {
                    for (q=0, kcol = -1; q < 3; kcol++,q++)
                    {   
                        temp_ptr  = &((uchar*)(image->imageData + (image->widthStep*(row+krow))))[(col+kcol)*3];
                        for(int k=0; k < 3; k++)
                        Pixel[p][q].val[k]=temp_ptr[k];
                    }
                }
                for (i=0 ; i < 3; i++)
                {
                    for (j=0 ; j < 3; j++)
                    {
                        c = (Pixel[i][j].val[0]+Pixel[i][j].val[1]+Pixel[i][j].val[2])/Factor ;
                        nTemp += (double)c * kernel[i][j];
                    }
                }

            sum += nTemp;
            n++;
        }
    }
mean = ((double)sum / n);
    for ( row = 1; row < rows - 2; row++ )
    {
        for ( col = 1; col < cols - 2; col++ )
        {
            nTemp = 0.0;

                for (p=0, krow = -1 ; p < 3; krow++,p++)
                {
                    for (q=0, kcol = -1; q < 3; kcol++,q++)
                    {   
                        temp_ptr  = &((uchar*)(image->imageData + (image->widthStep*(row+krow))))[(col+kcol)*3];
                        for(int k=0; k < 3; k++)
                        Pixel[p][q].val[k]=temp_ptr[k];
                    }
                }
                for (i=0 ; i < 3; i++)
                {
                    for (j=0 ; j < 3; j++)
                    {
                        c = (Pixel[i][j].val[0]+Pixel[i][j].val[1]+Pixel[i][j].val[2])/Factor ;
                        nTemp += (double)c * kernel[i][j];
                    }
                }

            s = (mean - nTemp);
            d += (s * s);
        }
    }
    d = d / (n - 1);
    d = (sqrt(d));
    d=d* 2;
     // Write to image
    for ( row = 1; row < rows - 2; row++ )
    {
        for ( col = 1; col < cols - 2; col++ )
        {   
                nTemp = 0.0;
                for (p=0, krow = -1 ; p < 3; krow++,p++)
                {
                    for (q=0, kcol = -1; q < 3; kcol++,q++)
                    {   
                        temp_ptr  = &((uchar*)(image->imageData + (image->widthStep*(row+krow))))[(col+kcol)*3];
                        for(int k=0; k < 3; k++)
                        Pixel[p][q].val[k]=temp_ptr[k];
                    }
                }
                for (i=0 ; i < 3; i++)
                {
                    for (j=0 ; j < 3; j++)
                    {
                        c = (Pixel[i][j].val[0]+Pixel[i][j].val[1]+Pixel[i][j].val[2])/Factor ;
                        nTemp += (double)c * kernel[i][j];
                    }
                }
            temp_ptr  = &((uchar*)(image->imageData + (image->widthStep*row)))[col*3];
            if (nTemp > d)
                temp_ptr[0]=temp_ptr[1]=temp_ptr[2]=255;
            else
                temp_ptr[0]=temp_ptr[1]=temp_ptr[2]=0;
        }
    }

Where am i going wrong? I have implemented Gaussian Filtering in a similar manner, is there anything wrong in my algorithm?

Expert Novice
  • 1,943
  • 4
  • 22
  • 47
  • You could have at least posted fully compilable code. What you posted is useless – BЈовић Oct 27 '11 at 17:38
  • Looks about right, `nTemp` is either greater than `d` or not. Can you show us what you believe the output to be like? – user7116 Oct 27 '11 at 17:39
  • @VJo - Why do you think it is not compilable? Just pass an image called `img` in the function and it works fine. – Expert Novice Oct 27 '11 at 17:40
  • @sixlettervariables give me a minute...editing my question...and adding expected output// – Expert Novice Oct 27 '11 at 17:40
  • Why is this question being voted to be `closed`, i feel this is a valid question, i have posted relevant code, and am asking for information where i am stuck? – Expert Novice Oct 27 '11 at 17:44
  • Your code is extremely hard to follow, but if I had to take a guess you're probably not handling negative values properly. Try switching between taking the absolute value and clamping. – user786653 Oct 27 '11 at 17:53
  • I have been criticized for writing wrong code or algorithms when my code and algorithms are just fine, questions are being downvoted and closed simply at times when people dont know the answer...this is saddening. – Expert Novice Oct 27 '11 at 18:01
  • I can't speak for whoever downvoted you, but I think you'd get a better response if you took care to only include the *really* relevant parts. I know that in this case the seemingly irrelevant parts turned out to be the cause, but that's the issue. – user786653 Oct 27 '11 at 18:19
  • @user786653 - That comment was not exactly for you friend, you have been quite sensible and considerate, but just see how SO users behave, the 1st comment wanted a fully compilable code, when the code is 100% compilable, and as u said about relevant parts, i myself was at my wits end where i was going wrong... – Expert Novice Oct 27 '11 at 18:21
  • That look like a function, but might be only a part of a function. Also, missing main to make it full example. So, what kind of help do you expect if you post only partial, but fairly complex, example? – BЈовић Oct 27 '11 at 18:48
  • @Lohit Voting to close is not like downvoting and does not mean necessarily your question has something wrong per se. I voted to close your question b/c I think it is a perfect match for "code review" (another SE series site), and not here. Then I was meant to leave a comment like this, but forgot to do that. – Dr. belisarius Oct 28 '11 at 12:31

2 Answers2

1

It seems that your code (labeled "Write to image") overwrites the input image during the calculations. This is not good. Create a copy of the image, calculate its pixels, then delete the original image.

anatolyg
  • 26,506
  • 9
  • 60
  • 134
1

I noticed is that your code is needlessly complex and inefficient. You don't need to convolve the image before calculating its mean — the convolution just multiplies the mean by the sum of the kernel entries.

Also, since your convolution kernel sums to zero, the mean you get after convolving with it will also (almost) be zero. The only non-zero contributions will come from the edges of the image. I rather doubt that's actually what you want to calculate (and if it is, you could save a lot of time by summing only over the edges in the first place).

Third, as pointed out in another answer (I missed this one myself), you're not averaging over all the color channels, you're averaging over the red channel three times. (Besides, you should probably use a weighted average anyway, after applying gamma correction.)

And finally, as anatolyg said, you're overwriting the image data before you're done reading it. There are several ways to fix that, but the easiest is to write your output to a separate buffer.

Community
  • 1
  • 1
Ilmari Karonen
  • 49,047
  • 9
  • 93
  • 153
  • `you're not averaging over all the color channels` - Can you please explain this a bit more, are not `B G R` values stored in `temp_ptr[0]`,`temp_ptr[1]`,`temp_ptr[2]` - respectively in an `IplImage` structure in Opencv? – Expert Novice Oct 27 '11 at 19:17
  • Sorry, I was wrong. I suppose that's why the answer I linked to was deleted, too. :-/ I guess I should've read the code more carefully before including it my answer, but honestly, the line where you set `temp_ptr` is long and convoluted enough that it's an easy mistake to make. Seriously, there's _got to_ be a better way to write that. (If nothing else, at least don't mix pointer arithmetic and array indexing; use one or the other, but not both in the same expression. It's just confusing.) – Ilmari Karonen Oct 27 '11 at 22:43