2014-11-14 82 views
0

此代码假设显示网络摄像头供稿,并且当您按s键时将图像保存到所选路径中的文件中。它不工作。 imwrite使用此错误代码崩溃程序。如何在opencv C++中使用imwrite从cam中获取静止图像?

未指定的错误(找不到指定的分机作家)的CV :: imwrite_,文件........ \ OpenCV的\模块\ highgui的\ src \ loadsave.cpp,线275

这里是我的代码它崩溃在行99 imwrite(buffer,imgh);如果你能想出任何办法,我可以让它保存这张图片,我会非常感激。

#include <opencv/cv.h> 
#include "opencv2/highgui/highgui.hpp" 
#include <iostream> 
#include <cstdlib> 
#include <windows.h> 

using namespace cv; 
using namespace std; 

double Brightness; 
double Contrast; 
double Saturation; 
double Gain; 
double Exposure; 

char buffer[100]; 
int i = 0; 
int c = 1; 

int main(int argc, char* argv[]) 
{ 

    VideoCapture cap(0); // open the video camera no. 0 


    if (!cap.isOpened()){ 
     cout << "Cannot open the video cam" << endl; 
     return -1; 
    } 


    double dWidth = cap.get(CV_CAP_PROP_FRAME_WIDTH); //get the width of frames of the video 
    double dHeight = cap.get(CV_CAP_PROP_FRAME_HEIGHT); //get the height of frames of the video 
    cap.set(CV_CAP_PROP_AUTO_EXPOSURE,0.0);    // turn off auto exposure 

    // Get fixed Cam Properties 

    Brightness = cap.get(CV_CAP_PROP_BRIGHTNESS); 
    Contrast = cap.get(CV_CAP_PROP_CONTRAST); 
    Saturation = cap.get(CV_CAP_PROP_SATURATION); 
    Gain  = cap.get(CV_CAP_PROP_GAIN); 
    Exposure = cap.get(CV_CAP_PROP_AUTO_EXPOSURE); 


    // Display Them 

    cout<<"===================================="<<endl; 
    cout<<"Default Brightness -------> "<<Brightness<<endl; 
    cout<<"Default Contrast----------> "<<Contrast<<endl; 
    cout<<"Default Saturation--------> "<<Saturation<<endl; 
    cout<<"Default Gain--------------> "<<Gain<<endl; 
    cout<<"Default exp --------------> "<<Exposure<<endl; 
    cout<<"===================================="<<endl; 


    // console adjustment for testing 

    cout << "\nFrame size : " << dWidth << " x " << dHeight << endl; 
    int bright; 
    cout<< "\nbrightness level -100 - 100 " << endl; 
    cin>> bright; 
    cout <<"\nbrightness level: " <<bright <<endl; 

    namedWindow("Video"); //create a window called "MyVideo" 


    for(;;) 
    { 

     Mat frame; 

     cap >> frame; 

     int x = 200; 
     Sleep(x); 

     Mat imgH = frame + Scalar(0.0722*bright, 0.7152*bright, 0.7152*bright); // convert BGR to Luminance set to color space 

     char ch =waitKey(30); 
     sprintf(buffer, "C:\\pics\\image%d.jpg" ,c); 
     cvWaitKey(10); 

     //save image frmae 

     if (ch == 's'){ 
     cvWaitKey(10); 
     cout<< "new frame in window " <<buffer<<endl; 
     imshow("Video", imgH); 
     imwrite(buffer, imgH); // <-- this is not working die in a hole!!! 
     c++ ; 
     } 

     // Camera Properties 

     if (ch == 'p'){ 
     cap.set(CV_CAP_PROP_SETTINGS , 1); 
     } 

     //Exit 

     if (ch == 27){ 
     cout << "esc key is pressed by user" << endl; 
     return 0; 
     } 
    } 
    return 0; 
} 
+0

似乎在我的工作很好。你确定该文件夹存在吗? – Xonxt 2014-11-14 23:48:57

+0

该文件夹确实存在运行哪个版本的打开的cv? – 2014-11-15 02:20:59

+0

你确定'imgH'和/或'frame'是有效的图像吗? – AldurDisciple 2014-11-15 16:08:54

回答