用滤波器进行缩减像素采样
需要调整图像精度(重新采样)的情况屡见不鲜,降低图像精度的过程称为缩减像素采样,提升图像精度的过程称为提升像素采样。
为避免混叠现象的发生,在缩减图像之前必须进行低通滤波。低通滤波的作用是消除在缩减后的图像中无法表示的高频部分。这一现象称为奈奎斯特-香农定理,它表明如果
把图像缩小一半,那么其可见的频率带宽也将减少一半。
OpenCV中利用该原理的函数
void pyrDown(InputArray src, OutputArray dst, const Size_<int> &dstsize = Size(), int borderType = BORDER_DEFAULT)
程序代码(缩减像素采样)
#include <iostream>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
using namespace std;
int main(int argc, char** argv) {
if(argc != 2)
{
cerr << "please enter the right numbers of images" << endl;
return -1;
}
cv::Mat image = cv::imread(argv[1]);
if(image.empty())
{
cerr << "don`t get the data of the argv[1]" << endl;
return -1;
}
cv::Mat reduceImage;
const int64 start = cv::getTickCount();
cv::pyrDown(image,reduceImage);
double duration = (cv::getTickCount() - start) / cv::getTickFrequency();
cout << "function uses time: " << duration << endl;
cv::imshow("The original image", image);
cv::imshow("The procesed image", reduceImage);
cv::waitKey(0);
return 0;
}
void pyrUp(InputArray src, OutputArray dst, const Size_<int> &dstsize = Size(), int borderType = BORDER_DEFAULT)
程序代码(提升像素采样)
#include <iostream>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
using namespace std;
int main(int argc, char** argv) {
if(argc != 2)
{
cerr << "please enter the right numbers of images" << endl;
return -1;
}
cv::Mat image = cv::imread(argv[1]);
if(image.empty())
{
cerr << "don`t get the data of the argv[1]" << endl;
return -1;
}
cv::Mat reduceImage;
const int64 start = cv::getTickCount();
cv::pyrUp(image,reduceImage);
double duration = (cv::getTickCount() - start) / cv::getTickFrequency();
cout << "function uses time: " << duration << endl;
cv::imshow("The original image", image);
cv::imshow("The procesed image", reduceImage);
cv::waitKey(0);
return 0;
}
以上两个函数可以构建图像金字塔。
void resize(InputArray src, OutputArray dst, Size_<int> dsize, double fx = 0, double fy = 0, int interpolation = INTER_LINEAR)
示例程序
#include <iostream>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
using namespace std;
int main(int argc, char** argv) {
if(argc != 2)
{
cerr << "please enter the right numbers of images" << endl;
return -1;
}
cv::Mat image = cv::imread(argv[1]);
if(image.empty())
{
cerr << "don`t get the data of the argv[1]" << endl;
return -1;
}
cv::Mat resizedImage;
const int64 start = cv::getTickCount();
// cv::resize(image, resizedImage,cv::Size(image.cols*2, image.rows*2));
cv::resize(image, resizedImage, cv::Size(), 1.0/4.0, 1.0/4.0);
double duration = (cv::getTickCount() - start) / cv::getTickFrequency();
cout << "function uses time: " << duration << endl;
cv::imshow("The original image", image);
cv::imshow("The procesced image", resizedImage);
cv::waitKey(0);
return 0;
}