OpenCv图像处理之方框滤波-线性滤波
未完待续…
#include <iostream>
#include <opencv2/opencv.hpp>
using namespace std;
using namespace cv;
void resize_img(Mat &mat, int width, int height, int interpolation = INTER_AREA);
Mat zero_img(int width, int height, int type);
void box_filter_img(Mat &src, Mat &dst, int kernel = 3, int depth = -1, int x = -1, int y = -1, bool normalize = true);
int main() {
Mat img, clone_img, dst_img, merge_img;
img = imread("D:/cat.jpg", 3);
if (img.empty()) {
cout << "open the file failed" << endl;
return -1;
}
clone_img = img.clone();
double scale = 0.5;
int width = int(img.cols * scale);
int height = int(img.rows * scale);
int type = clone_img.type();
int kernel = 5;
resize_img(clone_img, width, height);
dst_img = zero_img(width, height, type);
merge_img = zero_img(width, height, type);
box_filter_img(clone_img, dst_img, kernel);
addWeighted(clone_img, 0.4, dst_img, 0.8, 0, merge_img);
imshow("original_img", clone_img);
imshow("box_filter_img", dst_img);
imshow("merge_img", merge_img);
waitKey(0);
return 0;
}
void resize_img(Mat &mat, int width, int height, int interpolate) {
resize(mat, mat, Size(width, height), 0, 0, interpolate);
}
Mat zero_img(int width, int height, int type) {
Mat mat = Mat::zeros(Size(width, height), type);
return mat;
}
void box_filter_img(Mat &src, Mat &dst, int kernel, int depth, int x, int y, bool normalize) {
boxFilter(src, dst, depth, Size(kernel, kernel), Point(x, y), normalize);
}
效果图
|