1
$\begingroup$

Running the exact same lines of code in a function produces just a return of the original image without any points identified, whereas when I run the exact body of the function line by line it works consistently. Is something wrong with my function?

points[img_] := (src = ColorConvert[img, "Grayscale"];
white = Closing[src, DiskMatrix[5]];
srcAdjusted = Image[ImageData[src]/ImageData[white]]; 
components = 
ComponentMeasurements[
 ColorNegate@Binarize[srcAdjusted], {"ConvexArea", "Mask"}][[All, 
2]];
largestComponent = Image[SortBy[components, First][[-1, 2]]];
mask = FillingTransform[Closing[largestComponent, 2]];
lY = ImageMultiply[
MorphologicalBinarize[
 GaussianFilter[srcAdjusted, 3, {2, 0}], {0.02, 0.05}], mask];
lX = ImageMultiply[
MorphologicalBinarize[
 GaussianFilter[srcAdjusted, 3, {0, 2}], {0.02, 0.05}], mask]; 
verticalGridLineMasks = 
SortBy[ComponentMeasurements[
   lX, {"CaliperLength", "Centroid", "Mask"}, # > 100 &][[All, 
  2]], #[[2, 1]] &][[All, 3]];
horizontalGridLineMasks = 
SortBy[ComponentMeasurements[
   lY, {"CaliperLength", "Centroid", "Mask"}, # > 100 &][[All, 
  2]], #[[2, 2]] &][[All, 3]];
centerOfGravity[l_] := 
 ComponentMeasurements[Image[l], "Centroid"][[1, 2]]
gridCenters = 
 Table[centerOfGravity[
   ImageData[Dilation[Image[h], DiskMatrix[2]]]*
    ImageData[Dilation[Image[v], DiskMatrix[2]]]], {h, 
   horizontalGridLineMasks}, {v, verticalGridLineMasks}];
Show[src, 
 Graphics[{Red, 
 MapIndexed[{Point[#1], Text[#2, #1, {1, 1}]} &, 
  gridCenters, {2}]}]])

Desired output:

enter image description here

$\endgroup$
2
  • $\begingroup$ Post the actual code by editing the question copying and pasting it into the question. Use the code button {} to format it. Also use the image button to upload the image to the question. No one is going to retype your code so you need to provide it for copy and paste. $\endgroup$
    – Edmund
    Commented Jan 31, 2016 at 16:22
  • 1
    $\begingroup$ You need a semicolon after ComponentMeasurements and before gridcenters. Also, it doesn't make a lot of sense to define the centerofGravity function inside the points function. $\endgroup$
    – bill s
    Commented Jan 31, 2016 at 18:12

0

Your Answer

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

Browse other questions tagged or ask your own question.