我正在尝试制作高斯模糊图像滤镜的移动快速版本。我已经阅读了其他问题,例如:Fast Gaussian blur on unsigned char image- ARM Neon Intrinsics- iOS Dev。为了我的目的,我只需要一个固定大小(7x7)和固定sigma(2)的高斯滤波器。因此,在优化ARM NEON之前,我正在使用C++实现1D高斯核,并将其与OpenCV GaussianBlur()方法在移动环境(带有NDK的Android)中直接进行性能比较。这样可以得到一个简单得多的代码来优化。然而,结果是我的实现比OpenCV4Android版本慢10倍。我已经阅读了OpenCV4 Tegra有优化的GaussianBlur实现,但我不认为标准的OpenCV4Android有这种优化,那么我的代码为什么这么慢呢?以下是我的实现(注意:在应用滤镜靠近边界时,使用reflect101进行像素反射):
Mat myGaussianBlur(Mat src){
Mat dst(src.rows, src.cols, CV_8UC1);
Mat temp(src.rows, src.cols, CV_8UC1);
float sum, x1, y1;
// coefficients of 1D gaussian kernel with sigma = 2
double coeffs[] = {0.06475879783, 0.1209853623, 0.1760326634, 0.1994711402, 0.1760326634, 0.1209853623, 0.06475879783};
//Normalize coeffs
float coeffs_sum = 0.9230247873f;
for (int i = 0; i < 7; i++){
coeffs[i] /= coeffs_sum;
}
// filter vertically
for(int y = 0; y < src.rows; y++){
for(int x = 0; x < src.cols; x++){
sum = 0.0;
for(int i = -3; i <= 3; i++){
y1 = reflect101(src.rows, y - i);
sum += coeffs[i + 3]*src.at<uchar>(y1, x);
}
temp.at<uchar>(y,x) = sum;
}
}
// filter horizontally
for(int y = 0; y < src.rows; y++){
for(int x = 0; x < src.cols; x++){
sum = 0.0;
for(int i = -3; i <= 3; i++){
x1 = reflect101(src.rows, x - i);
sum += coeffs[i + 3]*temp.at<uchar>(y, x1);
}
dst.at<uchar>(y,x) = sum;
}
}
return dst;
}