opencv的一些功能代码

opencv调用摄像头

#include<opencv2/opencv.hpp>
using namespace cv;

void main(){
    VideoCapture cap;
    cap.open(0); //打开摄像头

    if (!cap.isOpened())
        return;

    Mat frame;
    while (1)
    {
        //cap >> frame;//等价于cap.read(frame);
        cap.read(frame);
        if (frame.empty())
            break;
        imshow("video", frame);
        if (waitKey(20)>0)//按下任意键退出摄像头  因电脑环境而异,有的电脑可能会出现一闪而过的情况
            break;
    }
    cap.release();
    destroyAllWindows();//关闭所有窗口
}

 opencv调用测试代码:

#include<opencv2\opencv.hpp>
using namespace cv;
using namespace std;

int main()
{
    Mat img = imread("cluo.jpg");//图片必须添加到工程目录下
    imshow("测试程序", img);
    waitKey();
}

 opencv中FileStorage的使用:

支持多种数据类型的存储,能够直接存储vector<string>类型的参数,再读入数据的时候,参数的顺序不会变。

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

void WriteFileStorageData(string filename)
{
    cv::FileStorage fwrite(filename.c_str(), cv::FileStorage::WRITE);
    //2.write data
    float fx = 100;
    fwrite << "fx" << fx;

    Mat data = Mat::ones(8, 8, CV_32F);
    Mat label = Mat::zeros(8, 1, CV_8U);
    fwrite << "data" << data;
    fwrite << "label" << label;

    string name = "img/nears.jpg";
    fwrite << "name" << name;
    //3.close FileStorage
    fwrite.release();
    cout << "write done!" << endl;
}

void ReadFileStorageData(string filename)
{
    cv::FileStorage fread(filename.c_str(), cv::FileStorage::READ);

    if (!fread.isOpened())
    {
        cout << "Failed to open settings file at: " << filename << endl;
        return;
    }

    //3.read data
    float fxread;
    fread["fx"] >> fxread;
    cout << "fxread=" << fxread << endl;
    
    Mat data;
    Mat label;
    fread["data"] >> data;;
    fread["label"] >> label;
    cout << "data=" << data << endl;
    cout << "label=" << label << endl;

    string name;
    fread["name"] >> name;
    cout << "name=" << name<< endl;
    //close FileStorage
    fread.release();
    cout << "read success!" << endl;
}

int main(int argc, char** argv)
{
    string xmlfile = "./setting.xml";

    cv::FileStorage fread(xmlfile.c_str(), cv::FileStorage::READ);
    //judge the exist of xml file  
    if (!fread.isOpened())
    {
        cout << xmlfile << " is not exists!" << endl;

        cout << "ready to write xml file" << endl;
        WriteFileStorageData(xmlfile);
    }

    ReadFileStorageData(xmlfile);

    system("pause");
    return 0;
}

 打开摄像头,录视频,并存储

int main(int argc,char* argv[])
{
    Mat image;
    VideoCapture cap;
    image = cap.open(1);
    if (!cap.isOpened())
    {
        cout << "fail to open!" << endl;
        return -1;
    }

    VideoWriter writer;
    int isColor = 1;
    int frame_fps = 24;
    int frame_width = 640;
    int frame_height = 480;
    //string video_name = "out.avi";
    string video_name = "video.avi";
    writer = VideoWriter(video_name, CV_FOURCC('X', 'V', 'I', 'D'), frame_fps, Size(frame_width, frame_height), isColor);

    while (1)
    {
        cap >> image;
        if (!image.data)
            break;
        writer.write(image);
        imshow("image", image);
        if (waitKey(1) >= 0)
            break;
    }
    cap.release();
    destroyAllWindows();
    return 1;
}

 遍历Mat

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

//通过索引方式遍历每个像素值
void color_traverse1(Mat& img)
{
	int w = img.cols;
	int h = img.rows;
	for (int i = 0; i < h; i++)
		for (int j = 0; j < w; j++)
		{
			Vec3b bgr = img.at<Vec3b>(i, j);
			bgr[0] = 255 - bgr[0];
			bgr[1] = 255 - bgr[1];
			bgr[2] = 255 - bgr[2];
			img.at<Vec3b>(i, j) = bgr;
		}
	cout << "color_traverse1 is over" << endl;
}

//行指针遍历
void color_traverse2(Mat& img)
{
	int w = img.cols;
	int h = img.rows;
	for (int i = 0; i < h; i++)
	{
		uchar* pData = img.ptr<uchar>(i);
		for (int j = 0; j < w; j++)
		{
			int B = *pData;
			*pData++ = 255 - B;
			int G = *pData;
			*pData++ = 255 - G;
			int R = *pData;
			*pData++ = 255 - R;
		}
	}
	cout << "color_traverse2 is over" << endl;
}

//获取Mat对象的指针
void color_traverse3(Mat& img)
{
	int w = img.cols;
	int h = img.rows;
	uchar* pData = img.data;
	for (int i = 0; i < h; i++)
		for (int j = 0; j < w; j++)
		{
			int B = *pData;
			*pData++ = 255 - B;
			int G = *pData;
			*pData++ = 255 - G;
			int R = *pData;
			*pData++ = 255 - R;
		}
	cout << "color_traverse3 is over" << endl;
}

int main()
{
	string imgpath = "E:/vs2015_project/Test/img/cluo.jpg";
	Mat img = imread(imgpath);
	cout << img.rows << " " << img.cols << endl;

	color_traverse3(img);

	imshow("img", img);
	waitKey(0);

	system("pause");
	return 0;
}

  

 

posted @ 2019-01-18 17:19  9分钟带帽丶  阅读(1168)  评论(0编辑  收藏  举报