opencv视频读写+手写数字视频的预处理



 
 在进行视频流手写数字识别时,需要先对视频进行预处理,并提取出数字所在的帧。
 用手机简短的拍了一个手写数字的小视频。但是发现视频的大小和拍摄方向不是很合适。
 于是读入视频后对每帧画面进行了旋转,并将画面大小调整为240*320.
 

opencv读入视频用的是VideoCapture,写视频用的是VideoWriter   

值得注意的是VideoWriter 的用法

C++: VideoWriter::VideoWriter(const string& filename, int fourcc, double fps, Size frameSize, bool isColor=true)

各个参数为:文件的名称,格式,帧率,帧大小,是否彩色。

                     帧大小和是否彩色一定要确保正确,否则会出现得到的视频是6KB或者0KB的结果

以下是程序:

#include <iostream>
#include <opencv2/opencv.hpp>
#include <opencv2/ml/ml.hpp>
#include <vector>
 
using namespace std;
using namespace cv;
 

Mat yuchuli(Mat& src,int new_width,int new_hight,int erzhithreshold) // 预处理,得到清晰的黑底白字
{
	Mat pic,out;
	resize(src,pic,Size(new_width,new_hight)); //化为统一的大小
 	if (pic.channels()==3)                //处理成单通道
		cvtColor(pic,out,COLOR_BGR2GRAY);
	else
		out=pic.clone();
	//adaptiveThreshold(out,out,255, CV_ADAPTIVE_THRESH_MEAN_C, CV_THRESH_BINARY, 3, 5);  
	 threshold(out,out, erzhithreshold, 255, CV_THRESH_BINARY_INV);//二值化
		//由于轮廓检测算法需要从黑色的背景中搜索白色的轮廓,所有此处的threshold最后一项参数为cv.CV_THRESH_BINARY_INV,即反转黑白色。
	 medianBlur(out,out,5);//中值滤波,二值化图还有一些小的白点需要去除

	 while(out.cols>700||out.rows>700)
	  resize(out,out,Size(out.cols/2,out.rows/2)); //若图像过大
	
	return out;
}

void getroi(Mat& src,Mat& dst)  //寻找包围数字的最小矩形,并切割出来,采用的是黑底白字
{
	int left,right,top,bottom;
	left=src.cols;
	right=0;
	top=src.rows;
	bottom=0;
	for (int i=0;i<src.rows;i++)
	{
		for (int j=0;j<src.cols;j++)
		{
			if (src.at<uchar>(i,j)>0)
			{
				if (j<left) left=j;
				if (j>right) right=j;
				if (i<top) top=i;
				if (i>bottom) bottom=i;
			}
		}
	}
	int width=right-left;
	int height=bottom-top;
	Rect dstrect(left,top,width,height);
	dst=dst(dstrect);
	 resize(dst,dst,Size(228,228));
	 
}
Mat newpreprocess(Mat& src, int size1,int size2) //寻找包围数字的最小矩形,并切割出来,采用的是白底黑字
{
	int left,right,top,bottom;
	left=src.cols;
	right=0;
	top=src.rows;
	bottom=0;
	for (int i=0;i<src.rows;i++)
	{
		for (int j=0;j<src.cols;j++)
		{
			if (src.at<uchar>(i,j)<1)
			{
				if (j<left) left=j;
				if (j>right) right=j;
				if (i<top) top=i;
				if (i>bottom) bottom=i;
			}
		}
	}
	int width=right-left;
	int height=bottom-top;
	Rect dstrect(left,top,width,height);
	Mat dst=src(dstrect);
	resize(dst,dst,Size(size1,size2));
	return dst;
}

Mat setmidle(Mat& pic,int new_width,int new_height)  //将数字放在图片中间,并缩放图片
{
	Mat src=pic.clone();
	int size=(src.rows>src.cols)?src.rows:src.cols;
	Mat dst(Size(size,size),src.type(),Scalar(255,255,255));//黑底
	int x =(int)floor((float)(size-src.cols)/2.0f);
	int y =(int)floor((float)(size-src.rows)/2.0f);
	Rect r(x,y,src.cols,src.rows);
	Mat dstroi=dst(r);
	addWeighted(dstroi,0,src,1,0,dstroi); 
	resize(dst,dst,Size(new_width,new_width));
	return dst;
}
 
int main()
{
	 
	VideoCapture capture("1.mp4");  
	 
	if (!capture.isOpened())
	{
		cout<<"can not find"<<endl;
		return 1;
	}

	double rate=capture.get(CV_CAP_PROP_FPS); // 获取帧率
	bool stop(false); // 是否停止播放视频
	Mat frame;        // 用来保存当前帧
	int delay= 1000/rate;  // 每一帧的延迟,用100 很快,1000正常速度  10000 很慢

	
		/************************视频写入 ******************/
	#if 1
	
		Size videoSize(capture.get(CV_CAP_PROP_FRAME_WIDTH),capture.get(CV_CAP_PROP_FRAME_HEIGHT));
		VideoWriter writer;
		writer.open("预处理.avi",CV_FOURCC('M','J','P','G'),rate,Size(240,360),0);//由于对frame进行了大小调整,这里的size要与调整之后的一致
	    VideoWriter writer1("调整.avi",CV_FOURCC('M','J','P','G'),rate,Size(240,360),1);
   #endif
	// 按帧播放视频
	while(!stop)
	{
		 capture>>frame;
		if(!capture.read(frame))   
			break;
		imshow("原视频",frame); 
		flip(frame,frame,0);
		transpose(frame,frame);
		//while(frame.cols>700||frame.rows>700)   //若图像过大,将图像压缩
	   // resize(frame,frame,Size(frame.cols/2,frame.rows/2));  
		resize(frame,frame,Size(240,360)); 
		 imshow("视频大小角度调整",frame);
    	 writer1<<frame;

	 	Mat result=frame.clone();//用于在图上画出轮廓

	 	/**********************************对每一帧图像进行预处理***************************************/
		Mat pic1 = yuchuli(frame,frame.cols,frame.rows,38);//对于所选的视频文件阈值为38比较合适
	    imshow("预处理",pic1);
	    writer<<pic1;
	   Mat picroi=pic1.clone();

#if 1

		/*************************************画出轮廓*****************************************/
		 
		vector<vector<Point>>contours;
	    findContours(pic1,contours, RETR_EXTERNAL,CHAIN_APPROX_SIMPLE); //查找所有外轮廓,输入图像必须为二值图
		drawContours(result,contours,-1,Scalar(0,0,255),1);// 画出轮廓
		imshow("轮廓",result);
		 
       /******************************画出数字最小包围矩形***********************************************/
    	for (int i=0;i<contours.size();i++)
			{
				Rect r =boundingRect(Mat(contours[i]));
				rectangle(frame,r,Scalar(0,255,0),1);
				imshow("矩形框定位置",frame);
			}
		/******切割数字最小包围矩形********/
		if ( contours.size()>0)
		{
			 getroi(picroi,picroi);
			 imshow("getroi",picroi); 
		}

#endif
		
	///退出视频的方法///
		if(waitKey(delay)>=0)   //播放的是已经存在视频文件用这个
			stop=true;          //当调用摄像头需要注销这个退出方法,不然摄像头视频一直停在第一帧

		if (waitKey(1) == 27)   // 按下Esc退出程序
			break;
    //************退出视频**************///
	 

	}
 
	capture.release();   
 
	return 0;
}


程序运行效果如下:

可以看到原视频方向是横着的
















  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值