I first processed the sample image from your question to create a logical mask (you already have an example of how to do that here).
Once you have this mask, there is a really easy way to generate the polygon you want using the bwtraceboundary
function from the Image Processing Toolbox. This will give you a set of pixel indices in order around the perimeter of your masked region:
[r, c] = find(mask, 1);
coords = bwtraceboundary(mask, [r c], 'N');
And we can visualize it like so:
imagesc(mask);
colormap([0.9 0.9 0.9; 0.6 0.6 0.6]);
axis equal
set(gca, 'XLim', [0.5 0.5+size(mask, 2)], 'YLim', [0.5 0.5+size(mask, 1)]);
hold on;
plot(coords(:, 2), coords(:, 1), 'r', 'LineWidth', 2);
plot(coords(1, 2), coords(1, 1), 'go', 'LineWidth', 2);
The coordinates for the red line are ordered starting from the green circle and moving clockwise around the perimeter pixels of the masked region.
If you would prefer to generate a boundary outline that follows along the perimeter pixel edges of the region instead of the perimeter pixel centers, you can use the solution from my answer to a related question. This will yield the following:
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…