如果你有
2-D grayscale intensity image
存储在矩阵中
A
,可以通过执行以下操作将下半部分设置为黑色:
centerIndex = round(size(A,1)/2); %# Get the center index for the rows
A(centerIndex:end,:) = cast(0,class(A)); %# Set the lower half to the value
%# 0 (of the same type as A)
这是通过首先获取
一
使用函数
SIZE
,除以2,四舍五入,得到靠近图像高度中心的整数索引。然后,向量
centerIndex:end
索引从中心索引到结尾的所有行,以及
:
索引所有列。所有这些索引元素都设置为0表示黑色。
函数
CLASS
用于获取的数据类型
一
以便可以使用函数将0强制转换为该类型
CAST
. 这个
可以
不过,这不是必需的,因为0可能会自动转换为
一
没有他们。
如果要创建
logical index
要用作遮罩,可以执行以下操作:
mask = true(size(A)); %# Create a matrix of true values the same size as A
centerIndex = round(size(A,1)/2); %# Get the center index for the rows
mask(centerIndex:end,:) = false; %# Set the lower half to false
现在,
mask
是一个逻辑矩阵
true
(即“1”)表示要保留的像素,以及
false
(即“0”)表示要设置为0的像素。您可以设置更多元素
面具
到
假
如你所愿。然后,当要应用遮罩时,可以执行以下操作:
A(~mask) = 0; %# Set all elements in A corresponding
%# to false values in mask to 0