【问题标题】:Convolution only for selected pixels or nlfilter() for selected pixels仅对选定像素进行卷积或对选定像素进行 nlfilter()
【发布时间】:2015-03-08 15:24:19
【问题描述】:

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

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

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

谢谢。

编辑:

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

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. 我们检查所有指定点的内核是否适合矩阵。否则,我们使用一种可能的填充技术来填充矩阵:“零”、“复制”、“对称”。 padding的过程与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

我刚刚将它与已经旋转的内核矩阵一起应用。

【问题讨论】:

  • 在几个点上展示您的代码如何使用?
  • @Divakar,您想要签名还是特定用例?
  • 使用一些样本最小输入图像数据、点数组、样本内核并显示所需的输出。解释它必须如何在边界周围表现。很高兴看到问题中涵盖了这些内容。
  • @Divakar,我已经添加了它。实际上,这是我第二次不得不使用这样的功能。我之前已经实现了一个类似的功能,但它不会那么快。 nlfilter 仅适用于选定像素也足够了。但是我没有找到内置函数。
  • 您能补充一下您是如何获得所需输出的吗?基本上,我们希望有一个最小的可重现代码,它接收输入数据(正如您已经在问题中添加的那样)并产生所需的输出,当然假设您已经尝试了一些东西。

标签: performance matlab image-processing filtering convolution


【解决方案1】:

这是一个函数代码,对边界周围的点执行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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-05-23
    • 1970-01-01
    • 2017-10-29
    • 2019-05-03
    • 2017-03-21
    • 1970-01-01
    • 2020-04-09
    • 2017-09-03
    相关资源
    最近更新 更多