应用滑动窗口操作后,如何设置边框像素的值? [英] How can I set the value of border pixels after applying a sliding window operation?
问题描述
将 stdfilt(i,ones(5,5))
等滑动窗口操作应用到MATLAB中的图像后,我想设置边框像素,使用填充计算的像素, NaN
将其标记为无意义。在这个例子中,我将最外面的两行和列设置为 NaN
。给定M * M窗口,如何将此边界(M-1)/ 2像素宽设置为特定值?
After applying a sliding-window operation like stdfilt(i, ones(5, 5))
to an image in MATLAB, I would like to set the border pixels, the pixels calculated using padding, to NaN
to mark them as meaningless. In this example, I would set the outermost two rows and columns to NaN
. Given an M * M window, how can I set this border that is (M - 1) / 2 pixels wide to a specific value?
推荐答案
假设, r
是矩阵( stdfilt
的结果)。以下是如何将NaN分配给边框的示例。
一般情况下,您需要指定正确的索引。
Suppose, r
is the matrix (result of stdfilt
). Here is an example how to assign NaN to borders.
In general, you need to specify the correct index.
>> r = rand(4)
r =
0.8147 0.6324 0.9575 0.9572
0.9058 0.0975 0.9649 0.4854
0.1270 0.2785 0.1576 0.8003
0.9134 0.5469 0.9706 0.1419
>> r([1, end], :) = nan
r =
NaN NaN NaN NaN
0.9058 0.0975 0.9649 0.4854
0.1270 0.2785 0.1576 0.8003
NaN NaN NaN NaN
>> r(:, [1, end]) = nan
r =
NaN NaN NaN NaN
NaN 0.0975 0.9649 NaN
NaN 0.2785 0.1576 NaN
NaN NaN NaN NaN
更新:一般情况下,因为图像是一个矩阵,你的问题是如何设置某些矩阵值?。答案是:使用矩阵的索引。有很多方法可以做到这一点(例如,通过构建索引矩阵,逻辑掩码或使用行/列坐标)。决定使用什么方法通常取决于此特定任务的操作速度。例如,如果您需要用NaN替换零,则使用逻辑掩码会更快: r(r == 0)= nan
,依此类推。
update: in general, since image is a matrix, your question is "how to set certain matrix values?". The answer is: "using matrix's indexes". There are many ways to do it (for example, by building matrix of indexes, logical mask, or using rows/columns coordinates). The decision what method to use usually depends on the speed of the operation for this specific task. For example, if you need to replace zeros by NaN's, it's faster to use a logical mask: r(r == 0) = nan
, and so on.
这篇关于应用滑动窗口操作后,如何设置边框像素的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!