Matlab中filter2与opencv中filter2D的比较
比较一:横向卷积
Matlab中filter2:
opencv中filter2D:
Mat C =(Mat_<float>(3,4)<<1,2,3,4,5,6,7,8,9,10,11,12); float *data = NULL; for (int row = 0; row < C.rows; row++) { data = (float *)C.ptr<float>(row); cout << "["; for (int col = 0; col < C.cols; col++) { cout << data[col] << " "; } cout << "]" << endl; } Mat D; copyMakeBorder(C, D, 2, 2, 2, 2, BORDER_REPLICATE); for (int row = 0; row < D.rows; row++) { data = (float *)D.ptr<float>(row); cout << "["; for (int col = 0; col < D.cols; col++) { cout << data[col] << " "; } cout << "]" << endl; } cout << endl; D = (Mat_<float>(1,3) << 0.1, 0.2 ,0.3); Mat E; filter2D(C, E, -1, D,Point(-1,-1),0, BORDER_REPLICATE); for (int row = 0; row < E.rows; row++) { data = (float *)E.ptr<float>(row); cout << "["; for (int col = 0; col < E.cols; col++) { cout << data[col] << " "; } cout << "]" << endl; } cout << endl;
结果
比较二:纵向卷积
Matlab中filter2:
opencv中filter2D:
Mat C =(Mat_<float>(3,4)<<1,2,3,4,5,6,7,8,9,10,11,12); float *data = NULL; for (int row = 0; row < C.rows; row++) { data = (float *)C.ptr<float>(row); cout << "["; for (int col = 0; col < C.cols; col++) { cout << data[col] << " "; } cout << "]" << endl; } Mat D; copyMakeBorder(C, D, 2, 2, 2, 2, BORDER_REPLICATE); for (int row = 0; row < D.rows; row++) { data = (float *)D.ptr<float>(row); cout << "["; for (int col = 0; col < D.cols; col++) { cout << data[col] << " "; } cout << "]" << endl; } cout << endl; D = (Mat_<float>(3,1) << 0.1, 0.2 ,0.3); Mat E; filter2D(C, E, -1, D,Point(-1,-1),0, BORDER_REPLICATE); for (int row = 0; row < E.rows; row++) { data = (float *)E.ptr<float>(row); cout << "["; for (int col = 0; col < E.cols; col++) { cout << data[col] << " "; } cout << "]" << endl; } cout << endl;
结果
结论:Matlab中filter2与opencv中filter2D可以得到完全一样的结果。