插值性能对比(resize)
OpenCV中提供的resize函数可以实现图像大小变换,默认插值方法为双线性插值。
resize(ImputArray src,OutputArray dst,Size dsize,double fx=0,double fy=0,int interpolation=INTER_LINEAR);
实现图像尺寸大小变换功能。参数dsize表示表示输出图像的尺寸,如果设置为0,尺寸将会被计算为dsize=Size(round(fx*src.cols),round(fy*src,rows)),fx,fy均为非零参数;参数 fx 是水平缩放因子,fx 将会被计算为(double)dsize.width/src.cols;参数 fy 是垂直缩放因子,fy 将会被计算为(double)dsize.height/src.rows;参数interpolation是插值方法,OpenCV提供了5种插值方法:最邻近、双线性、基于像素区域、立方插值及兰索斯插值。
#include
#include
#include
#include
using namespace cv;
using namespace std;
void ResizeExample(Mat srcImage)
{
//判断输入有效性
CV_Assert(srcImage.data != NULL);
imshow("srcImage", srcImage);
Mat dstImage(256, 256, CV_8UC3);
//测试1:默认参数为双线性插值
double tTime;
tTime = (double)getTickCount();
const int nTimes = 100;
for (int i = 0; i < nTimes; i++)
{
resize(srcImage, dstImage, dstImage.size(), 0, 0);
}
tTime = 1000 * ((double)getTickCount() - tTime) / getTickFrequency();
tTime /= nTimes;
cout << "text1: " << tTime << endl;
imshow("1 default parameters:dstImage", dstImage);
//测试2:最邻近插值
tTime = (double)getTickCount();
for (int i = 0; i < nTimes; i++)
{
resize(srcImage, dstImage,Size(256,256), 0, 0,INTER_NEAREST);
}
tTime = 1000 * ((double)getTickCount() - tTime) / getTickFrequency();
tTime /= nTimes;
cout << "text2: " << tTime << endl;
imshow("2 INTER_NEAREST:dstImage", dstImage);
//测试3:像素区域插值
tTime = (double)getTickCount();
for (int i = 0; i < nTimes; i++)
{
resize(srcImage, dstImage, Size(256,256), 0.5, 0.5,INTER_AREA);
}
tTime = 1000 * ((double)getTickCount() - tTime) / getTickFrequency();
tTime /= nTimes;
cout << "text3: " << tTime << endl;
imshow("3 INTER_AREA : dstImage", dstImage);
//测试4:三次插值
tTime = (double)getTickCount();
for (int i = 0; i < nTimes; i++)
{
resize(srcImage, dstImage, Size(), 0.5, 0.5, INTER_CUBIC);
}
tTime = 1000 * ((double)getTickCount() - tTime) / getTickFrequency();
tTime /= nTimes;
cout << "text4: " << tTime << endl;
imshow("4 INTER_CUBIC : dstImage", dstImage);
//测试5:三次插值
tTime = (double)getTickCount();
for (int i = 0; i < nTimes; i++)
{
resize(srcImage, dstImage, Size(), 0.5, 0.5, INTER_LANCZOS4);
}
tTime = 1000 * ((double)getTickCount() - tTime) / getTickFrequency();
tTime /= nTimes;
cout << "text5: " << tTime << endl;
imshow("5 INTER_LANCZOS4 : dstImage", dstImage);
}
int main()
{
Mat srcImage = imread("D:\\1.jpg");
if (!srcImage.data)
return -1;
ResizeExample(srcImage);
waitKey(0);
return 0;
}
通过运行测试时间可以看出,resize函数中双线性插值与最邻近插值的时间复杂度相对较低。