0

I used a for loop for reading 300 frames and for accumulating them.I gave an imshow command inside to print the frames continuously but they are not printed during the for loop is processing but it comes as a single image

Here's my code:

enter code here

#include<iostream>
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include<stdlib.h>
#include<stdio.h>
using namespace cv;
using namespace std;
int main()
{
  char k;
  int learningframes=300;
  VideoCapture cap(0);
  if(cap.isOpened()==0)
  {
    cout<<"ERROR";
    return -1;
  }

  //while(1)
  //{
  Mat frame;
  cap>>frame;
  Mat frameaccF,frameacc,framethr32,framehsv,framethr;
  frameaccF=Mat::zeros(frame.size(),CV_32FC1);
  for(int i=0;i<=learningframes;i++)
  {
    cap>>frame;
    imshow("nn",frame); 
    cvtColor(frame,framehsv,CV_BGR2HSV);

    inRange(framehsv,Scalar(0,30,0),Scalar(50,150,255),framethr);

    framethr.convertTo(framethr,CV_32F);



    accumulate(framethr,frameaccF);
  }
  frameaccF=frameaccF/300;
  frameaccF.convertTo(frameaccF,CV_8U);
  imshow("frame",frame);
  imshow("frameacc",frameaccF);

    waitKey(0);
  //if(k=='q')
  //break;
  //}

  return 0;
  }
Lonewolf
  • 66
  • 2
  • 10

1 Answers1

2

You need to put the waiKey() inside the for loop

for(int i=0;i<=learningframes;i++)
  {
    cap>>frame;
    imshow("nn",frame); 
    cvtColor(frame,framehsv,CV_BGR2HSV);

    inRange(framehsv,Scalar(0,30,0),Scalar(50,150,255),framethr);

    framethr.convertTo(framethr,CV_32F);



    accumulate(framethr,frameaccF);

    waitKey(0);
  }
GPPK
  • 6,546
  • 4
  • 32
  • 57
  • thank you it worked fine by adding waitKey(1) (else have to press a button continuously) , but why didn't it work in the first place?? – Lonewolf Jun 04 '15 at 16:22
  • 1
    @Lonewolf Because it wasn't inside the for loop – GPPK Jun 04 '15 at 16:28
  • what is the role of waitKey inside the loop,Doesn't it just wait for the specified time and pass on? – Lonewolf Jun 04 '15 at 16:35
  • yeah, but you have an imshow() in the if loop so you need it in there, it cant see the waitkey() outside of the loop – GPPK Jun 04 '15 at 16:40
  • why does it want to see the waitKey()? – Lonewolf Jun 04 '15 at 16:41
  • 1
    @Lonewolf - http://stackoverflow.com/questions/5217519/what-does-opencvs-cvwaitkey-function-do . Read the very last point of the most upvoted answer. That explains why. `tl;dr`: You need the `waitKey` there because you need to give the `highgui` library time to draw the images on the screen. If you don't do `waitKey`, nothing is drawn because you don't give it sufficient time for drawing. – rayryeng Jun 04 '15 at 21:18