OpenCV: Assertion failed ((npoints = prevPtsMat.checkVector(2, CV_32F, true)) grad= 0)
-
On the advice of one participant, it was optical flow, but not very good.
Wrong error
Assertion failed ((npoints = prevPtsMat.checkVector(2, CV_32F, true)) >= 0)
Says there's something wrong with parameter three.
calcOpticalFlowPyrLK
I mean,InputArray prevPts
which is a vector of 2D points on which the optical flow will be built. There's a whole set of questions on the English-language Internet, but the answers seem to depend on the situation.Here's my simplified code:
// создание cv::Mat объекта, вырезанного из кадра // через прямоугольник cv::Rect collection auto object = (*frame)(collection);
// конвертация в одноканальное изображение
cvtColor(object, object, cv::COLOR_BGR2GRAY);// создание векторов
std::vector<cv::Point2f> prev_points; // станет *points.first
std::vector<cv::Mat> prev_pyr; // станет *pyramids.first// заполнение векторов
goodFeaturesToTrack(object, prev_points, 200, 0.01, 0.1);
buildOpticalFlowPyramid(object, prev_pyr, cv::Size(21, 21), 5);// кидаем векторы в std::map, луковично выглядит, конечно
points_pyramids.insert(
make_pair
(
make_pair // потом будет прочтено как optical_flow.first
(
make_pair(&prev_points, nullptr), // optical_flow.first.first
make_pair(&prev_pyr, nullptr) // optical_flow.first.second
),
std::make_pair(nullptr, nullptr) // optical_flow.second
)
);
In another method we work with flow:
// достаём из std::map, optical_flow - одно из его значений
auto points = optical_flow.first.first;
auto pyramids = optical_flow.first.second;
auto stats = optical_flow.second;// убираем пустые ссылки
if (points.second == nullptr)
{
std::vector<cv::Point2f> next_points;
points.second = &next_points;
}
if (pyramids.second == nullptr)
{
std::vector<cv::Mat> next_pyramids;
// находим пирамиды для нового кадра
buildOpticalFlowPyramid(*gray, next_pyramids, cv::Size(21, 21), 5);
pyramids.second = &next_pyramids;
}
if (stats.first == nullptr)
{
std::vector<uchar> status;
stats.first = &status;
}
if (stats.second == nullptr)
{
std::vector<float> err;
stats.second = &err;
}// Сам метод Лукаса-Канаде в OpenCV, и здесь происходит ошибка
calcOpticalFlowPyrLK(*pyramids.first, *pyramids.second,
*points.first, *points.second, *stats.first, *stats.second);
Description:
*pyramids.first
pyramids built specifically on the reference object;*pyramids.second
- There are pyramids built on a whole new footage;*points.first
- There are local 2D points special to the facility;*points.second
- The same 2D points found on the new footage will be added.*stats.first
♪status
indicating whether there is an uncertain conduct or not;*stats.second
♪error
Something like a quadramatic deviation.
I'll remind you that the error is due to the third parameter, and that's it.
*points.first
♪ Value added throughgoodFeaturesToTrack(object, prev_points, 200, 0.01, 0.1);
♪
-
I realized the mistake. It's not about parameters, it's that I tried to use a vector from another method when, as a first-stage developer, I cleaned that vector, and
calcOpticalFlowPyrLK
I read two empty originators, the one that was supposed to be with points, and the one with pyramids.We had to use a dynamic vector.
The pyramids should've built a pyramid, not an object, but a footage where the object was found.