Suppose we have the following image at the input:

Load the image into the matrix in shades of gray:
cv::Mat src_mat = cv::imread("line.png", cv::IMREAD_GRAYSCALE);
Despite the fact that in the picture, in general, we see only two colors, in fact, on the border pixels between the white background and the black line there are intermediate values, ranging from 0 to 255.
To search for black line contours such a variety of values is not required. Binaryization will help to clearly separate the colors into black and white:
cv::Mat bin_mat; cv::threshold(src_mat, bin_mat, 127, 255, cv::THRESH_BINARY_INV);
That will give here such an image enclosed in bin_mat :

The cv::THRESH_BINARY_INV instead of the commonly used cv::THRESH_BINARY selected so that the so-called “ cv::THRESH_BINARY . reverse binarization, when the color of each pixel is reversed to the opposite of a simple formula:
pixel_value = 255 - pixel_value
In OpenCV, in many functions, the default is that the background pixels should be 0 , and objects should be any value from 1 to 255 inclusive (however, it is customary to take 255 behind the scenes). Since in this case the line is an object of interest, it is for this reason that the reverse binarization was performed in the code.
Now you can search for the contour line:
std::vector<std::vector<cv::Point> > contours; cv::findContours(bin_mat, contours, cv::RETR_EXTERNAL, cv::CHAIN_APPROX_SIMPLE);
If you draw it, you get the following image:

The search for contours always returns closed, encircling the original image of the figure, even if the source thickness in some places is no more than one pixel.
To obtain the contour position (the upper left point of the matrix is always taken for the zero mark), it suffices to use a construction that will return a rectangle minimally described around the line:
cv::Rect line_rect = cv::boundingRect(contours[0]);
However, if the center is also required, it is easier to use:
cv::RotatedRect rot_rect = cv::minAreaRect(contours[0]); // Центр фигуры. float center = rot_rect.center; // Такой же прямоугольник, что и в предыдущем примере. cv::Rect line_rect = rot_rect.boundingRect();
Now the line_rect object of the cv::Rect structure contains the coordinates of the rectangle whose sides lie on the leftmost ( line_rect.x ), right ( line_rect.x + line_rect.width ), top ( line_rect.y ) and bottom ( line_rect.y + line_rect.height ) contour points.
findContours()(which seems to hint at some awareness in the subject), but at the same time, on the other, something confuses you: "... I can not figure out how." What exactly is the question? How did you try to solve the problem yourself? It is best to give a sample code. - alexis031182