Opencv保存从网络摄像头捕获的图像
opencv saving image captured from webcam
当代码试图在HD上保存图像时,我收到一个错误"SIGABRT error "。
我正在用MacBook Pro Mountain Lion使用最后的XCODE,库被很好地重新配置。
有人有解决方案或想法吗?
#include "cv.h"
#include "highgui.h"
#include <stdio.h>
using namespace cv;
// A Simple Camera Capture Framework
int main() {
CvCapture* capture = cvCaptureFromCAM( CV_CAP_ANY );
if ( !capture ) {
fprintf( stderr, "ERROR: capture is NULL n" );
getchar();
return -1;
}
// Create a window in which the captured images will be presented
cvNamedWindow( "mywindow", CV_WINDOW_AUTOSIZE );
// Show the image captured from the camera in the window and repeat
while ( 1 ) {
// Get one frame
IplImage* frame = cvQueryFrame( capture );
if ( !frame ) {
fprintf( stderr, "ERROR: frame is null...n" );
getchar();
break;
}
cvShowImage( "mywindow", frame );
// Do not release the frame!
if ( (cvWaitKey(10) & 255) == 's' ) {
CvSize size = cvGetSize(frame);
IplImage* img= cvCreateImage(size, IPL_DEPTH_16S, 1);
img = frame;
cvSaveImage("matteo.jpg",&img);
}
if ( (cvWaitKey(10) & 255) == 27 ) break;
}
// Release the capture device housekeeping
cvReleaseCapture( &capture );
cvDestroyWindow( "mywindow" );
return 0;
}
问题是你混合了你的指针语法。您正在使用IplImage* img= cvCreateImage(size, IPL_DEPTH_16S, 1);
创建一个新的IplImage
,但是在下一行中,当您使用frame
覆盖指针img
时,您将丢失此结构。
导致符号的代码是你发送指针到指针的地方cvSaveImage("matteo.jpg",&img);
。你不应该做&img
,因为img
已经是一个指针。下面是正确的:
cvSaveImage("matteo.jpg",img);
实际上没有理由创建一个新的IplImage
,除非你想在将其保存到文件之前做一些预处理。
我将您的if
-子句修改为以下内容,在我的计算机上运行良好:
if ( cvWaitKey(10) < 0 ) {
cvSaveImage("matteo.jpg",frame);
}
我花了几天时间在网上寻找简单键盘输入的正确解决方案。使用cv::waitKey
时总是有一些延迟。
我找到的解决方案是在从网络摄像头捕获帧后添加Sleep(5)
。
下面的例子是不同论坛主题的组合。
它的工作没有任何延迟/延迟。Windows操作系统。
按"q"键捕捉并保存画面
总是有一个网络摄像头。您可以更改序列以显示捕获的帧/图像。
PS "tipka" -意思是键盘上的"键"。
问候,Andrej
#include <opencv2/opencv.hpp>
#include <iostream>
#include <stdio.h>
#include <windows.h> // For Sleep
using namespace cv;
using namespace std;
int ct = 0;
char tipka;
char filename[100]; // For filename
int c = 1; // For filename
int main(int, char**)
{
Mat frame;
//--- INITIALIZE VIDEOCAPTURE
VideoCapture cap;
// open the default camera using default API
cap.open(0);
// OR advance usage: select any API backend
int deviceID = 0; // 0 = open default camera
int apiID = cv::CAP_ANY; // 0 = autodetect default API
// open selected camera using selected API
cap.open(deviceID + apiID);
// check if we succeeded
if (!cap.isOpened()) {
cerr << "ERROR! Unable to open cameran";
return -1;
}
//--- GRAB AND WRITE LOOP
cout << "Start grabbing" << endl
<< "Press a to terminate" << endl;
for (;;)
{
// wait for a new frame from camera and store it into 'frame'
cap.read(frame);
if (frame.empty()) {
cerr << "ERROR! blank frame grabbedn";
break;
}
Sleep(5); // Sleep is mandatory - for no leg!
// show live and wait for a key with timeout long enough to show images
imshow("CAMERA 1", frame); // Window name
tipka = cv::waitKey(30);
if (tipka == 'q') {
sprintf_s(filename, "C:/Images/Frame_%d.jpg", c); // select your folder - filename is "Frame_n"
cv::waitKey(10);
imshow("CAMERA 1", frame);
imwrite(filename, frame);
cout << "Frame_" << c << endl;
c++;
}
if (tipka == 'a') {
cout << "Terminating..." << endl;
Sleep(2000);
break;
}
}
// the camera will be deinitialized automatically in VideoCapture destructor
return 0;
}
相关文章:
- C++,OpenCV,尝试显示图像时"OpenCV(4.3.0) Error: Assertion failed (size.width>0 && size.height>0)"此错误
- 如何使用OpenCV将RBG图像转换为HSV,并将H、S和V值保存为C++中的3个独立图像
- OpenCV EqualizeHist()从彩色图像创建黑白图像
- 从C++调用网络摄像头并在 Python 中显示图像
- 如何使用OpENCV解决图像处理摄像头IO延迟
- 意外的摄像头校准结果与JPEG图像和EXIF方向上的OPENCV结果
- 使用Opencv直接从网络摄像头获取灰度图像
- 摄像头和图像识别
- 使用openCV从网络摄像头捕获图像的函数仅在第一次调用时起作用
- 从网络摄像头预览捕获图像的最佳DirectShow方式?SampleGrabber已弃用
- 打开简历.从网络摄像头的实时馈送中复制或裁剪图像,而不会泄漏内存
- OpenCV中的网络摄像头图像处理C++速度很慢
- 网络摄像头图像以C++内存
- 使用OpenCV从多个网络摄像头捕获多个静止图像时带宽过高
- opencv/ c++:将保存的图像放置在具有面部识别功能的网络摄像头流上
- Opencv保存从网络摄像头捕获的图像
- 是否可以使用c++ /OpenFrameworks将图像(或图像序列)作为硬件设备(网络摄像头)传递给Mac OSX ?
- 无法保存从网络摄像头捕获的图像(OpenCV 2.3 的 imwrite 编译错误)
- USB网络摄像头图像捕获在没有openCV(LINUX)的C++中
- 在 opencv 中使用网络摄像头时未显示图像