高斯核二維捲積 代碼

高斯核二維捲積。
呼叫方式:
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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值