">

How to Create Masks from Variable-Thickness Freehand Curves in MATLAB

MATLABSolutions. Oct 11 2025 · 7 min read
How to Create Masks from Variable-Thickness Freehand Curves

 

Working with images in MATLAB often requires creating masks — especially when you want to focus on specific regions of an image. But what if your region isn’t a neat rectangle or circle? What if you have a freehand curve with varying thickness? That’s where things get interesting.

In this guide, we’ll walk you through how to create smooth binary masks from open, freehand curves with variable thickness in MATLAB. By the end, you’ll know how to handle open curves, adjust thickness along the curve, and generate masks suitable for image processing and computer vision tasks.


Why Masks Are Important

Masks are essential in image processing because they let you isolate parts of an image for analysis or manipulation. Whether you’re doing object detection, image segmentation, or data annotation, having an accurate mask can make all the difference.

For freehand curves, the challenge is that they are usually open shapes and may have variable thickness along their path. A simple poly2mask on a closed shape won’t work here — you need a method that can handle irregular, flowing lines.


Step-by-Step Approach

Here’s a practical way to create a mask from a variable-thickness freehand curve:

  1. Get your curve points: Capture the (x, y) coordinates of your freehand curve. If your curve has varying thickness, keep an array of thickness values corresponding to each point.

  2. Apply thickness to the curve: Imagine each point as the center of a circle (or disk) whose radius is the local thickness. By overlapping these disks along the curve, you create a continuous region.

  3. Generate the binary mask: Convert this filled region into a binary mask where pixels inside the curve are set to 1 (true) and pixels outside are 0 (false).

Tips for Better Results


 

Creating masks from variable-thickness freehand curves doesn’t have to be tricky. With these steps and a little MATLAB code, you can generate accurate masks for any open curve, ready for image analysis, object detection, and computer vision projects.

 

Model Setup

% Freehand curve points

x = [50, 55, 60, 70, 80];

y = [100, 105, 110, 120, 130];

thickness = [5, 8, 12, 8, 5]; % variable thickness

 

% Create a blank mask

imgSize = [200, 200];

mask = false(imgSize);

 

% Add circular regions for each point along the curve

for i = 1:length(x)

    [X, Y] = meshgrid(1:imgSize(2), 1:imgSize(1));

    mask = mask | ((X - x(i)).^2 + (Y - y(i)).^2 <= thickness(i)^2);

end

 

imshow(mask)