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 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 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 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 lang-mma

