1+ // OpenCVTest.cpp : 定义控制台应用程序的入口点。
2+ //
3+
4+ #include " stdafx.h"
5+
6+ #include < iostream>
7+ #include < stdio.h>
8+ #include < cblas.h>
9+ #include < opencv2/opencv.hpp>
10+
11+ using namespace std ;
12+ using namespace cv ;
13+
14+ // 使用OpenMp和OpenBlas加速filter2D(RGB度图),且卷积核的长宽相等,这里实现的是长宽均为3且通道数为3的卷积核(相当于Tensor)
15+ // A加pad的运算
16+ void get_Pad (int pad_Height, int pad_Width, int row, int col, int channel, float *A_pad, const float *A) {
17+ int pad_x = (pad_Height - row) >> 1 ;
18+ int pad_y = (pad_Width - col) >> 1 ;
19+ printf (" pad_x: %d pad_y: %d\n " , pad_x, pad_y);
20+ const int pad_one_channel = pad_Height * pad_Width;
21+ const int org_one_channel = row * col;
22+ for (int c = 0 ; c < channel; c++) {
23+ for (int i = 0 ; i < pad_Height; i++) {
24+ for (int j = 0 ; j < pad_Width; j++) {
25+ int index = c * pad_one_channel + i * pad_Height + j;
26+ if (i <= pad_x || i + pad_x > pad_Height) {
27+ A_pad[index] = 0 ;
28+ }
29+ else {
30+ if (j <= pad_y || j + pad_y > pad_Width) {
31+ A_pad[index] = 0 ;
32+ }
33+ else {
34+ A_pad[index] = A[c * org_one_channel + (i - 1 ) * row + j - 1 ];
35+ }
36+ }
37+ }
38+ }
39+ }
40+ }
41+
42+
43+ int main () {
44+ // RGB图
45+ Mat src = cv::imread (" F:\\ 1.jpg" );
46+ int row = src.rows ;
47+ int col = src.cols ;
48+ int channel = src.channels ();
49+ float *A = new float [row * col * channel];
50+ int cnt = 0 ;
51+ for (int k = 0 ; k < channel; k++) {
52+ for (int i = 0 ; i < row; i++) {
53+ for (int j = 0 ; j < col; j++) {
54+ A[cnt++] = 1.0 * src.at <Vec3b>(i, j)[k];
55+ }
56+ }
57+ }
58+ const int KernelHeight = 3 ;
59+ const int KernelWidth = 3 ;
60+ float *B = new float [KernelHeight * KernelWidth * channel];
61+ cnt = 0 ;
62+ // 这里随便赋值测试
63+ for (int k = 0 ; k < channel; k++) {
64+ for (int i = 0 ; i < KernelHeight; i++) {
65+ for (int j = 0 ; j < KernelWidth; j++) {
66+ B[cnt++] = k + 1 ;
67+ }
68+ }
69+ }
70+ // 卷积核参数初始化为
71+ const int pad = (KernelHeight - 1 ) / 2 ; // 需要pad的长度
72+ const int stride = 1 ; // 卷积核滑动的步长
73+ const int OutHeight = (row - KernelHeight + 2 * pad) / stride + 1 ;
74+ const int OutWidth = (col - KernelWidth + 2 * pad) / stride + 1 ;
75+ // 计算3维pad_A
76+ const int pad_Height = row + 2 * pad;
77+ const int pad_Width = col + 2 * pad;
78+ float *A_pad = new float [pad_Height * pad_Width * channel];
79+ get_Pad (pad_Height, pad_Width, row, col, channel, A_pad, A);
80+ //
81+ }
0 commit comments