我正在寻找一个循环,用户必须输入一些数字才能转售图像。如果用户输入了限制范围内的内容,但是当他们输入的数字太大时,重新缩放将起作用。
#include <opencv2\core\core.hpp>
#include <opencv2\highgui\highgui.hpp>
#include <opencv2\imgproc\imgproc.hpp>
#include <iostream>
#include <cstdlib>
#include <cctype>
using namespace std;
using namespace cv;
int main()
{
float Sx = 0;
float Sy = 0;
float NewX;
float NewY;
Mat img, ScaledImg;
img = imread("C:/Jakob/tower.jpg");
do
{
cout << "The current image size is: " << img.rows << "x" << img.cols << endl;
cout << "First enter the new width for the image: ";
cin >> NewX;
cout << "Seond enter the new height for the image: ";
cin >> NewY;
if ((NewX >= 1) && (NewX <= 2000))
if ((NewY >=1) && (NewY <= 2000))
{
Sx = (NewX/img.rows);
cout << "You entered " << NewX << " For Width" << endl;
Sy = (NewY/img.cols);
cout << "You entered " << NewY << " For Height" << endl;
}
else
{
cout << "The number you entered does not match the requirements " << endl;
cout << "Please start over " << endl;
}
}
while (NewX < 1 && NewX >= 2000 && NewY < 1 && NewY >= 2000);
cout << "Sx = " << Sx << endl;
cout << "Sy = " << Sy << endl;
resize(img, ScaledImg, Size(img.cols*Sx,img.rows*Sy));
imwrite("C:/Jakob/ScaledImage.jpg", ScaledImg);
cout << "Rows: " << ScaledImg.rows << " and Cols: " << ScaledImg.cols << endl;
imshow("Original", img);
imshow("Scaled Image", ScaledImg);
/*system("PAUSE");*/
waitKey(0);
return 0;
}
运行此代码后我得到的错误是 Sx 在没有被初始化的情况下被使用。仅当数字不在 1-2000 范围内时才会发生这种情况