Ask Your Question

Save image, C++

asked 2014-10-23 08:31:17 -0600

ringholm gravatar image

I am trying to save a color image from a SoftKinetic DS311 camera, using OpenCV.

However, the imagefile that is saved is black.

CvCapture *capture = cvCreateCameraCapture(0);
cvSetCaptureProperty(capture, CV_CAP_PROP_FRAME_WIDTH, 640);
cvSetCaptureProperty(capture, CV_CAP_PROP_FRAME_HEIGHT, 480);

// Get one frame
IplImage* frame;

for (int i = 0; i < 25; i++)
    frame = cvQueryFrame(capture);
printf("Image captured \n");
cvSaveImage("test.jpg", frame);

printf("Image Saved \n");

This is the part of the code that is supposed to save the .jpeg The program is in a function that is called continously in the main loop, so it should not be because the camera is not initialized?

edit retag flag offensive close merge delete


OpenCV 2.4.X or OpenCV3?

Doombot gravatar imageDoombot ( 2014-10-23 08:41:20 -0600 )edit

OpenCV 2.4.9 :)

ringholm gravatar imageringholm ( 2014-10-23 08:44:21 -0600 )edit

Can you please use C++ and not C? Maybe it is because it is old? Use cv::Mat instead of IplImage*, cv::imwrite instead of cvSaveImage, cv::VideoCapture instead of CvCapture, capture >> frame instead of cvQueryFrame, etc...

thdrksdfthmn gravatar imagethdrksdfthmn ( 2014-10-23 09:02:42 -0600 )edit

Just for fun, could you add the following just after your printf:

cv::imshow("Some title", frame); waitKey(0);

If you haven't already done so, it will show you the image that is about to be saved. So you will be able to determine whether it is the saving function (maybe missing plugin for jpg) or something else. If it isn't that, I don't know (I am not an OpenCV master yet ;) )

Doombot gravatar imageDoombot ( 2014-10-23 09:04:57 -0600 )edit

By the way, You are saving just the last frame, since the save function is outside the for loop.

thdrksdfthmn gravatar imagethdrksdfthmn ( 2014-10-23 09:22:48 -0600 )edit

I now changed it, I didn't realize I had it mixed up :/

VideoCapture cap(0);

Mat frame;

    cap &gt;&gt; frame;

printf("Image captured \n");
imshow("test", frame);
printf("Image Saved \n");

However, the problem is the same, it only shows a black image. Would you like to see the entire code? I added it here in a .txt file

ringholm gravatar imageringholm ( 2014-10-24 02:30:51 -0600 )edit

3 answers

Sort by ยป oldest newest most voted

answered 2014-10-24 08:30:56 -0600

Doombot gravatar image

OK, try this code. This is a complete code to display a webcam still on screen with a spacebar push. (Not the best code in the world but it works for me!)

#include "opencv2/opencv.hpp"
#include <opencv2/imgcodecs.hpp>
#include <opencv2/videoio/videoio.hpp>
#include <opencv2/highgui/highgui.hpp>

#include <iostream>
#include <stdio.h>

using namespace cv;
using namespace std;

int main(int, char**)

    VideoCapture cap(0); // Try with 0,1,2, etc. this is the number of your camera 
    if(!cap.isOpened())  // check if we succeeded
        return -1;

    int wow;
    Mat frame;
    for(;;) // Actually this is kind like while(1)
        wow = waitKey(0); // (wait for a keyboard input)

        if(wow==0x1B) break; // This is when you press Esc
        else if(wow == 0x20) {  //When you press the Spacebar

            cap >> frame; // get a new frame from camera    
            imshow("Your Image", frame);
    // the camera will be deinitialized automatically in VideoCapture destructor
    return 0;

So be cautious with the number of the VideoCapture cap(0), this could be a non existent camera. Yours could be VideoCapture(1)...

edit flag offensive delete link more



You can also add an if (!frame.empty()) { cv::imwrite("image name", frame); }

thdrksdfthmn gravatar imagethdrksdfthmn ( 2014-10-27 03:52:14 -0600 )edit

answered 2014-10-29 04:45:26 -0600

ringholm gravatar image

Thank you for the help guys. i actually figured it out. It turned out some other functions in the sample program I was modifying was trying to access the camera as well. It only did not output anything. Weird, but it works now. Thanks a lot!

edit flag offensive delete link more


Glad we could help!

Doombot gravatar imageDoombot ( 2014-10-29 07:09:04 -0600 )edit

answered 2014-11-04 08:38:15 -0600

Ahmad bagus gravatar image

I just try your source code,not error but your code can't open frame of camera.How must I do?

edit flag offensive delete link more



Whose code have you tried? And have you tested if the camera is connected? What version of OpenCV are you using?

thdrksdfthmn gravatar imagethdrksdfthmn ( 2014-11-04 09:05:41 -0600 )edit

If you mean mine, have you changed the number of the camera, as stated in the comments in the code?

Doombot gravatar imageDoombot ( 2014-11-05 10:41:29 -0600 )edit

Question Tools


Asked: 2014-10-23 08:31:17 -0600

Seen: 3,510 times

Last updated: Nov 04 '14