Talk:Fractals/Computer graphic techniques/2D

Error in sobel filter pseudo code?
The pseudo code implementation at Fractals/Computer_graphic_techniques/2D does not seem to yield expected results. I think there are some wrong offsets in the Gv/Gh lines. I think these would be the correct ones: Gv= data[iY+1][iX+1] + 2*data[iY+1][iX] + data[iY+1][iX-1] - data[iY-1][iX-1] - 2*data[iY-1][iX] - data[iY-1][iX+1]; Gh= data[iY+1][iX+1] + 2*data[iY][iX+1] + data[iY-1][iX+1] - data[iY+1][iX-1] - 2*data[iY][iX-1] - data[iY-1][iX-1]; Could someone please verify? - Invitam (discuss • contribs) 19:10, 10 September 2011 (UTC)


 * This code which you want to verify comes from this program. It works. The result is the same after changing to your fragment of code. Maybe there is an error ( I'm not an expert ) but the resulting image is the same. Please write more about differences.

Now I use slightly chaged code for 1D array : Gv= data[f(iX-1,iY+1)] + 2*data[f(iX,iY+1)] + data[f(iX-1,iY+1)] - data[f(iX-1,iY-1)] - 2*data[f(iX-1,iY)] - data[f(iX+1,iY-1)]; Gh= data[f(iX+1,iY+1)] + 2*data[f(iX+1,iY)] + data[f(iX-1,iY-1)] - data[f(iX+1,iY-1)] - 2*data[f(iX-1,iY)] - data[f(iX-1,iY-1)];

where : unsigned int f(unsigned int _iX, unsigned int _iY) /*  gives position of point (iX,iY) in 1D array  ; uses also global variables it does not check if index is good so memory error is possible */ {return (_iX + (iYmax-_iY-1)*iXmax );} Best regards. --Adam majewski (discuss • contribs) 10:52, 11 September 2011 (UTC)
 * I noticed the supposed bugs when applying a horizontal-only filter to a 9x7 pixel "image". The resulting values didn't match at all. Alright, let's see. I don't think the error is in your 1D code. I'll arrange the summands in order of their corresponding relative position: (x right, y up)

//Mine Gv= -data[iY-1][iX-1] -2*data[iY-1][iX] -data[iY-1][iX+1] /*            */ /*             */  /*             */  +data[iY+1][iX-1] +2*data[iY+1][iX] +data[iY+1][iX+1]; Gh= -data[iY+1][iX-1] /*     */ +data[iY-1][iX+1] -2*data[iY][iX-1] /*     */ +2*data[iY][iX+1] -data[iY-1][iX-1] /*     */ +data[iY+1][iX+1];
 * As you can see, each offset -1/+1 corresponds to an entry of the governing sobel filter matrix. -1 -2 -1 in the "top row" and 1 2 1 in the "lower row" for a vertical filter, also -1 -2 -1 in the "left column" and 1 2 1 in the "right column" for the horizontal filter. (Careful with what "horizontal" and "vertical" mean. Ymmv.)
 * Now let me arrange your code:

//Yours Gv= +data[f(iX-1,iY+1)] +data[f(iX-1,iY+1)] +2*data[f(iX,iY+1)] /*              */ -2*data[f(iX-1,iY)]                    /*               */ /*               */ -data[f(iX-1,iY-1)]                    /*               */ -data[f(iX+1,iY-1)]; Gh= +data[f(iX+1,iY+1)]                    /*               */ /*              */ +2*data[f(iX+1,iY)]                    /*               */ -2*data[f(iX-1,iY)] +data[f(iX-1,iY-1)] -data[f(iX-1,iY-1)] /*              */ -data[f(iX+1,iY-1)];
 * Notice that you address same offset, e.g. "iX-1,iY-1" twice in your calculation, which effectively sets their resulting directional derivative to "0". The two filter matrices as implemented in your code are 2 2 0 / 2 0 0 / -1 0 -1 vertically and 1 0 0 / 2 0 -2 / 0 0 -1 horizontally. Together they may be working to something similar as a sobel filter, but they're not the filters advertised in this article.
 * I don't know why the resulting picture would look fine. I guess it's because of the  normalisation. But I don't think your code could yield the expected results by applying a vertical filter only. - Invitam (discuss • contribs) 11:47, 12 September 2011 (UTC)
 * OK. Thx for explanations. Please correct code if you like or I will do it later. I have also to correct code of some of these images. Best regards. --Adam majewski (discuss • contribs) 12:13, 12 September 2011 (UTC)
 * Done. -Invitam (discuss • contribs) 15:53, 12 September 2011 (UTC)