2014-11-24 227 views
0

这看起来相当简单,但对于我的生活,我无法弄清楚为什么这不起作用。我有类似的代码,每次它读取图像时都会写入图像,并且可以很好地保存最后看到的图像。我对此感到困惑,因为这为img0和img1保存了相同的图像。如果你们可以摆脱一些光线,那太棒了!非常感谢您花时间阅读本文。在opencv中拍摄多张照片

#include "highgui.hpp" 
#include "imgproc.hpp" 
#include "opencv.hpp" 
#include <iostream> 
#include <string> 
#include <unistd.h> 
using namespace std; 
using namespace cv; 

int main(){ 
    VideoCapture stream(0); 
    if(!stream.isOpened()){ 
     cout << "No camera :(\n"; 
    } 
    stream.set(CV_CAP_PROP_FRAME_WIDTH, 640); 
    stream.set(CV_CAP_PROP_FRAME_HEIGHT, 480); 
    int img_num = 0; 
    int num_pics; 
    cout << "How many images do you want to take?\n"; 
    cin >> num_pics; 
    Mat image; 
    while(img_num < num_pics){ 
     cout << "Picture in...\n"; 
     cout << "3...\n"; 
     sleep(1); 
     cout << "2...\n"; 
     sleep(1); 
     cout << "1...\n"; 
     sleep(1); 

     stream.read(image); 
     imshow("pic",image); 
     imwrite(format("img_%d.jpg",img_num),image); 
     waitKey(3000); 
     img_num += 1; 
    } 
    return 0; 
} 

编辑添加简单的代码保存捕捉每一帧(为同一个文件,所以最终应被视为最后的图像):

#include "/home/sarah/opencv-2.4.9/modules/highgui/include/opencv2/highgui/highgui.hpp" 
#include "/home/sarah/opencv-2.4.9/modules/imgproc/include/opencv2/imgproc/imgproc.hpp" 
#include "/home/sarah/opencv-2.4.9/include/opencv2/opencv.hpp" 
#include <iostream> 
using namespace std; 
using namespace cv; 

int main(){ 
    VideoCapture stream(0); 
    //stream.set(CV_CAP_PROP_FPS,1); 
    if(!stream.isOpened()){ 
     cout << "No camera :(\n"; 
    } 
    cout << "After\n"; 
    stream.set(CV_CAP_PROP_FRAME_WIDTH, 640); 
    stream.set(CV_CAP_PROP_FRAME_HEIGHT, 480); 
    Mat cameraFrame; 
    while(1){ 
     stream.read(cameraFrame); 
     imshow("camera",cameraFrame); 
     imwrite("img.jpg",cameraFrame); 
     if(waitKey(30) == 13){ 
      break; 
     } 
    } 
    return 0; 
} 
+0

你是什么意思“我有写的图像每次读取的图像类似的代码,并且工作正常”?代码在哪里? – Samer 2014-11-24 23:09:59

+0

做过'imshow(“camera”,cameraFrame);'显示预期结果? – lanpa 2014-11-25 05:04:42

+0

不,它没有@lanpa – marshmallow 2014-11-25 16:12:21

回答

0

这里的罪魁祸首:

imwrite(filename,image); 

atm,它会将任何图像保存为相同的文件名(从而覆盖任何先前的文件)。你想要的东西可能更类似于:

imwrite(format("img_%d.jpg",img_num) ,image); 
+0

问题不在于文件被覆盖。因为即使进行此编辑,所拍摄的第一个图像也会保存到所有后续的.jpg文件中。 – marshmallow 2014-11-24 23:45:34