如何在opencv c ++中使用imwrite从凸轮中获取静止图像?

时间:2014-11-14 23:13:44

标签: c++ opencv

此代码用于显示网络摄像头源,当您按s键时,将图像保存到所选路径中的文件中。它不起作用。 imwrite使用此错误代码崩溃程序。

未指定错误(找不到指定扩展名的编写器)在cv :: imwrite_,file ........ \ opencv \ modules \ 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;
}

1 个答案:

答案 0 :(得分:0)

工作正常。问题出在我正在使用的visual studio版本中。我切换到2010并动态链接库,代码工作。

如何在visual studio 2010中安装opencv和链接库 https://www.youtube.com/watch?v=cgo0UitHfp8&list=PLvwB65U8V0HHCEyW2UTyOJym5FsdqfbHQ

感谢您的关注。