Convert world coordinates to pixel subscripts
Convert pixel subscripts from one level to another via the world coordinates to refer to the same spatial region.
Create a blocked image from a sample image included with the toolbox.
bim = blockedImage('tumor_091R.tif');
Define a region of interest in the finest resolution level in pixel subscripts.
level1PixelSubStart = [1700, 1550 1]; level1PixelSubEnd = [2100, 2000 3];
Get the image data from the region of interest on the resolution level 1 image.
imr = getRegion(bim, level1PixelSubStart, level1PixelSubEnd, "Level", 1); size(imr)
ans = 1×3 401 451 3
Convert the pixel subscripts that define the region of interest into world coordinates. By default,
sub2world converts the coordinates at level 1, the finest resolution.
worldRegion = sub2world(bim,[level1PixelSubStart; level1PixelSubEnd]);
Compute a binary mask at the coarsest level.
bbw = apply(bim, @(bs)imbinarize(im2gray(bs.Data)), "Level", 3);
Convert the world coordinates of the region of interest to pixel subscripts of the mask. Note that the mask has only two dimensions.
worldRegion = worldRegion(:,1:2); maskPixelSubs = world2sub(bbw,worldRegion);
Corresponding mask region.
bwr = getRegion(bbw, maskPixelSubs(1,:), maskPixelSubs(2,:)); size(bwr)
ans = 1×2 51 58
View the original image and the mask.
bim— Blocked image
Blocked image, specified as a
world— World coordinates
World coordinates, specified as a K-by-N
numeric vector, where K is the number of world coordinate vectors and
N is the number of dimensions of the
pixelsub— Pixel subscripts
Pixel subscripts, returned as a K-by-N integer-valued vector, N is the number of dimensions and K is the number of coordinates.
World coordinates are theoretically continuous domain values represented by floating
point numbers. Subscripts are discrete integer values that can be used to index into the
underlying array. Floating point computation and rounding may cause small changes in world
coordinates around the edge of pixels to map to different neighboring subscript locations.
world2sub rounds up world coordinate values on the edge of two
pixels, except for pixels on the border, where it rounds down to the last pixel.