【问题标题】:How to add a Gaussian shaped object to an image?如何将高斯形状的对象添加到图像中?
【发布时间】:2014-07-05 18:05:12
【问题描述】:

我有兴趣将单个高斯形状的对象添加到现有图像中,类似于附加图像中的内容。我想将对象添加到的基本图像是 8 位无符号值,其值范围为 0-255。附图中的亮物体实际上是一棵由归一化差分植被指数(NDVI)数据表示的树。随附的脚本是我到目前为止所拥有的。如何将值在 110-155 之间的高斯形状的对象(即树)添加到现有的 NDVI 图像?

Sample data available here可以和这个脚本一起使用来计算NDVI


file = 'F:\path\to\fourband\image.tif';
[I R] = geotiffread(file);
outputdir = 'F:\path\to\output\directory\'

%% Make NDVI calculations
NIR = im2single(I(:,:,4));
red = im2single(I(:,:,1));
ndvi = (NIR - red) ./ (NIR + red);
ndvi = double(ndvi);

%% Stretch NDVI to 0-255 and convert to 8-bit unsigned integer
ndvi = floor((ndvi + 1) * 128); % [-1 1] -> [0 256]
ndvi(ndvi < 0) = 0;             % not really necessary, just in case & for symmetry
ndvi(ndvi > 255) = 255;         % in case the original value was exactly 1
ndvi = uint8(ndvi);             % change data type from double to uint8

%% Need to add a random tree in the image here


%% Write to geotiff
tiffdata = geotiffinfo(file);
outfilename = [outputdir 'ndvi_' '.tif'];  
geotiffwrite(outfilename, ndvi, R, 'GeoKeyDirectoryTag', tiffdata.GeoTIFFTags.GeoKeyDirectoryTag) 

【问题讨论】:

    标签: image matlab image-processing


    【解决方案1】:

    你的帖子是在问如何做三件事:

    1. 我们如何生成高斯形状的对象?
    2. 我们如何才能使值介于 110 - 155 之间?
    3. 我们如何将其放置在我们的图像中?

    让我们分别回答每个问题,每个问题的顺序建立在前面问题的知识基础上。

    我们如何生成高斯形状的对象?

    您可以使用图像处理工具箱中的fspecial 为您生成高斯:

    mask = fspecial('gaussian', hsize, sigma);
    

    hsize 指定高斯的大小。你没有在你的问题中指定它,所以我假设你会想自己玩这个。这将产生一个hsize x hsize 高斯矩阵。 sigma 是高斯分布的标准差。同样,您也没有指定这是什么。 sigmahsize 携手并进。参考我的previous post on how to determine sigma,将您的掩码的标准差设置为3-sigma 规则通常是一个很好的规则。因此,一旦您设置了hsize,您就可以计算出sigma 为:

    sigma = (hsize-1) / 6;
    

    因此,找出hsize 是什么,然后计算出你的sigma。之后,像我上面那样调用fspecial。将hsize 设为奇数 整数通常是个好主意。原因是当我们最终将其放置在您的图像中时,执行此操作的语法将允许您的蒙版对称放置。当我们谈到最后一个问题时,我会谈到这个。

    我们怎样才能使值介于 110 - 155 之间?

    我们可以通过调整 mask 中的值来做到这一点,使最小值为 110,而最大值为 155。这可以通过以下方式完成:

    %// Adjust so that values are between 0 and 1
    maskAdjust = (mask - min(mask(:))) / (max(mask(:)) - min(mask(:)));
    
    %//Scale by 45 so the range goes between 0 and 45
    %//Cast to uint8 to make this compatible for your image
    maskAdjust = uint8(45*maskAdjust);
    
    %// Add 110 to every value to range goes between 110 - 155
    maskAdjust = maskAdjust + 110;
    

    一般来说,如果你想调整你的高斯掩码中的值,使其从[a,b]开始,你会首先在 0 和 1 之间进行归一化,然后执行:

    maskAdjust = uint8((b-a)*maskAdjust) + a;
    

    您会注意到我们将此掩码转换为uint8。我们这样做的原因是为了使蒙版兼容放置在您的图像中。

    我们如何将它放置在我们的图像中?

    您所要做的就是找出您希望放置高斯蒙版中心的行和列。假设这些变量存储在rowcol 中。因此,假设你想把它放在ndvi,你所要做的就是:

    hsizeHalf = floor(hsize/2); %// hsize being odd is important
    %// Place Gaussian shape in our image
    ndvi(row - hsizeHalf : row + hsizeHalf, col - hsizeHalf : col + hsizeHalf) = maskAdjust;
    

    hsize 应该是奇数的原因是允许图像中的形状是偶数的。例如,如果掩码大小为 5 x 5,则上述 ndvi 的语法简化为:

    ndvi(row-2:row+2, col-2:col+2) = maskAdjust;
    

    从蒙版的中心开始,它向上延伸 2 行,向下延伸 2 行。列从左侧的 2 列延伸到右侧的 2 列。如果掩码大小是偶数,那么我们将在如何放置掩码方面做出模棱两可的选择。如果以 4 x 4 为例,我们应该选择第二行还是第三行作为中心轴?因此,为简化操作,请确保您的掩码大小为奇数,或 mod(hsize,2) == 1

    这应该有望并充分回答您的问题。祝你好运!

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-07-31
    • 2016-01-24
    • 2020-10-15
    • 2022-01-20
    • 1970-01-01
    • 2020-08-04
    • 2018-02-19
    • 1970-01-01
    相关资源
    最近更新 更多