Main Content


Put data in specific block of blocked image

Since R2021a



setBlock(bim,blocksub,blockdata) sets the block content blockdata at the specified block subscript location, blocksub in the blockedImage object bim.

setBlock(bim,blocksub,blockdata,'Level',L) sets the block content at the L'th level of a multiresolution blockedImage. By default, L is 1.


collapse all

Create a blocked image to which you can write data. You specify the format of the blocked image in the destination parameter. To write to memory, specify an empty matrix. You must also specify the size of the image and the size of the blocks into which you want the image chunked. The initial value parameter depends on the format you specified in destination. To create a writable blocked image, specify the 'Mode' parameter with the value 'w' for write mode.

destination = [];
imgsize = [5 7];
blocksize = [2 2];
initval = uint8(0);
bim = blockedImage(destination,imgsize,blocksize,initval, "Mode", 'w');

Write data to the specified blocks in the blocked image by using the setBlock object function. The blocksubs parameter specifies the coordinates of the block to which you want to write data. The blockdata parameter specifies the data to write to the specified block. The size of blockdata must match the block size.

blocksubs = [1 1];
blockdata = ones(2,2,"uint8");
setBlock(bim, blocksubs, blockdata)

Close the image for writing.

Switch the blocked image to read mode by setting the 'Mode' parameter to 'r' for read.

bim.Mode = 'r'
bim = 

  blockedImage with properties:

   Read only properties
             Source: "    {5x7 uint8}..."
            Adapter: [1x1 images.blocked.InMemory]
               Size: [5 7]
       SizeInBlocks: [3 4]
    ClassUnderlying: "uint8"

   Settable properties
          BlockSize: [2 2]

Create the full image by using the gather function to collect all the individual blocks.

fullImage = gather(bim);

Display details of the blocked image at the command line.

   1   1   0   0   0   0   0
   1   1   0   0   0   0   0
   0   0   0   0   0   0   0
   0   0   0   0   0   0   0
   0   0   0   0   0   0   0

Create a blocked image.

bim = blockedImage('tumor_091R.tif');

Display the blocked image and draw a circular ROI on the image.

h = bigimageshow(bim);
hROI = drawcircle(gca, 'Radius', 470, 'Position', [1477 2284]);

Specify the resolution level at which to create the mask.

maskLevel = 3;

Create a writable blocked image in memory.

bmask = blockedImage([], [200 200], bim.Size(maskLevel,1:2), false, "Mode", "w");

Specify the start and ending points for the mask.

bmask.WorldStart = bim.WorldStart(maskLevel, 1:2);
bmask.WorldEnd = bim.WorldEnd(maskLevel, 1:2);

Display the number of blocks.

     1     1

Convert the ROI coordinates to pixel level.

roiPositionsRC = fliplr(hROI.Vertices); % x,y to row,column
roiPosSub = world2sub(bmask, roiPositionsRC, "level", 1);

for cSub = 1:bmask.SizeInBlocks(2)
    for rSub = 1:bmask.SizeInBlocks(1)
        blockSub = [rSub, cSub];
        [pStart, pEnd] = blocksub2sub(bmask, blockSub, "Level", 1);
        % Create a grid encompassing all pixels in the block in X-Y order
        [xgrid,ygrid] = meshgrid(pStart(2):pEnd(2), pStart(1):pEnd(1));
        % Create in/out mask for this block
        tileMask = inpolygon(xgrid, ygrid,...
                roiPosSub(:,2), roiPosSub(:,1));
        % Write out the block
        setBlock(bmask, blockSub, tileMask);

Switch the blocked image to read mode.

bmask.Mode = 'r';

Display the mask.


Input Arguments

collapse all

Blocked image, specified as a blockedImage object.

Block subscript vector, specified as a 1-by-N integer-valued block subscript vector. Valid elements range from 1 to the corresponding element in SizeInBlocks property.

Example: [3, 2, 1]

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

Block of data, specified as a numeric array with dimensions that match BlockSize. The type matches the type specified by the ClassUnderlying property. setBlock automatically trims blocks along the edges to fit the Size property.

Version History

Introduced in R2021a