zoukankan      html  css  js  c++  java
  • 11 帧差法获取运动

    帧差法

    三帧差法

    录频工具转gif

    #include "core/core.hpp"
    #include "highgui/highgui.hpp"
    #include "imgproc/imgproc.hpp"
    
    using namespace cv;
    
    int main(int argc, char *argv[])
    {
    	VideoCapture videoCap(0);
    	if (!videoCap.isOpened())
    	{
    		return -1;
    	}
    	double videoFPS = videoCap.get(CV_CAP_PROP_FPS);  //获取帧率
    	double videoPause = 1000 / videoFPS;
    	Mat framePrePre; //上上一帧
    	Mat framePre; //上一帧
    	Mat frameNow; //当前帧
    	Mat frameDet; //运动物体
    	videoCap >> framePrePre;
    	videoCap >> framePre;
    	cvtColor(framePrePre, framePrePre, CV_RGB2GRAY);
    	cvtColor(framePre, framePre, CV_RGB2GRAY);
    	int save = 0;
    	while (true)
    	{
    		videoCap >> frameNow;
    	//	if (frameNow.empty() || waitKey() == 27)
    	if (frameNow.empty() )
    		{
    			break;
    		}
    		cvtColor(frameNow, frameNow, CV_RGB2GRAY);
    		Mat Det1;
    		Mat Det2;
    		absdiff(framePrePre, framePre, Det1);  //帧差1
    		absdiff(framePre, frameNow, Det2);     //帧差2
    		threshold(Det1, Det1, 0, 255, CV_THRESH_OTSU);  //自适应阈值化
    		threshold(Det2, Det2, 0, 255, CV_THRESH_OTSU);
    		Mat element = getStructuringElement(0, Size(3, 3));  //膨胀核
    		dilate(Det1, Det1, element);    //膨胀
    		dilate(Det2, Det2, element);
    		bitwise_and(Det1, Det2, frameDet);
    		framePrePre = framePre;
    		framePre = frameNow;
    		imshow("Video", frameNow);
    		imshow("Detection", frameDet);
    		waitKey(1);
    	}
    	return 0;
    }
    

      

    画正弦曲线

    #include "core/core.hpp"
    #include "highgui/highgui.hpp"
    #include "imgproc/imgproc.hpp"
    #include "opencv2/imgproc/imgproc.hpp" 
    #include "opencv2/highgui/highgui.hpp" 
    #include <stdlib.h> 
    #include <stdio.h> 
    #include <math.h> 
    #include "opencv/cv.h" 
    #include "opencv/highgui.h" 
    
    using namespace cv;
    
    int main(int argc, char *argv[])
    {
    	double y[400];
    	float x;
    	for (x = 0; x<400; x++)
    	{
    	//	y[(int)floor(x)] = sin(x);
    		y[(int)floor(x)] = 200 + 100 * sin(x*0.2);
    
    	}
    	
    
    	Mat grf(410, 410, CV_8UC3);// CV_8UC3  CV_32F
    
    	for (int x = 0; x<400; x++)
    	{
    		line(grf,       /* the dest image */
    			cvPoint(x, y[x]), /* start point */
    
    			cvPoint(x + 1, y[x + 1]),   /* end point */
    			Scalar(0, 255, 0),  /* the color; green */
    			2, 4, 0);     /* thickness, line type, shift */
    
    	}
    	cvNamedWindow("img", CV_WINDOW_AUTOSIZE);
    	imshow("img", grf);
    
    
    	VideoCapture videoCap(0);
    	if (!videoCap.isOpened())
    	{
    		return -1;
    	}
    	double videoFPS = videoCap.get(CV_CAP_PROP_FPS);  //获取帧率
    	double videoPause = 1000 / videoFPS;
    	Mat framePrePre; //上上一帧
    	Mat framePre; //上一帧
    	Mat frameNow; //当前帧
    	Mat frameDet; //运动物体
    	videoCap >> framePrePre;
    	videoCap >> framePre;
    	cvtColor(framePrePre, framePrePre, CV_RGB2GRAY);
    	cvtColor(framePre, framePre, CV_RGB2GRAY);
    	int save = 0;
    	while (true)
    	{
    		videoCap >> frameNow;
    	//	if (frameNow.empty() || waitKey() == 27)
    	if (frameNow.empty() )
    		{
    			break;
    		}
    		cvtColor(frameNow, frameNow, CV_RGB2GRAY);
    		Mat Det1;
    		Mat Det2;
    		absdiff(framePrePre, framePre, Det1);  //帧差1
    		absdiff(framePre, frameNow, Det2);     //帧差2
    		threshold(Det1, Det1, 0, 255, CV_THRESH_OTSU);  //自适应阈值化
    		threshold(Det2, Det2, 0, 255, CV_THRESH_OTSU);
    		Mat element = getStructuringElement(0, Size(9, 9));  //膨胀核
    		dilate(Det1, Det1, element);   
    		dilate(Det2, Det2, element);
    		bitwise_and(Det1, Det2, frameDet);
    		framePrePre = framePre;
    		framePre = frameNow;
    
    
    
    		imshow("Video", frameNow);
    
    		cvtColor(frameDet, frameDet, CV_GRAY2RGB);
    		for (int x = 0; x<400; x++)
    		{
    			line(frameDet,       /* the dest image */
    				cvPoint(x, y[x]), /* start point */
    
    				cvPoint(x + 1, y[x + 1]),   /* end point */
    				Scalar(0, 255, 0),  /* the color; green */
    				2, 4, 0);     /* thickness, line type, shift */
    
    		}
    
    		imshow("Detection", frameDet);
    		waitKey(1);
    	}
    	return 0;
    }
    

      

  • 相关阅读:
    万字长文|Hadoop入门笔记(附资料)
    大数据最后一公里——2021年五大开源数据可视化BI方案对比
    非结构化数据怎么存?——开源对象存储方案介绍
    (三、四)Superset 1.3图表篇——透视表-Pivot Table
    数据湖搭建指南——几个核心问题
    (二)Superset 1.3图表篇——Time-series Table
    DorisDB升级为StarRocks,全面开源!
    (一)Superset 1.3图表篇——Table
    HCNP Routing&Switching之BGP基础
    HCNP Routing&Switching之路由引入导致的问题及解决方案
  • 原文地址:https://www.cnblogs.com/kekeoutlook/p/11638613.html
Copyright © 2011-2022 走看看