1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
| __global__ void GaussianBlurCore(PtrStepSz<uchar3> inputMat, PtrStepSz<uchar3> outputMat) { int tidx = threadIdx.x + blockDim.x * blockIdx.x; int tidy = threadIdx.y + blockDim.y * blockIdx.y;
float blur[5][5] = { {0.00390625, 0.015625, 0.0234375, 0.015625, 0.00390625}, {0.015625, 0.0625, 0.09375, 0.0625, 0.015625 }, {0.0234375, 0.09375, 0.140625, 0.09375, 0.0234375 }, {0.015625, 0.0625, 0.09375, 0.0625, 0.015625 }, {0.00390625, 0.015625, 0.0234375, 0.015625, 0.00390625} };
int img_cols_max_index = inputMat.cols - 1; int img_rows_max_index = inputMat.rows - 1;
if (tidx < outputMat.cols && tidy < outputMat.rows) { float b, g, r; for (int x = -2; x <= 2; x++) { for (int y = -2; y <= 2; y++) { int image_x = tidx + x; if (image_x < 0) { image_x = abs(image_x); } if (image_x >= inputMat.cols) { image_x = img_cols_max_index - (x - (img_cols_max_index - tidx)); } int image_y = tidy + y; if (image_y < 0) { image_y = abs(image_y); } if (image_y >= inputMat.rows) { image_y = img_rows_max_index - (y - (img_rows_max_index - tidy)); } b += (float)inputMat(image_y, image_x).x * (float)blur[y + 2][x + 2]; g += (float)inputMat(image_y, image_x).y * (float)blur[y + 2][x + 2]; r += (float)inputMat(image_y, image_x).z * (float)blur[y + 2][x + 2]; } } outputMat(tidy, tidx).x = (int)b; outputMat(tidy, tidx).y = (int)g; outputMat(tidy, tidx).z = (int)r; } }
extern "C" Mat GaussianBlurGpu(Mat img) { GpuMat inputMat(img);
int width = img.cols; int height = img.rows; auto outputMat = GpuMat(height, width, CV_8UC3);
dim3 block(32, 32); dim3 grid((width + block.x - 1) / block.x, (height + block.y - 1) / block.y); GaussianBlurCore << <grid, block >> > (inputMat, outputMat);
cudaThreadSynchronize();
Mat dstImg; outputMat.download(dstImg); return dstImg; }
__global__ void GaussianBlurFourfoldCore(PtrStepSz<uchar3> inputMat, PtrStepSz<uchar3> outputMat) { int tidx = threadIdx.x + blockDim.x * blockIdx.x; int tidy = threadIdx.y + blockDim.y * blockIdx.y;
float blur[5][5] = { {0.00390625, 0.015625, 0.0234375, 0.015625, 0.00390625}, {0.015625, 0.0625, 0.09375, 0.0625, 0.015625 }, {0.0234375, 0.09375, 0.140625, 0.09375, 0.0234375 }, {0.015625, 0.0625, 0.09375, 0.0625, 0.015625 }, {0.00390625, 0.015625, 0.0234375, 0.015625, 0.00390625} };
int img_cols_max_index = inputMat.cols - 1; int img_rows_max_index = inputMat.rows - 1;
if (tidx < outputMat.cols && tidy < outputMat.rows) { float b, g, r; for (int x = -2; x <= 2; x++) { for (int y = -2; y <= 2; y++) { int image_x = tidx + x; if (image_x < 0) { image_x = abs(image_x); } if (image_x >= inputMat.cols) { image_x = img_cols_max_index - (x - (img_cols_max_index - tidx)); } int image_y = tidy + y; if (image_y < 0) { image_y = abs(image_y); } if (image_y >= inputMat.rows) { image_y = img_rows_max_index - (y - (img_rows_max_index - tidy)); } b += (float)inputMat(image_y, image_x).x * ((float)blur[y + 2][x + 2] * 4); g += (float)inputMat(image_y, image_x).y * ((float)blur[y + 2][x + 2] * 4); r += (float)inputMat(image_y, image_x).z * ((float)blur[y + 2][x + 2] * 4); } } outputMat(tidy, tidx).x = (int)b; outputMat(tidy, tidx).y = (int)g; outputMat(tidy, tidx).z = (int)r; } }
extern "C" Mat GaussianBlurFourfoldGpu(Mat img) { GpuMat inputMat(img);
int width = img.cols; int height = img.rows; auto outputMat = GpuMat(height, width, CV_8UC3);
dim3 block(32, 32); dim3 grid((width + block.x - 1) / block.x, (height + block.y - 1) / block.y); GaussianBlurFourfoldCore << <grid, block >> > (inputMat, outputMat);
cudaThreadSynchronize();
Mat dstImg; outputMat.download(dstImg); return dstImg; }
|