# Add a margin of desired thickness around the edge of an image

26 views (last 30 days)
Nina on 6 Feb 2013
I have drawn an ROI, made a mask of it, used convex hull and shaded this in. I then want to add a margin/border to this shape of a desired thickness that starts at the edge of the plotted xy convex hull coordinates (in pixels preferably). I am using DICOM images and want to draw around a lesion then add a safety margin to it for excision.
I've searched for a while to find a suitable function and the nearest I have gotten to a solution is to plot the convex hull xy points with a line thickness of however many pixels - this line starts to have 'cracks' in once it is thicker than a few mm and is centered on the plot coordinates rather than starting at the edge. Here is what I did from creating the convex hull:
%user enters desired margin thickess
Mar = inputdlg('Enter desired margin width in mm');
MAR =str2double(Mar);
%Some math to convert pixel to mm
n = MAR / pixel;
m = (n ./ pixel)*2;
%Fill convex hull and plot line with width m
[y, x] = find(bw);
k = convhull(x, y);
hold on
plot(x, y, 'c.');
plot(x(k), y(k), 'c', 'LineWidth', m);
hold off

Image Analyst on 6 Feb 2013
There is a bwconvhull() function you know. You don't have to use the inefficient method of find() and convhull().
I don't know what you want to do. Do you simply want to plot a thick line in the overlay? Or do you want to enlarge the ROI defined by your convex hull? If it's the latter, you could use imdilate() on the convex hull image to enlarge the blobs.

Nina on 6 Feb 2013
I solved my problem by using the bwdist function to get the distance (in pixels) from the edge of my roi, then filtered out which pixels I wouldn't like to include in my fill area:
Mar = inputdlg('Enter desired margin width in mm');
MAR =str2double(Mar);
n = MAR / pixel;
m = (n ./ pixel)*2;
D = bwdist(bw);
F = double(D<m);
[y, x] = find(F);
hold on
plot(x, y, 'c.')
hold off

Image Analyst on 6 Feb 2013
You're finding every pixel in the mask then plotting a dot there. That's strange. I don't know why you don't just use plot() to plot the outline, or use patch() to overlay a colored patch over the image.
Nina on 8 Feb 2013
I shall amend my code to do that. Thank Image Analyst!