opencv打开工业相机并保存视频

#include 
#include 
#include     
#include     

using namespace Pylon;
using namespace cv;

void main()
{
	VideoCapture capture(1);

		if (!capture.isOpened())
		{
			printf("Open camera is failed");
			system("pause");
			return ;
		}
		//	// 设置摄像头的拍摄属性为 分辨率640x480,帧率30fps
		capture.set(CV_CAP_PROP_FRAME_HEIGHT, 1024 );
		capture.set(CV_CAP_PROP_FRAME_WIDTH, 1280 );
		capture.set(CAP_PROP_FPS, 30.0);

		//	//获得帧率  
		double frameCount = capture.get(CV_CAP_PROP_FPS);
		VideoWriter writer("VideoTest.avi", CV_FOURCC('M', 'J', 'P', 'G'), frameCount, Size(1280, 1024));

	while (capture.isOpened())
	{
		Mat frame;

		if ((frame.rows != 0) || (frame.cols != 0))
		{
			printf("frame capture failed\n");
			system("pause");
			exit(0);
		}
		capture >> frame;
		writer << frame;
		imshow("VideoPlay", frame);

	   if (waitKey(3) == 27 || waitKey(3) == 32 || waitKey(3) == 13)
		{
			break;
		}
	}
	// 释放相关对象
	   capture.release();
		writer.release();
}

-------------------------------------------
opencv打开工业相机并保存视频_第1张图片

#include 
#include 
//#include     
//#include    
#include 

using namespace Pylon;
using namespace cv;

#define EXE_OpenBaslerCamera 0
#define DLL_OpenBaslerCamera 10

#if DLL_OpenBaslerCamera

#define EXPORT_API __declspec(dllexport)  

VideoCapture basler_camera;
VideoWriter writer_Video("F://Buffers//Bandicam//VideoTest.avi", CV_FOURCC('M', 'J', 'P', 'G'), 25, Size(1280, 1024));


extern "C" bool EXPORT_API openCamera()
{
	bool isOpen = false;
	while (!basler_camera.isOpened())
	{
		std::cout << "Cannot open the camera!" << std::endl;
		//0123
		basler_camera.open(3);
	}
	basler_camera.set(CV_CAP_PROP_FRAME_WIDTH, 1280);
	basler_camera.set(CV_CAP_PROP_FRAME_HEIGHT, 1024);

	if (basler_camera.isOpened())
	{
		isOpen = true;
		//保存
		double frameCount = basler_camera.get(CV_CAP_PROP_FPS);
	}
	return isOpen;
}

extern "C" void   EXPORT_API recieveFrame(uchar* texturePtr)
{

	Mat my_frameBGR;
	Mat my_frameRBG;
	basler_camera >> my_frameBGR;
	//写视频
	writer_Video << my_frameBGR;

	if (my_frameBGR.data)
	{
	
		cvtColor(my_frameBGR, my_frameRBG, CV_BGR2RGB);
		memcpy(texturePtr, my_frameRBG.data, my_frameRBG.cols*my_frameRBG.rows*my_frameRBG.channels()*sizeof(uchar));
	}


}

extern "C" void EXPORT_API closeCamera()
{
	if (basler_camera.isOpened())
	{
		basler_camera.release();
		writer_Video.release();
	}
}
#endif




#if EXE_OpenBaslerCamera
void main()
{
	VideoCapture capture(3); 
		

		if (!capture.isOpened())
		{
			printf("Open camera is failed");
			system("pause");
			return ;
		}
		//	// 设置摄像头的拍摄属性为 分辨率640x480,帧率30fps
		capture.set(CV_CAP_PROP_FRAME_HEIGHT, 1024 );
		capture.set(CV_CAP_PROP_FRAME_WIDTH, 1280 );
		//capture.set(CAP_PROP_FPS, 30.0);

		//	//获得帧率  
		double frameCount = capture.get(CV_CAP_PROP_FPS);
		VideoWriter writer("VideoTest.avi", CV_FOURCC('M', 'J', 'P', 'G'), frameCount, Size(1280, 1024));

	while (capture.isOpened())
	{
	
		Mat frame;

		if ((frame.rows != 0) || (frame.cols != 0))
		{
			printf("frame capture failed\n");
			system("pause");
			exit(0);
		}
		capture >> frame;
		writer << frame;
		imshow("VideoPlay", frame);

	   if (waitKey(3) == 27 || waitKey(3) == 32 || waitKey(3) == 13)
		{
			break;
		}
	}
	// 释放相关对象
	   capture.release();
		writer.release();
}

#endif

你可能感兴趣的:(C/C++,Opencv/Halcon,unity)