▶ CPU 图像卷积,共四种方法。分别为基本串行,使用模板,使用局部内存,使用AVX指令优化

● 全部的代码,仅在主函数中选择调用的函数名即可。

 #include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <opencv2/opencv.hpp> const char *inputFile = "R:/1.png";
const char *outputFile = "R:/2.png"; bool floatEq(const float a, const float b)// 相等返回 1
{
if (b == )
return fabs(a) < 0.001;
return fabs(a / b - ) < 0.001;
} void convolution01(const float *input, float *output, const int inputRow, const int inputCol,
const float *filter, const int filterWidth)
{
const int halfFilterWidth = filterWidth / ;
int row, col, rr, cc;
float sum; //memset(output, 0, sizeof(float) * inputRow * inputCol); // 使用 memset 将 output 全部凃成 0 #pragma omp parallel for num_threads(8) default(none) shared(halfFilterWidth, output, inputRow, inputCol) private(row, col)
for (row = ; row < halfFilterWidth; row++) // 人工将边角涂成 0
{
for (col = ; col < inputCol; col++)
output[row*inputCol + col] = output[(inputRow - - row)*inputCol + col] = ;
}
#pragma omp parallel for num_threads(8) default(none) shared(halfFilterWidth, output, inputRow, inputCol) private(row, col)
for (row = halfFilterWidth; row < inputRow - halfFilterWidth; row++)
{
for (col = ; col < halfFilterWidth; col++)
output[row*inputCol + col] = output[row*inputCol + inputCol - - col] = ;
} #pragma omp parallel for num_threads(8) default(none) shared(halfFilterWidth, input, output, inputRow, inputCol, filter) private(row, col, rr, cc, sum)
for (row = halfFilterWidth; row < inputRow - halfFilterWidth; row++)// 内部计算部分
{
for (col = halfFilterWidth; col < inputCol - halfFilterWidth; col++)
{
for (sum = 0.0f, rr = -halfFilterWidth; rr <= halfFilterWidth; rr++)
{
for (cc = -halfFilterWidth; cc <= halfFilterWidth; cc++)
sum += filter[(rr + halfFilterWidth) * filterWidth + cc + halfFilterWidth] * input[(row + rr)*inputCol + col + cc];
}
output[row * inputCol + col] = sum;
}
}
/*
for (row = 0; row < inputRow; row++) // 全范围循环,在最里层判断
{
for (col = 0; col < inputCol; col++)
{
if (row < halfFilterWidth || row >= inputRow - halfFilterWidth || col < halfFilterWidth || col >= inputCol - halfFilterWidth)
{
output[row * inputCol + col] = 0;
continue;
}
for (sum = 0.0f, rr = -halfFilterWidth; rr <= halfFilterWidth; rr++)
{
for (cc = -halfFilterWidth; cc <= halfFilterWidth; cc++)
sum += filter[(rr + halfFilterWidth) * filterWidth + cc + halfFilterWidth] * input[(row + rr)*inputCol + col + cc];
}
output[row * inputCol + col] = sum; }
}
*/
} template<int filterWidth>
void convolution02(const float *input, float *output, const int inputRow, const int inputCol, const float *filter)// 卷积宽度作为模板
{
const int halfFilterWidth = filterWidth / ;
int row, col, rr, cc;
float sum; memset(output, , sizeof(float) * inputRow * inputCol);
#pragma omp parallel for num_threads(8) default(none) shared(halfFilterWidth, input, output, inputRow, inputCol, filter) private(row, col, rr, cc, sum)
for (row = halfFilterWidth; row < inputRow - halfFilterWidth; row++)
{
for (col = halfFilterWidth; col < inputCol - halfFilterWidth; col++)
{
for (sum = 0.0f, rr = -halfFilterWidth; rr <= halfFilterWidth; rr++)
{
for (cc = -halfFilterWidth; cc <= halfFilterWidth; cc++)
sum += filter[(rr + halfFilterWidth) * filterWidth + cc + halfFilterWidth] * input[(row + rr)*inputCol + col + cc];
}
output[row * inputCol + col] = sum;
}
}
} template<int filterWidth, int blockRow, int blockCol>
void convolution03(const float *input, float *output, const int inputRow, const int inputCol, const float *filter)// 使用局部内存块
{
const int halfFilterWidth = filterWidth / ;
int row, col, rr, cc, i, j;
float filterElement; if (inputRow % blockRow || inputCol % blockCol) // 要求图片长宽为局部内存块的整数倍
{
printf("Failed, outputRow %% blockRow || outputCol %% blockCol\n");
return;
} memset(output, , sizeof(float) * inputRow * inputCol);
#pragma omp parallel for num_threads(8)
for (row = halfFilterWidth; row < inputRow - halfFilterWidth; row += blockRow)
{
for (col = halfFilterWidth; col < inputCol - halfFilterWidth; col += blockCol)
{
float sum[blockRow * blockCol] = { 0.0f };
for (rr = -halfFilterWidth; rr <= halfFilterWidth; rr++)
{
for (cc = -halfFilterWidth; cc <= halfFilterWidth; cc++)
{
filterElement = filter[(rr + halfFilterWidth) * filterWidth + cc + halfFilterWidth];
for (i = ; i < blockRow; i++)
{
for (j = ; j < blockCol; j++)
{
if (row + rr + i >= inputRow || col + cc + j >= inputCol)
break;
sum[i * blockCol + j] += filterElement * input[(row + rr + i) * inputCol + col + cc + j];
}
}
}
}
for (i = ; i < blockRow; i++)
{
for (j = ; j < blockCol; j++)
{
if (row + i >= inputRow || col + j >= inputCol)
continue;
output[(row + i) * inputCol + col + j] = sum[i * blockCol + j];
}
}
}
}
} template<int filterWidth, int blockRow, int blockCol>
void convolution04(const float *input, float *output, const int inputRow, const int inputCol, const float *filter)// 使用 AVX 指令扩展
{
const int halfFilterWidth = filterWidth / ;
int row, col, rr, cc, i, j; if (inputRow % blockRow || inputCol % (blockCol * ))
{
printf("Failed, inputRow %% blockRow || inputCol %% blockCol\n");
return;
} memset(output, , sizeof(float) * inputRow * inputCol);
#pragma omp parallel for num_threads(8)
for (row = halfFilterWidth; row < inputRow - halfFilterWidth; row += blockRow)
{
for (col = halfFilterWidth; col < inputCol - halfFilterWidth; col += blockCol * )
{
__m256 sum[blockRow * blockCol] = {_mm256_setzero_ps()};
for (rr = -halfFilterWidth; rr <= halfFilterWidth; rr++)
{
for (cc = -halfFilterWidth; cc <= halfFilterWidth; cc++)
{
__m256 filterElement = _mm256_broadcast_ss(filter + (rr + halfFilterWidth) * filterWidth + cc + halfFilterWidth);
for (i = ; i < blockRow; i++)
{
for (j = ; j < blockCol; j++)
{
//if (row + rr + i >= inputRow || col + cc + j * 8 >= inputCol)// 在局部内存块较大时需要越界检查
// continue;
__m256 imageElement = _mm256_loadu_ps(input + (row + rr + i)*inputCol + col + cc + j * );
sum[i * blockCol + j] = _mm256_fmadd_ps(filterElement, imageElement, sum[i * blockCol + j]);
}
}
}
}
for (i = ; i < blockRow; i++)
{
for (j = ; j < blockCol; j++)
{
//if (row + i >= inputRow || col + j * 8 >= inputCol)
// continue;
_mm256_storeu_ps(output + (row + i)*inputCol + col + j * , sum[i * blockCol + j]);
}
}
}
}
} int main()
{
int i, k;
clock_t time;
float filterSum; // 卷积窗口相关
const int filterWidth = , filterSize = filterWidth * filterWidth, halfFilterWidth = filterWidth / ;
float filter[filterSize] =
{// 模糊窗口
, , , , ,
, .f / , .f / , .f / , ,
, .f / , .f / , .f / , ,
, .f / , .f / , .f / , ,
, , , ,
};
for (filterSum = 0.0f, i = ; i < filterSize; filterSum += filter[i++]);
if (!floatEq(filterSum, ))// 非归零的卷积窗口(如模糊)需要归一化
for (i = ; i < filterSize; filter[i] /= filterSum, i++); // 图片相关
cv::Mat input = cv::imread(inputFile), output = input, channel[];
cv::split(input, channel);
const int inputRow = input.rows, inputCol = input.cols, inputDataSize = inputRow * inputCol;
float *inputData = (float*)malloc(sizeof(float) * inputDataSize);
float *outputData = (float*)malloc(sizeof(float) * inputDataSize); for (k = ; k < ; k++)// 三个通道,分别为蓝、绿、红
{
for (i = ; i < inputRow * inputCol; inputData[i] = (float)channel[k].data[i], i++);
time = clock();
convolution01(inputData, outputData, inputRow, inputCol, (const float *)filter, filterWidth);
//convolution02<filterWidth>(inputData, outputData, inputRow, inputCol, filter);
//convolution03<filterWidth, 4, 4>(inputData, outputData, inputRow, inputCol, filter);
//convolution04<filterWidth, 4, 4>(inputData, outputData, inputRow, inputCol, filter);
time = clock() - time;
printf("Time for channel[%d]:%d ms\n", k, time);
for (i = ; i < inputRow * inputCol; channel[k].data[i] = (unsigned char)outputData[i], i++);
} cv::merge(channel, , output);
cv::imwrite(outputFile, output);
//imshow("merge", output);
//cv::waitKey(0);
free(inputData);
free(outputData);
printf("\nFinished.\n");
getchar();
return ;
}

