图像的放缩又叫上下采样
想想一座金子塔,层层都是图像,上层为小图,越往下越大。由上层推出下层为上采样,由下层抽样得上层为下采样。
假设采用均值采样。
下采样:
#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);
}
下采样用高斯采样更加好。