Ask Your Question

how can I make imwrite work with a Mat video frame?

asked 2014-11-12 15:43:24 -0600

doulos_21 gravatar image

updated 2014-11-14 18:32:10 -0600

In my code where I state if (c == 's')

how do I save Mat imgH = frame + Scalar(0.299bright, 0.587bright, 0.114*bright); to a index-able image to my hard drive? I tried imwrite and it just crashes any ideas what I'm missing?

#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; 
//const std::string fileName = "c://pics//image%u.jpg";

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<<"Default Brightness -------> "<<Brightness<<endl;
    cout<<"Default Contrast----------> "<<Contrast<<endl;
    cout<<"Default Saturation--------> "<<Saturation<<endl;
    cout<<"Default Gain--------------> "<<Gain<<endl;
    cout<<"Default exp --------------> "<<Exposure<<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"


        Mat frame;

        cap >> frame;   

        int x = 200;

        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); 

        //save image frame 

        if (ch == 's'){

        cout<< "new frame in window " <<buffer<<endl;  
        imshow("Video", imgH );
        imwrite(buffer, imgH); //  <-- this is not working!
        c++ ;

        // Camera Properties 

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


        if (ch == 27){ 

            cout << "esc key is pressed by user" << endl;

            return 0;  

    return 0;
edit retag flag offensive close merge delete



where you used imwrite ?

FLY gravatar imageFLY ( 2014-11-13 02:29:53 -0600 )edit

in if (c == 's') I tried bool bSuccess = imwrite("C:/TestImage.jpg", imgH); and it crashed. I don't know how to convert imgH to a savable image.

doulos_21 gravatar imagedoulos_21 ( 2014-11-13 12:24:48 -0600 )edit

this is the error OpenCV Error: Unspecified error (could not find a writer for the specified exten sion) in cv::imwrite_, file ........\opencv\modules\highgui\src\loadsave.cpp, line 275

doulos_21 gravatar imagedoulos_21 ( 2014-11-13 13:07:33 -0600 )edit

Have you tried another save format than .jpg? I know that depending of the build you are using, no extension modules are included by default. In Windows, I guess the available downloads tends to have it included, but I think you must do some extra steps in Linux or other OS. This is only a comment, I am not really sure this is the reason of your problem!

Doombot gravatar imageDoombot ( 2014-11-17 08:24:17 -0600 )edit

2 answers

Sort by ยป oldest newest most voted

answered 2014-11-18 01:51:01 -0600

doulos_21 gravatar image

this code works fine. The issue was in the version of visual studio I was using and dynamically linking the dll lib files. It would of helped to put a version number when it was said the new open cv only works with the latest visual studio version. 2.49 will only work well with visual studio 2010 and up.

if your having trouble linking lib files and installing open cv 2.49 or higher check out this link.

edit flag offensive delete link more

answered 2014-11-16 23:51:38 -0600

According to your error, the problem is that OpenCV doesn't know how to save files with jpg extension. Do you have libjpeg?

edit flag offensive delete link more

Question Tools


Asked: 2014-11-12 15:43:24 -0600

Seen: 1,943 times

Last updated: Nov 18 '14