码迷,mamicode.com
首页 > 其他好文 > 详细

opencv实现运动追踪(2)

时间:2015-05-21 19:36:01      阅读:264      评论:0      收藏:0      [点我收藏+]

标签:opencv   图像处理   

简介

  本篇继续讲解opencv上使用BackgroundSubtractorGMG,进行运动物体跟踪,并将跟踪到运动物体用圆框选起来。本篇是基于opecncv官方实例:
bgfg_gmg.cpp,进行讲解和修改。

BackgroundSubtractorGMG使用

具体代码

#include <opencv2/opencv.hpp>
#include <iostream>
#include "opencv2/core/core.hpp"                                                                                                     
#include "opencv2/video/background_segm.hpp"
#include "opencv2/imgproc/imgproc_c.h"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/legacy/legacy.hpp"
 
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
 
using namespace cv;
 
int main(int argc, char** argv){
    Mat frame, fgmask, segm;
 
    Ptr<BackgroundSubtractorGMG> fgbg = Algorithm::create<BackgroundSubtractorGMG>("BackgroundSubtractor.GMG");
    if (fgbg.empty()){
        std::cerr << "Failed to create BackgroundSubtractor.GMG Algorithm." << std::endl;
        return -1;
    }
 
    fgbg->set("initializationFrames", 20);
    fgbg->set("decisionThreshold", 0.7);
 
    VideoCapture cap;
    cap.open(argv[1]);
    if (!cap.isOpened()){
        std::cerr << "Cannot read video. Try moving video file to sample directory." << std::endl;
        return -1;
    }
    namedWindow("FG Segmentation", WINDOW_NORMAL);
    for (;;){
	if (!cap.read(frame)){
	   break;
	}
 
	(*fgbg)(frame, fgmask);
 
        frame.copyTo(segm);
	IplImage ImaskCodeBook = fgmask;
	cvSegmentFGMask(&ImaskCodeBook);
        add(frame, Scalar(100, 100, 0), segm, fgmask);
 
        imshow("FG Segmentation", segm);
        imshow("mask", fgmask);
 
        int c = waitKey(30);
        if (c == ‘q‘ || c == ‘Q‘ || (c & 255) == 27)
            break;
    }
 
    return 0;
}

代码讲解

  1、首先定义一个BackgroundSubtractorGMG结构,并设置它最开始初始化帧数为20,算法阀值参数为0.7
    Ptr<BackgroundSubtractorGMG> fgbg = Algorithm::create<BackgroundSubtractorGMG>("BackgroundSubtractor.GMG");
    if (fgbg.empty()){
        std::cerr << "Failed to create BackgroundSubtractor.GMG Algorithm." << std::endl;
        return -1;
    }
 
    fgbg->set("initializationFrames", 20);
    fgbg->set("decisionThreshold", 0.7);
  2、打开视频文件到cap中。
VideoCapture cap;
    cap.open(argv[1]);
    if (!cap.isOpened()){
        std::cerr << "Cannot read video. Try moving video file to sample directory." << std::endl;
        return -1;
    }
  3、取出图像中数据,使用(*fgbg)一帧一帧的进行跟踪处理,将处理后跟踪到的运动物体位置,保存到fgmask中。接着使用cvSegmentFGMask
做连通域分割,最后把fgmask作为掩码,将当前帧中运动物体颜色修改为:Scalar(100, 100, 0),并保存到segm中,最后将图像segm和掩码fgmask
都实时显示出来。
        if (!cap.read(frame)){
	   break;
	}
 
	(*fgbg)(frame, fgmask);
 
        frame.copyTo(segm);
	IplImage ImaskCodeBook = fgmask;
	cvSegmentFGMask(&ImaskCodeBook);
        add(frame, Scalar(100, 100, 0), segm, fgmask);
 
        imshow("FG Segmentation", segm);
        imshow("mask", fgmask);

效果演示

  对应的效果演示如下:
   技术分享                技术分享 

框选运动目标

  继续我们要做的下一步,是在之前基础上进行修改,在原video文件播放时候,用圆框选出运动目标。

具体代码

#include <opencv2/opencv.hpp>
#include <iostream>
#include "opencv2/core/core.hpp"                                                                                                     
#include "opencv2/video/background_segm.hpp"
#include "opencv2/imgproc/imgproc_c.h"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/legacy/legacy.hpp"
 
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
 
using namespace cv;
 
int main(int argc, char** argv){
    Mat frame, fgmask, segm, find_circle;
	vector<vector<Point> > contours;
	vector<Vec4i> hierarchy;
 
    Ptr<BackgroundSubtractorGMG> fgbg = Algorithm::create<BackgroundSubtractorGMG>("BackgroundSubtractor.GMG");
    if (fgbg.empty()){
        std::cerr << "Failed to create BackgroundSubtractor.GMG Algorithm." << std::endl;
        return -1;
    }
 
    fgbg->set("initializationFrames", 20);
    fgbg->set("decisionThreshold", 0.7);
 
    VideoCapture cap;
    cap.open(argv[1]);
    if (!cap.isOpened()){
        std::cerr << "Cannot read video. Try moving video file to sample directory." << std::endl;
        return -1;
    }
    namedWindow("FG Segmentation", WINDOW_NORMAL);
	for (;;){
		if (!cap.read(frame)){
			break;
		}
 
		(*fgbg)(frame, fgmask);
 
                frame.copyTo(segm);
		IplImage ImaskCodeBook = fgmask;
		cvSegmentFGMask(&ImaskCodeBook);
		fgmask.copyTo(find_circle);
 
		findContours(find_circle, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, Point(0, 0));
		vector<vector<Point> > contours_poly( contours.size());
		vector<Point2f>center( contours.size());
		vector<float>radius( contours.size());
		for( int i = 0; i < contours.size(); i++){
			approxPolyDP( Mat(contours[i]), contours_poly[i], 3, true);
			minEnclosingCircle( contours_poly[i], center[i], radius[i]);
		}
		for(int i = 0; i< contours.size(); i++ ){
			circle(segm, center[i], (int)radius[i], Scalar(100, 100, 0), 2, 8, 0);	
		}
 
        imshow("FG Segmentation", segm);
        imshow("mask", fgmask);
 
        int c = waitKey(30);
        if (c == ‘q‘ || c == ‘Q‘ || (c & 255) == 27)
            break;
    }
 
    return 0;
}

代码讲解

  这里主要多加了的操作步骤,就是对(*fgbg)(frame, fgmask);处理生成的掩码图像fgmask进行轮廓探测,然后根据探测结果,生成出可以框选住该轮廓
的圆的坐标与直径,之后在原图像中根据这个坐标和直径,框选好该运动目标。
                findContours(find_circle, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, Point(0, 0));
		vector<vector<Point> > contours_poly( contours.size());
		vector<Point2f>center( contours.size());
		vector<float>radius( contours.size());
		for( int i = 0; i < contours.size(); i++){
			approxPolyDP( Mat(contours[i]), contours_poly[i], 3, true);
			minEnclosingCircle( contours_poly[i], center[i], radius[i]);
		}
		for(int i = 0; i< contours.size(); i++ ){
			circle(segm, center[i], (int)radius[i], Scalar(100, 100, 0), 2, 8, 0);	
		}

效果演示

  效果演示如下:
  技术分享                技术分享

opencv实现运动追踪(2)

标签:opencv   图像处理   

原文地址:http://blog.csdn.net/u011630458/article/details/45895649

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!