I have written a function FindTopEnd which takes some arguments as shown. This function moves from bottom to up in a straight vertical line and checks for black pixel. If black pixel is found then it checks for a black pixel 3 pixels above that point. If a black pixel is found there also then it stores that initial pixel value in the variables and also stores the length of the line. Else it continues on the image.On running the source code, it works fine but in testing, it is stopping in between without any error and giving wrong outputs.
I have tried it on different images and debugged the code step by step for output but everytime the if() conditions are satisfied on 150 pixels and the for loop is breaked giving wrong output.
NOTE: In test case 280 pixel is given as input just for testing purpose in the blank space.
//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
I expect the output - LineHead_x = 280, LineHead_y = 0, LineLength = 300. Actual results - LineHead_x = 280, LineHead_y = 149, LineLength = 150.
This is the image of the output of the source code. This proves that the lines are being created as the way they are needed as to be tested. Image of output of source code - https://ibb.co/XVCCXFY
This is the image of the output from the test case where the line is stopped at 150 pixels Image of output of test case - https://ibb.co/qpD1KwS
During testing, I was giving a 3 channel image as input but while running the source code, the input image for this function was a single channel grayscale image. After correcting it, the testing worked fine. But a question still remains is that why the lines were stopping at 150 pixels at every time.