I am a novice user in image processing and working on a project to determine number of yarns in a fabric image i.e. warps and wefts (Perpendicular and parallel yarns to x-axis).

sample image

I am trying to detect the yarns by taking the sum of columns and rows for warps and weft respectively but it doesn't seem working because the image is not very well focused and sharp. Also the method is much sensitive to the alignment of yarns in the fabric.

Please propose a better algorithm if possible for detection of yarns. Also if there is any possibility to make yarns straight for every captured image. I have tried IMROTATE but it creates the black regions in image which disturbs the sum of rows and algorithm gets failed.

Any help in this regard will be of much worth to me. Thanks.

  • $\begingroup$ Use an FFT. Is this homework? $\endgroup$
    – endolith
    Jul 8, 2012 at 23:03
  • $\begingroup$ Yeah, a Fourier transform is probably the correct starting point. It would "pull" the regularity of the image out of the fuzziness. Not sure how to handle partial rotation, though. Maybe you could transform stripes and note their phase, then determine if adjacent stripes were skewing left or right. $\endgroup$ Jul 9, 2012 at 0:41

2 Answers 2


A fourier transform will give you the period of any periodic features in the image - eg it will tell you there is structure event 3pixels horizontally and 5 pixels vertically, from this and the pixel scale you can calculate the number of yarns


Some code in Mathematica:

i = ColorNegate@Import@"https://i.stack.imgur.com/Jlhgw.jpg";
i3 = DeleteSmallComponents[Binarize[i, .4], 10];
lines = ImageLines[i3, .6];
Show[i, Graphics[{Thick, Orange, Line /@ lines}]]
(*y coord mean increments at x=0 *)
b = Mean@Differences@ Sort[(#[[2, 1]] #[[1, 2]] - #[[1, 1]] #[[2, 2]])/(#[[1,1]] - #[[2, 1]]) & /@ lines];
(*mean slope*)
a = Mean[-(-#[[1, 2]] + #[[2, 2]])/(#[[1, 1]] - #[[2, 1]]) & /@ lines];
- a ImageDimensions[i3][[1]]/b 
2 ImageDimensions[i3][[2]]/b


enter image description here


  • 1
    $\begingroup$ It would be nice if you explain what you are doing in this code. $\endgroup$ Jul 16, 2012 at 14:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.