//不处理边界
void smartBlur(Mat &Img, Mat &OutImg, int Radius, int Threshold) {
OutImg.create(Img.size(),CV_8UC3);
for (int i = Radius; i < Img.rows - Radius; i++) {
uchar *curPtr = Img.ptr<uchar>(i);
uchar *outPtr = OutImg.ptr<uchar>(i);
for (int j = Radius; j < Img.cols - Radius; j++) {
int curValue = curPtr[j * 3];
int curValue2 = curPtr[j * 3 + 1];
int curValue3 = curPtr[j * 3 + 2];
int low = curValue - Threshold;
int low2 = curValue2 - Threshold;
int low3 = curValue3 - Threshold;
int high = curValue + Threshold;
int high2 = curValue2 + Threshold;
int high3 = curValue3 + Threshold;
int Index1 = 0;
int Index2 = 0;
int Index3 = 0;
int cvx1 = 0;
int cvx2 = 0;
int cvx3 = 0;
for (int r = -Radius; r <= Radius; r++) {
uchar *tPtr = Img.ptr<uchar>(i + r);
for (int c = -Radius; c <= Radius; c++) {
int r = tPtr[(j + c) * 3];
int g = tPtr[(j + c) * 3 + 1];
int b = tPtr[(j + c) * 3 + 2];
if (r > low&&r < high) {
cvx1 += r;
Index1++;
}
if (g > low2&&g < high2) {
cvx2 += g;
Index2++;
}
if (b > low3&&b < high3) {
cvx3 += b;
Index3++;
}
}
}
outPtr[j * 3] = cvx1 / Index1;
outPtr[j * 3+1] = cvx2 / Index2;
outPtr[j * 3+2] = cvx3 / Index3;
}
}
}
效果图对比