使用 Catch2Farmework 测试我的函数时出现问题

Problem in testing my function with Catch2Farmework

本文关键字:问题 函数 Catch2Farmework 测试 我的 使用      更新时间:2023-10-16

我写了一个函数FindTopEnd,它接受一些参数,如图所示。此函数沿垂直直线从下到上移动,并检查黑色像素。如果找到黑色像素,则检查该点上方 3 像素的黑色像素。如果在那里找到一个黑色像素,那么它将该初始像素值存储在变量中,并存储线的长度。否则,它会继续在图像上。在运行源代码时,它工作正常,但在测试中,它停止在两者之间没有任何错误并给出错误的输出。

我已经在不同的图像上尝试过,并逐步调试了代码以进行输出,但是每次在 150 像素上满足 if() 条件并且 for 循环中断时都会给出错误的输出。

注意:在测试用例中,280 像素作为输入给出,仅用于在空白处进行测试。

//This is my function
void FindTopEnd(Mat InputImage, std::int16_t LineBase_x, std::int16_t LineBase_y, std::int64_t* LineHead_x, std::int64_t* LineHead_y, std::int64_t* ArrayBox)
{
std::int16_t LineLengthFound = 0;       //If length of line is found = 1
*LineHead_x = LineBase_x;
*LineHead_y = 299;
std::int16_t x = InputImage.rows;
for (std::int16_t LineLength = 0; LineLength < x; LineLength++)
{
if (InputImage.at<uint8_t>(((InputImage.rows - 1) - LineLength), *LineHead_x) == 0)// && InputImage.at<uint8_t>(((InputImage.rows - 1) - LineLength - 5), (*LineHead_x)) == 0)
{
if (((InputImage.rows - 1) - LineLength - 3) >= 0)
{
if (InputImage.at<uint8_t>(((InputImage.rows - 1) - LineLength - 3), (*LineHead_x)) == 0)
{
*LineHead_y = (InputImage.rows - 1) - LineLength;
*ArrayBox = LineLength;
LineLengthFound = 1;
break;
}
}
}
*LineHead_y = (InputImage.rows - 1) - LineLength;
line(InputImage, Point(LineBase_x, LineBase_y), Point(*LineHead_x, *LineHead_y), Scalar(0, 90, 0));
imshow("line", InputImage);
waitKey(5);
}
if (!LineLengthFound)       //If length is not found then the line is of length of frame (no obstacle)
{
*ArrayBox = InputImage.rows;
*LineHead_y = 0;
}
}

//This is my test case
SECTION("white space")
{
std::int64_t LineHead_x, LineHead_y, LineLength;
FindTopEnd(InputTestImage, 280, 299, &LineHead_x, &LineHead_y, &LineLength);
std::cout << LineHead_x <<std::endl << LineHead_y << std::endl << LineLength;
REQUIRE(CIR(280, 0, LineHead_x) == 1);
CHECK(CIR(0, 3, LineHead_y) == 1);
CHECK(CIR(300, 2, LineLength) == 1);
std::cout << LineHead_y;
std::cout << " " << LineLength;
}
//CIR function checks that the variable is in the range of +-error allowed.
//You can ignore that

我期待输出 - LineHead_x = 280,LineHead_y = 0,线长 = 300。 实际结果 - LineHead_x = 280,LineHead_y = 149,线长 = 150。

这是源代码输出的图像。这证明这些线是按照需要测试的方式创建的。 源代码输出的图像 - https://ibb.co/XVCCXFY

这是测试用例的输出图像,其中行停止在 150 像素处 测试用例输出的图像 - https://ibb.co/qpD1KwS

在测试过程中,我给出了一个 3 通道图像作为输入,但在运行源代码时,此功能的输入图像是单通道灰度图像。更正后,测试工作正常。但仍然存在一个问题,为什么线条每次都停在 150 像素处。