测试周围的非零元素 [英] Test the surrounding non-zeros elements

查看:102
本文介绍了测试周围的非零元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是以下内容的一部分:

This is the following part of the below:

2)其他问题:

在获得非零邻居的平均值之后,我还想测试邻居元素是否等于,小于或大于非零平均值.如果大于或等于,则为"1",否则为"0".

After getting the average of the non-zero neighbors, I also want to test if the neighbor elements are equal, lesser, or greater than the average of the nonzeros. If it is greater or equal then '1' or else '0'.

注意:如果邻居的半径在两个或多个中心的范围内,则以最小的中心平均值进行测试.

Note: if the neighbors are with in the radius of the two or more centres, take the smallest centre average to test.

    0    12     9     
    4  **9**    15     
   11    19     0 

中间的'9'位于12、15和19个中心的半径内,因此取最小值的最小值的平均值[9.000,9.000,8.000] = 8.000

The '9' in the middle is within the radius of 12, 15, and 19 centres, so take the minimum average of those min[9.000, 9.000, 8.000]=8.000

例如,当半径= 1 m或1个元素远时.

For example, when radius = 1 m or 1 element away.

new_x =

     0         0          0          0         0
     0         0     **9.0000**    9.0000      0
     0      4.0000     9.0000    **9.0000**    0
     0    **8.3333** **8.0000**      0         0
     0      2.0000     4.0000      8.0000      0
     0      4.0000     5.0000      8.0000      0
     0         0          0          0         0

Test_x =

     0         0           0           0         0
     0         0       **9.0000**      1         0
     0         0           1       **9.0000**    0
     0    **8.3333**   **8.0000**      0         0
     0         0           0           0         0
     0         0           0           0         0
     0         0           0           0         0

================================================ ================================

=================================================================================

1)说我是否有一个矩阵,如下所示,

1) Say if I have a matrix, shown as below,

X =

 0     0     0     0     0
 0     0    12     9     0
 0     4     9    15     0
 0    11    19     0     0
 0     2     4     8     0
 0     4     5     8     0
 0     0     0     0     0

,并且我想找到周围的非零元素的平均值,该平均值大于10.其余元素仍保持不变,即元素< 10.

and I want to find the average of the surrounding non-zero elements that is greater than 10. The rest of the elements still remain the same i.e. elements < 10.

所以我希望我的解决方案看起来像

So I want my solution to look something like,

new_x =

     0         0         0         0         0
     0         0    9.0000    9.0000         0
     0    4.0000    9.0000    9.0000         0
     0    8.3333    8.0000         0         0
     0    2.0000    4.0000    8.0000         0
     0    4.0000    5.0000    8.0000         0
     0         0         0         0         0

不是:我只是在看元素的邻居,这些邻居的价值大于某个值(在本例中为10).

Not: that I am NOT only looking at the neighbors of the element thats greather than some value (i.e. 10 in this case).

让我们说任何大于10的元素都是中心",我们想找到半径为1 m的非零的半径. 1米=距中心1个元素.

Lets say any elements thats are greater than 10 are the 'centre' and we want to find the avearge of the non-zeros with the radius of say 1 m. where 1 metre = 1 element away from the centre.

注意:半径不一定总是相距1米,即可以等于2或更大.在这种情况下,它不会只是中心的顶部,底部,左侧和右侧.

Note: It might not always be 1 meter away in radius i.e. can be 2 or more. In this case it wont be just top, bottom, left and right of the centre.

****也请注意矩阵边界.例如,当radius = 2或更大时,某些非零邻居的平均值不在边界之内.**

****Also Be aware of the matrix boundary. For example, when radius = 2 or more, some of the average of nonzero neighbors are out side the boundary.**

例如

对于半径= 1 m = 1个元素的距离, new_x = [(i + 1,j),(i-1,j),(i,j + 1)和(i,j-1)]的平均值-中心的上,下,右和左.

For radius =1 m = 1 element away, new_x = average of [(i+1,j) , (i-1,j) , (i,j+1) and (i,j-1)] - top, bottom, right, and left of the centre.

对于半径= 2 m = 2个元素的距离, new_x = [[i + 1,j),(i + 2,j),(i-1,j),(i-2,j),(i,j + 1),(i,j + 2),(i,j-1),(i,j-2),(i + 1,j + 1),(i + 1,j-1),(i-1,j-1)和(i-1,j + 1)].

For radius =2 m = 2 elements away, new_x = average of [(i+1,j), (i+2,j) , (i-1,j) , (i-2,j), (i,j+1), (i,j+2), (i,j-1), (i,j-2), (i+1,j+1), (i+1,j-1), (i-1,j-1), and (i-1,j+1)].

================================================ ==================

==================================================================

我之前已经尝试过一些方法,但是我对这些功能并不熟悉.

I have tried a few things before, however I am not familiar with the functions.

所以请帮助我解决问题.

So please help me to solve the problem.

谢谢.

推荐答案

以下是我认为您在问题中描述的算法.对于每个像素:

Here is the algorithm I think you are describing in your question. For each pixel:

  • 如果像素值小于10,则不执行任何操作.
  • 如果像素值大于或等于10,则用非零个4连接的最近邻居的平均值替换像素值.

如果这是正确的(似乎是从您提供的示例矩阵中得出的),则可以使用函数

If this is correct (as it appears to be from the sample matrices you gave), then you could use the function NLFILTER from the Image Processing Toolbox (if you have access to it) to perform this operation:

fcn = @(x) [x(5) sum(x(2:2:8))/max(sum(x(2:2:8) > 0),1)]*[x(5) < 10; x(5) >= 10];
new_x = nlfilter(X,[3 3],fcn);


编辑:如果您无权访问
图像处理工具箱,您也可以使用内置的 CONV2 函数,如下所示:


If you don't have access to the Image Processing Toolbox, you can also do this using the built-in CONV2 function, like so:

kernel = [0 1 0; ...                      %# Convolution kernel
          1 0 1; ...
          0 1 0];
sumX = conv2(X,kernel,'same');            %# Compute the sum of neighbors
                                          %#   for each pixel
nX = conv2(double(X > 0),kernel,'same');  %# Compute the number of non-zero
                                          %#   neighbors for each pixel
index = (X >= 10);                        %# Find logical index of pixels >= 10
new_x = X;                                %# Initialize new_x
new_x(index) = sumX(index)./max(nX(index),1);  %# Replace the pixels in index
                                               %#   with the average of their
                                               %#   non-zero neighbors

以上处理您的半径= 1种情况.要解决您的radius = 2的情况,只需将卷积内核更改为以下内容,然后重新运行上面的代码:

The above handles your radius = 1 case. To address your radius = 2 case, you just have to change the convolution kernel to the following and rerun the above code:

kernel = [0 0 1 0 0; ...
          0 1 1 1 0; ...
          1 1 0 1 1; ...
          0 1 1 1 0; ...
          0 0 1 0 0];

这篇关于测试周围的非零元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