I'm guessing that you generally like the red shape (rather than the black outline shape) but you want the ends of the red pieces to be connected to the closest other piece so that the curve/boundary is closed. So you want "edge linking" (Google it).
One way is to
- skeletonize the image with binaryImage = bwmorph(binaryImage, 'skel', inf)
- find endpoints with binaryImage = bwmorph(binaryImage, 'endPoints')
- label the blobs with labeledImage = bwlabel(binaryImage).
- use a for loop to find the closest endpoints that are not on the same labeled blob
- connect these endpoints with imline (attached demos might help)
Now all your end points will have a line connecting them to the nearest other piece. Now you can simply use imfill to make the shape solid, then use bwboundaries to get a list of (x,y) coordinates around the entire shape.
binaryImage = imfill(binaryImage, 'holes');
boundaries = bwboundaries(binaryImage);
The only steps that are slightly more than trivial are steps 4 and 5 but I'm sure you're smart enough to handle it. If not, let me know. Steps 1-3 area all one liners that I've already given you.