C++?OpenCV實現(xiàn)boxfilter方框濾波的方法詳解
box filter簡單解釋
box filter的作用很簡單,即對局部區(qū)域求平均,并把值賦給某個點,一般我們賦給區(qū)域中心。用公式表達(dá)如下:

其中patch是以(row,col)為中心的一塊區(qū)域。
為了跟后面的公式及程序?qū)?yīng),我們做如下定義:
- r:patch的半徑。半徑在寬高方向可以不相等,但是本文目的不在于對半徑的處理,所以簡單起見設(shè)為相等。
- n:patch的長度,等于(2∗r+1)。
- (rows,cols):圖像的尺寸,行數(shù)和列數(shù)。
- (row,col):對完整圖像的索引。
- (i,j):對圖像patch的索引
- k:對通道的索引。
1. 暴力實現(xiàn)——四循環(huán)
外層兩個循環(huán)是關(guān)于完整圖像(row,col)的循環(huán),內(nèi)層兩個循環(huán)是關(guān)于圖像patch(i,j)的循環(huán)。
注意:如果圖像是多通道的話實際上還有一個通常維度的循環(huán),但是通道數(shù)不是本文優(yōu)化的重心,所以本文不再贅述這個因素,后文也不再提,并且在計算量的估計中也會把這個因素省略掉。
這個實現(xiàn)比較簡單,需要做的計算有:
- rows∗cols∗n∗n次加法,內(nèi)層循環(huán)的計算量o(n2),非常大。
- rows∗cols次除法:除法為了求平均
2. 行列分離
patch的平均可以進(jìn)行行列分離,也就是先對行方向做平均,并緩存結(jié)果,再對緩存的結(jié)果做列方向的平均。以公式的形式表達(dá)如下:

舉個例子展開寫會容易理解,比如3*3的patch,共9個數(shù):

