resize函数参数列表及含义
void resize(InputArray src, OutputArray dst, Size dsize, double fx, double fy, int interpolation=INTER_LINEAR);
各个组件代表的意思:
src:输入,原图像,即待改变大小的图像;
dst:输出,改变大小之后的图像;
dsize:输出图像的大小。Size(width,height)指定的大小;
如果这个参数为0,那么原图像缩放之后的大小:dsize = Size(round(fx*src.cols), round(fy*src.rows)).
fx和fy:图像width方向和height方向的缩放比例,与dsize参数不可同时为0;
interpolation:这个是指定插值的方式
常见差值方式有以下几种:
INTER_NEAREST - 最邻近插值
INTER_LINEAR - 双线性插值,如果最后一个参数你不指定,默认使用这种方法
INTER_AREA - 区域插值
INTER_CUBIC - 4x4像素邻域内的双立方插值
INTER_LANCZOS4 - 8x8像素邻域内的Lanczos插值
opencv内可选差值方法
//! interpolation algorithm
enum InterpolationFlags{
INTER_NEAREST = 0, // nearest neighbor interpolation 最邻近插值法
INTER_LINEAR = 1, // bilinear interpolation 双线性插值法(默认)
// f(i+u,j+v) = (1-u)*(1-v)*f(i,j) + (1-u)*v*f(i,j-1) + u*(1-v)*f(i+1,j) + u*v*f(i+1,j+1)
INTER_CUBIC = 2, /** bicubic interpolation 双三次差值 */
INTER_AREA = 3, //区域相关性差值法 average filter 在某个区域取均值
//参考链接:https://zhuanlan.zhihu.com/p/38493205
INTER_LANCZOS4 = 4, /** Lanczos interpolation over 8x8 neighborhood */
INTER_LINEAR_EXACT = 5, /** Bit exact bilinear interpolation */
INTER_MAX = 7, /** mask for interpolation codes */
WARP_FILL_OUTLIERS = 8, /** flag, fills all of the destination image pixels. If some of them correspond to outliers in the
source image, they are set to zero */
/** flag, inverse transformation
For example, #linearPolar or #logPolar transforms:
- flag is __not__ set: \f$dst( \rho , \phi ) = src(x,y)\f$
- flag is set: \f$dst(x,y) = src( \rho , \phi )\f$
*/
WARP_INVERSE_MAP = 16
};
resize和pyrDown pyrUp区别
resize 可以用于任何比例的缩放
pyrDown 用高斯模板卷积一下,任何删掉偶数行列
pyrUp 用0填充放大后的偶数行列,然后用高斯核卷积一下
实例笔记:
构造将单通道转换成三通道并实现分辨率的缩放成原来的一半:
cv::Mat convertTo3Channels(const cv::Mat& binImg)
{
Mat binImg_;
double scale_up_x = 0.5;
double scale_up_y = 0.5;
resize(binImg, binImg_, Size(), scale_up_x, scale_up_y, INTER_LINEAR);
cv::Mat three_channel = cv::Mat::zeros(binImg_.rows, binImg_.cols, CV_8UC3);
vector<cv::Mat> channels;
for (int i = 0; i < 3; i++)
{
channels.push_back(binImg_);
}
merge(channels, three_channel);
return three_channel;
}
cv::Mat imageMat_ = convertTo3Channels(imageMat);