直方图平滑
我有一个可能非常简单的问题,但我仍然不确定!
实际上我只想平滑直方图,并且我不确定以下哪种方法是正确的。我会这样做吗:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你的直觉是对的:你需要一个临时向量。否则,您最终将使用部分旧值和部分新值,并且结果将不正确。用一个简单的例子在纸上亲自尝试一下。
有两种方法可以编写此算法:
直方图
读取并写入临时向量;然后从临时向量复制回直方图。为了防止不必要的数据复制,您可以使用
vector::swap
。这是交换两个向量内容的极其快速的操作。使用上面的策略 2,这将导致: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:
histogram
. This is what you did in your first code fragment.histogram
and write to a temporary vector; then copy from the temporary vector back tohistogram
.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: