• 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;
    }
    

      

  • 相关阅读:
    【Oracle SQL】把某表列值借助码值表由代号转换成名称的SQL,子查询效率未必低
    【Java/线程】使用线程非阻塞调用外界接口的门面模式
    【java/线程】基于生产者/队列/消费者模型的表迁移器
    【Java线程】BlockingQueue的抵达容量上限自动阻塞效果展示
    【SpringBoot】使用属性文件给成员变量配置值对final成员变量是无效的,只对普通成员有效
    【脱敏迁移】脱敏迁移提速关键步骤回顾
    【java/date】两种线程安全的取得当前日期的方式
    【SpringBoot】使用属性文件给成员变量配置值对静态成员变量是无效的,只对普通成员有效
    [Java/Jdbc]ResultSet对象的setFetchSize对于大批量数据读取的显著提速作用
    【java线程】新版基于生产者队列消费者的表迁移器
  • 原文地址:https://www.cnblogs.com/kekeoutlook/p/11638613.html
Copyright © 2020-2023  润新知