OpenCV C++实现Lucas-Kanade光流:Python转译轨迹绘制问题
求助:OpenCV C++中实现Lucas-Kanade光流轨迹绘制的思路提示
作为OpenCV和Python的新手,我正在做一个小项目——给视频里的多个运动物体绘制轨迹,最终输出包含物体最终状态和轨迹的图像。我参考了OpenCV官方的Lucas-Kanade光流示例,折腾了好一阵把Python代码的第一部分转成了C++,但到轨迹绘制的环节就卡壳了。
以下是我已经完成的C++代码:
#include "opencv2/imgcodecs.hpp" #include "opencv2/highgui.hpp" #include "opencv2/video/tracking.hpp" #include "opencv2/imgproc.hpp" #include <iostream> using namespace cv; using namespace std; /** * @function main */ int main(int argc, char** argv) { //TODO VideoCapture src = VideoCapture("C:\\Users\\youss\\Desktop\\opencv\\movingCars.mp4"); Mat currentFrame, oldGrayedFrame, grayedFrame, mask ; vector<Point2f> prevPts, nextPts; vector<uchar> status; vector<float> err; vector<Point2f> corners; Mat good_new = Mat(), good_old = Mat(); int maxCorners = 100, minDistance = 7, blockSize = 7; double qualityLevel = 0.3; src.read(currentFrame); cvtColor(currentFrame, oldGrayedFrame, COLOR_BGR2GRAY); goodFeaturesToTrack(oldGrayedFrame, corners, maxCorners, qualityLevel, minDistance, Mat(), blockSize); mask = Mat::zeros(currentFrame.size(), currentFrame.type()); prevPts = corners; //while (true) { src.read(currentFrame); cvtColor(currentFrame, grayedFrame, COLOR_BGR2GRAY); calcOpticalFlowPyrLK(oldGrayedFrame, grayedFrame, prevPts, nextPts, status, err); int i, k; for (i = k = 0; i < nextPts.size(); i++) { if (!status[i]) continue; prevPts[k] = prevPts[i]; nextPts[k] = nextPts[i]; err[k] = err[i]; k++; } prevPts.resize(k); nextPts.resize(k); err.resize(k); //} }
我并不希望大家直接帮我写完代码,只想要一些思路或者提示,帮我把下面这段Python的轨迹绘制代码转译成C++:
# draw the tracks for i,(new,old) in enumerate(zip(good_new,good_old)): a,b = new.ravel() c,d = old.ravel() mask = cv2.line(mask, (a,b),(c,d), color[i].tolist(), 2) frame = cv2.circle(frame,(a,b),5,color[i].tolist(),-1) img = cv2.add(frame,mask)
内容的提问来源于stack exchange,提问作者hereForLearing
相关产品推荐
相关产品推荐