這種方式的計算量:
- 2∗rows∗cols∗n次加法,相對于暴力版本,內(nèi)層循環(huán)降低了一個數(shù)量級的算力,變成o(n)了
- 2∗rows∗cols次除法
3. 行列分離優(yōu)化版
第二種實現(xiàn)可以對求和做進(jìn)一步優(yōu)化。在單個維度做求和時,可以對當(dāng)前一維patch的和做一個緩存,當(dāng)中心點移動后,減去彈出像素的值,加上新增像素的值,這樣就避免了重復(fù)性求和操作。
這種方案需要對patch的和做一個初始化和緩存,該方案的計算量為:
- 2∗rows∗cols次減法,2∗rows∗cols次加法,內(nèi)層循環(huán)的計算變?yōu)閛(1)了,進(jìn)一步降低了一個數(shù)量級算力。
- 2∗rows∗cols次除法
代碼
上面做計算量估計的時候沒有考慮邊界條件,在具體代碼實現(xiàn)的時候需要仔細(xì)處理邊界,防止數(shù)組訪問越界。
代碼同時跟opencv做了個效果和性能的對比,第三種方式雖然仍然比opencv慢,但性能基本處于同一量級了,opencv可能還做了一些其他跟算法無關(guān)的優(yōu)化,比如指令集、并行化之類的。
注意:下面為了方便比較,opencv boxFilter的邊界處理參數(shù)選擇BORDER_CONSTANT。即使是邊界處patch不滿覆蓋的情況下,opencv仍然除以n2 ,也就是說除以的數(shù)字有點大了,所以邊界會逐漸發(fā)黑,特別是kernel_size(對應(yīng)于radius)比較大時候視覺效果更明顯。
#include <opencv2/opencv.hpp>
#include <iostream>
#include <vector>
#include <string>
#include <ctime>
using namespace std;
using namespace cv;
Mat BoxFilter_1(const Mat& image, int radius);
Mat BoxFilter_2(const Mat& image, int radius);
Mat BoxFilter_3(const Mat& image, int radius);
int main()
{
clock_t time_beg;
clock_t time_end;
Mat image = imread("lena_std.bmp", IMREAD_UNCHANGED);
image.convertTo(image, CV_32FC3);
image /= 255.0f;
int radius = 9;
int ksize = radius * 2 + 1;
Mat image_box_filter_cv;
time_beg = clock();
boxFilter(image, image_box_filter_cv, -1, Size(ksize, ksize), Point(-1, -1), true, BORDER_CONSTANT);
time_end = clock();
cout << "box-filter-cv time cost: " << time_end - time_beg << endl;
Mat image_box_filter_1 = BoxFilter_1(image, radius);
Mat image_box_filter_2 = BoxFilter_2(image, radius);
Mat image_box_filter_3 = BoxFilter_3(image, radius);
namedWindow("original_image", 1);
imshow("original_image", image);
namedWindow("cv_box_filter", 1);
imshow("cv_box_filter", image_box_filter_cv);
namedWindow("box_filter-1", 1);
imshow("box_filter-1", image_box_filter_1);
namedWindow("box_filter-2", 1);
imshow("box_filter-2", image_box_filter_2);
namedWindow("box_filter-3", 1);
imshow("box_filter-3", image_box_filter_3);
Mat diff;
cv::absdiff(image_box_filter_2, image_box_filter_3, diff);
namedWindow("diff", 1);
imshow("diff", 50 * diff);
waitKey(0);
destroyAllWindows();
return 0;
}
Mat BoxFilter_1(const Mat& image, int radius)
{
int cols = image.cols;
int rows = image.rows;
int channels = image.channels();
int row_bound = rows - 1;
int col_bound = cols - 1;
Mat result(rows, cols, CV_32FC3);
clock_t time_beg;
clock_t time_end;
time_beg = clock();
for (int row = 0; row < rows; ++row) {
int row_beg = max(row - radius, 0);
int row_end = min(row + radius, row_bound);
for (int col = 0; col < cols; ++col) {
int col_beg = max(col - radius, 0);
int col_end = min(col + radius, col_bound);
vector<float> sums(channels, 0.0f);
int count = 0;
for (int i = row_beg; i <= row_end; ++i) {
for (int j = col_beg; j <= col_end; ++j) {
count++;
for (int k = 0; k < channels; ++k) {
sums[k] += image.at<Vec3f>(i, j)[k];
}
}
}
for (int k = 0; k < channels; ++k) {
result.at<Vec3f>(row, col)[k] = sums[k] / static_cast<float>(count);
// opencv BORDER_CONSTANT:
/*float COUNT = (float)(2 * radius + 1) * (2 * radius + 1);
result.at<Vec3f>(row, col)[k] = sums[k] / COUNT;*/
}
}
}
result = cv::max(cv::min(result, 1.0), 0.0);
time_end = clock();
cout << "box-filter-1 time cost: " << time_end - time_beg << endl;
return result;
}
Mat BoxFilter_2(const Mat& image, int radius)
{
int cols = image.cols;
int rows = image.rows;
int channels = image.channels();
int row_bound = rows - 1;
int col_bound = cols - 1;
Mat result(rows, cols, CV_32FC3);
clock_t time_beg;
clock_t time_end;
time_beg = clock();
// compute mean for row-wise
Mat row_result(rows, cols, CV_32FC3);
for (int row = 0; row < rows; ++row) {
for (int col = 0; col < cols; ++col) {
int col_beg = max(col - radius, 0);
int col_end = min(col + radius, col_bound);
vector<float> sums(channels, 0.0f);
int count = 0;
for (int j = col_beg; j <= col_end; ++j) {
count++;
for (int k = 0; k < channels; ++k) {
sums[k] += image.at<Vec3f>(row, j)[k];
}
}
for (int k = 0; k < channels; ++k) {
row_result.at<Vec3f>(row, col)[k] = sums[k] / static_cast<float>(count);
}
}
}
// compute mean for column-wise
for (int col = 0; col < cols; ++col) {
for (int row = 0; row < rows; ++row) {
int row_beg = max(row - radius, 0);
int row_end = min(row + radius, row_bound);
vector<float> sums(channels, 0.0f);
int count = 0;
for (int i = row_beg; i <= row_end; ++i) {
count++;
for (int k = 0; k < channels; ++k) {
sums[k] += row_result.at<Vec3f>(i, col)[k];
}
}
for (int k = 0; k < channels; ++k) {
result.at<Vec3f>(row, col)[k] = sums[k] / static_cast<float>(count);
}
}
}
result = cv::max(cv::min(result, 1.0), 0.0);
time_end = clock();
cout << "box-filter-2 time cost: " << time_end - time_beg << endl;
return result;
}
Mat BoxFilter_3(const Mat& image, int radius)
{
int cols = image.cols;
int rows = image.rows;
int channels = image.channels();
Mat result(rows, cols, CV_32FC3);
clock_t time_beg;
clock_t time_end;
time_beg = clock();
// compute mean for row-wise
Mat row_result(rows, cols, CV_32FC3);
for (int row = 0; row < rows; ++row) {
// initialize sums for row
vector<float> sums(channels, 0.0f);
int count = 0;
for (int col = 0; col < radius; ++col) {
if (col < cols) {
count++;
for (int k = 0; k < channels; ++k) {
sums[k] += image.at<Vec3f>(row, col)[k];
}
}
}
// process row
for (int col = 0; col < cols; ++col) {
int left = col - radius - 1;
int right = col + radius;
if (left >= 0) {
count--;
for (int k = 0; k < channels; ++k) {
sums[k] -= image.at<Vec3f>(row, left)[k];
}
}
if (right < cols) {
count++;
for (int k = 0; k < channels; ++k) {
sums[k] += image.at<Vec3f>(row, right)[k];
}
}
for (int k = 0; k < channels; ++k) {
row_result.at<Vec3f>(row, col)[k] = sums[k] / static_cast<float>(count);
}
}
}
// compute mean for column-wise
for (int col = 0; col < cols; ++col) {
// initialize sums for column
vector<float> sums(channels, 0.0f);
int count = 0;
for (int row = 0; row < radius; ++row) {
if (row < rows) {
count++;
for (int k = 0; k < channels; ++k) {
sums[k] += row_result.at<Vec3f>(row, col)[k];
}
}
}
// process column
for (int row = 0; row < rows; ++row) {
int up = row - radius - 1;
int down = row + radius;
if (up >= 0) {
count--;
for (int k = 0; k < channels; ++k) {
sums[k] -= row_result.at<Vec3f>(up, col)[k];
}
}
if (down < rows) {
count++;
for (int k = 0; k < channels; ++k) {
sums[k] += row_result.at<Vec3f>(down, col)[k];
}
}
for (int k = 0; k < channels; ++k) {
result.at<Vec3f>(row, col)[k] = sums[k] / static_cast<float>(count);
}
}
}
result = cv::max(cv::min(result, 1.0), 0.0);
time_end = clock();
cout << "box-filter-3 time cost: " << time_end - time_beg << endl;
return result;
}以上就是C++ OpenCV實現(xiàn)boxfilter方框濾波的方法詳解的詳細(xì)內(nèi)容,更多關(guān)于C++ OpenCV boxfilter方框濾波的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
C++中關(guān)于[]靜態(tài)數(shù)組和new分配的動態(tài)數(shù)組的區(qū)別分析
這篇文章主要介紹了C++中關(guān)于[]靜態(tài)數(shù)組和new分配的動態(tài)數(shù)組的區(qū)別分析,很重要的概念,需要的朋友可以參考下2014-08-08

