主要内容

本页翻译不是最新的。点击此处可查看最新英文版本。

blockedImageDatastore

用于 blockedImage 对象中的图像块的数据存储

自 R2021a 起

    说明

    blockedImageDatastore 对象管理属于一个或多个 blockedImage 对象的图像块集合。blockedImageDatastoreimageDatastore 类似,后者管理无关联图像的集合。

    创建对象

    描述

    bimds = blockedImageDatastore(images) 创建一个 blockedImageDatastore 对象,该对象管理一个或多个 blockedImage 对象的图像块的集合,即 images

    默认情况下,如果 images 包含一个具有多个分辨率级别的分块图像,则 blockedImageDatastore 仅选择最精细分辨率级别的块。默认情况下,数据存储的块大小等于 images 中最精细分辨率级别的第一个元素的 BlockSize 属性。

    示例

    bimds = blockedImageDatastore(sources)sources 指定的文件创建数据存储。

    bimds = blockedImageDatastore(___,Name=Value) 使用一个或多个名称-值参量设置可写属性。例如,blockedImageDatastore(images,ReadSize=8) 在每次调用 read 函数时从数据存储中返回八个图像块。

    输入参量

    全部展开

    blockedImageDatastore 提供图像块的分块图像,指定为由 blockedImage 对象组成的数组。images 的所有元素必须具有相同的维数和相同的数据类型。

    此参量设置 Images 属性。

    分块图像文件的名称,指定为字符向量元胞数组、字符串标量或 FileSet 对象。

    blockedImageDatastore 对象将文件中的图像转换为分块图像并将这些图像设置为 Images 属性。

    名称-值参数

    全部展开

    将可选的参量对组指定为 Name1=Value1,...,NameN=ValueN,其中 Name 是参量名称,Value 是对应的值。名称-值参量必须出现在其他参量后,但对各个参量对组的顺序没有要求。

    示例: bimds = blockedImageDatastore(images,ReadSize=8) 在每次调用 read 函数时从数据存储中返回八个图像块。

    要包含在数据存储中的图像块,指定为 blockLocationSet 对象。该对象指定要包含分块图像中的哪些图像块。您可以重复或忽略单个图像块。

    您仅可指定 BlockLocationSetBlockSize 名称-值参量之一。指定 BlockLocationSet 参量时,blockedImageDatastore 函数会:

    • BlockLocationSet 属性设置为 blockLocationSet 对象。

    • BlockSize 属性设置为 blockLocationSet 对象的图像块大小。

    如果您既不指定 BlockLocationSet 也不指定 BlockSize 输入参量,则 blockedImageDatastore 使用 BlockLocationSet 属性的默认值。默认值是调用未指定块大小的 selectBlockLocations 函数返回的 blockLocationSet 对象。

    图像块大小,指定为 1×D 数值向量。DImages 中处于 Levels 中第一个分辨率级别的第一个分块图像的维数。

    您仅可指定 BlockLocationSetBlockSize 名称-值参量之一。指定 BlockSize 参量时,blockedImageDatastore 函数会:

    如果您既不指定 BlockLocationSet 也不指定 BlockSize 名称-值参量,则 blockedImageDatastore 使用 BlockSize 属性的默认值。默认值为 Images 中处于最精细分辨率级别的第一个分块图像的块大小。

    每个维度中其他图像块边框元素的大小,指定为由非负整数组成的 1×D 向量。DImages 中处于 Levels 中的第一个分辨率级别的第一个分块图像的维数。默认值为 zeros(1,D)

    此参量设置 BorderSize 属性。

    用于填充不完整图像块的方法,指定为下表中的值之一。默认情况下,数据存储使用 Images 中第一个分块图像的 InitialValue 属性的值填充数值类型的图像块。

    意义

    数值、逻辑或分类标量

    使用指定值的元素填充数组。PadMethod 的数据类型必须与分块图像的 ClassUnderlying 属性匹配。

    "replicate"

    通过重复块的边界元素进行填充。

    "symmetric" (自 R2023a 起)

    使用同一图像块内的像素的镜面反射进行填充。

    此参量设置 PadMethod 属性。

    填充位于边缘的部分图像块,指定为逻辑标量 truefalse。当为 true 时,分块图像数据存储根据 PadMethod 名称-值参量中指定的填充方法添加填充。

    此参量设置 PadPartialBlocks 属性。

    每次调用 read 函数时返回的图像块数,指定为正整数。每次调用 read 函数时最多读取 ReadSize 个图像块。

    此参量设置 ReadSize 属性。

    属性

    全部展开

    此属性在对象创建后为只读属性。

    数据存储中要包含的块,以 blockLocationSet 对象形式返回。该对象指定要包含分块图像 Images 中的哪些块。

    此属性在对象创建后为只读属性。

    图像块大小,以 1×D 数值向量形式返回。DImages 中处于 Levels 中第一个分辨率级别的第一个分块图像的维数。

    每个维度中其他图像块边框元素的大小,指定为由非负整数组成的 1×D 向量。DImages 中处于 Levels 中的第一个分辨率级别的第一个分块图像的维数。默认值为 zeros(1,D)

    此属性在对象创建后为只读属性。

    blockedImageDatastore 提供块的分块图像,以 blockedImage 对象数组形式返回。Images 的所有元素具有相同的维数和相同的数据类型。

    用于填充不完整图像块的方法,指定为下表中的值之一。默认情况下,数据存储使用 Images 中第一个分块图像的 InitialValue 属性的值填充数值类型的图像块。

    意义

    数值、逻辑或分类标量

    使用指定值的元素填充数组。PadMethod 的数据类型必须与分块图像的 ClassUnderlying 属性匹配。

    "replicate"

    通过重复块的边界元素进行填充。

    "symmetric" (自 R2023a 起)

    使用同一图像块内的像素的镜面反射进行填充。

    填充位于边缘的部分图像块,指定为逻辑标量 truefalse。当为 true 时,分块图像数据存储根据 PadMethod 属性中指定的填充方法添加填充。

    每次调用 read 函数时返回的图像块数,指定为正整数。每次调用 read 函数时最多读取 ReadSize 个块

    此 属性 为只读。

    可用块的总数,以数值标量形式返回。

    对象函数

    combine合并来自多个数据存储的数据
    countEachLabelCounts number of pixel labels for each class
    hasdataReturns true if more data is available in blocked image datastore
    numpartitions数据存储分区数
    partitionReturn partitioned part of blocked image datastore
    preview预览数据存储中的数据子集
    readRead data and metadata from blocked image datastore
    readallRead all data from blocked image datastore
    reset将数据存储重置为初始状态
    shuffleShuffle data in datastore
    subset创建数据存储或 FileSet 的子集
    transform变换数据存储
    writeallWrite blocked image datastore to files

    示例

    全部折叠

    创建一个分块图像。

    bim = blockedImage("tumor_091R.tif");

    创建一个数据存储,指定分辨率级别和图像块大小。

    bls = selectBlockLocations(bim,Levels=2,BlockSize=[512 512]);
    bimds = blockedImageDatastore(bim,BlockLocationSet=bls);

    读取该数据存储中的所有图像块。

    b = readall(bimds)
    b=9×1 cell array
        {512×512×3 uint8}
        {512×512×3 uint8}
        {512×512×3 uint8}
        {512×512×3 uint8}
        {512×512×3 uint8}
        {512×512×3 uint8}
        {512×512×3 uint8}
        {512×512×3 uint8}
        {512×512×3 uint8}
    
    

    显示分块图像。

    montage(b)

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

    创建一个 FileSet 对象,其中包含 PNG 文件格式的多个图像文件。

    fs = matlab.io.datastore.FileSet( ...
         fullfile(matlabroot,"toolbox","images","imdata"), ...
        "FileExtensions",".png");

    创建一个 blockedImage 对象,指定适配器。这种方式无需检查每个文件来选择合适的适配器,从而节省时间。

    readAdapter = images.blocked.GenericImage;
    bims = blockedImage(fs,"Adapter",readAdapter);

    所有图像必须具有相同维数,因此仅保留 RGB 图像。

    bims = bims([bims.NumDimensions]==3);
    bimds = blockedImageDatastore(bims,"BlockSize",[300 500], ...
         "PadMethod","replicate");

    显示 blockedImageDatastore 中的所有图像块。

    montage(readall(bimds),"Border",2,"BackgroundColor","w");

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

    创建一个分块图像。

    bim = blockedImage("tumor_091R.tif");

    指定重叠图像块。

    blockSize = [512 512];
    overlapPct = 0.5;
    blockOffsets = round(blockSize.*overlapPct);
    bls = selectBlockLocations(bim,BlockSize=blockSize, ...
          BlockOffSets=blockOffsets,ExcludeIncompleteBlocks=true);

    创建分块图像数据存储。

    bimds = blockedImageDatastore(bim,BlockLocationSet=bls);

    显示重叠数据块。

    bimds.ReadSize = 6;
    blocks = read(bimds);
    montage(blocks, BorderSize=5,BackgroundColor="b");

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

    创建一个 blockedImage。

    bim = blockedImage('tumor_091R.tif');

    创建一个最粗略级别的掩膜。

    bmask = apply(bim, @(bs)~imbinarize(im2gray(bs.Data)),"Level",3);

    为掩膜定义的染色区域中至少 90% 像素为“on”的图像块创建一个 blockedImageDatastore

    mbls = selectBlockLocations(bim,...
         'Levels', 1, ...
         'Masks', bmask, 'InclusionThreshold', 0.90,...
         'BlockSize', [256 256]);
    bimds = blockedImageDatastore(bim, 'BlockLocationSet', mbls);

    读取并显示这些图像块。

    bimds.ReadSize = 5;
    blocks = read(bimds);
    montage(blocks, "BorderSize", 5, "BackgroundColor", 'b')

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

    从数值数据和标注数据创建分块图像。

    bim = blockedImage("yellowlily.jpg",BlockSize=[512 512]);
    bimLabels = blockedImage("yellowlily-segmented.png",BlockSize=[512 512]);

    为每个分块图像创建 blockedImageDatastore 对象。

    bimds = blockedImageDatastore(bim);
    bimdsLabels = blockedImageDatastore(bimLabels);

    将标注的数值数据变换为分类数据。

    classes = ["Unknown","Flower","Leaf","Background"];
    classIDs = [0 1 2 3];
    bimdsCategorical = transform(bimdsLabels, ...
          @(bs){categorical(bs{1},classIDs,classes)});

    将原始 blockedImageDatastore 与分类数据存储进行组合。

    bimdsCombined = combine(bimds,bimdsCategorical);  

    从组合的数据存储中读取并显示数据。第一个元胞是图像数据,第二个元胞是分类标签。

    data = read(bimdsCombined)
    data=1×2 cell array
        {512×512×3 uint8}    {512×512 categorical}
    
    
    imshow(labeloverlay(data{1},data{2}));  

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

    版本历史记录

    在 R2021a 中推出

    全部展开