在C ++/C中模糊图像 [英] Bluring an image in C++/C
问题描述
所以我正在盯着一个有关使用C ++进行图像处理的项目.
So I'm staring a project about image processing with C++.
问题是,我在网上找到的所有相关信息(在C ++中将图像模糊化)都随CUDA或OpenCV一起提供.
The thing is that everything I find online about this matter (blurring an image in C++) comes either with CUDA or with OpenCV.
有没有一种方法可以仅使用C ++来模糊图像?(对于初学者)
Is there a way to blur an image with C++ only? (for starters)
如果是,可以请他人分享代码或进行解释吗?
If yes, can somebody please share the code or explain?
谢谢!
推荐答案
首先,您需要在内存中存储图像.
Firstly you need the image in memory.
然后,您需要第二个缓冲区用作工作区.
Then you need a second buffer to use as a workspace.
然后,您需要一个过滤器.常见的过滤器是
Then you need a filter. A common filter would be
1 4 1
4 -20 4
1 4 1
对于每个像素,我们应用滤镜.因此,我们将图像设置为周围像素的加权平均值,然后进行减法以避免整个图像变亮或变暗.
For each pixel, we apply the filter. So we're setting the image to a weighted average of the pixels around it, then subtracting to avoid the overall image going lighter or darker.
应用小型过滤器非常简单.
Applying a small filter is very simple.
for(y=0;y<height;y++)
for(x=0;x<width;x++)
{
total = image[(y+1)*width+x+1];
for(fy=0; fy < 3; fy++)
for(fx = 0; fx < 3; fx++)
total += image[(y+fy)*width+x+fx] * filter[fy*3+x];
output[(y+1)*width+x+1] = clamp(total, 0, 255);
}
您需要对边缘进行特殊处理,这很简单,但没有增加任何理论上的复杂性.
You need to special case the edges, which is just fiddly but doesn't add any theoretical complexity.
当我们使用较幼稚的算法更快时,正确设置边缘变得很重要.然后,您可以在频域中进行计算,使用大型滤波器可以更快地进行计算.
When we use faster algorithms that the naive one it becomes important to set up edges correctly. You then do the calculations in the frequency domain and it's a lot faster with a big filter.
这篇关于在C ++/C中模糊图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!