初学图像处理+opencv----跟踪视频中运动物体

news/2024/7/21 5:24:39 标签: VS2010, opencv, 图像处理, 视频, 计算机视觉

视频中用图形框起运动物体

代码拷贝http://blog.csdn.net/u011630458/article/details/45895649,我在其中将圆形框注释掉,换成矩形框。代码如下:

// moving detection.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"




#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_shape;
	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("C:\\Users\\Administrator\\Desktop\\数据\\chuli\\chuli\\car.avi");
    if (!cap.isOpened()){
        std::cerr << "Cannot read video. Try moving video file to sample directory." << std::endl;
        return -1;
    }
    namedWindow("detection", WINDOW_NORMAL);
	for (;;){
		if (!cap.read(frame)){
			break;
		}
 
		(*fgbg)(frame, fgmask);
 
                frame.copyTo(segm);
		IplImage ImaskCodeBook = fgmask;
		cvSegmentFGMask(&ImaskCodeBook);
		fgmask.copyTo(find_shape);
 
		findContours(find_shape, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, Point(0, 0));
		//vector<vector<Point> > contours_poly( contours.size());
		vector<Rect>boundRect(contours.size());
		//vector<Point2f>center( contours.size());
		//vector<float>radius( contours.size());
		for( int i = 0; i < contours.size(); i++){
			boundRect[i]=boundingRect( Mat(contours[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++ ){
			rectangle(segm,boundRect[i].tl(),boundRect[i].br(), Scalar(255, 0, 0), 2, 8, 0);
			//circle(segm, center[i], (int)radius[i], Scalar(100, 100, 0), 2, 8, 0);	
		}
 
        imshow("detection", segm);
       // imshow("mask", fgmask);
 
        int c = waitKey(30);
        if (c == 'q' || c == 'Q' || (c & 255) == 27)
            break;
    }
 
    return 0;
}




http://www.niftyadmin.cn/n/1773087.html

相关文章

历届试题 核桃的数量(蓝桥杯)

问题描述小张是软件项目经理&#xff0c;他带领3个开发组。工期紧&#xff0c;今天都在加班呢。为鼓舞士气&#xff0c;小张打算给每个组发一袋核桃&#xff08;据传言能补脑&#xff09;。他的要求是&#xff1a;1. 各组的核桃数量必须相同2. 各组内必须能平分核桃&#xff08…

OPPO大数据平台运营研发实践分享

今天想给大家分享一下OPPO大数据平台规模极速增长历程中我们所做的一些工作&#xff0c;包括我们之前遇到的一些坑和收获的心得。 之前已经提到过OPPO的互联网发展是非常快的&#xff0c;对应到我们数据的规模和平台的规模在过去两三年也是在极速增长&#xff0c;那么我们怎么应…

九度1008:最短路径问题(Dijkstra)

题目描述&#xff1a;给你n个点&#xff0c;m条无向边&#xff0c;每条边都有长度d和花费p&#xff0c;给你起点s终点t&#xff0c;要求输出起点到终点的最短距离及其花费&#xff0c;如果最短距离有多条路线&#xff0c;则输出花费最少的。输入&#xff1a;输入n,m&#xff0c…

win下通过pip安装TensorFlow

官方介绍&#xff08;超详细&#xff09;&#xff1a;https://www.tensorflow.org/install/pip 按照官方介绍&#xff0c;不同的TensorFlow版本只支持特定的python版本所以你要是下载.whl包安装的话&#xff0c;需要选择好合适的版本&#xff0c;我第一次安装的时候不知道这些&…

(Dijstra + 优先队列,Floyd,BellmanFord,SPFA)HDU-1874

http://blog.csdn.net/u011742541/article/details/12916609某省自从实行了很多年的畅通工程计划后&#xff0c;终于修建了很多路。不过路多了也不好&#xff0c;每次要从一个城镇到另一个城镇时&#xff0c;都有许多种道路方案可以选择&#xff0c;而某些方案要比另一些方案行…

进程间通信:共享内存+互斥锁

上一篇进程间通信&#xff1a;共享内存没有实现互斥锁保护&#xff0c;今天用信号量实现一个进程间互斥锁&#xff0c;保护共享变量的修改。 参考资料&#xff1a; http://man7.org/linux/man-pages/man7/sem_overview.7.html http://man7.org/linux/man-pages/man3/sem_init.3…

九度1006:ZOJ问题

题目描述&#xff1a;对给定的字符串(只包含z,o,j三种字符),判断他是否能AC。是否AC的规则如下&#xff1a;1. zoj能AC&#xff1b;2. 若字符串形式为xzojx&#xff0c;则也能AC&#xff0c;其中x可以是N个o 或者为空&#xff1b;3. 若azbjc 能AC&#xff0c;则azbojac也能AC&a…

Apache CouchDB 2.3.1 发布,文档型数据库

Apache CouchDB 2.3.1 发布了&#xff0c;CouchDB 是一个面向文档的数据库管理系统。它提供以 JSON 作为数据格式的 REST 接口来对其进行操作&#xff0c;并可以通过视图来操纵文档的组织和呈现。CouchDB 是 Apache 基金会的顶级开源项目。 这是一个 bugfix 版本&#xff0c;更…