图片缩放
2021-06-20 本文已影响0人
wjundong
#include <opencv2/opencv.hpp>
#include <stdio.h>
using namespace cv;
int main(int argc, char const *argv[])
{
Mat m1 = imread("image.jpg");
Mat m2(240, 256, CV_8UC3);
int w1 = m1.cols;
int h1 = m1.rows;
int w2 = m2.cols;
int h2 = m2.rows;
int x_ratio = (int)((w1 << 16) / w2) + 1;
int y_ratio = (int)((h1 << 16) / h2) + 1;
int x2, y2;
for (int i = 0; i < m2.rows; ++i)
{
for (int j = 0; j < m2.cols; j++)
{
x2 = ((j * x_ratio) >> 16);
y2 = ((i * y_ratio) >> 16);
m2.at<Vec3b>(i, j)[0] = m1.at<Vec3b>(y2, x2)[0]; // BLUE
m2.at<Vec3b>(i, j)[1] = m1.at<Vec3b>(y2, x2)[1];
m2.at<Vec3b>(i, j)[2] = m1.at<Vec3b>(y2, x2)[2];
}
}
imshow("原始图", m1);
imshow("结果", m2);
waitKey(0);
return 0;
}