How to detect the intensity gradient direction
<p>Having a Mat that is square area of grayscale pixels. How to create a straight line whose direction is created as a perpendicular to most pixel values change direction (average gradient, aerage over the whole Mat, the result would be just one direction (which can be then drawn as a line))?</p>

<p>For example having</p>

<p><a href="https://i.stack.imgur.com/LP24W.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/LP24W.png" alt="enter image description here"></a></p>

<p>it would look like</p>

<p><a href="https://i.stack.imgur.com/sz28i.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/sz28i.png" alt="enter image description here"></a></p>

<p>How can one do such thing in OpenCV (in python or C++)?</p>