Skip to main content
deleted 2 characters in body
Source Link
Ali Hashmi
  • 9.1k
  • 4
  • 23
  • 44
Module[{seg, img = img, cm, newComps, func}, seg = MorphologicalComponents[img]; cm = ComponentMeasurements[{seg, ColorNegate@img}, {"MaskedImage","BoundingBox"}]; newComps = ImageAdjust /@ DistanceTransform /@ ColorNegate /@ cm[[;; , 2, 1]]; func[matrix_, {ind_, imgs_}] := Block[{mat = matrix, cellindpos, smallImgData, vals}, cellindpos = Position[seg, ind]; smallImgData = ImageData@imgs; vals = Extract[#, Position[#, x_ /; x != 0]] &@smallImgData ; ReplacePart[mat, Thread[cellindpos -> vals]] ]; Fold[func, ConstantArray[0, Reverse@ImageDimensions@img], Thread[{Range@Length@newComps, newComps}]]] // Image 

enter image description hereenter image description here

Module[{seg, img = img, cm, newComps, func}, seg = MorphologicalComponents[img]; cm = ComponentMeasurements[{seg, ColorNegate@img}, {"MaskedImage","BoundingBox"}]; newComps = ImageAdjust /@ DistanceTransform /@ ColorNegate /@ cm[[;; , 2, 1]]; func[matrix_, {ind_, imgs_}] := Block[{mat = matrix, cellindpos, smallImgData, vals}, cellindpos = Position[seg, ind]; smallImgData = ImageData@imgs; vals = Extract[#, Position[#, x_ /; x != 0]] &@smallImgData ; ReplacePart[mat, Thread[cellindpos -> vals]] ]; Fold[func, ConstantArray[0, Reverse@ImageDimensions@img], Thread[{Range@Length@newComps, newComps}]]] // Image 

enter image description here

Module[{seg, img = img, cm, newComps, func}, seg = MorphologicalComponents[img]; cm = ComponentMeasurements[{seg, ColorNegate@img}, {"MaskedImage","BoundingBox"}]; newComps = ImageAdjust /@ DistanceTransform /@ ColorNegate /@ cm[[;; , 2, 1]]; func[matrix_, {ind_, imgs_}] := Block[{mat = matrix, cellindpos, smallImgData, vals}, cellindpos = Position[seg, ind]; smallImgData = ImageData@imgs; vals = Extract[#, Position[#, x_ /; x != 0]] &@smallImgData ; ReplacePart[mat, Thread[cellindpos -> vals]] ]; Fold[func, ConstantArray[0, Reverse@ImageDimensions@img], Thread[{Range@Length@newComps, newComps}]]] // Image 

enter image description here

Source Link
Ali Hashmi
  • 9.1k
  • 4
  • 23
  • 44

Module[{seg, img = img, cm, newComps, func}, seg = MorphologicalComponents[img]; cm = ComponentMeasurements[{seg, ColorNegate@img}, {"MaskedImage","BoundingBox"}]; newComps = ImageAdjust /@ DistanceTransform /@ ColorNegate /@ cm[[;; , 2, 1]]; func[matrix_, {ind_, imgs_}] := Block[{mat = matrix, cellindpos, smallImgData, vals}, cellindpos = Position[seg, ind]; smallImgData = ImageData@imgs; vals = Extract[#, Position[#, x_ /; x != 0]] &@smallImgData ; ReplacePart[mat, Thread[cellindpos -> vals]] ]; Fold[func, ConstantArray[0, Reverse@ImageDimensions@img], Thread[{Range@Length@newComps, newComps}]]] // Image 

enter image description here