摄像头录制及调试

//视频录制******保存路径为工程默认路径
//#include<iostream>
//#include<opencv2/opencv.hpp>
//
//using namespace cv;
//using namespace std;
//
//int main()
//{
//	VideoCapture capture;
//	capture.open(1);
//	if (!capture.isOpened())
//	{
//		printf("can not open ...
");
//		return -1;
//	}
//	capture.set(CV_CAP_PROP_FRAME_WIDTH, 640);
//	capture.set(CV_CAP_PROP_FRAME_HEIGHT, 480);
//	Size size = Size(capture.get(CV_CAP_PROP_FRAME_WIDTH), capture.get(CV_CAP_PROP_FRAME_HEIGHT));
//	cout << capture.get(CV_CAP_PROP_FRAME_WIDTH) << endl;
//	cout << capture.get(CV_CAP_PROP_FRAME_HEIGHT) << endl;
//	VideoWriter writer;
//	writer.open("a2.mp4", CV_FOURCC('m', 'p', '4', 'v'), 10, size, true);
//
//	Mat frame, gray;
//	namedWindow("output", CV_WINDOW_AUTOSIZE);
//
//	while (capture.read(frame))
//	{
//		imshow("output", frame);
//		writer.write(frame);
//		if (waitKey(30) == 27)
//			break;
//	}
//
//	//waitKey(0);
//	capture.release();
//	cv::destroyAllWindows();
//	return 0;
//}





////空格图片截取 
//#include<opencv2opencv.hpp>  
//#include<iostream>
//#pragma warning(disable : 4996)
//
//using namespace cv;
//using namespace std;
//
//int main(int argc, char* argv)
//{
//	VideoCapture  capture(1);
//	capture.set(CV_CAP_PROP_FRAME_WIDTH, 640);
//	capture.set(CV_CAP_PROP_FRAME_HEIGHT, 480);
//	Mat frame;
//	if (!capture.isOpened())
//	{
//		cout << "摄像头呢????" << endl;
//		return -1;
//	}
//	char key;
//	char filename[200];
//	int count = 0;
//	while (1)
//	{
//		key = waitKey(30);
//		capture >> frame;
//		imshow("frame", frame);
//
//
//
//		////HSV
//		//Mat imghsv, img_h, img_s, img_v;
//		//vector<Mat> hsv_vec;
//		//cvtColor(frame, imghsv, CV_BGR2HSV);
//		//split(imghsv, hsv_vec);
//		//img_h = hsv_vec[0];
//		//img_s = hsv_vec[1];
//		//img_v = hsv_vec[2];
//		//imshow("h", img_h);
//		//imshow("s", img_s);
//		//imshow("v", img_v);
//
//
//
//		if (key == 27)
//			break;//ESC
//		if (key == 32)//ո
//		{
//			sprintf(filename, "E:\picture\%d.jpg", count++);//
//			//resize(frame, frame, Size(1280, 720));
//			imwrite(filename, frame);
//
//			imshow("h", frame);
//		}
//	}
//	destroyAllWindows();
//
//	return 0;
//}


//
////******获取图片某点处RGB及HSV值******
//#include<opencv2opencv.hpp>
//#include <ctype.h>
//#include "iostream"
//using namespace std;
//using namespace cv;
//void on_mouse(int EVENT, int x, int y, int flags, void* userdata);
//int main(int argc, char** argv)
//{
//
//	Mat src, hsv;
//	//此处更改图片地址
//	src = imread("E:\picture\0.jpg");
//	namedWindow("【display】");
//	setMouseCallback("【display】", on_mouse, &src);
//	while (1)
//	{
//		imshow("【display】", src);
//		waitKey(40);
//	}
//}
//void on_mouse(int EVENT, int x, int y, int flags, void* userdata)
//{
//	Mat rgb, hsv;
//	rgb = *(Mat*)userdata;
//	Mat temp;
//	cvtColor(*(Mat*)userdata, hsv, CV_RGB2HSV);
//	Point p(x, y);
//	switch (EVENT)
//	{
//	case EVENT_LBUTTONDOWN:
//	{
//
//		printf("b=%d	", rgb.at<Vec3b>(p)[0]);
//		printf("g=%d	", rgb.at<Vec3b>(p)[1]);
//		printf("r=%d
", rgb.at<Vec3b>(p)[2]);
//
//		printf("H=%d	", hsv.at<Vec3b>(p)[0]);
//		printf("S=%d	", hsv.at<Vec3b>(p)[1]);
//		printf("V=%d
", hsv.at<Vec3b>(p)[2]);
//		circle(rgb, p, 2, Scalar(255), 3);
//	}
//	break;
//
//	}
//}











