当前位置:网站首页>Image scaling with aspect ratio preserving by opencv
Image scaling with aspect ratio preserving by opencv
2022-06-11 03:36:00 【szfhy】
It's been done before C For aspect ratio maintenance RGB Image zoom , In fact, the computational efficiency is not high .
opencv Many implementations of are built-in mmx, sse Accelerated . So let's study how to use opencv To achieve aspect ratio preserving image scaling .
Have a look opencv resize Detailed definition of function :
resize function
The function prototype :
void cv::resize(
InputArray _src,
OutputArray _dst,
Size dsize,
double inv_scale_x = 0,
double inv_scale_y = 0,
int interpolation = INTER_LINEAR
)
Parameter description :
src, The input image ,Mat The type is enough ;dst, Output image , When it is non-zero , with dsize( The third parameter ) The size of or having src.size() Work it out ;dsize, The size of the output image . If it's equal to 0, It is calculated from the following formula :dsize = Size( round(fx*src.cols, round(fy*src.rows))); among fx,fy,dsize Can't do it 0fx, Scaling factor along the horizontal axis , The default value is 0, And it's equal to 0 when , It is calculated from the following formula :inv_scale_x = (double)dsize.width/ssize.width;fy, Scaling factor along the vertical axis , The default value is 0, And it's equal to 0 when , It is calculated from the following formula :inv_scale_y = (double)dsize.height/ssize.height;interpolation, Used to specify the interpolation method , The default value is INTER_LINEAR( linear interpolation ), The optional interpolation methods are as follows :
| Value | explain |
|---|---|
| INTER_NEAREST | Nearest neighbor interpolation |
| INTER_LINEAR | linear interpolation ( The default value is ) |
| INTER_AREA | Area interpolation ( Resampling interpolation using pixel region relationship ) |
| INTER_CUBIC | Cubic spline interpolation ( exceed 4×4 Bicubic interpolation in pixel domain ) |
| INTER_LANCZOS4 | Lanczos interpolation ( exceed 8×8 Pixel neighborhood Lanczos interpolation ) |
Be careful : To reduce the image , In general INTER_AREA To interpolate ; And to enlarge the image , In general INTER_CUBIC( The efficiency is not high , Not recommended ) or INTER_LINEAR( Efficient , recommend )
But this function can not realize the image scaling with arbitrary aspect ratio .
Think about it , You can do it first crop operation , And then zoom , A two-step , This allows for image scaling with aspect ratio preserved .
The code is as follows :
#include "opencv2/opencv.hpp"
#include "opencv2/opencv.hpp"
#include<fstream>
#include <chrono>
using namespace std;
using namespace cv;
int main(int, char**)
{
cv::Size szSize(3840 , 2160);
uchar *b_Buffer = new uchar[szSize.width * szSize.height * 2];
uchar *rgb_Buffer = new uchar[szSize.width * szSize.height * 3];
string binFile("input.yuv");
ifstream File_VideoFile;
File_VideoFile.open(binFile, ios::in | ios::binary);
if (!File_VideoFile.is_open())
{
std::cerr << "[ERROR] cannot open the YUV Input File " << binFile << endl;
std::cerr << std::endl;
assert(0);
}
File_VideoFile.read((char*)b_Buffer, sizeof(uchar)*szSize.width*szSize.height*2);
File_VideoFile.close();
cv::Mat mSrc(szSize,CV_8UC2, b_Buffer);
cv::Mat mSrc_BGR(szSize, CV_8UC3);
auto time1 = std::chrono::steady_clock::now();
cvtColor(mSrc, mSrc_BGR, COLOR_YUV2BGR_YUYV);
auto time2 = std::chrono::steady_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::microseconds>(time2-time1).count();
printf("cv time: %.2f ms.\n", (float)duration / 1000);
Size dstSize(1600, 1600);
Size srcSize(3840, 2160);
Mat dst_img(dstSize, CV_8UC3);
//crop first
double fx = dstSize.width / srcSize.width;
double fy = dstSize.height / srcSize.height;
int xmin = 0, xmax = 0;
int ymin = 0, ymax = 0;
if (fx > fy) {
//crop y
xmin = 0;
xmax = srcSize.width;
ymin = (srcSize.height - srcSize.width) >> 1;
ymax = srcSize.height - ymin;
} else {
//crop x
ymin = 0;
ymax = srcSize.height;
xmin = (srcSize.width - srcSize.height) >> 1;
xmax = srcSize.width - xmin;
}
Mat crop_img = mSrc_BGR(Range(ymin, ymax), Range(xmin, xmax));
//scale second
resize(crop_img, dst_img, dstSize);
imwrite( "dst.bmp", dst_img);
imwrite( "rgb24.bmp", mSrc_BGR);
delete[] b_Buffer;
delete[] rgb_Buffer;
return 0;
}
边栏推荐
- 一文搞懂单片机驱动8080LCD
- Difference between idea open and import project
- 基于SSM框架的连锁超市购物零售后台管理系统
- Le tri le plus couramment utilisé de JS - - série JS déchiquetée à la main
- canvas旋转绘图h5动画js特效
- 突破中国品牌创新技术实力,TCL做对了什么?
- 2022 年 5 月产品大事记
- The tide play power is really firepower! The first big screen cinema for young people? Cool open TV Max 86 "sudden attack
- WEB上传文件预览
- RHEL7 切换字符编码为GBK
猜你喜欢

The tide play power is really firepower! The first big screen cinema for young people? Cool open TV Max 86 "sudden attack

svg实现纸飞机自由的飞翔动画

J. Balanced Tree

Checkbox beautify button selected style

LVGL中文字体制作

Oppo reno6 turned sour by "inner roll"

【ELT.ZIP】OpenHarmony啃论文俱乐部——快速随机访问字符串压缩

单片机通信数据延迟问题排查

J. Balanced Tree

Canvas interactive star animation background JS special effect
随机推荐
Azure Kubernates Service 更新|提升开发体验和效率
亚马逊测评自养号,小白应该如何开始?
thinkphp3.2.3反序列化利用链分析
Tweenmax colorful ball bouncing animation
Instructor add function_ Enable auto fill_ Instructor modification function
postgresql copy语句
Why is vfly, a high-end brand of Yadi that does not live up to its name, not high-end?
canvas交互式星星动画背景js特效
Technology Pro strength evaluation: comprehensive PK of high-end massage chair market, who is worthy of the machine king?
R bioinformatics statistical analysis
Path count 2 (DP + number of combinations)
Unity之数据持久化——Json
J. Balanced Tree
canvas绘图——如何把图形放置画布中心
Picking peaches (double pointer)
Mavros控制无人机在gazebo环境下进行双目SLAM
js实现柯里化
JS click the sun and moon to switch between day and night JS special effect
jlink最新版本下载
Thinkphp3.2.3 deserialization using chain analysis