使用Catch2Farmework测试我的功能时出现问题

时间:2019-06-15 11:51:04

标签: c++ opencv catch2

我已经编写了一个函数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,LineLength = 300。 实际结果 - LineHead_x = 280,LineHead_y = 149,LineLength = 150。

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

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

1 个答案:

答案 0 :(得分:0)

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