gpt4 book ai didi

c++ - if-else 循环不循环 C++ opencv visualstudio

转载 作者:太空宇宙 更新时间:2023-11-03 22:06:08 25 4
gpt4 key购买 nike

我想创建一个循环,用户必须在其中输入一些数字才能转售图像。如果用户输入的内容在限制范围内,但当他们输入的数字太大时,代码就会中断,重新缩放会起作用。

#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 范围内时才会发生这种情况

最佳答案

是的,因为默认情况下,C++ 中的变量是用垃圾初始化的。做这样的事情:

float Sx = 0.0;
float Sy = 0.0;

这一行:

resize(img, ScaledImg, Size(img.cols*Sx,img.rows*Sy));

当然只有当 Sx 和 Sy 不为 0 时才有效,因此如果您的其他参数超出 2000 范围,您必须将它们初始化为对您有意义的值。

关于c++ - if-else 循环不循环 C++ opencv visualstudio,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19335355/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com