Skip to main content

image processing - Measure perimeter of black edge in Mathematica


I have a pattern like the one below. How could I measure the total length of the perimeter of the black/white edge? This would give me a measure of spatial frequency. Any tips would be great!


Thanks!





Answer



Edit


You should be aware that there are 2 basic ways of counting perimeter pixels: 4-ways neighbors or 8-ways neighbors. The difference is about whether counting or not the yellow pixels in this figure:


Mathematica graphics


If you want to count them, then almost manually:


i = Import["http://i.stack.imgur.com/7jt0I.gif"]; 
mask = Complement[Partition[#, 3] & /@ ({0, 0, 0, 0, 1, 0, 0, 0, 0} -
RotateLeft[{1, 0, 0, 0, 0, 0, 0, 0, 0}, #] & /@ Range[9]), {Array[0 &, {3, 3}]}];
ip = ImagePad[i, 10, RGBColor[0.4980 {1, 1, 1}]];
is = Binarize@

ImageSubtract[ HitMissTransform[ColorConvert[ip, "Grayscale"], mask, .1],
ImageSubtract[HitMissTransform[ColorConvert[ip, "Grayscale"], mask, .1],
HitMissTransform[ColorConvert[ip, "Grayscale"], mask, .8]]]

ImageMeasurements[is, "Total"]



14715




If you don't want to count the diagonal neighbors, then we change the mask to consider only four neighbors per pixel:


i = Import["http://i.stack.imgur.com/7jt0I.gif"];
mask = {{{0, -1, 0}, {0, 1, 0}, {0, 0, 0}}, {{0, 0, 0}, {-1, 1, 0}, {0, 0, 0}},
{{0, 0, 0}, {0, 1, -1}, {0, 0, 0}}, {{0, 0, 0}, { 0, 1, 0}, {0,-1, 0}}};

ip = ImagePad[i, 10, RGBColor[0.4980 {1, 1, 1}]];
is = Binarize@
ImageSubtract[ HitMissTransform[ColorConvert[ip, "Grayscale"], mask, .1],
ImageSubtract[HitMissTransform[ColorConvert[ip, "Grayscale"], mask, .1],
HitMissTransform[ColorConvert[ip, "Grayscale"], mask, .8]]];


ImageMeasurements[is, "Total"]


10411



Comments