//#include <time.h>
//#include "opencv2/highgui/highgui.hpp"
//#include "opencv2/imgproc/imgproc.hpp"
//#include <math.h>
//#include <iostream>
//
//
//using namespace std;
//using namespace cv;
//
//
//int main()
//{
//	//init_serial();
//	clock_t start, finish;
//	double totaltime, heights[16];
//	int hi = 0;
//	//VideoCapture capture("/home/einstein/save_10.avi");
//	//VideoCapture capture(1);
//	VideoCapture capture("E://hh.mp4");
//	Mat frame, binary;
//	RotatedRect RA[16], R[16];
//
//
//	for (;;)
//	{
//		start = clock();
//		capture >> frame;
//		frame.copyTo(binary);
//
//		//cvtColor(frame, frame, COLOR_BGR2GRAY);
//
//		Mat imgHSV, imgdst1, imgdst2, imgdst;
//		cvtColor(frame, imgHSV, COLOR_BGR2HSV);
//		inRange(imgHSV, Scalar(0, 43, 46), Scalar(10, 255, 255), imgdst1);
//		inRange(imgHSV, Scalar(156, 43, 46), Scalar(180, 255, 255), imgdst2);
//		imgdst = imgdst1 | imgdst2;
//		//imshow("imgdst1", imgdst1);
//		//imshow("imgdst2", imgdst2);
//		imshow("imgdst", imgdst);
//
//		Mat threshold_output;
//		threshold(imgdst, threshold_output, 50, 255, cv::THRESH_BINARY);//
//		// Find all the contours in the thresholded image
//		vector<vector<Point>> contours;
//		imshow("yu", threshold_output);
//		findContours(threshold_output, contours, RETR_LIST, CHAIN_APPROX_NONE);
//
//
//		for (size_t i = 0; i < contours.size(); i++) {
//
//			vector<Point> points;
//			double area = contourArea(contours[i]);
//			if (area < 20 || 1e3 < area) continue;
//			drawContours(frame, contours, static_cast<int>(i), Scalar(0), 2);
//
//			double high;
//			points = contours[i];
//
//			RotatedRect rrect = fitEllipse(points);
//			cv::Point2f* vertices = new cv::Point2f[4];
//			rrect.points(vertices);
//
//
//			for (int j = 0; j < 4; j++)
//			{
//				cv::line(binary, vertices[j], vertices[(j + 1) % 4], cv::Scalar(0, 255, 0), 4);
//			}
//
//			//ellipse(binary,rrect,Scalar(0));
//			high = rrect.size.height;
//
////
////			for (size_t j = 1; j < contours.size(); j++) {
////
////				vector<Point> pointsA;
////				double area = contourArea(contours[j]);
////				if (area < 20 || 1e3 < area) continue;
////
////
////				double highA, distance;
////				double slop;
////				pointsA = contours[j];
////
////				RotatedRect rrectA = fitEllipse(pointsA);
////
////				slop = abs(rrect.angle - rrectA.angle);
////				highA = rrectA.size.height;
////				distance = sqrt((rrect.center.x - rrectA.center.x)*(rrect.center.x - rrectA.center.x) +
////					(rrect.center.y - rrectA.center.y)*(rrect.center.y - rrectA.center.y));
////
////
////				double max_height, min_height;
////				if (rrect.size.height > rrectA.size.height) {
////					max_height = rrect.size.height;
////					min_height = rrectA.size.height;
////				}
////				else {
////					max_height = rrectA.size.height;
////					min_height = rrect.size.height;
////				}
////
////				double line_x = abs(rrect.center.x - rrectA.center.x);
////				double difference = max_height - min_height;
////				double aim = distance / ((highA + high) / 2);//距离/高
////				double difference3 = abs(rrect.size.width - rrectA.size.width);
////				double height = (rrect.size.height + rrectA.size.height) / 200;
////				double slop_low = abs(rrect.angle + rrectA.angle) / 2;
////
////
////				if ((aim < 3.0 - height && aim > 2.0 - height     
////					&& slop <= 5 && difference <= 8 && difference3 <= 5
////					&& (slop_low <= 30 || slop_low >= 150) && line_x > 0.6*distance)
////					|| (aim < 5.0 - height && aim > 3.2 - height 
////						&& slop <= 7 && difference <= 15 && difference3 <= 8
////						&& (slop_low <= 30 || slop_low >= 150) && line_x > 0.7*distance)) {
////
////					heights[hi] = (rrect.size.height + rrectA.size.height) / 2;
////					R[hi] = rrect;
////					RA[hi] = rrectA;
////					/*
////					cout << "height_diffrence  "<<difference <<endl;
////					cout <<"slop_low  "  << slop_low<<"  " <<endl;
////					cout <<"width_difference  " <<difference3 <<endl;
////					cout <<"aim  " <<aim <<endl;
////					cout <<"slop" <<slop <<endl;
////					cout <<"angle" <<rrect.angle <<" " <<rrectA.angle <<endl;
////					cout <<"distance" <<distance <<endl;
////				  cout <<"height" <<height <<endl <<endl;
////*/
////					hi++;
////				}
////			}
//		}
//
//
//		double max = 0;
//		int mark;
//		for (int i = 0; i < hi; i++) {     //v
//			if (heights[i] >= max) {
//				max = heights[i];
//				mark = i;
//			}
//		}
//		if (hi != 0) {
//			cv::circle(binary, Point((R[mark].center.x + RA[mark].center.x) / 2,
//				(R[mark].center.y + RA[mark].center.y) / 2),
//				15, cv::Scalar(0, 0, 255), 4);
//
//			//cout  << abs(R[mark].center.x-RA[mark].center.x);
//			double lessx = 320 - (R[mark].center.x + RA[mark].center.x) / 2;   
//			double lessy = 240 - (R[mark].center.y + RA[mark].center.y) / 2;
//			cout <<  lessx << "  " << lessy << " " <<endl;
//
//		}
//
//		imshow("okey", binary);
//		waitKey(2);
//
//		finish = clock();
//		totaltime = (double)(finish - start) / CLOCKS_PER_SEC;
////		cout<<"Time whole"<<totaltime<<""<<endl;
//		hi = 0;
//	}
//
//}









