仅对选定像素进行卷积,或对选定像素进行 nlfilter()



是否有任何内置函数仅用于对图像上的像素子集?

基本上,我知道这些点的坐标,我想得到以这些点为中心的卷积核应用的结果。

我想在Hessian-Laplace特征检测器的实现中使用它。我不想构建整个比例的空间立方体,我只想将拉普拉斯应用于黑森探测器发现的兴趣点。

谢谢。

编辑:

我正在寻找具有以下签名的函数:

function [ results ] = selected_conv( input_matrix, ...
coords, kernel, border_treatment_mode )

用法示例:

% define the kernel and the input image
h = [1 2 3;
     0 0 0;
     6 5 4];
I = [1 5 2 3;
     8 7 3 6;
     3 3 9 1]
% Points coordinates in original image to convolve.
points_coords_to_convolve = [[2, 2]; [2, 3]];
% The third parameter is a parameter like for padarray(): 'zeros', 'replicate', 'symmetric'.
result = selected_conv(I, h, 'zeros')

输出:

[65, 76]

上述代码的细分:

  1. 内核矩阵的大小总是不均匀的。将内核矩阵旋转 180 度。(通常如何使用卷积完成)。我们代码的结果:

     h = [4 5 6;
          0 0 0;
          3 2 1];
    
  2. 我们检查内核是否适合所有指定的点到矩阵中。否则,我们用一种可能的填充技术填充矩阵:"零"、"复制"、"对称"。填充的过程与 matlab 中的padarray()函数相同。

  3. 将旋转的内核居中在原始图像的每个指定点上并计算响应。对所有指定的点以相同的方式进行。在我们的示例中[[2, 2]; [2, 3]] .每行的第一个数字是行号,第二个是列号。在我们的例子中,它将是数字73或原始矩阵。
  4. 第一个数字的响应为 4 + 5*5 + 6*2 + 3*3 + 2*3 + 9 = 65

我的 nlfileter() 代码

function [ results ] = selected_nlfilter( input_matrix, coords, ...
func_handler, sliding_window_size, border_treatment_mode ) 
    Kernel_x = sliding_window_size(1);
    Kernel_y = sliding_window_size(2);
    pad_row = floor(Kernel_x/2);
    pad_col = floor(Kernel_y/2);
    padded_matrix = pad_matrix(input_matrix, pad_row, pad_col, border_treatment_mode);
    results  = zeros(size(coords, 1), 1, 'double');
    amount_of_coords = size(coords, 1);
    for coord_count = 1:amount_of_coords
        row = coords(coord_count, 1);
        col = coords(coord_count, 2);
        frame = padded_matrix(row:row+(2*pad_row),col:col+(2*pad_col));
        sliding_window_size;
        results(coord_count) = func_handler(frame);
    end 
  end

我刚刚将其应用于已经旋转的内核矩阵。

这是一个

函数代码,它对边界周围的点进行zero-padding,并实现"selective convolution" -

function out = selected_conv(I,pts,h)
%// Parameters
hsz = size(h);
bxr = (hsz-1)/2;
Isz = size(I);
%// Get padding lengths across all 4 sides
low_padlens = max(bsxfun(@minus,bxr+1,pts),[],1);
low_padlens = (low_padlens + abs(low_padlens))./2;
high_padlens = bxr - min(bsxfun(@minus,Isz,pts),[],1);
high_padlens = (high_padlens + abs(high_padlens))./2;
%// Get zeros padded array
Ip = zeros(Isz + high_padlens + low_padlens);
Ip(low_padlens(1)+1:Isz(1)+low_padlens(1),...
    low_padlens(2)+1:Isz(2)+low_padlens(2)) = I;
pts = bsxfun(@plus,pts,low_padlens); %// modified points based on padding
lin_idx = sub2ind(size(Ip),pts(:,1),pts(:,2)); %//'#linear indices of points
%// Calculate neighborhood offsets and then the actual neighboring elements
off1 = bsxfun(@plus,[-bxr(1):bxr(1)]',[-bxr(2):bxr(2)]*size(Ip,1)); %//'
all_idx = bsxfun(@plus,off1(:),lin_idx(:).'); %//'# all neighbouring indices
vals = Ip(all_idx);  %// all neighbouring elements
out = h(:).'*vals; %//'# Finally get the weighted output
return;

示例用法

h = [4 5 6;
    0 0 0;
    3 2 1];
I = [1 5 2 3;
    8 7 3 6;
    3 3 9 1]
pts = [[2, 2]; [2, 3]]
out = selected_conv(I,pts,h)

输出-

out =
    65    76

最新更新