c++ opencv 实现哈尔小波正反变

最近看了小波变换图像增强与去噪,就去百度了一下小波变换,发现基于c++ opencv的小波变换代码没有比较完整的实现,然后就顺手写了一个简单的哈尔小波正反变换,共享源码欢迎入坑。

void harr_wave_decompose(const Mat &src_img, Mat &dst_img, int level) {
	Mat image;
	src_img.convertTo(image, CV_32FC1);
	dst_img = Mat::zeros(image.size(), CV_32FC1);
	for (int k = 0; k < level; ++k) {
		int row = image.rows;
		int col = image.cols;
		Mat image1(row, col, CV_32FC1, Scalar::all(0));
		Mat image2(row, col, CV_32FC1,Scalar::all(0));
		int half_row = row / 2;
		int half_col = col / 2;
		
		for (int i = 0; i < row; ++i) {
			float *psrc = image.ptr<float>(i);
			float *ptmp = image1.ptr<float>(i);
			for (int j = 0; j < half_col; ++j) {
				int a = j << 1;
				ptmp[j] = (psrc[a] + psrc[a + 1]) * 0.5;
				ptmp[j + half_col] = (psrc[a] - psrc[a + 1]) * 0.5;
			}
		}
		
		for (int i = 0; i < half_row; ++i) {
			float *pcurrent = image1.ptr<float>(2 * i);
			float *pnext = image1.ptr<float>(2 * i + 1);

			float *p1 = image2.ptr<float>(i);
			float *p2 = image2.ptr<float>(i + half_row);

			for (int j = 0; j < col; ++j) {
				p1[j] = (pcurrent[j] + pnext[j])*0.5;
				p2[j] = (pcurrent[j] - pnext[j])*0.5;
			}
		}
		image = image2(Rect(0, 0, image2.cols / 2, image2.rows / 2));
		image2.copyTo(dst_img(Rect(0, 0, image2.cols, image2.rows)));
	}
}
void harr_wave_recover(const Mat &src_img, Mat &dst_img, int level) {
	Mat image = src_img.clone();
	src_img.convertTo(image, CV_32FC1);
	dst_img = Mat::zeros(image.size(), CV_32FC1);
	for (int k = 0; k < level; ++k) {
		int row = image.rows;
		int col = image.cols;
		Rect rect(0, 0, col / pow(2, level - k - 1), row / pow(2, level - k - 1));
		Mat image1 = image(rect).clone();
		int current_rows = image1.rows;
		int current_cols = image1.cols;
		int half_row = current_rows / 2;
		int half_col = current_cols / 2;
		Mat temp_img1(current_rows, current_cols, CV_32FC1);
		int temp_int = 0;
		for (int i = 0; i < half_row; ++i) {
			float *pup = image1.ptr<float>(i);
			float *pdown = image1.ptr<float>(i + half_row);
			temp_int = i * 2;
			float *p1 = temp_img1.ptr<float>(temp_int);
			float *p2 = temp_img1.ptr<float>(temp_int +1);
			for (int j = 0; j < current_cols; ++j) {
				p1[j] = (pup[j] + pdown[j]);
				p2[j] = (pup[j] - pdown[j]);
			}
		}

		Mat image2(image1.size(), CV_32FC1);
		for (int i = 0; i < current_rows; ++i) {
			float *psrc = temp_img1.ptr<float>(i);
			float *ptmp = image2.ptr<float>(i);
			for (int j = 0; j < half_col; ++j) {
				temp_int = j << 1;
				ptmp[temp_int] = psrc[j] + psrc[j + half_col];
				ptmp[temp_int+1] = psrc[j] - psrc[j + half_col];
			}
		}
		image2.copyTo(image(rect));
	}
	image.convertTo(dst_img, CV_8UC1);

}

  • 4
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值