# 图像处理卷积算法实现

今天心血来潮，想把传统的卷积算法实现一份不采用各种加速方式，仅优化算法逻辑的纯净版本。

inline unsigned char Clamp2Byte(int n) {
return (((255 - n) >> 31) | (n & ~(n >> 31)));
}
void Convolution2D(unsigned char * data, unsigned int width, unsigned int height, unsigned int channels, int * filter, unsigned char filterW, unsigned char cfactor, unsigned char bias) {
unsigned char * tmpData = (unsigned char * ) malloc(width * height * channels);
int factor = 256 / cfactor;
int halfW = filterW / 2;
if (channels == 3 || channels == 4) {
for (int y = 0; y < height; y++) {
int y1 = y - halfW + height;
for (int x = 0; x < width; x++) {
int x1 = x - halfW + width;
int r = 0;
int g = 0;
int b = 0;
unsigned int p = (y * width + x) * channels;
for (unsigned int fx = 0; fx < filterW; fx++) {
int dx = (x1 + fx) % width;
int fidx = fx * (filterW);
for (unsigned int fy = 0; fy < filterW; fy++) {
int pos = (((y1 + fy) % height) * width + dx) * channels;
int * pfilter = & filter[fidx + (fy)];
r += data[pos] * ( * pfilter);
g += data[pos + 1] * ( * pfilter);
b += data[pos + 2] * ( * pfilter);
}
}
tmpData[p] = Clamp2Byte(((factor * r) >> 8) + bias);
tmpData[p + 1] = Clamp2Byte(((factor * g) >> 8) + bias);
tmpData[p + 2] = Clamp2Byte(((factor * b) >> 8) + bias);
}
}
} else
if (channels == 1) {
for (int y = 0; y < height; y++) {
int y1 = y - halfW + height;
for (int x = 0; x < width; x++) {
int r = 0;
unsigned int p = (y * width + x);
int x1 = x - halfW + width;
for (unsigned int fx = 0; fx < filterW; fx++) {
int dx = (x1 + fx) % width;
int fidx = fx * (filterW);
for (unsigned int fy = 0; fy < filterW; fy++) {
int pos = (((y1 + fy) % height) * width + dx);
int szfilter = filter[fidx + (fy)];
r += data[pos] * szfilter;
}
}
tmpData[p] = Clamp2Byte(((factor * r) >> 8) + bias);
}
}
}
memcpy(data, tmpData, width * height * channels);

free(tmpData);
}

调用例子：

例子
//模糊
int Blurfilter[25] = {
0, 0, 1, 0, 0,
0, 1, 1, 1, 0,
1, 1, 1, 1, 1,
0, 1, 1, 1, 0,
0, 0, 1, 0, 0,
};
Convolution2D(imgData, imgWidth, imgHeight, imgChannels, Blurfilter, 5, 13, 0);

// 运动模糊
int MotionBlurfilter[81] = {
1, 0, 0, 0, 0, 0, 0, 0, 0,
0, 1, 0, 0, 0, 0, 0, 0, 0,
0, 0, 1, 0, 0, 0, 0, 0, 0,
0, 0, 0, 1, 0, 0, 0, 0, 0,
0, 0, 0, 0, 1, 0, 0, 0, 0,
0, 0, 0, 0, 0, 1, 0, 0, 0,
0, 0, 0, 0, 0, 0, 1, 0, 0,
0, 0, 0, 0, 0, 0, 0, 1, 0,
0, 0, 0, 0, 0, 0, 0, 0, 1
};
Convolution2D(imgData, imgWidth, imgHeight, imgChannels, MotionBlurfilter, 9, 9, 0);

//边缘探测1
int edges1filter[25] = {
-1, 0, 0, 0, 0,
0, -2, 0, 0, 0,
0, 0, 6, 0, 0,
0, 0, 0, -2, 0,
0, 0, 0, 0, -1,
};
Convolution2D(imgData, imgWidth, imgHeight, imgChannels, edges1filter, 5, 1, 0);

//边缘探测2
int edges2filter[9] = {
-1, -1, -1, -1, 8, -1, -1, -1, -1
};
Convolution2D(imgData, imgWidth, imgHeight, imgChannels, edges2filter, 3, 1, 0);

