Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Saving an image in OpenCV

I am new to OpenCV, and trying to capture an image, and then save it to a file. I am posting the code for your reference, below.

The jpg file is being saved, but it is black.

// Capture the Image from the webcam CvCapture *pCapturedImage = cvCreateCameraCapture(0);  // Get the frame IplImage *pSaveImg = cvQueryFrame(pCapturedImage);  // Save the frame into a file cvSaveImage("test.jpg". ,pSaveImg); // A JPG FILE IS BEING SAVED                                     // OF 6KB , BUT IT IS BLACK 

All of the functions are succesful. I have tried the above code in both XP and Vista - the result is a black image on both. Please let me know what I am missing out.

like image 318
Sujay Ghosh Avatar asked May 12 '09 08:05

Sujay Ghosh


People also ask

How do I save an image in Python?

Using the PIL module to save image to file in Pythonsave() function. This function is used to export an image to an external file. But to use this function, first, we should have an object which contains an image. We can set the format using the format parameter.

Which method of OpenCV is used to save images?

cv2. imwrite() method is used to save an image to any storage device. This will save the image according to the specified format in current working directory.

How do I save an image in OpenCV CPP?

OpenCV provides imwrite() function to save an image to a specified file. The file extension represents the image format. Here, "Destination" is where we want to save the image. In this program, we save the image as "Lakshmi.

How are images stored in OpenCV?

The Mat class of OpenCV library is used to store the values of an image. It represents an n-dimensional array and is used to store image data of grayscale or color images, voxel volumes, vector fields, point clouds, tensors, histograms, etc.


1 Answers

If you use C++, it is best to use C++ interface:

using namespace cv; // Capture the Image from the webcam VideoCapture cap(0);  // Get the frame Mat save_img; cap >> save_img;  if(save_img.empty()) {   std::cerr << "Something is wrong with the webcam, could not get frame." << std::endl; } // Save the frame into a file imwrite("test.jpg", save_img); // A JPG FILE IS BEING SAVED 
like image 54
Barney Szabolcs Avatar answered Oct 07 '22 09:10

Barney Szabolcs