Stroke Width Transform: Gradient Direction and Rays
You basically have to sample points from p in the direction of p's gradient dp.
You get the dp from the x and y gradient maps. And sample from where? from the edge map and the gradient maps. First check if it is a edge from the edge map. If it is not then take another step in the dp direction. If it is an edge then compute the gradient direction from the x and y gradient map. If that gradient points back to p with about π/6 of tolerance then that is the q you are looking for.

Be careful with back over while vs white over black. The gradients will point in opposites directions when you invert the image. That is why you have to run the algorithm twice since SWT does not know which case are we dealing with. One with the original gradient directions and another flipping the gradients in the opposite direction.