● 输出结果,使用一张 4608 × 6656 的图片(bmp87.7MB)进行测试,使用主函数中那个边长为5、实际窗口长度为 3 的均值窗口。图片太大喘不上来,偷梁换柱成小图看效果

     

■ 计时结果

// convolution01,memset + 内部计算,无 OpenMP
Time for channel[]: ms
Time for channel[]: ms
Time for channel[]: ms Finished. // convolution01,手动除边 + 内部计算,无 OpenMP
Time for channel[]: ms
Time for channel[]: ms
Time for channel[]: ms Finished. // convolution01,循环内判断,无 OpenMP
Time for channel[]: ms
Time for channel[]: ms
Time for channel[]: ms Finished. // convolution01,手动除边 + 内部计算,有 OpenMP
Time for channel[]: ms
Time for channel[]: ms
Time for channel[]: ms Finished. // convolution02,有 OpenMP
Time for channel[]: ms
Time for channel[]: ms
Time for channel[]: ms Finished. // convolution03<filterWidth, 4, 4>,无 OpenMP
Time for channel[]: ms
Time for channel[]: ms
Time for channel[]: ms Finished. // convolution04<filterWidth, 4, 4>,无 OpenMP
Time for channel[]: ms
Time for channel[]: ms
Time for channel[]: ms Finished.

