UBLAS 矩阵查找单元格的周围值?

发布于 2024-08-09 07:54:37 字数 213 浏览 7 评论 0原文

我正在寻找一种优雅的方式来实现这一点。基本上我有 amxn 矩阵。其中每个单元格代表像素值,行和列代表图像的像素行和像素列。

因为我基本上映射了 HDF 文件中的点及其相应的像素值。我们基本上有很多空像素。其中填充了 0。

现在我需要做的是取周围单元格的平均值,以对丢失单元格的像素值进行平均。

现在我可以暴力破解它,但它很快就会变得丑陋。有什么优雅的解决方案吗?

I am looking for an elegant way to implement this. Basically i have a m x n matrix. Where each cell represents the pixel value, and the rows and columns represent the pixel rows and pixel columns of the image.

Since i basically mapped points from a HDF file, along with their corresponding pixel values. We basically have alot of empty pixels. Which are filled with 0.

Now what i need to do is take the average of the surrounding cell's, to average out of a pixel value for the missing cell.

Now i can brute force this but it becomes ugly fast. Is there any sort of elegant solution for this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

孤星 2024-08-16 07:54:37

对于这个过滤问题有一个众所周知的优化。

  • 沿一个方向(例如水平)对单元
  • 格进行积分 沿另一方向(例如垂直)对单元格进行积分
  • 计算每个单元格与其左侧第 N 个相邻单元格之间的差值。
  • 取每个单元与其第 N 个较低邻居之间的差异,

如下所示:

    for (i = 0; i < h; ++i)
    for (j = 0; j < w-1; ++j)
       A[i][j+1] += A[i][j];
    for (i = 0; i < h-1; ++i)
    for (j = 0; j < w; ++j)
       A[i+1][j] += A[i][j]
    for (i = 0; i < h; ++i)
    for (j = 0; j < w-N; ++j)
       A[i][j] -= A[i][j+N];
    for (i = 0; i < h-N; ++i)
    for (j = 0; j < w; ++j)
       A[i][j] -= A[i-N][j];

其作用是:

  • 第一遍使每个单元格成为该行左侧所有单元格(包括其自身)的总和。
  • 在第二遍之后,每个单元格是其上方和左侧矩形中所有单元格的总和(包括它自己的行和列)。
  • 在第三遍之后,每个单元格是其上方和右侧矩形中的所有单元格的总和本身,N 列宽。
  • 第 4 遍之后,每个单元格都是其下方和右侧的 NxN 矩形的总和。

每个单元格需要执行 4 次操作才能计算总和,而暴力破解则需要 8 次操作(假设您正在执行 3x3 平均滤波器)。

很酷的是,如果您使用普通的补码算术,则不必担心前两遍中的任何溢出;他们在最后两次传递中抵消了。

There's a well-known optimization to this filtering problem.

  • Integrate the cells in one direction (say horizontally)
  • Integrate the cells in the other direction (say vertically)
  • Take the difference between each cell and it's N'th neighbor to the left.
  • Take the difference between each cell and it's N'th lower neighbor

Like this:

    for (i = 0; i < h; ++i)
    for (j = 0; j < w-1; ++j)
       A[i][j+1] += A[i][j];
    for (i = 0; i < h-1; ++i)
    for (j = 0; j < w; ++j)
       A[i+1][j] += A[i][j]
    for (i = 0; i < h; ++i)
    for (j = 0; j < w-N; ++j)
       A[i][j] -= A[i][j+N];
    for (i = 0; i < h-N; ++i)
    for (j = 0; j < w; ++j)
       A[i][j] -= A[i-N][j];

What this does is:

  • The first pass makes each cell the sum of all of the cells on that row to it's left, including itself.
  • After the 2nd pass , each cell is the sum of all of the cells in a rectangle above and left of itselt (including it's own row and column)
  • After the 3rd pass, each cell is the sum of a rectangle above and to the right of itself, N columns wide.
  • After the 4th pass each cell is the sum of an NxN rectangle below and to the right of itself.

This takes 4 operations per cell to compute the sum, as opposed to 8 for brute force (assuming you're doing a 3x3 averaging filter).

The cool thing is that if you use ordinary two's-complement arithmetic, you don't have to worry about any overflows in the first two passes; they cancel out in the last two passes.

小嗲 2024-08-16 07:54:37

这里的主要问题是利用所有可用的核心和缓存效率。

您可能对检查卷积的快速实现感兴趣。

但是,由于您使用 Boost 进行此操作,因此您可以在 此 Boost 示例

我相信您只需更改卷积核即可完成您的专门任务。

The main issues here are utilizing all available cores and cache effeciency.


You might be interested in checking fast implementation of convolution.


However, since you do it with Boost, you can check how this is done in this Boost example


I beleive you have to change only the convolution kernel for your specialized task.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文