OpenCV中resize函数插值算法的实现过程(五种)
Posted 贺二公子
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了OpenCV中resize函数插值算法的实现过程(五种)相关的知识,希望对你有一定的参考价值。
原文地址:https://blog.csdn.net/fengbingchun/article/details/17335477
最新版OpenCV2.4.7中,cv::resize函数有五种插值算法:最近邻、双线性、双三次、基于像素区域关系、兰索斯插值。下面用for循环代替cv::resize函数来说明其详细的插值实现过程,其中部分代码摘自于cv::resize函数中的源代码。
每种插值算法的前部分代码是相同的,如下:
cv::Mat matSrc, matDst1, matDst2;
matSrc = cv::imread("lena.jpg", 2 | 4);
matDst1 = cv::Mat(cv::Size(800, 1000), matSrc.type(), cv::Scalar::all(0));
matDst2 = cv::Mat(matDst1.size(), matSrc.type(), cv::Scalar::all(0));
double scale_x = (double)matSrc.cols / matDst1.cols;
double scale_y = (double)matSrc.rows / matDst1.rows;
1、最近邻
公式
for (int i = 0; i < matDst1.cols; ++i)
int sx = cvFloor(i * scale_x);
sx = std::min(sx, matSrc.cols - 1);
for (int j = 0; j < matDst1.rows; ++j)
int sy = cvFloor(j * scale_y);
sy = std::min(sy, matSrc.rows - 1);
matDst1.at<cv::Vec3b>(j, i) = matSrc.at<cv::Vec3b>(sy, sx);
cv::imwrite("nearest_1.jpg", matDst1);
cv::resize(matSrc, matDst2, matDst1.size(), 0, 0, 0);
cv::imwrite("nearest_2.jpg", matDst2);
2、双线性
由相邻的四像素(2*2)计算得出,公式,
uchar* dataDst = matDst1.data;
int stepDst = matDst1.step;
uchar* dataSrc = matSrc.data;
int stepSrc = matSrc.step;
int iWidthSrc = matSrc.cols;
int iHiehgtSrc = matSrc.rows;
for (int j = 0; j < matDst1.rows; ++j)
float fy = (float)((j + 0.5) * scale_y - 0.5);
int sy = cvFloor(fy);
fy -= sy;
sy = std::min(sy, iHiehgtSrc - 2);
sy = std::max(0, sy);
short cbufy[2];
cbufy[0] = cv::saturate_cast<short>((1.f - fy) * 2048);
cbufy[1] = 2048 - cbufy[0];
for (int i = 0; i < matDst1.cols; ++i)
float fx = (float)((i + 0.5) * scale_x - 0.5);
int sx = cvFloor(fx);
fx -= sx;
if (sx < 0)
fx = 0, sx = 0;
if (sx >= iWidthSrc - 1)
fx = 0, sx = iWidthSrc - 2;
short cbufx[2];
cbufx[0] = cv::saturate_cast<short>((1.f - fx) * 2048);
cbufx[1] = 2048 - cbufx[0];
for (int k = 0; k < matSrc.channels(); ++k)
*(dataDst+ j*stepDst + 3*i + k) = (*(dataSrc + sy*stepSrc + 3*sx + k) * cbufx[0] * cbufy[0] +
*(dataSrc + (sy+1)*stepSrc + 3*sx + k) * cbufx[0] * cbufy[1] +
*(dataSrc + sy*stepSrc + 3*(sx+1) + k) * cbufx[1] * cbufy[0] +
*(dataSrc + (sy+1)*stepSrc + 3*(sx+1) + k) * cbufx[1] * cbufy[1]) >> 22;
cv::imwrite("linear_1.jpg", matDst1);
cv::resize(matSrc, matDst2, matDst1.size(), 0, 0, 1);
cv::imwrite("linear_2.jpg", matDst2);
3、双三次
由相邻的4*4像素计算得出,公式类似于双线性
int iscale_x = cv::saturate_cast<int>(scale_x);
int iscale_y = cv::saturate_cast<int>(scale_y);
for (int j = 0; j < matDst1.rows; ++j)
float fy = (float)((j + 0.5) * scale_y - 0.5);
int sy = cvFloor(fy);
fy -= sy;
sy = std::min(sy, matSrc.rows - 3);
sy = std::max(1, sy);
const float A = -0.75f;
float coeffsY[4];
coeffsY[0] = ((A*(fy + 1) - 5*A)*(fy + 1) + 8*A)*(fy + 1) - 4*A;
coeffsY[1] = ((A + 2)*fy - (A + 3))*fy*fy + 1;
coeffsY[2] = ((A + 2)*(1 - fy) - (A + 3))*(1 - fy)*(1 - fy) + 1;
coeffsY[3] = 1.f - coeffsY[0] - coeffsY[1] - coeffsY[2];
short cbufY[4];
cbufY[0] = cv::saturate_cast<short>(coeffsY[0] * 2048);
cbufY[1] = cv::saturate_cast<short>(coeffsY[1] * 2048);
cbufY[2] = cv::saturate_cast<short>(coeffsY[2] * 2048);
cbufY[3] = cv::saturate_cast<short>(coeffsY[3] * 2048);
for (int i = 0; i < matDst1.cols; ++i)
float fx = (float)((i + 0.5) * scale_x - 0.5);
int sx = cvFloor(fx);
fx -= sx;
if (sx < 1)
fx = 0, sx = 1;
if (sx >= matSrc.cols - 3)
fx = 0, sx = matSrc.cols - 3;
float coeffsX[4];
coeffsX[0] = ((A*(fx + 1) - 5*A)*(fx + 1) + 8*A)*(fx + 1) - 4*A;
coeffsX[1] = ((A + 2)*fx - (A + 3))*fx*fx + 1;
coeffsX[2] = ((A + 2)*(1 - fx) - (A + 3))*(1 - fx)*(1 - fx) + 1;
coeffsX[3] = 1.f - coeffsX[0] - coeffsX[1] - coeffsX[2];
short cbufX[4];
cbufX[0] = cv::saturate_cast<short>(coeffsX[0] * 2048);
cbufX[1] = cv::saturate_cast<short>(coeffsX[1] * 2048);
cbufX[2] = cv::saturate_cast<short>(coeffsX[2] * 2048);
cbufX[3] = cv::saturate_cast<short>(coeffsX[3] * 2048);
for (int k = 0; k < matSrc.channels(); ++k)
matDst1.at<cv::Vec3b>(j, i)[k] = abs((matSrc.at<cv::Vec3b>(sy-1, sx-1)[k] * cbufX[0] * cbufY[0] + matSrc.at<cv::Vec3b>(sy, sx-1)[k] * cbufX[0] * cbufY[1] +
matSrc.at<cv::Vec3b>(sy+1, sx-1)[k] * cbufX[0] * cbufY[2] + matSrc.at<cv::Vec3b>(sy+2, sx-1)[k] * cbufX[0] * cbufY[3] +
matSrc.at<cv::Vec3b>(sy-1, sx)[k] * cbufX[1] * cbufY[0] + matSrc.at<cv::Vec3b>(sy, sx)[k] * cbufX[1] * cbufY[1] +
matSrc.at<cv::Vec3b>(sy+1, sx)[k] * cbufX[1] * cbufY[2] + matSrc.at<cv::Vec3b>(sy+2, sx)[k] * cbufX[1] * cbufY[3] +
matSrc.at<cv::Vec3b>(sy-1, sx+1)[k] * cbufX[2] * cbufY[0] + matSrc.at<cv::Vec3b>(sy, sx+1)[k] * cbufX[2] * cbufY[1] +
matSrc.at<cv::Vec3b>(sy+1, sx+1)[k] * cbufX[2] * cbufY[2] + matSrc.at<cv::Vec3b>(sy+2, sx+1)[k] * cbufX[2] * cbufY[3] +
matSrc.at<cv::Vec3b>(sy-1, sx+2)[k] * cbufX[3] * cbufY[0] + matSrc.at<cv::Vec3b>(sy, sx+2)[k] * cbufX[3] * cbufY[1] +
matSrc.at<cv::Vec3b>(sy+1, sx+2)[k] * cbufX[3] * cbufY[2] + matSrc.at<cv::Vec3b>(sy+2, sx+2)[k] * cbufX[3] * cbufY[3] ) >> 22);
cv::imwrite("cubic_1.jpg", matDst1);
cv::resize(matSrc, matDst2, matDst1.size(), 0, 0, 2);
cv::imwrite("cubic_2.jpg", matDst2)以上是关于OpenCV中resize函数插值算法的实现过程(五种)的主要内容,如果未能解决你的问题,请参考以下文章
极智AI | OpenCV and torchvision.transforms 实现图像等比例缩放方法
短道速滑一OpenCV中cvResize函数使用双线性插值缩小图像到长宽大小一半时速度飞快(比最近邻还快)之异象解析和自我实现。