This post is somewhat connected to my past post "how to get a deformable mesh from this binarized image" although not quite important for what I intend to ask here.
I have an image of an animal tissue and have done some segmentation procedure on the image to get a mask (kindly see below):
image of cell
mask after segmentation
Highlighted[img,imgmask]
shows that the segmentation is not good enough.
Since the segmentation is imperfect, I have devised a crude strategy to "delete edges" where incorrect segmentation occurs and furthermore, "add edges" where segmentation does not result in a boundary.
I have tweaked the code from my previous post and am posting it here so that both functions yield a mask at the end with an added or removed edge.
Edge Removal
(* function for removing edges *)
deleteEdgeSegments[origimage_, initmask_, selectedpts_,
color_: RGBColor[0.2, 0.5, 0.9]] :=
Module[{pts = selectedpts, original = origimage, mask = initmask },
pts = Map[FromDigits, pts, {2}];
mask = DeleteSmallComponents[ReplacePixelValue[mask, pts -> 0]];
If[False,Print@GraphicsRow[{HighlightImage[
HighlightImage[original, initmask], {color,
AbsolutePointSize[10], Point@pts}],
HighlightImage[original, mask]}, ImageSize -> Full]];
Return@mask;]
similarly the function for adding edges is given below:
Edge Addition
(* code for edge addition *)
interpPoints[selectedpts_, interpthresh_] := Module[{pts = selectedpts,
ptsToChange, positionReversal,reverselist, xrange, yrange, xs, ys,
ascendListOrder, interpolatedList, threshold = interpthresh},
pts = Map[FromDigits, pts, {2}];
pts = Partition[pts, 2, 1];
positionReversal = Position[pts, {{x_, _}, {x_, _}}];
reverselist = pts /. t : {{x_, _}, {x_, _}} :> Reverse /@ t;
ascendListOrder = Map[SortBy[#, First] &, reverselist];
interpolatedList = Map[Function[{x},
xs = x[[All, 1]];
ys = x[[All, 2]];
xrange = Range[Sequence @@ xs, threshold];
yrange = Subdivide[Sequence @@ ys, Length[xrange] - 1];
ptsToChange = Thread@{xrange, yrange}], ascendListOrder] // Round;
Flatten[MapAt[Map[Reverse, #] &, interpolatedList, positionReversal], 1]
]
Now I wish to implement a Controls related scheme to integrate the disjointed pieces of code together.
Specifically, I wish to import an image and its associated mask so that a user may perform an "Edge Deletion" and "Edge Addition" operations on the mask - with a list of points selected using MousePosition
from HighlightedImage[image,mask]
.
If the mask comes out to be incorrect/defective (checked by HighlightedImage[image,newmask]
) after a single operation (add or delete edge) then the user can undo the change on the mask i.e. the old mask stays. However, if the resulting mask is reasonable after the operation the user can replace the old mask
with the newmask
.
The user can do operations for as many times as necessary to get a mask
that fits correctly when superimposed on the image.
The question is how to implement such a Control
for the process. I have not worked with Controls before.
I am pasting a schematic that I have drawn to elucidate the process.