图像金字塔与上下采样

图像的放缩又叫上下采样
想想一座金子塔,层层都是图像,上层为小图,越往下越大。由上层推出下层为上采样,由下层抽样得上层为下采样。
假设采用均值采样。

下采样:

#include<opencv2\opencv.hpp>
#include<iostream>
using namespace cv;
using namespace std;


int main(){
	int i, j;
	Mat src = imread("D:/a.jpg",0);
	Mat dst(src.rows/2,src.cols/2,CV_8UC1);

	//for (i = 0; i < dst.rows; i++){
	//	uchar* q = dst.ptr<uchar>(i);

	//	uchar* w = src.ptr<uchar>(i*2);
	//	uchar* e = src.ptr<uchar>(i * 2 + 1);
	//	for (j = 0; j < dst.cols; j++){
	//		q[j] = (w[j * 2] + w[j * 2 + 1] + e[j * 2] + e[j * 2 + 1])/4;
	//	}
	//}

	for (i = 0; i < src.rows-1; i+=2){
		uchar* q = dst.ptr<uchar>(i/2);

		uchar* w = src.ptr<uchar>(i);
		uchar* e = src.ptr<uchar>(i + 1);
		for (j = 0; j < src.cols; j++){
			q[j / 2] = (w[j] + w[j + 1] + e[j] + e[j + 1]) / 4;
		}
	}

	imshow("dst", dst);
	waitKey(0);
}

原始图片缩小之后的图片
上采样

#include<opencv2\opencv.hpp>
#include<iostream>
using namespace cv;
using namespace std;


int main(){
	int i, j;
	Mat src = imread("D:/a.jpg",0);
	Mat dst(src.rows*2,src.cols*2,CV_8UC1);
	for (i = 0; i < dst.rows; i+=2){
		uchar* p = dst.ptr<uchar>(i);
		uchar*e = dst.ptr<uchar>(i+1);
		uchar*w = src.ptr<uchar>(i/2);
		for (j = 0; j < dst.cols; j+=2){
			p[j] = w[j / 2];
			p[j + 1] = w[j / 2];
			e[j + 1] = w[j / 2];
			e[j] = w[j / 2];
		}
	}
	imshow("dst", dst);
	waitKey(0);
}

放大之后的图片
下采样用高斯采样更加好。

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值