Calculate weights for image pixels based on image gradient
calculates the pixel weight for each pixel in image
W = gradientweight(
I based on the gradient magnitude at
that pixel, and returns the weight array
weight of a pixel is inversely related to the gradient values at the
pixel location. Pixels with small gradient magnitude (smooth
regions) have a large weight and pixels with large gradient
magnitude (such as on the edges) have a small weight.
the weight array
W = gradientweight(___,
W using name-value pairs to
control aspects of weight computation.
Segment Image Using Weights Derived from Image Gradient
This example segments an image using the Fast Marching Method based on the weights derived from the image gradient.
Read image and display it.
I = imread('coins.png'); imshow(I) title('Original Image')
Compute weights based on image gradient.
sigma = 1.5; W = gradientweight(I, sigma, 'RolloffFactor', 3, 'WeightCutoff', 0.25);
Select a seed location.
R = 70; C = 216; hold on; plot(C, R, 'r.', 'LineWidth', 1.5, 'MarkerSize',15); title('Original Image with Seed Location')
Segment the image using the weight array.
thresh = 0.1; [BW, D] = imsegfmm(W, C, R, thresh); figure, imshow(BW) title('Segmented Image') hold on; plot(C, R, 'r.', 'LineWidth', 1.5, 'MarkerSize',15);
Geodesic distance matrix
D can be thresholded using different thresholds to get different segmentation results.
figure, imshow(D) title('Geodesic Distances') hold on; plot(C, R, 'r.', 'LineWidth', 1.5, 'MarkerSize',15);
I — Grayscale image
Grayscale image, specified as a numeric matrix.
sigma — Standard deviation for derivative of Gaussian
1.5 (default) | positive number
Standard deviation for derivative of Gaussian, specified as a positive number.
Specify optional pairs of arguments as
the argument name and
Value is the corresponding value.
Name-value arguments must appear after other arguments, but the order of the
pairs does not matter.
Before R2021a, use commas to separate each name and value, and enclose
Name in quotes.
W = gradientweight(I,1.5,'RolloffFactor',3,'WeightCutoff',0.25);
RolloffFactor — Output weight roll-off factor
3 (default) | positive scalar
Output weight roll-off factor, specified as the comma-separated
pair consisting of
'RolloffFactor' and a positive
scalar of class
double. Controls how fast weight
values fall as a function of gradient magnitude. When viewed as a
2-D plot, pixel intensity values might vary gradually at the edges
of regions, creating a gentle slope. In your segmented image, you
might want the edge to be more well-defined. Using the roll-off factor,
you control the slope of the weight value curve at points where intensity
values start to change. If you specify a high value, the output weight
values fall off sharply around the edges of smooth regions. If you
specify a low value, the output weight has a more gradual fall-off
around the edges. The suggested range for this parameter is
WeightCutoff — Threshold for weight values
0.25 (default) | positive number in the range
Threshold for weight values, specified as the comma-separated pair consisting of
'WeightCutoff' and a positive
number in the range
If you use this parameter to set a threshold on
weight values, it suppresses any weight values
less than the value you specify, setting these
pixels to a small constant value (1e-3). This
parameter can be useful in improving the accuracy
of the output when you use the output weight array
W as input to Fast Marching
Method segmentation function,
W — Weight array
Weight array, returned as a numeric array of the same size as the input image,
I. The weight array is of class
in which case it is of class
gradientweightuses double-precision floating point operations for internal computations for all classes of
I, except when
Iis of class
single, in which case
gradientweightuses single-precision floating point operations internally.
Introduced in R2014b