title: Which way is my yarn oriented?
body: <p>I have an image processing problem.  I have pictures of yarn:</p>
  
  <p><img src="https://i.stack.imgur.com/o3pNw.png" alt="Yarn :)"></p>
  
  <p>The individual strands are partly (but not completely) aligned.  I would like to find the predominant direction in which they are aligned. In the center of the example image, this direction is around 30-34 degrees from horizontal.  The result could be the average/median direction for the whole image, or just the average in each local neighborhood (producing a vector map of local directions).  </p>
  
  <p>What I've tried: I rotated the image in small steps (1 degree) and calculated statistics in the vertical vs horizontal direction of the rotated image (for example: standard deviation of summed rows or summed columns).  I reasoned that when the strands are oriented exactly vertically or exactly horizontally the difference in statistics would be greatest, and so that angle of rotation is the correct direction in the original image.  However, for at least several kinds of statistical properties I tried, this did not work.  </p>
  
  <p>I further thought that perhaps this wasn't working because there were too many different directions at the same time in the whole image, so I tired it in a small neighborhood.  In this case, there is always a very clear preferred direction (different for each neighborhood), but it is not the direction that the fibers <em>really</em> go...  I can post my sample code but it is basically useless.  </p>
  
  <p>I keep thinking there has to be some kind of simple linear algebra/statistical property of the whole image, or some value derived from the 2D FFT that would give the correct direction in one step... but how?</p>
  
  <p>What probably won't work: detecting individual fibers.  They are not necessarily the same color, and the image can shade from light to dark so edge detectors don't work well, and the image may not even be in focus sometimes.  Because of that, it is not always even possible to see individual fibers for a human (see top-right in the example), they kinda have to be detected as preferred direction in a statistical sense.</p>