Ask Your Question

How to take an snapshot from a ethernet cam?

asked 2013-10-01 05:06:17 -0500

akoo gravatar image

updated 2013-10-01 06:56:13 -0500

Hello everybody,

I've got a code where I show a video from a camera, but now I want to take a snapshot when a key is pressed, but I don't know exactly what function I need to use and how. Could anyone help me please?

Thanks. I post my code.

int main( ) {

  VideoCapture vcap;
  const std::string VideoStreamAddress ="rtsp://";

  if (! {
    std::cout << "Error abriendo stream de video o archivo" << std::endl;
    return 0;
  while (true) {
            if (! {
            std::cout << "No frame" << std::endl;
            imshow("original", image);

            if (waitKey(1) >= 0){
                            //TAKE A PHOTO
     return 0;


Hi again, The code to take snapshots it works fine, I can take a photo when a key is pressed, but now I'm trying to recognize text in that photo using tesseract engine and I'm having an error like this:

Error: Illegal min or max specification! signal_termination_handler:Error:Signal_termination_handler called:Code 5002

I've only just added the code for text recognition within the waitkey if as follows:

if (waitKey(10) >= 0){

            imwrite( format("foto_%3d.png",cuenta++), image); //take snapshot when key is pressed

            // initilize tesseract OCR engine
                 tesseract::TessBaseAPI *myOCR = new tesseract::TessBaseAPI();

                 if (myOCR->Init(NULL, "spa")) {
                          fprintf( stderr, " Could not intialize tesseract.\n" );

                // read image
                  namedWindow("tesseract-opencv", 0);
                  Mat imagen = imread(format("/home/adrian/workspace/foto_%3d.png",cuenta), 0);

                // set region of interest (ROI), i.e. regions that contain text
                  Rect text1ROI(162, 110, 157 , 129);

                // recognize text
                   myOCR->TesseractRect(, 1, imagen.step1(), text1ROI.x, text1ROI.y, text1ROI.width, text1ROI.height);
                   const char *text1 = myOCR->GetUTF8Text();

                // remove "newline"
                   string t1(text1);
                   t1.erase( std::remove(t1.begin(), t1.end(), '\n'), t1.end() );

                // print found text
                   printf("text: \n");
                   printf( "%s",t1.c_str() );

Any idea of that error????


edit retag flag offensive close merge delete


// have a counter var above the while loop:

int count = 0;

// then:

if ( waitKey(10) >=0 ) { imwrite( format("shot_%03d.png", count++), image); }

berak gravatar imageberak ( 2013-10-01 05:20:11 -0500 )edit

Thank you berak! It works fine!

akoo gravatar imageakoo ( 2013-10-01 05:30:15 -0500 )edit

[concerning your edit:]

  • you don't have to read it from disk again:

    Mat imagen = imread(format("/home/adrian/workspace/foto_%3d.png",cuenta), 0);

    cuenta got increased above, right ? so it's no more the same number... and imagen will most likely be empty ( it does not exist )

    just convert the original image to gray:

    `Mat imagen; cvtConvert(image,imagen,CV_BGR2GRAY);

    and use that instead.

  • the actual error does not seem to come from opencv, more from tesseract, so please check the args here carefully:

    myOCR->TesseractRect(, 1, imagen.step1(), text1ROI.x, text1ROI.y,

berak gravatar imageberak ( 2013-10-01 07:16:44 -0500 )edit

I know cuenta is not the same value now, I have changed that just after post the code here.

akoo gravatar imageakoo ( 2013-10-01 07:20:03 -0500 )edit

1 answer

Sort by ยป oldest newest most voted

answered 2013-10-01 05:22:09 -0500


  std::cerr << "Something is wrong with the webcam, ....." << std::endl;
imwrite("snapshot.jpg", image);

edit flag offensive delete link more
Login/Signup to Answer

Question Tools


Asked: 2013-10-01 05:06:17 -0500

Seen: 439 times

Last updated: Oct 01 '13