Main Content

imblend

Blend two images

Since R2024b

    Description

    I = imblend(fg,bg) blends the foreground image fg with the background image bg using alpha blending.

    example

    I = imblend(fg,bg,mask) blends only the region of the foreground image specified by mask on the background image.

    example

    I = imblend(___,Name=Value) fine-tunes the image blending using one or more optional name-value arguments, in addition to any combination of input arguments from previous syntaxes. For example, Mode="Guided" specifies to use the guided mode for image blending.

    example

    Examples

    collapse all

    Load an image into the workspace as the background image.

    bg = imread("cameraman.tif");
    figure
    imshow(bg)

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

    Create a foreground image of a digital object to blend, as augmented reality, with the cameraman scene.

    fg = ones(size(bg));
    fg(200:225,200:225) = 0;
    figure
    imshow(fg)

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

    Blend the foreground image with the background image using alpha blending. Visualize the blended image.

    I = imblend(fg,bg);
    figure
    imshow(I)

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

    To blend only the digital object in the image, create a mask by segmenting the object in the foreground image. Then, perform alpha blending of the foreground and background images with the specified mask and visualize the blended image.

    thresh = graythresh(fg);
    mask = fg <= thresh;
    figure
    imshow(mask)

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

    I = imblend(fg,bg,mask);
    figure
    imshow(I)

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

    Load an image into the workspace as the background image.

    bg = imread("peppers.png");
    figure
    imshow(bg)

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

    Create a foreground image of a digital object to blend, as augmented reality, with the natural scene.

    fg = ones(size(bg,1),size(bg,2));
    fg(200:225,200:225) = 0;
    figure
    imshow(fg)

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

    Create a mask by segmenting the object in the foreground image.

    thresh = graythresh(fg);
    mask = fg <= thresh;

    Blend the foreground image on the background image using the mask and different blend modes. Visualize and compare the blended images from the different blend modes.

    • Mode Alpha creates a partial transparency effect.

    • Mode Guided changes the trnasition region between the foreground and background images.

    • Mode Min retains the darkest areas in the foreground and background images.

    • Mode Max retains the brightest areas in the foreground and background images.

    • Mode Average averages the foreground and background images.

    • Mode Overlay retains the highlights and shadows of the background image while integrating the textures of the foreground image.

    I1 = imblend(fg,bg,mask,Mode="Alpha",ForegroundOpacity=0.6);
    I2 = imblend(fg,bg,mask,Mode="Guided",FilterSize=[5 5]);
    I3 = imblend(fg,bg,mask,Mode="Min");
    I4 = imblend(fg,bg,mask,Mode="Max");
    I5 = imblend(fg,bg,mask,Mode="Average");
    I6 = imblend(fg,bg,mask,Mode="Overlay");
    figure
    tiledlayout(2,3)
    nexttile
    imshow(I1)
    title("Mode = Alpha")
    nexttile
    imshow(I2)
    title("Mode = Guided")
    nexttile
    imshow(I3)
    title("Mode = Min")
    nexttile
    imshow(I4)
    title("Mode = Max")
    nexttile
    imshow(I5)
    title("Mode = Average")
    nexttile
    imshow(I6)
    title("Mode = Overlay")

    Figure contains 6 axes objects. Hidden axes object 1 with title Mode = Alpha contains an object of type image. Hidden axes object 2 with title Mode = Guided contains an object of type image. Hidden axes object 3 with title Mode = Min contains an object of type image. Hidden axes object 4 with title Mode = Max contains an object of type image. Hidden axes object 5 with title Mode = Average contains an object of type image. Hidden axes object 6 with title Mode = Overlay contains an object of type image.

    Input Arguments

    collapse all

    Foreground image, specified as a 2-D grayscale image or 2-D RGB image. The size of the foreground image can be different from the size of the background image. The function uses the first minRows rows and the first minCols columns of the foreground image for blending, where minRows is the minimum of the number of rows between the foreground and background images, and minCols is the minimum of the number of columns between the foreground and background images.

    Data Types: single | double | int16 | uint8 | uint16

    Background image, specified as a 2-D grayscale image or 2-D RGB image.

    Data Types: single | double | int16 | uint8 | uint16

    Mask of foreground pixels to blend, specified as a logical 1 (true) or 0 (false), or a logical matrix of the same size as the foreground image. If mask is a scalar with a value of true, all pixels of the foreground image. are included in the mask.

    Data Types: logical

    Name-Value Arguments

    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: imblend(F,B,Location=[10 12]) specifies that the blending of the foreground image must start from the 10th row and 12th column of the background image.

    Blend mode, specified as "Alpha", "Guided", "Min", "Max", "Average", or "Overlay".

    Blend ModeAlgorithmOutputApplications
    "Alpha"
    • Linearly combines foreground and background images.

    • Uses foreground opacity to determine weights of linear combination.

    • Creates partial transparency effect.

    Image Blending Mode Alpha

    You can use this mode in applications such as augmented reality, computer-generated graphics, and multimodal medical image visualization.
    "Guided"
    • Linearly combines foreground and background images.

    • Smooths edges of the mask using the foreground image as guidance.

    • Uses smooth mask to determine weights of linear combination.

    • Retains foreground image in mask region and background image in non-mask region with a smooth transition at the mask edges.

    Image Blending Mode Guided

    You can use this mode in applications such as augmented reality and computer-generated graphics.
    "Min"
    • Uses minimum intensity projection.

    • Blended image retains the minimum of the intensities of the foreground and background images.

    • Retains the darkest areas in the foreground and background images.

    Image Blending Mode Min

    You can use this mode in applications such as astrophotography, for noise reduction, or in security applications to enhance details in dark regions.
    "Max"
    • Uses maximum intensity projection.

    • Blended image retains the maximum of the intensities of the foreground and background images.

    • Retains the brightest areas in the foreground and background images.

    Image Blending Mode Max

    You can use this mode in applications such as medical imaging to emphasize certain features, or in surveillance to detect motion or changes in a series of images.
    "Average"
    • Blended image retains the average of the intensities of the foreground and background images.

    Image Blending Mode Average

    You can use this mode in applications such as noise reduction in photography, where averaging multiple shots of the same scene can reduce random noise.
    "Overlay"
    • Combines two images by either multiplying or screening pixels, depending on their brightness.

    • Retains the highlights and shadows of the background image while integrating the textures of the foreground image.

    Image Blending Mode Overlay

    You can use this mode in applications such as digital art and photo editing to add textures, adjust lighting, or create special effects by blending textures or patterns with original photographs.

    Data Types: char | string

    Starting location of blending in the background image, specified as a 1-by-2 numeric vector. The first and second elements specify the x-coordinate (column index) and y-coordinate (row index), respectively, of the location in the background image from where the top-left corner of the foreground image is blended. The row and column indices of the starting location can be zero, negative, or even fractional. The function determines the mapping between the pixels of the background and foreground images for blending by using image translation and interpolation.

    Data Types: single | double

    Opacity of the foreground in alpha blending, specified as a positive scalar less than or equal to 1. Decreasing the foreground opacity increases the partial transparency effect in the blended image.

    Note

    Specify ForegroundOpacity only when the Mode is "Alpha".

    Data Types: single | double

    Size of the filter in guided blending, specified as a 2-element vector of positive integers. Increasing the filter size increases the smoothing of the edges of the mask.

    Note

    Specify FilterSize only when the Mode is "Guided".

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

    Output Arguments

    collapse all

    Blended image, returned as a 2-D grayscale or RGB image with the same size and data type as the background image.

    Version History

    Introduced in R2024b