//锐化1
int sharpen1filter[9] = {
-1, -1, -1, -1, 9, -1, -1, -1, -1
};
Convolution2D(imgData, imgWidth, imgHeight, imgChannels, sharpen1filter, 3, 1, 0);
//锐化2
int sharpen2filter[25] = {
-1, -1, -1, -1, -1, -1, 2, 2, 2, -1, -1, 2, 8, 2, -1, -1, 2, 2, 2, -1, -1, -1, -1, -1, -1,
};
Convolution2D(imgData, imgWidth, imgHeight, imgChannels, sharpen2filter, 5, 8, 0);
//锐化3
int sharpen3filter[9] = {
1, 1, 1,
1, -7, 1,
1, 1, 1
};
Convolution2D(imgData, imgWidth, imgHeight, imgChannels, sharpen3filter, 3, 1, 0);

// 浮雕1
int Embossfilter[9] = {
-1, -1, 0, -1, 0, 1,
0, 1, 1
};
Convolution2D(imgData, imgWidth, imgHeight, imgChannels, Embossfilter, 3, 1, 128);
// 浮雕2
int emboss2filter[25] = {
-1, -1, -1, -1, 0, -1, -1, -1, 0, 1, -1, -1, 0, 1, 1, -1, 0, 1, 1, 1,
0, 1, 1, 1, 1
};
Convolution2D(imgData, imgWidth, imgHeight, imgChannels, emboss2filter, 5, 1, 128);
// 均值模糊1
int meanfilter[9] = {
1, 1, 1,
1, 1, 1,
1, 1, 1
};
Convolution2D(imgData, imgWidth, imgHeight, imgChannels, meanfilter, 3, 9, 0);
// 均值模糊2
int mean2filter[81] = {
1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1
};
Convolution2D(imgData, imgWidth, imgHeight, imgChannels, mean2filter, 9, 81, 0);

博主在一张大小为960x1280的图片,进行了边缘探测卷积核的处理,在博主机子上耗时是100毫秒。

//边缘探测1
int edges1filter[25] = {
-1, 0, 0, 0, 0,
0, -2, 0, 0, 0,
0, 0, 6, 0, 0,
0, 0, 0, -2, 0,
0, 0, 0, 0, -1,
};
Convolution2D(imgData, imgWidth, imgHeight, imgChannels, edges1filter, 5, 1, 0);

效果图：

0 条评论

• ### 双边滤波算法的简易实现bilateralFilter

没怎么看过双边滤波的具体思路，动手写一写，看看能不能突破一下。 最后，感觉算法还是要分开 水平 与 垂直 方向进行分别处理，才能把速度提上去。 没耐性写下去了，...

• ### 自动红眼移除算法 附c++完整代码

“红眼”一般是指在人物摄影时，当闪光灯照射到人眼的时候，瞳孔放大而产生的视网膜泛红现象。

• ### C++中动态申请数组

动态申请一维数组 申请使用new，释放使用delete[] 可以通过数组名[下标]和*(数组名+下标)的方式访问数组

• ### Java中实现找到两个数组交集的2种方法，开发实用

1、用HashSet实现的解决方法 实例代码如下： public int[] intersection(int[] nums1, int[] nums2) { ...

• ### L3-004. 肿瘤诊断

在诊断肿瘤疾病时，计算肿瘤体积是很重要的一环。给定病灶扫描切片中标注出的疑似肿瘤区域，请你计算肿瘤的体积。

• ### NYOJ 116 士兵杀敌(二) (线段树+树状数组)

题目链接：http://acm.nyist.edu.cn/JudgeOnline/problem.php?pid=116

• ### 最少联通代价(dfs+曼哈顿距离)

现在要把这 2 个连通块连通， 求最少需要把几个’.’转变成’X’。上图的例子中， 最少只需要把 3个’.’转变成’X’。下图用’*’表示转化为’X’的格点...

• ### BZOJ5249: [2018多省省队联测]IIIDX(线段树 贪心)

不难发现题目给出的是一个树，其中$$\frac{i}{K}$$是$$i$$的父亲节点