OpenCV应用


img2video

#include <iostream>
#include <sstream>
#include<opencv2/opencv.hpp>

using namespace cv;
using namespace std;

int main(int argc, char **argv){
	Mat img;
	stringstream ss;
	string dir;
	int n=0;

	ss<<"../imgs/"<<n<<".png";
	ss>>dir;

	img=imread(dir);
	VideoWriter video("test.avi", CV_FOURCC('M', 'J', 'P', 'G'), 25.0, Size(img.cols, img.rows));

	while(true){
		ss.str("");
		ss.clear();
		ss<<"../imgs/"<<n++<<".png";
		ss>>dir;
		img=imread(dir);
		imshow("1",img);
		waitKey(1);
		video<<img;
	}
}

video2img

#include <iostream>
#include <iomanip>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>

using namespace std;
using namespace cv;

int main(int argc, char **argv){
	assert(argc==2);
	VideoCapture capture;
	capture.open(argv[1]);
	int frames=capture.get(CV_CAP_PROP_FRAME_COUNT);
	int now=0;
	stringstream ss;
	Mat src;

	double ratio;
	int qw=-1;

	while(true){
		ratio=(int)100.0*now/frames;
		if((int)ratio%5==0 && ratio!=qw){
			qw=ratio;
			cout<<setw(3)<<setfill(' ')<<ratio<<"% ..."<<endl;
		}

		capture>>src;
		if(src.empty())
			break;
		ss.clear();
		ss.str("");
		ss<<"../img/"<<now<<".jpg";
		imwrite(ss.str(),src);
		now++;
	}
}

imgResize

//https://blog.csdn.net/sss_369/article/details/52982574

#include <string>
#include <opencv2/opencv.hpp>
#include <opencv2/imgproc/imgproc.hpp>

using namespace cv;
using namespace std;

int main(int argc, char* argv[])
{
    string path = "2.jpg";
    int resize_height = 1000;
    int resize_width = 1000;
    Mat src = imread(path);
    Mat dst;
    imshow("src", src);
    //INTER_NEAREST INTER_LINEAR INTER_AREA NTER_LANCZOS4  
    resize(src, dst, Size(resize_width, resize_height), (0, 0), (0, 0), INTER_LINEAR);
    imwrite("3.jpg", dst);
    //waitKey(0);
    return 0;
}

lsd

#include <iostream>
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
using namespace std;
using namespace cv;

int main(int argc, char** argv){
    if (argc != 3){
        std::cout << "./exe 填充图 原图" << std::endl;
	exit(0);
    }
    Mat image = imread(argv[1], IMREAD_GRAYSCALE);//读入填充图
    Mat image2 =imread(argv[2], IMREAD_COLOR);//读入原图
    Ptr<LineSegmentDetector> ls = createLineSegmentDetector(LSD_REFINE_STD);//LSD_REFINE_NONE
    vector<Vec4f> lines_std;
    ls->detect(image, lines_std);//在填充图上检测直线
    Mat drawnLines(image2);
    ls->drawSegments(drawnLines, lines_std);//在原图上画直线
    imwrite("./r.jpg",drawnLines);
    return 0;
}

videoRecoder

/* OpenCV 3.1.0 is the best fit */

#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>
using namespace cv;
using namespace std;

int main(int argc,char *argv[])
{
	VideoCapture capture(1);//如果是笔记本,0打开的是自带的摄像头,1 打开外接的相机
	VideoWriter writer(argv[1],CV_FOURCC('X', 'V', 'I', 'D'), capture.get(CV_CAP_PROP_FPS),cvSize(640,480));
	Mat frame;

	while (capture.isOpened())
	{
		capture >> frame;
		writer << frame;
		imshow("video", frame);
		if (waitKey(1) == 27)//27是键盘摁下esc时,计算机接收到的ascii码值
		{
			break;
		}
	}
	cout<<"Video has been saved as "<<argv[1]<<" !"<<endl;
	return 0;
}

opencvMat

/* OpenCV 3.1.0 is the best fit */

#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>
using namespace cv;
using namespace std;

int main(int argc,char *argv[])
{
	VideoCapture capture(1);//如果是笔记本,0打开的是自带的摄像头,1 打开外接的相机
	VideoWriter writer(argv[1],CV_FOURCC('X', 'V', 'I', 'D'), capture.get(CV_CAP_PROP_FPS),cvSize(640,480));
	Mat frame;

	while (capture.isOpened())
	{
		capture >> frame;
		writer << frame;
		imshow("video", frame);
		if (waitKey(1) == 27)//27是键盘摁下esc时,计算机接收到的ascii码值
		{
			break;
		}
	}
	cout<<"Video has been saved as "<<argv[1]<<" !"<<endl;
	return 0;
}

opencv

//初始化
Mat A=(Mat_<double>(3,3) << 1,2,3,0,1,4,5,6,0);
Mat E=Mat::eye(3,4,CV_64F);//单位矩阵
Mat P=Mat::zeros(3,4,CV_64F);//全0
Mat.at<float>(i,j)=x;
Mat N;或Mat N=Mat();//这样的声明,输出为“[]”
Mat M(2,3,CV_32F);//这样的声明,输出为全0
//运算
Mat.inv();//逆
Mat.t();//转置
norm(Mat);//模长
determinant(A);//行列式
//访问
Mat.ptr<float>(i)[j];//i行j列
Mat.row(i).clone();//i行行向量
Mat.col(i).clone();//i列列向量
//操作
MatA.push_back(MatB);//MatA和MatB列数要相等。在MatA的底部加入MatB,MatA的行数增加
Mat.reshape(0,x);//通道数不变,行数变为x
Mat.resize(x);//截取前x行,x大于行数会填0

猜你喜欢

转载自blog.csdn.net/qq_26697045/article/details/86549907