// checks basic properties of the filtering result void Dip2::test_adaptiveFilter(void){ Mat input = Mat::ones(9,9, CV_32FC1); input.at<float>(4,4) = 255; Mat output = adaptiveFilter(input, 5, 255); Mat output2 = adaptiveFilter(input, 5, -1); if ( (input.cols != output.cols) || (input.rows != output.rows) ){ cout << "ERROR: Dip2::adaptiveFilter(): input.size != output.size --> Wrong border handling?" << endl; return; } if ( (sum(output.row(0) < 0).val[0] > 0) or (sum(output.row(0) > 255).val[0] > 0) or (sum(output.row(8) < 0).val[0] > 0) or (sum(output.row(8) > 255).val[0] > 0) or (sum(output.col(0) < 0).val[0] > 0) or (sum(output.col(0) > 255).val[0] > 0) or (sum(output.col(8) < 0).val[0] > 0) or (sum(output.col(8) > 255).val[0] > 0) ){ cout << "ERROR: Dip2::adaptiveFilter(): Border of result contains too large/small values --> Wrong border handling?" << endl; return; }else{ if ( (sum(output < 0).val[0] > 0) or (sum(output > 255).val[0] > 0) ){ cout << "ERROR: Dip2::adaptiveFilter(): Result contains too large/small values!" << endl; return; } } float ref[9][9] = {{0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 1, 1, 1, 1, 1, 1, 1, 0}, {0, 1, (24+255)/25., (24+255)/25., (24+255)/25., (24+255)/25., (24+255)/25., 1, 0}, {0, 1, (24+255)/25., (24+255)/25., (24+255)/25., (24+255)/25., (24+255)/25., 1, 0}, {0, 1, (24+255)/25., (24+255)/25., (24+255)/25., (24+255)/25., (24+255)/25., 1, 0}, {0, 1, (24+255)/25., (24+255)/25., (24+255)/25., (24+255)/25., (24+255)/25., 1, 0}, {0, 1, (24+255)/25., (24+255)/25., (24+255)/25., (24+255)/25., (24+255)/25., 1, 0}, {0, 1, 1, 1, 1, 1, 1, 1, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0}}; float ref2[9][9] = {{0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 1, 1, 1, 1, 1, 1, 1, 0}, {0, 1, 1, 1, 1, 1, 1, 1, 0}, {0, 1, 1, (8+255)/9., (8+255)/9., (8+255)/9., 1, 1, 0}, {0, 1, 1, (8+255)/9., (8+255)/9., (8+255)/9., 1, 1, 0}, {0, 1, 1, (8+255)/9., (8+255)/9., (8+255)/9., 1, 1, 0}, {0, 1, 1, 1, 1, 1, 1, 1, 0}, {0, 1, 1, 1, 1, 1, 1, 1, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0}}; for(int y=1; y<8; y++){ for(int x=1; x<8; x++){ if ( (abs(output.at<float>(y,x) - ref[y][x]) > 0.0001) || (abs(output2.at<float>(y,x) - ref2[y][x]) > 0.0001) ){ cout << "ERROR: Dip2::adaptiveFilter(): Result contains wrong values!" << endl; return; } } } cout << "Message: Dip2::adaptiveFilter() seems to be correct" << endl; }
/* src: input image method: name of noise reduction method that shall be performed "average" ==> moving average "median" ==> median filter "adaptive" ==> edge preserving average filter "bilateral" ==> bilateral filter kSize: (spatial) kernel size param: if method == "adaptive" : threshold ; if method == "bilateral" standard-deviation of radiometric kernel can be ignored otherwise (default value = 0) return: output image */ Mat Dip2::noiseReduction(Mat& src, string method, int kSize, double param){ // apply moving average filter if (method.compare("average") == 0){ return averageFilter(src, kSize); } // apply median filter if (method.compare("median") == 0){ return medianFilter(src, kSize); } // apply adaptive average filter if (method.compare("adaptive") == 0){ return adaptiveFilter(src, kSize, param); } // apply bilateral filter if (method.compare("bilateral") == 0){ return bilateralFilter(src, kSize, param); } // if none of above, throw warning and return copy of original cout << "WARNING: Unknown filtering method! Returning original" << endl; cout << "Press enter to continue" << endl; cin.get(); return src.clone(); }