在光流路径的周围创建边界框 [英] creating a bounding box around a field of optical flow paths
问题描述
我已经使用cv::calcOpticalFlowFarneback
在openFrameworks中使用ofxOpenCv计算当前和先前视频帧中的光通量.
I have used cv::calcOpticalFlowFarneback
to calculate the optical flow in the current and previous frames of video with ofxOpenCv in openFrameworks.
然后,我在视频流场顶部绘制视频,然后绘制矢量来显示在某个阈值以上区域中的运动流.
I then draw the video with the optical flow field on top and then draw vectors showing the flow of motion in areas that are above a certain threshold.
我现在要做的是创建一个运动区域的边界框,并获取质心并将该x
,y
位置存储在变量中以进行跟踪.
What I want to do now is create a bounding box of those areas of motion and get the centroid and store that x
,y
position in a variable for tracking.
如果可以的话,这就是我绘制流场的方式.
This is how I'm drawing my flow field if that helps.
if (calculatedFlow){
ofSetColor( 255, 255, 255 );
video.draw( 0, 0);
int w = gray1.width;
int h = gray1.height;
//1. Input images + optical flow
ofPushMatrix();
ofScale( 4, 4 );
//Optical flow
float *flowXPixels = flowX.getPixelsAsFloats();
float *flowYPixels = flowY.getPixelsAsFloats();
ofSetColor( 0, 0, 255 );
for (int y=0; y<h; y+=5) {
for (int x=0; x<w; x+=5) {
float fx = flowXPixels[ x + w * y ];
float fy = flowYPixels[ x + w * y ];
//Draw only long vectors
if ( fabs( fx ) + fabs( fy ) > .5 ) {
ofDrawRectangle( x-0.5, y-0.5, 1, 1 );
ofDrawLine( x, y, x + fx, y + fy );
}
}
}
}
推荐答案
我通过从flowX和flowY创建新图像解决了我的问题.这是通过将flowX和flowY添加到新的CV FloatImage来完成的.
I resolved my problem by creating new image from my flowX, and flowY. This was done by adding flowX and flowY to a new CV FloatImage.
flowX +=flowY;
flowXY = flowX;
然后,我能够从新创建的图像的像素中进行轮廓查找,然后可以存储所有运动斑点的所有质心.
Then I was able to do the contour finding from the pixels of the newly created image and then I could store all the centroids of all the blobs of movement.
像这样:
contourFinder.findContours( mask, 10, 10000, 20, false );
//Storing the objects centers with contour finder.
vector<ofxCvBlob> &blobs = contourFinder.blobs;
int n = blobs.size(); //Get number of blobs
obj.resize( n ); //Resize obj array
for (int i=0; i<n; i++) {
obj[i] = blobs[i].centroid; //Fill obj array
}
我最初注意到由于负值,仅在x轴和y轴的一个方向上跟踪运动.我通过在cv :: Mat中调用abs()函数来更改光流的计算来解决此问题.
I initially noticed that movement was only being tracked in one direction in the x-axis and y-axis because of negative values. I resolved this by changing the calculation for my optical flow by calling the abs() function in cv::Mat.
Mat img1( gray1.getCvImage() ); //Create OpenCV images
Mat img2( gray2.getCvImage() );
Mat flow;
calcOpticalFlowFarneback( img1, img2, flow, 0.7, 3, 11, 5, 5, 1.1, 0 );
//Split flow into separate images
vector<Mat> flowPlanes;
Mat newFlow;
newFlow = abs(flow); //abs flow so values are absolute. Allows tracking in both directions.
split( newFlow, flowPlanes );
//Copy float planes to ofxCv images flowX and flowY
IplImage iplX( flowPlanes[0] );
flowX = &iplX;
IplImage iplY( flowPlanes[1] );
flowY = &iplY;
这篇关于在光流路径的周围创建边界框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!