title: how do I grab a still image from a cam using imwrite in opencv c++?
body: <p>this code is suppose to display a webcam feed and when you press the s key save the image to a file in the path selected. It is not working. imwrite crashes the program with this error code. </p>
  
  <p>Unspecified error (could not find a writer for the specified extension) in cv::imwrite_, file ........\opencv\modules\highgui\src\loadsave.cpp, line 275</p>
  
  <p>here is my code it crashes at line 99 imwrite(buffer, imgh); if you can think of any way I can make it save this image I'd be very grateful.   </p>
  
  <pre><code>#include &lt;opencv/cv.h&gt; 
  #include "opencv2/highgui/highgui.hpp"
  #include &lt;iostream&gt;
  #include &lt;cstdlib&gt;
  #include &lt;windows.h&gt; 
  
  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 &lt;&lt; "Cannot open the video cam" &lt;&lt; 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&lt;&lt;"===================================="&lt;&lt;endl;
      cout&lt;&lt;"Default Brightness -------&gt; "&lt;&lt;Brightness&lt;&lt;endl;
      cout&lt;&lt;"Default Contrast----------&gt; "&lt;&lt;Contrast&lt;&lt;endl;
      cout&lt;&lt;"Default Saturation--------&gt; "&lt;&lt;Saturation&lt;&lt;endl;
      cout&lt;&lt;"Default Gain--------------&gt; "&lt;&lt;Gain&lt;&lt;endl;
      cout&lt;&lt;"Default exp --------------&gt; "&lt;&lt;Exposure&lt;&lt;endl; 
      cout&lt;&lt;"===================================="&lt;&lt;endl;
  
  
      // console adjustment for testing  
  
      cout &lt;&lt; "\nFrame size : " &lt;&lt; dWidth &lt;&lt; " x " &lt;&lt; dHeight &lt;&lt; endl;
      int bright; 
      cout&lt;&lt; "\nbrightness level -100 - 100 " &lt;&lt; endl; 
      cin&gt;&gt; bright;  
      cout &lt;&lt;"\nbrightness level: " &lt;&lt;bright &lt;&lt;endl; 
  
      namedWindow("Video"); //create a window called "MyVideo"
  
  
      for(;;) 
      {
  
          Mat frame;
  
          cap &gt;&gt; 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&lt;&lt; "new frame in window " &lt;&lt;buffer&lt;&lt;endl;  
          imshow("Video", imgH );
          imwrite(buffer, imgH); //  &lt;-- 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 &lt;&lt; "esc key is pressed by user" &lt;&lt; endl;
          return 0;  
         }
       }
      return 0;
  }
  </code></pre>