OpenCV 在 ROI 中绘制 Farneback 光流。

Posted

技术标签:

【中文标题】OpenCV 在 ROI 中绘制 Farneback 光流。【英文标题】:OpenCV drawing Farneback optical flow in ROI. 【发布时间】:2017-02-20 11:55:53 【问题描述】:

我编写了一个代码来创建边界框并在其中绘制 Farneback 光流。光流通常是事先计算好的,然后为每个 ROI 框单独绘制。

当我绘制流程时,问题就来了。流动看起来正常,但向下和向右移动。这是输出,注意右下角有移动的人流。

这是随处绘制流的框架,显示应在何处绘制流。

为简单起见,所附代码已被删除,如果有一些未声明的矩阵或其他内容,请见谅。

#include ...

using namespace cv;
using namespace std;

Mat currentImage, img, printr, gray ,prevgray, flow;

void getRectanglesandROI(Mat &Mask, Mat &imgTmp, Mat &imgOut, vector<Rect> &outBoxes);

void DrawFlowMap(Mat Image, Mat ROI, Rect Box, Point centre);

int main (int argc, char *argv[]) 

    VideoCapture inVid("input.avi");

    if (!inVid.isOpened()) 
        cout << "Failed to open the input video" << endl;
        exit(5);

    int loop=0, count =0, MaxTargets=0;
bool test=true; 

    namedWindow("Detected");

    int ex = inVid.get(CV_CAP_PROP_FOURCC);
    double fps = inVid.get(CV_CAP_PROP_FPS);
    int wait=1000/fps;
    Size S = Size(  (int) inVid.get(CV_CAP_PROP_FRAME_WIDTH), (int) inVid.get(CV_CAP_PROP_FRAME_HEIGHT));
    int fr =inVid.get(CV_CAP_PROP_FRAME_COUNT);

    VideoWriter output;                                        // Open the output
    output.open("output.avi", ex, fps, S, true);
    if (!output.isOpened())
        
            cout  << "Could not open the output video for write: " << endl;
            return -1;
        
//=============4EVR=================
    while(test)

    inVid>>currentImage;
        if (currentImage.empty())
        
            count++;
            //if (count==1)if (waitKey(0)==27)waitKey(2);
            if (count==1)fs.release(); break;
            cout <<"Max Targets=" <<MaxTargets<< endl<< "End of video, looping" << endl<<endl;
            inVid.set(CV_CAP_PROP_POS_AVI_RATIO, 0);
            loop=0;
        

        cvtColor(currentImage, gray,CV_RGB2GRAY);
        if (prevgray.empty())gray.copyTo(prevgray);

        currentImage.copyTo(img);

        calcOpticalFlowFarneback(prevgray,gray,flow,0.5,3,21,20,5,1.2,0);

        vector<Rect> outputBoxes;
        getRectanglesandROI(fgMaskMOG2, img, currentImage, outputBoxes);
        gray.copyTo(prevgray);

        imshow("Detected", currentImage);
        waitKey(wait);
    
    return 0;

//============END===========================================================

void getRectanglesandROI(Mat &Mask, Mat &imgTmp, Mat &imgOut, vector<Rect> &outBoxes)

    vector<vector<Point> > v; 
vector<int> targets;
int tarArea=1;

    findContours(Mask, v, CV_RETR_EXTERNAL/*CV_RETR_LIST*/, CV_CHAIN_APPROX_SIMPLE);

    for (int j = 0; j < v.size(); j++) 
            if (tarArea < v[j].size())  // excluding tiny contours
                    targets.push_back(j);
            
    
        for (int j = 0; j < targets.size(); j++)   
            drawContours(imgTmp, v, targets[j], Scalar(255, 0, 255), 1, 8);
            Rect rect = boundingRect(v[targets[j]]);

            roi=currentImage(rect);
            DrawFlowMap(currentImage, roi, rect);
            


void DrawFlowMap(Mat Image, Mat ROI, Rect Box)

Point pt1 = Point(Box.x, Box.y);

for( int y=0; y<roi.rows; y+=5)        //this is the issue area, probably.
    for (int x=0;x<roi.cols;x+=5)
        const Point2f& flowatxy=flow.at<Point2f>(y,x);

            line(Image, Point(cvRound(pt1.x+x),             cvRound(pt1.y+y)),
                        Point(cvRound(pt1.x+x+flowatxy.x),  cvRound(pt1.y+y+flowatxy.y)),   Scalar(0,255,0)); ///FLOW LINES
    


【问题讨论】:

【参考方案1】:

Easy peasy,看了一会儿图片(哭了),我注意到它在正确的地方绘制了流,但是那个地方的 flowatxy 是错误的。所以我把flowatxy声明改成如下:

const Point2f& flowatxy=flow.at<Point2f>( pt1.y+y , pt1.x+x );

【讨论】:

以上是关于OpenCV 在 ROI 中绘制 Farneback 光流。的主要内容,如果未能解决你的问题,请参考以下文章

在openCV2 Python中绘制convexHull

OpenCV 人脸检测 ROI 断言失败

Opencv检测边界和ROI掩码

opencv文字旋转 putText旋转90°

OpenCV 例程200篇217. 鼠标交互获取多边形区域(ROI)

opencv 实现 ROI 框选功能