opencv的一些功能代碼

opencv調用攝像頭ios

#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調用測試代碼:ide

#include<opencv2\opencv.hpp>
using namespace cv; using namespace std; int main() { Mat img = imread("cluo.jpg");//圖片必須添加到工程目錄下
    imshow("測試程序", img); waitKey(); }

 opencv中FileStorage的使用:測試

支持多種數據類型的存儲,可以直接存儲vector<string>類型的參數,再讀入數據的時候,參數的順序不會變。ui

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

 打開攝像頭,錄視頻,並存儲spa

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;
}
相關文章
相關標籤/搜索