高斯核二維捲積。
呼叫方式:
Mat test = Gauss_Kernel(frame, 5, 1);//frame=處理圖片, 5=處理大小,1=權重比
Mat Gauss_Kernel(Mat photo, int size, float sigmal)
{
Mat arr = Gaussian(size, sigmal);
Mat picture = Mat::zeros(photo.size(), photo.type());
Mat gus = Mat(size, size, CV_32SC3);
unsigned int total[3] = { 0, 0, 0 };
int W = 0, H = 0;
int loc = size / 2;
for (int i = 0; i < photo.rows; i += size){
for (int j = 0; j < photo.cols; j += size){
for (int H = 0; H < size; H++){
for (int W = 0; W < size; W++){
picture.at<Vec3w>(i + H, j + W)[0] = photo.at<Vec3w>(i + H, j + W)[0];
picture.at<Vec3w>(i + H, j + W)[1] = photo.at<Vec3w>(i + H, j + W)[1];
picture.at<Vec3w>(i + H, j + W)[2] = photo.at<Vec3w>(i + H, j + W)[2];
gus.at<Vec3w>(H, W)[0] = photo.at<Vec3w>(i + H, j + W)[0];
gus.at<Vec3w>(H, W)[1] = photo.at<Vec3w>(i + H, j + W)[1];
gus.at<Vec3w>(H, W)[2] = photo.at<Vec3w>(i + H, j + W)[2];
gus.at<Vec3w>(H, W)[0] = (float)gus.at<Vec3w>(H, W)[0] * arr.at<float>(H, W);
gus.at<Vec3w>(H, W)[1] = (float)gus.at<Vec3w>(H, W)[1] * arr.at<float>(H, W);
gus.at<Vec3w>(H, W)[2] = (float)gus.at<Vec3w>(H, W)[2] * arr.at<float>(H, W);
total[0] += gus.at<Vec3w>(H, W)[0];
total[1] += gus.at<Vec3w>(H, W)[1];
total[2] += gus.at<Vec3w>(H, W)[2];
}
}
printf("before = %d after =%d \n", picture.at<Vec3w>(i + loc, j + loc)[0], total[0]);
printf("before = %d after =%d \n", picture.at<Vec3w>(i + loc, j + loc)[1], total[1]);
printf("before = %d after =%d \n", picture.at<Vec3w>(i + loc, j + loc)[2], total[2]);
picture.at<Vec3w>(i + loc, j + loc)[0] = total[0];
picture.at<Vec3w>(i + loc, j + loc)[1] = total[1];
picture.at<Vec3w>(i + loc, j + loc)[2] = total[2];
//for (int H = 0; H < size; H++){
// for (int W = 0; W < size; W++){
// picture.at<Vec3w>(i + H, j + W)[0] = total[0];
// picture.at<Vec3w>(i + H, j + W)[1] = total[1];
// picture.at<Vec3w>(i + H, j + W)[2] = total[2];
// }
//}
total[0] = 0;
total[1] = 0;
total[2] = 0;
}
}
return picture;
}
Mat Gaussian(char size, float sigmal)
{
double PI = 3.14159265;
int loc = size / 2;
float sum = 0;
int count;
Mat gus = Mat(size, size, CV_32FC1);
for (int i = -loc; i <= loc; i++){//
for (int j = loc; j >= -loc; j--){
gus.at<float>((i + loc), (loc - j)) = (1 / (2 * PI*sigmal*sigmal))*exp(-(i*i + j*j) / (2 * sigmal*sigmal));
sum += gus.at<float>((i + loc), (loc - j));
}
}
for (int i = 0; i < size; i++)
for (int j = 0; j < size; j++){
gus.at<float>(i, j) = gus.at<float>(i, j) / sum;
}
return gus;
}