How do I calculate gradient and hessian matrix by two operators?
Show older comments
For each point (x,y), the gradients should be calculated by two operators like:
operator x:1/4*[1,0,-1;0,0,0;1,0,-1] operator y:1/4*[-1,0,-1;0,0,0;1,0,1]
now should I first measure the gradients by its function and then convolve them with these matrices as below? (I have no image processing toolbox)
Then hessian operators are [-1,1] for x , and [-1;1] for y. Well, I know how to generate hessian matrix but don't know how to do with these operators in a way that finally I can calculate the sum of all these measurements (their dimension would be different after convolution)
Answers (1)
David Young
on 3 Mar 2014
On the first part of your question, I think you should not call gradient, because convolution with the operators is what calculates the gradients. This makes more sense:
opx = [1,0,-1;0,0,0;1,0,-1]/4;
opy = [-1,0,-1;0,0,0;1,0,1]/4;
Gx = conv2(im, opx);
Gy = conv2(im, opy);
However, the choice of operators puzzles me. [1 0 -1; 2 0 -1; 1 0 -1] would be a more conventional choice for an x-gradient operator.
To get the elements of the Hessian, you can convolve Gx and Gy with gradient operators, something like
Gxx = conv2(Gx, opx);
and so on. Again, the motivation for the choice of operator isn't clear - the operators [-1 1] and [-1; 1] introduce a half-pixel offset into the results, so can be an awkward choice.
An alternative approach is just to use the gradient function instead of convolution. However, convolution gives you more control, and permits the introduction of some smoothing and the use of symmetrical operators.
Depending on your data, you may well need some smoothing as well as differencing to form estimates of gradients or Hessian elements.
8 Comments
Klara
on 3 Mar 2014
David Young
on 3 Mar 2014
Yes, that's correct.
Klara
on 4 Mar 2014
David Young
on 5 Mar 2014
Edited: David Young
on 6 Mar 2014
Not sure what the problem is, but you could try using my smoothing+differencing functions at this location on the File Exchange.
[Edit] Just remembered you don't have the IPT. I think the only thing you need to replace to use my FEX functions is fspecial, to get the Gaussian kernel. It's not complex - let me know if you need help with that.
David Young
on 16 Mar 2014
The FEX submission I linked to above does, I think, what you want.
Klara
on 18 Mar 2014
David Young
on 19 Mar 2014
filter2 just calls conv2. Both have a 'same' option which avoids a change in size; the difference is that it is the default for filter2. In both cases the array is padded with zeros to compute values near the boundaries, which may or may not be what you want.
Categories
Find more on Object Analysis in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!