I am a beginner of image processing, how can I extract the 5 type "vertical", "horizontal", "diagnal 45", "diagnal 135", "non-directional" edges with opencv?
Asked
Active
Viewed 1,703 times
0
-
1Sorry @Berriel , I am the beginner of image processing, for me the questions are not the same. I didn't understand the return value of sobel operator of opencv very well, so I asked the other question. It looks like no one answer them, except this thread. But like you wish, I already deleted the other topic. – xirururu Sep 09 '16 at 21:38
1 Answers
2
First you will want to apply the sobel filters to your image. See the OpenCV documentation for some example code.
Once you've gotten your gradient images in X and Y (I'll call them Gx
and Gy
) you can compute orientation of the edges using the formula theta = atan2(Gy,Gx)
and magnitude M = sqrt(Gx*Gx + Gy*Gy)
. Once you have Gx
, Gy
, theta
, and M
you can compute the magnitude of the oriented edges at each pixel using the following formulas.
Vertical edges: abs(Gy)
Horizontal edges: abs(Gx)
45 Degree edges: M*abs(cos(theta - pi/4))
135 Degree edges: M*abs(cos(theta - 3*pi/4))
Non-direction edges: M
Except for the non-direction edges we are simply computing the magnitude of the projection of the gradient onto a particular direction.

jodag
- 19,885
- 5
- 47
- 66
-
Hi @jodag Thanks very much for the answer. I did that like what you said. So I got 5 matrices. But how can I find out, which category is a point in? For example, the first pixel in the image, I get five values from the five matrix, which one belong to it? – xirururu Sep 09 '16 at 22:21
-
You can assign a direction (theta) to every edge in the image as long as `Gx` and `Gy` are no zero, so I'm not sure what exactly a "non-directional edge" is. In some cases the application of the Sobel filter is known as a non-directional edge filter because the magnitude it gives you `M` is independent of the edge direction. If you want, you could assign all pixels with a magnitude `M` less than some threshold to "non-directional" and then for the remaining pixels assign them to one of the remaining four categories, whichever has the highest magnitude. – jodag Sep 10 '16 at 03:37
-
Are you attempting to create a local edge histogram descriptor? Like this paper: https://www.dcc.fc.up.pt/~mcoimbra/lectures/VC_1415/VC_1415_P8_LEH.pdf – jodag Sep 10 '16 at 03:50
-
Thanks so much for the explanation. Originally, I just want to implement this paper, but I have no idea, why the author do in this way. Since sobel operator can calculate the gradient, why not just use sobel operator calculate the gradient at first, then create the local edge histogram? This topic also related to this paper, http://stackoverflow.com/questions/909542/opencv-edge-extraction?rq=1 But the formel @user101375 listed has 6 type of edges. I really very confused about this. – xirururu Sep 13 '16 at 07:28