Main Content

graydiffweight

Calculate weights for image pixels based on grayscale intensity difference

Description

W = graydiffweight(I,refGrayVal) calculates the pixel weights W for all pixels in the 2-D or 3-D grayscale image I using a single reference grayscale intensity, refGrayVal. Pick a reference grayscale intensity value that is representative of an object that you want to segment.

The weight of a pixel is inversely related to the absolute value of the difference between the intensity of the pixel and the reference grayscale intensity. If the difference is small (intensity value close to refGrayVal), the weight value is large. If the difference is large (intensity value very different from refGrayVal), the weight value is small.

You can use the weights returned by the graydiffweight function for image or volume segmentation using the imsegfmm function. For more information, see More About.

W = graydiffweight(I,mask) calculates the pixel weights, where the reference grayscale intensity value is the average of the intensity values of all the pixels in I that are marked as logical true in mask. Using the average of several pixels to calculate the reference grayscale intensity value can be more effective than using a single reference intensity value, as in the previous syntax.

W = graydiffweight(I,C,R) calculates the pixel weights using a reference value that is averaged from selected pixels in the 2-D grayscale image, I. The selected pixels have column and row indices C and R.

W = graydiffweight(I,C,R,P) calculates the voxel weights using a reference value that is averaged from selected voxels in the 3-D grayscale volumetric image, I. The selected voxels have column, row, and plane indices C, R, and P.

W = graydiffweight(___,Name=Value) uses additional name-value arguments to control aspects of the weight calculation.

example

Examples

collapse all

This example segments an object in an image using Fast Marching Method using grayscale intensity difference weights calculated from the intensity values at the seed locations.

Read image and display it.

I = imread('cameraman.tif');
imshow(I)
title('Original Image')

Figure contains an axes object. The hidden axes object with title Original Image contains an object of type image.

Specify row and column index of pixels for use a reference grayscale intensity value.

seedpointR = 159;
seedpointC = 67;

Calculate the grayscale intensity difference weight array for the image and display it. The example does log-scaling of W for better visualization.

W = graydiffweight(I, seedpointC, seedpointR,'GrayDifferenceCutoff',25);
figure, imshow(log(W),[])

Figure contains an axes object. The hidden axes object contains an object of type image.

Segment the image using the grayscale intensity difference weight array. Specify the same seed point vectors you used to create the weight array.

thresh = 0.01;
BW = imsegfmm(W, seedpointC, seedpointR, thresh);
figure, imshow(BW)
title('Segmented Image')

Figure contains an axes object. The hidden axes object with title Segmented Image contains an object of type image.

Input Arguments

collapse all

Grayscale image or volume, specified as a 2-D numeric matrix or 3-D numeric array, respectively.

Data Types: single | double | int8 | uint8 | int16 | uint16 | int32 | uint32

Reference grayscale intensity value, specified as a numeric scalar.

Data Types: double

Reference grayscale intensity mask, specified as a logical array of the same size as I.

Data Types: logical

Column index of reference pixels (or voxels), specified as a vector of positive integers.

Data Types: double

Row index of reference pixels (or voxels), specified as a vector of positive integers.

Data Types: double

Plane index of reference voxels, specified as a vector of positive integers.

Data Types: double

Name-Value Arguments

collapse all

Specify optional pairs of arguments as Name1=Value1,...,NameN=ValueN, where Name is 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.

Example: W = graydiffweight(I,seedC,seedR,GrayDifferenceCutoff=25) specifies the threshold for absolute grayscale intensity difference values as 25.

Before R2021a, use commas to separate each name and value, and enclose Name in quotes.

Example: W = graydiffweight(I,seedpointC,seedpointR,"GrayDifferenceCutoff",25);

Output weight roll-off factor, specified as a positive number. The weight roll-off factor controls how fast the output weight decreases as the function of the absolute difference between an intensity value and the reference grayscale intensity. The suggested range for this argument is [0.5 4].

When viewed as a 2-D plot, pixel intensity values can 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 regions of change intensity. If you specify a low value, the output weight has a more gradual fall-off around the regions of changing intensity.

Data Types: double

Threshold for absolute grayscale intensity difference values, specified as a nonnegative number. The default value of this argument is Inf, which means that there is no hard cutoff.

When you apply the threshold, you strongly suppress the weights of pixels whose intensity is much larger or much smaller than the reference value. graydiffweight assigns these pixels the smallest weight value, 1. When the output weight array W is used for Fast Marching Method based segmentation (as input to imsegfmm), this argument can be useful in improving the accuracy of the segmentation output.

Example: 25 assigns the smallest weight to all pixels whose absolute difference with the reference intensity is greater than 25.

Data Types: double

Output Arguments

collapse all

Weight array, specified as numeric array of the same size as the input image I or volume V. W is of data type double, unless the input image or volume is of data type single, in which case W is of data type single.

More About

collapse all

Version History

Introduced in R2014b