■ 没法给 convolution03 和 convolution04 加 OpenMP,一加就各种内存冲突,便捷判断都挡不住。

OpenCL 图像卷积 3 使用 CPU的更多相关文章

  1. OpenCL 图像卷积 2

    ▶ 上一篇图像卷积 http://www.cnblogs.com/cuancuancuanhao/p/8535569.html.这篇使用了 OpenCV 从文件读取彩色的 jpeg 图像,进行边缘检测 ...

  2. OpenCL 图像卷积 1

    ▶ 书上的代码改进而成,从文件读入一张 256 阶灰度图,按照给定的卷积窗口计算卷积,并输出到文件中. ● 代码,使用 9 格的均值窗口,居然硬读写 .bmp 文件,算是了解一下该文件的具体格式,留作 ...

  3. SSE图像算法优化系列十一:使用FFT变换实现图像卷积。

    本文重点主要不在于FFT的SSE优化,而在于使用FFT实现快速卷积的相关技巧和过程. 关于FFT变换,有很多参考的代码,特别是对于长度为2的整数次幂的序列,实现起来也是非常简易的,而对于非2次幂的序列 ...

  4. 图像卷积、相关以及在MATLAB中的操作

    图像卷积.相关以及在MATLAB中的操作 2016年7月11日 20:34:35, By ChrisZZ 区分卷积和相关 图像处理中常常需要用一个滤波器做空间滤波操作.空间滤波操作有时候也被叫做卷积滤 ...

  5. zz图像卷积与滤波的一些知识点

    Xinwei: 写的通俗易懂,终于让我这个不搞CV.不搞图像的外行理解卷积和滤波了. 图像卷积与滤波的一些知识点 zouxy09@qq.com http://blog.csdn.net/zouxy09 ...

  6. 对抗生成网络-图像卷积-mnist数据生成(代码) 1.tf.layers.conv2d(卷积操作) 2.tf.layers.conv2d_transpose(反卷积操作) 3.tf.layers.batch_normalize(归一化操作) 4.tf.maximum(用于lrelu) 5.tf.train_variable(训练中所有参数) 6.np.random.uniform(生成正态数据

    1. tf.layers.conv2d(input, filter, kernel_size, stride, padding) # 进行卷积操作 参数说明:input输入数据, filter特征图的 ...

  7. UFLDL教程笔记及练习答案五(自编码线性解码器与处理大型图像**卷积与池化)

    自己主动编码线性解码器 自己主动编码线性解码器主要是考虑到稀疏自己主动编码器最后一层输出假设用sigmoid函数.因为稀疏自己主动编码器学习是的输出等于输入.simoid函数的值域在[0,1]之间,这 ...

  8. TensorFlow实现图像卷积并可视化示例

    图片尺寸要自己修改. 看起来好像没啥意思,不知道下一步能干什么,先卷了再说.由于weights是随机生成的(tf.random_normal作用:用于从服从指定正太分布的数值中取出随机数),所以每次卷 ...

  9. opencv:图像卷积

    卷积基本概念 C++代码实现卷积 #include <opencv2/opencv.hpp> #include <iostream> using namespace cv; u ...

随机推荐

  1. LAMP环境安装实例

  2. 为什么我们不应该使用微信或者 QQ 作为团队协作的 IM 工具?

    如果你的团队没有觉得微信是低效的团队 IM 工具,那只有两种可能: 团队成员很少使用微信进行私人的生活和娱乐. 你就是一个低效的团队,而且还不自知. 本文内容 微信,连接一切 每个人都有微信 微信,低 ...

  3. poj2253 最短路

    题意:青蛙跳石头,给出石头的坐标,然后要确定一条路径,使路径上的最大跨度最小,其实也是一道最短路问题,只要将更新条件从总距离最短改为最大跨度最小就行,即从某点到当前点路径上的最大跨度如果小于当前点原本 ...

  4. hdu1331&&hdu1579记忆化搜索(DP+DFS)

    这两题是一模一样的``` 题意:给了一系列递推关系,但是由于这些递推很复杂,所以递推起来要花费很长的时间,所以我要编程序在有限的时间内输出答案. w(a, b, c): 如果a,b,c中有一个值小于等 ...

  5. ZOJ3545 Rescue the Rabbit

    分析 未知定长串中不同已知模板串的出现次数问题,一般做法是AC自动机上dp. 考虑背包,\(dp(i,j,k)\)表示当前串长为\(i\),在AC自动机上对应节点\(j\),已匹配的模板串的状态为\( ...

  6. day13 python学习 迭代器,生成器

    1.可迭代:当我们打印 print(dir([1,2]))   在出现的结果中可以看到包含 '__iter__', 这个方法,#次协议叫做可迭代协议 包含'__iter__'方法的函数就是可迭代函数 ...

  7. torodb docker 运行试用

    torodb 可以方便的让你迁移到pg,同时使用标准原生的sql 查询 使用官方的docker-compose 进行测试 环境准备 docker-compose 文件 wget https://raw ...

  8. TFTP error: 'Only absolute filenames allowed' (2)

    hisilicon # tftp 0x82000000 u-boot-hi3518ev200.bin Hisilicon ETH net controler MAC: ----- eth0 : phy ...

  9. Jmeter中Websocket协议支持包的使用(转)

    转自:http://blog.csdn.net/typing_yes_no/article/details/49512167 参考的来源是国外一篇文章,已经整理成pdf格式(http://yunpan ...

  10. Zigbee 的 mesh功能设置

    1. 在编译选项中加入ZIGBEEPRO 最大的节点深度为20,网络模式为mesh网络 如果不配置,则为HOME_CONTROLS,支持5级路由深度,每个路由器最多可连接20个节点(最多包括6个路由器 ...