直方图平滑

发布于 2024-09-08 14:24:23 字数 1115 浏览 10 评论 0原文

我有一个可能非常简单的问题,但我仍然不确定!

实际上我只想平滑直方图,并且我不确定以下哪种方法是正确的。我会这样做吗:

vector<double> mask(3);
mask[0] = 0.25; mask[1] = 0.5; mask[2] = 0.25;

vector<double> tmpVect(histogram->size());
for (unsigned int i = 0; i < histogram->size(); i++)
  tmpVect[i] = (*histogram)[i];

for (int bin = 1; bin < histogram->size()-1; bin++) {
  double smoothedValue = 0;
  for (int i = 0; i < mask.size(); i++) {
    smoothedValue += tmpVect[bin-1+i]*mask[i];
  }
  (*histogram)[bin] = smoothedValue;
}

或者你通常会这样做吗?:

vector<double> mask(3);
mask[0] = 0.25; mask[1] = 0.5; mask[2] = 0.25;

for (int bin = 1; bin < histogram->size()-1; bin++) {
  double smoothedValue = 0;
  for (int i = 0; i < mask.size(); i++) {
    smoothedValue += (*histogram)[bin-1+i]*mask[i];
  }
  (*histogram)[bin] = smoothedValue;
}

我的Questin是:首先将直方图复制到额外的向量中是否合理,这样当我在bin i处平滑时我可以使用原始的i-1值或者我简单地做smoothedValue += (*histogram)[bin-1+i]*mask[i];,这样我使用已平滑的 i-1 值代替原始值。

问候&感谢您的回复。

I have a probably pretty simple question but I am still not sure!

Actually I only want to smooth a histogram, and I am not sure which of the following to methods is correct. Would I do it like this:

vector<double> mask(3);
mask[0] = 0.25; mask[1] = 0.5; mask[2] = 0.25;

vector<double> tmpVect(histogram->size());
for (unsigned int i = 0; i < histogram->size(); i++)
  tmpVect[i] = (*histogram)[i];

for (int bin = 1; bin < histogram->size()-1; bin++) {
  double smoothedValue = 0;
  for (int i = 0; i < mask.size(); i++) {
    smoothedValue += tmpVect[bin-1+i]*mask[i];
  }
  (*histogram)[bin] = smoothedValue;
}

Or would you usually do it like this?:

vector<double> mask(3);
mask[0] = 0.25; mask[1] = 0.5; mask[2] = 0.25;

for (int bin = 1; bin < histogram->size()-1; bin++) {
  double smoothedValue = 0;
  for (int i = 0; i < mask.size(); i++) {
    smoothedValue += (*histogram)[bin-1+i]*mask[i];
  }
  (*histogram)[bin] = smoothedValue;
}

My Questin is: Is it resonable to copy the histogram in a extra vector first so that when I smooth at bin i I can use the original i-1 value or would I simply do smoothedValue += (*histogram)[bin-1+i]*mask[i];, so that I use the already smoothed i-1 value instead the original one.

Regards & Thanks for a reply.

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

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

发布评论

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

评论(1

涙—继续流 2024-09-15 14:24:23

你的直觉是对的:你需要一个临时向量。否则,您最终将使用部分旧值和部分新值,并且结果将不正确。用一个简单的例子在纸上亲自尝试一下。

有两种方法可以编写此算法:

  1. 首先将数据复制到临时向量;然后从该数据中读取数据,并将其写入直方图。这就是您在第一个代码片段中所做的事情。
  2. 直方图读取并写入临时向量;然后从临时向量复制回直方图。

为了防止不必要的数据复制,您可以使用vector::swap。这是交换两个向量内容的极其快速的操作。使用上面的策略 2,这将导致:

vector<double> mask(3);
mask[0] = 0.25; mask[1] = 0.5; mask[2] = 0.25;

vector<double> newHistogram(histogram->size());

for (int bin = 1; bin < histogram->size()-1; bin++) {
  double smoothedValue = 0;
  for (int i = 0; i < mask.size(); i++) {
    smoothedValue += (*histogram)[bin-1+i]*mask[i];
  }
  newHistogram[bin] = smoothedValue;
}

histogram->swap(newHistogram);

Your intuition is right: you need a temporary vector. Otherwise, you will end up using partly old values, and partly new values, and the result will not be correct. Try it yourself on paper with a simple example.

There are two ways you can write this algorithm:

  1. Copy the data to a temporary vector first; then read from that one, and write to histogram. This is what you did in your first code fragment.
  2. Read from histogram and write to a temporary vector; then copy from the temporary vector back to histogram.

To prevent needless copying of data, you can use vector::swap. This is an extremely fast operation that swaps the contents of two vectors. Using strategy 2 above, this would result in:

vector<double> mask(3);
mask[0] = 0.25; mask[1] = 0.5; mask[2] = 0.25;

vector<double> newHistogram(histogram->size());

for (int bin = 1; bin < histogram->size()-1; bin++) {
  double smoothedValue = 0;
  for (int i = 0; i < mask.size(); i++) {
    smoothedValue += (*histogram)[bin-1+i]*mask[i];
  }
  newHistogram[bin] = smoothedValue;
}

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