二维阵列中值滤波中值、阵列

2023-09-11 06:28:30 作者:Display(张扬)

我想写code实现中值滤波一个二维数组。 这里有一个图片来说明:

的程序开始于所述阵列的开头。最大的数组大小为100。我知道,我可以用这样一个数组:

  INT [100] [100]
 

存储输入,而且我可以遍历使用该阵列的一部分两个循环是这样的:

 为(i = 0; I< size_filter;我++)
为(J = 0; J< size_filter; J ++)
      临时[I] ​​[J] = A [1] [J]。//不太确定
 

但我怎么能做出这种code在阵列中的每个元素的邻居循环,计算出它们的中位数和中位数取代中心元素?

求一种中值滤波器的设计方案

有关的什么,我试图做一些例子,假设输入的是一个5x5的矩阵,所以输入大小为5,我想在其上运行一个3×3的中值滤波器,即每个元素应及时更换由围绕它的3×3的元素的中值。

该程序将启动角落指数(0,0)的。对于这个指标,它扫描3x3的区域周围(其中只有四项指标是在于输入数组内),其中包含了值0,这些值0,1,和0中值是0,所以这是什么code必须输出这个数组的索引。

在下面的图片中,在数量的的粗体斜体的为中心的细胞,和平原粗体数字是3×3范围内的邻居周围地区:

  0    0  0 0 0
 1   0  0 1 0
1 1 0 0 0
0 1 1 0 0
0 0 0 0 0

下面是另外一个例子,这一次与中心指数(0,1):

 0    0    0  0 0
 1   0   0  1​​ 0
1 1 0 0 0
0 1 1 0 0
0 0 0 0 0

这个时间,在3×3区域(不包括输入数组以外)中的元素的值分别为0,0,0,1,0,和0,并再次,它们的中值,因此是0

下面的又一例子,这次从输入的中间,在中心索引(3,2):

0 0 0 0 0
 1   0   0  1​​ 0
 1    1    0  0 0
 0   1   1  0 0
0 0 0 0 0

这个时间,3×3区域内的元件具有在因此1的值1,0,0,1,1,0,0,1和1,以及它们的中值

最后一个例子:

 <阵&GT的尺寸;<大小滤光器> <数据>
8
3
0 0 0 0 0 0 0 0
0 5 0 0 6 0 0 0
0 0 0 0 0 7 0 0
0 0 0 0 5 0 0 0
0 0 0 5 6 0 0 0
0 0 8 5 5 0 0 0
0 0 0 7 0 0 9 0
0 0 0 0 0 0 0 0

输出:
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 5 5 0 0 0
0 0 0 5 5 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
 

解决方案

它看起来像你想实现一个二维的中值滤波。要实现这样的过滤器的简单的方法是让四的嵌套循环:两个外循环在x和整个图像的y坐标,和两个内部循环在中心像素的邻域

这也许更容易来形容这个在code不是文本,所以这里的一些Python式的伪code来说明:

#假设: #*图像是含有高x宽阵列源像素值 #*过滤是一种高x宽阵列来存储结果像素值 #*尺寸为奇数给出滤波器区域的直径 半径=(尺寸 - 1)/ 2#大小= 3 - >半径= 1 y的从0至高度-1:     顶部= MAX(Y - 半径,0)     底部= MIN(Y +半径,高度1)     对于x从0到宽度-1:         左= MAX(X - 半径,0)         右= MIN(X +半径,宽度1)         值=新的列表         中V从上到下:             在V从左至右:                 添加图片[V] [U]到值         过滤[Y] [X] =位数(值)

将这种code到C被留作练习。

也有可能通过注意到相邻阵列单元的邻域重叠显著,使那些相邻小区的值可以在整个外部循环的连续迭代​​中重复使用,以优化此code。由于该算法的现代CPU的性能基本上由RAM访问延迟的限制,这样的复用可以提供一个显著加速,特别是大型的过滤器尺寸。

I'm trying to write code that implements median filtering on a two-dimensional array. Here's an image to illustrate:

The program starts at the beginning of the array. The maximum array size is 100. I know that I can use an array like:

int a[100][100];

to store the input, and that I can iterate over a part of this array using two for loops like this:

for(i=0;i<size_filter;i++)
for(j=0;j<size_filter;j++)
      temp[i][j]=a[i][j]     // not so sure

But how can I make this code loop over the neighbors of every element in the array, calculate their median, and replace the center element with the median?

For some examples of what I'm trying to do, let's say that the input is a 5x5 matrix, so the input size is 5. And I want to run a 3x3 median filter on it, i.e. each element should be replaced by the median of the 3x3 elements surrounding it.

The program starts at the corner index (0,0). For this index, it scans the 3x3 region surrounding it (of which only four indexes actually lie within the input array), which contains the values 0, 0, 1, and 0. The median of these values is 0, so that's what the code should output for this array index.

In the picture below, the number in bold italics is the center cell, and the plain bold numbers are its neighbors within the 3x3 region surrounding it:

0 0 0 0 0
1 0 0 1 0
1 1 0 0 0
0 1 1 0 0
0 0 0 0 0 

Here's another example, this time with the center index (0,1):

0 0 0 0 0
1 0 0 1 0
1 1 0 0 0
0 1 1 0 0
0 0 0 0 0 

This time, the elements in the 3x3 region (excluding those outside the input array) have the values 0, 0, 0, 1, 0, and 0, and again, their median is therefore 0.

Here's yet another example, this time from the middle of the input, at center index (3,2):

0 0 0 0 0
1 0 0 1 0
1 1 0 0 0
0 1 1 0 0
0 0 0 0 0 

This time, the elements within the 3x3 region have the values 1, 0, 0, 1, 1, 0, 0, 1, and 1, and their median in therefore 1.

Final example:

<size of array><size filter> <data>
8
3
0 0 0 0 0 0 0 0
0 5 0 0 6 0 0 0
0 0 0 0 0 7 0 0
0 0 0 0 5 0 0 0
0 0 0 5 6 0 0 0
0 0 8 5 5 0 0 0
0 0 0 7 0 0 9 0
0 0 0 0 0 0 0 0

Output:
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 5 5 0 0 0
0 0 0 5 5 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0

解决方案

It looks like you're trying to implement a two-dimensional median filter. The straightforward way to implement such a filter is to have four nested loops: two outer loops over the x and y coordinates of the whole image, and two inner loops over the neighborhood of the center pixel.

It's perhaps easier to describe this in code than in text, so here's some Python-esque pseudocode to illustrate:

# assumptions:
#  * image is a height x width array containing source pixel values
#  * filtered is a height x width array to store result pixel values in
#  * size is an odd number giving the diameter of the filter region

radius = (size - 1) / 2   # size = 3 -> radius = 1

for y from 0 to height-1:
    top = max(y - radius, 0)
    bottom = min(y + radius, height-1)

    for x from 0 to width-1:
        left = max(x - radius, 0)
        right = min(x + radius, width-1) 
        values = new list

        for v from top to bottom:
            for u from left to right:
                add image[v][u] to values

        filtered[y][x] = median(values)

Translating this code into C is left as an exercise.

It's also possible to optimize this code by noting that the neighborhoods of adjacent array cells overlap significantly, so that the values of those neighboring cells can be reused across successive iterations of the outer loops. Since the performance of this algorithm on modern CPUs is essentially limited by RAM access latency, such reuse can provide a significant speedup, especially for large filter sizes.