setBlock
Description
Examples
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: " {5×7 uint8}↵"
Adapter: [1×1 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.
disp(fullImage)
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.
disp(bmask.SizeInBlocks);
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); end end
Switch the blocked image to read mode.
bmask.Mode = "r";Display the mask.
figure bigimageshow(bmask)

Input Arguments
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
See Also
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Sélectionner un site web
Choisissez un site web pour accéder au contenu traduit dans votre langue (lorsqu'il est disponible) et voir les événements et les offres locales. D’après votre position, nous vous recommandons de sélectionner la région suivante : .
Vous pouvez également sélectionner un site web dans la liste suivante :
Comment optimiser les performances du site
Pour optimiser les performances du site, sélectionnez la région Chine (en chinois ou en anglais). Les sites de MathWorks pour les autres pays ne sont pas optimisés pour les visites provenant de votre région.
Amériques
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)