//
//#include<iostream>
//#include <opencv2/opencv.hpp>
//
//using namespace std;
//using namespace cv;
//
//
//int main()
//{
//	VideoCapture cap(1);
//
//	if (!cap.isOpened())
//	{
//		printf("open video failed!
");
//		return 1;
//	}
//
//	Mat Frame;
//
//
//	string outputVideoPath = "test.avi";
//	
//	cap.set(CV_CAP_PROP_FRAME_WIDTH,640);
//	cap.set(CV_CAP_PROP_FRAME_HEIGHT,480);
//	Size size = Size(CV_CAP_PROP_FRAME_WIDTH, CV_CAP_PROP_FRAME_HEIGHT);
//	VideoWriter outputVideo;
//	outputVideo.open(outputVideoPath, CV_FOURCC('M', 'P', '4', '2'), 25.0, size);
//
//	while (cap.isOpened())
//	{
//		cap >> Frame;
//		if (Frame.empty()) break;
//		outputVideo << Frame;
//
//		imshow("img", Frame);
//
//		char c = (char)waitKey(10);
//		if (c == 'q') break;
//
//	}
//
//	outputVideo.release();
//	system("pause");
//}




//
////漫水
//#include <iostream>
//#include "opencv2/opencv.hpp"
//#include <math.h>
//
//
//using namespace std;
//using namespace cv;
//
//
//int main()
//{
//	
//	VideoCapture cap("E://hh.mp4");
//	// if webcam is not available then exit the program
//	if (!cap.isOpened())
//	{
//		cout << "Cannot open the web cam" << endl;
//		return -1;
//	}
//
//	Mat image, binary;
//	while (true) {
//		// read a new frame from webcam
//		bool flag = cap.read(image);
//		if (!flag)
//		{
//			cout << "Cannot read a frame from webcam" << endl;
//			break;
//		}
//		image.copyTo(binary);
//		/*resize(image, image, Size(image.cols*0.5, binary.rows*0.5));
//		resize(binary, binary, Size(binary.cols*0.5, binary.rows*0.5));*/
//
//		cvtColor(image, image, COLOR_BGR2GRAY);
//
//		threshold(image, image, 100, 255, THRESH_BINARY);
//		imshow("threshold",image);
//
//		floodFill(image, Point(5, 100), Scalar(255), 0, FLOODFILL_FIXED_RANGE);
//		circle(image, Point(5, 100), 30, Scalar(0, 0, 255), -1);
//		threshold(image, image, 100, 255, THRESH_BINARY_INV);
//
//		vector<vector<Point>> contours;
//		findContours(image, contours, RETR_LIST, CHAIN_APPROX_NONE);
//		for (size_t i = 0; i < contours.size(); i++) {
//
//			vector<Point> points;
//			double area = contourArea(contours[i]);
//			if (area < 800 || 1000 < area) continue;           
//			drawContours(image, contours, static_cast<int>(i), Scalar(0), 2);
//
//			points = contours[i];
//			//cout <<area <<endl;
//			RotatedRect rrect = fitEllipse(points);
//			cv::Point2f* vertices = new cv::Point2f[4];
//			rrect.points(vertices);
//
//
//			for (int j = 0; j < 4; j++)
//			{
//				cv::line(binary, vertices[j], vertices[(j + 1) % 4], cv::Scalar(0, 255, 0), 4);
//			}
//
//			float middle = 100000;
//
//			for (size_t j = 1; j < contours.size(); j++) {
//
//				vector<Point> pointsA;
//				double area = contourArea(contours[j]);
//				if (area < 1200 || 5000 < area) continue;   //���Ӳ���������С
//
//				pointsA = contours[j];
//
//				RotatedRect rrectA = fitEllipse(pointsA);
//
//				float distance = sqrt((rrect.center.x - rrectA.center.x)*(rrect.center.x - rrectA.center.x) +
//					(rrect.center.y - rrectA.center.y)*(rrect.center.y - rrectA.center.y));
//
//
//
//				if (middle > distance)
//					middle = distance;
//			}
//
//			if (middle > 100) {                        //Ҫ������ֵ��С
//				cv::circle(binary, Point(rrect.center.x, rrect.center.y), 15, cv::Scalar(0, 0, 255), 4);
//			}
//
//
//		}
//
//
//		imshow("frame", binary);
//		imshow("Original", image);
//
//		if (waitKey(100) == 'q')
//		{
//			break;
//		}
//
//	}
//}
原文地址:https://www.cnblogs.com/xingkongcanghai/p/11773237.html