Main Content

sizesMatch

Determine if object and image are size-compatible

Description

example

TF = sizesMatch(R,A) returns True if the size of image A is consistent with the ImageSize property of spatial referencing object R.

Examples

collapse all

Read a 2-D grayscale image into the workspace. View the size of the image.

I = imread('cameraman.tif');
size(I)
ans = 1×2

   256   256

Create an imref2d spatial referencing object with the same dimensions as the image.

R = imref2d(size(I))
R = 
  imref2d with properties:

           XWorldLimits: [0.5000 256.5000]
           YWorldLimits: [0.5000 256.5000]
              ImageSize: [256 256]
    PixelExtentInWorldX: 1
    PixelExtentInWorldY: 1
    ImageExtentInWorldX: 256
    ImageExtentInWorldY: 256
       XIntrinsicLimits: [0.5000 256.5000]
       YIntrinsicLimits: [0.5000 256.5000]

Confirm that the size of the image matches the ImageSize property of the object.

res = sizesMatch(R,I)
res = logical
   1

Read another 2-D grayscale image that has a different size. View the size of this image.

I2 = imread('coins.png');
size(I2)
ans = 1×2

   246   300

Check if the size of this image matches the size of the original spatial referencing object.

res2 = sizesMatch(R,I2)
res2 = logical
   0

The result is false, as expected.

Read an RGB image into the workspace. View the size of the image.

I = imread('peppers.png');
size(I)
ans = 1×3

   384   512     3

Create an imref2d spatial referencing object with the same dimensions as the image. The object does not retain information about the third dimension of the image array.

R = imref2d(size(I))
R = 
  imref2d with properties:

           XWorldLimits: [0.5000 512.5000]
           YWorldLimits: [0.5000 384.5000]
              ImageSize: [384 512]
    PixelExtentInWorldX: 1
    PixelExtentInWorldY: 1
    ImageExtentInWorldX: 512
    ImageExtentInWorldY: 384
       XIntrinsicLimits: [0.5000 512.5000]
       YIntrinsicLimits: [0.5000 384.5000]

Check if the size of the image is compatible with the ImageSize property of the object.

res = sizesMatch(R,I)
res = logical
   1

Read a 3-D volume into the workspace. This image consists of 27 frames of 128-by-128 pixel grayscale images.

load mri;
D = squeeze(D);
D = ind2gray(D,map);
size(D)
ans = 1×3

   128   128    27

Create an imref3d spatial referencing object associated with the volume.

R = imref3d(size(D))
R = 
  imref3d with properties:

           XWorldLimits: [0.5000 128.5000]
           YWorldLimits: [0.5000 128.5000]
           ZWorldLimits: [0.5000 27.5000]
              ImageSize: [128 128 27]
    PixelExtentInWorldX: 1
    PixelExtentInWorldY: 1
    PixelExtentInWorldZ: 1
    ImageExtentInWorldX: 128
    ImageExtentInWorldY: 128
    ImageExtentInWorldZ: 27
       XIntrinsicLimits: [0.5000 128.5000]
       YIntrinsicLimits: [0.5000 128.5000]
       ZIntrinsicLimits: [0.5000 27.5000]

Confirm that the size of the volume matches the ImageSize property of the object.

res = sizesMatch(R,D)
res = logical
   1

The sizes match, as expected.

Read another image that has a different size. This image a 3-D array representing an RGB image.

I = imread('peppers.png');
size(I)
ans = 1×3

   384   512     3

Check if the size of this image matches the size of the original spatial referencing object.

res2 = sizesMatch(R,I)
res2 = logical
   0

The result is false, as expected.

Input Arguments

collapse all

Spatial referencing object, specified as an imref2d or imref3d object.

Input image, specified as a numeric m-by-n or m-by-n-by-p array.

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

Output Arguments

collapse all

Flag indicating size compatibility, returned as a logical scalar. TF is True if the size of the image A is consistent with the referencing object R. When R is:

  • An imref2d spatial referencing object, TF returns true when R.ImageSize == [size(A,1) size(A,2)].

    Note

    The dimensionality of A does not need to match the dimensionality of an imref2d spatial referencing object. For example, an RGB image can be consistent with an imref2d object. In this case, sizesMatch ignores the third image dimension.

  • An imref3d spatial referencing object, TF returns true when R.ImageSize == size(A). A must be a 3-D array.

Data Types: logical

Introduced in R2013a