Community
cancel
Showing results for 
Search instead for 
Did you mean: 
Ladislav_K_
Beginner
61 Views

Documentation for GradientVectorSobel and GradientVectorPrewitt

Hi,
there is documented GradientVectorSobel function for example on https://software.intel.com/en-us/node/504474 and there is mentioned Gx kernel.
I am not sure if it is correct documented, IMO it should be :
+1  0  -1
+2  0  -2
+1  0  -1
It seems to me that this kernel is used in reality by GradientVectorSobel as stated also for example on https://en.wikipedia.org/wiki/Sobel_operator

0 Kudos
4 Replies
Valentin_K_Intel
Employee
61 Views

Hi Ladislav,

Unfortunately there is no any standard that describes operations like Sobel operator. So Wikipedia shows one formula https://en.wikipedia.org/wiki/Sobel_operator, ;
         +1  0  -1             +1  +2 +1
Gx = +2  0  -2    Gy =   0   0    0
         +1  0  -1             -1   -2   -1 

OpenCV documentation gives another formula http://docs.opencv.org/2.4/doc/tutorials/imgproc/imgtrans/sobel_derivatives/sobel_derivatives.html,
         -1  0  +1              -1   -2   -1
Gx = -2  0  +2    Gy =   0   0    0
         -1  0  +1             +1  +2 +1 

IPP implements the third formula https://software.intel.com/en-us/node/504474
         -1  0  +1             +1  +2 +1
Gx = -2  0  +2    Gy =   0   0    0
         -1  0  +1              -1   -2   -1 

All the formulas are correct in terms of math.

Thanks,
Valentin

Ladislav_K_
Beginner
61 Views

Hi Valentin,
thank you for response. Try this example:
Initialize for example image 8u 8x8 with these values:
0 1 2 3 4 5 6 7
0 1 2 3 4 5 6 7
0 1 2 3 4 5 6 7
0 1 2 3 4 5 6 7
0 1 2 3 4 5 6 7
0 1 2 3 4 5 6 7
0 1 2 3 4 5 6 7
0 1 2 3 4 5 6 7

Then compute ippiGradientVectorSobel_8u16s_C1R, result will be:
-4 -8 -8 -8 -8 -8 -8 -4
-4 -8 -8 -8 -8 -8 -8 -4
-4 -8 -8 -8 -8 -8 -8 -4
-4 -8 -8 -8 -8 -8 -8 -4
-4 -8 -8 -8 -8 -8 -8 -4
-4 -8 -8 -8 -8 -8 -8 -4
-4 -8 -8 -8 -8 -8 -8 -4
-4 -8 -8 -8 -8 -8 -8 -4

Which IMO corresponds to kernel Gx:
         +1  0  -1
Gx = +2   0  -2
         +1  0  -1
Not as documented on https://software.intel.com/en-us/node/504474
         -1  0  +1
Gx = -2  0  +2
         -1  0  +1

So there is inconsistence AFAIU, does not ?
Second question is what angle will be calculated if various kernels are used ? IMO different sign will lead to different angle, does not ? So used kernel matters!

I have used this test program (is in Pascal, but no problem to understand):
 for j:=0 to 7 do
    for i:=0 to 7 do
      b[i+8*j] := i;
  ippiGradientVectorGetBufferSize(roiSize, ippMskSize3x3, ippDT8u, 1, bufSize);
  buf := ippsMalloc_8u(bufSize);
  ippiGradientVectorSobel_8u16s_C1R( @b[0], roiSize.Width, @g[0], roiSize.Width*2, nil, 0, nil, 0, nil, 0,
                                     roiSize, ippMskSize3x3, ippNormL2, ippBorderRepl, 0, buf);
  ippsFree(buf);
  for j:=0 to 7 do begin
    writeln;
    for i:=0 to 7 do
      write(g[i+8*j], ' ');
  end;   

Valentin_K_Intel
Employee
61 Views

Ladislav,

Thank you for the question. The formulas from https://software.intel.com/en-us/node/504474 describe two-dimensional convolution, where a kernel is applied to the image in reverse order. So the function implementation corresponds to the documentation.

Best regards,
Valentin

Ladislav_K_
Beginner
61 Views

Yes, now I understand it. Rows and columns of kernal are flipped and then applied to image.
It is not very intuitive IMO but also from wikipedia POV: https://en.wikipedia.org/wiki/Sobel_operator :
"Applying convolution K to pixel group P can be represented in pseudocode as:
  N(x,y) = Sum of { K(i,j).P(x-i,y-j)}, for i,j running from -1 to 1.
N(x,y) represents the new matrix resulted after applying the Convolution K to P, where P is pixel matrix."

and https://en.wikipedia.org/wiki/Kernel_(image_processing)#Convolution
Describes your approach ...