OpenCV Code to save image

This little code-snippet saves image from live video stream (web-camera). Compile the code and hit “s” without qutoes to save the image. The last image which was grabbed by the OpenCV is saved.

The code was tested in Linux (Ubuntu 10.10) and should work on other platforms as well. The only place to be changed is the file path for saving images.

#include "cv.h"
#include "highgui.h"

int main(int argc,char** argv)
   char path[255];

   // For Linux, path to save the captured image

   // For Windows, path to save the captured image
   // strcpy(path,"c:\\home.jpg");

   // Pointer to current frame
   IplImage *frame;

   //Resize image to small size 
   IplImage *small;

  // Create capture device ready
  // here 0 indicates that we want to use camera at 0th index
   CvCapture *capture = cvCaptureFromCAM(0);

   // highgui.h is required

   frame = cvQueryFrame(capture);

   // Must be initialized before actually cvResize is used
   // we are creating image of size that is half of the original frame captured
   // 8 = Bits     3 = Channel 
   small = cvCreateImage(cvSize(frame->width/2,frame->height/2), 8, 3);

   // Query for Frame From Camera
   frame = cvQueryFrame(capture);

   // Resize the grabbed frame

   cvResize(frame, small);    //cvResize(source, destination)

   // Display the captured image
   cvShowImage("capture", small);

  char ch =  cvWaitKey(25);  // Wait for 25 ms for user to hit any key
  if(ch==27) break;  // If Escape Key was hit just exit the loop

  // Save image if s was keyboard

   // Release All Images and Windows
    return 0;

Hope this code is helpful for someone and usually the beginners in opencv.

Samundra Shrestha

I work as fullstack developer. Having spent more than 5 years in the web development technologies, I aspire to create an environment where we can share, learn and grow together.

10 thoughts on “OpenCV Code to save image

  • December 11, 2014 at 2:42 PM

    I have run this program on windows platform using visual studio
    It is giving error as
    “Unhandled exception at 0x00295ba6 in captureimage.exe: 0xC0000005: Access violation reading location 0x0000002c.”

    Capture image is name of my project in visual studio

    • February 28, 2015 at 4:47 PM

      I used linux for development purpose and thus can’t help much on visual studio. But it could possibly be that during setup of the development machine or visual studio some configurations might have slipped away. Please refer to Ashish Kumawat tutorial to setup visual studio and see if that works for you or not.


  • March 2, 2013 at 3:44 PM

    Great help live feed showing perfectly, however i can’t seem to get the image to save.

    char ch = cvWaitKey(25); // Wait for 25 ms for user to hit any key
    if(ch==27) break; // If Escape Key was hit just exit the loop

    // Save image if s was keyboard

    any ideas?

    • March 10, 2013 at 2:38 PM

      Sorry was busy with other stuffs. Can you let me know what problem you faced exactly ?

  • September 11, 2012 at 2:35 AM

    Very helpful! Thank you

  • August 25, 2012 at 2:14 AM

    Please add steps to compile this code. Being a beginner, its very difficult to compile this program as its give an error
    “cv.h: No such file or directory”
    “highgui.h: No such file or directory” etc

    After adding these files, it gave more errors from these files.

    So, please make it more easier for the beginners like me.

    • August 25, 2012 at 3:55 PM

      Hello Skg,

      The above code was supposed to be taken as code snippet. I know for begineers its really very annoying to get started with OpenCV. I assume you have already installed OpenCV in your computer and its in Linux.

      Since, You have asked for instruction to compile, I will shortly list that out here. I am in Ubuntu11.10 (Linux Platform) and I have kept this code
      alias gcv="g++ -I/usr/include/opencv -lcv -lcxcore -lcvaux -lhighgui -lm"
      in my terminal environment. So to compile the above code I just have to use gcv saveimage.cpp -o saveimage

      and then to run the executable file I will use $ ./saveimage.


Leave a Reply

Your email address will not be published. Required fields are marked *