//基于水平间隔
Mat imageReduction1(Mat &srcImage, float kx, float ky)
{
int nRows = cvRound(srcImage.rows * kx);
int nCols = cvRound(srcImage.cols * ky);
Mat resultImage(nRows, nCols, srcImage.type());
for (int i = 0; i < nRows;i++)
{
for (int j = 0; j < nCols; j++)
{
//根据水平因子计算坐标
int x = static_cast<int>((i + 1) / kx + 0.5) - 1; //static_cast 转换 () 为 < >
//根据垂直因子计算坐标
int y = static_cast<int>((j + 1) / ky + 0.5) - 1;
resultImage.at<Vec3b>(i, j) = srcImage.at<Vec3b>(x, y);
}
}
return resultImage;
}
int main()
{
Mat srcImage = imread("E:/Project/images/1.jpg");
if (!srcImage.data)
{
return -1;
}
imshow("srcImage", srcImage);
Mat resultImage = imageReduction1(srcImage, 0.5, 0.5);
imshow("resultImage", resultImage);
waitKey(0);
system("pause");
return 0;
}