how to work the loop and display the image

asked 2014-08-24 07:55:30 -0500

dazzi gravatar image

updated 2016-02-13 15:17:35 -0500

I am new to Qt and OpenCV and need help with the following problem.

I am developing a code to count vehicles coming to a park. Pre-obtained input video is used to develop the algorithm. I have chosen a Qt widget application with OpenCV for this. Currently when compiling, the loop below is skipped.

for( ; contour != 0; contour = contour->h_next ) 

                {
                    bndRect = cvBoundingRect(contour, 0);

                    ui->txtXYnew->appendPlainText("one contour");
                    pt1.x = bndRect.x;
                    pt1.y = bndRect.y;
                    pt2.x = bndRect.x + bndRect.width;
                    pt2.y = bndRect.y + bndRect.height;

                    printf("--------------------\n");
                 cvRectangle(newImage, pt1, pt2, CV_RGB(255,0,0), 1); 
                 }

and the compilation moves to the next section. Why is that, am I using the timer function wrong ? (I have tested this in a console application and it worked fine both Qt and Visual Studio).

I have used two labels, one to display the input frame, and the second for the processed frame. Currently in the processed frame a black frame is shown. but it should show the processed frame with rectangles drawn around contours.

Is there any way to correct this code ? Below is the complete code.

#include "dialog.h"
#include "ui_dialog.h"
#include  <QtCore>
#include  <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv/cv.h>
using namespace cv;

using namespace std;

Dialog::Dialog(QWidget *parent) :QDialog(parent), ui(new Ui::Dialog)
{
ui->setupUi(this);

inputMovie = cvCaptureFromAVI("E:\\pk.avi");
if (!inputMovie){
   ui->txtXYnew->appendPlainText("error video");
   return;
}


tmrTimer=new QTimer(this);
connect(tmrTimer,SIGNAL(timeout()),this,SLOT(processedframesandudateGUI()));
tmrTimer->start(25);


}
//////////////////////////////////////////////////////////////////////////////////
Dialog::~Dialog()
{
delete ui;
}

/////////////////////////////////////////////////////////////////////////////////
void Dialog::processedframesandudateGUI(){

    CvRect bndRect = cvRect(0,0,0,0);

    CvPoint pt1, pt2;


    CvSize imgSize;
    imgSize.width = 540;
    imgSize.height = 432;
greyImage = cvCreateImage( imgSize, IPL_DEPTH_8U, 1);
movingAverage = cvCreateImage( imgSize, IPL_DEPTH_32F, 3);
colourImage = cvQueryFrame(inputMovie);


bool first = true;


        if(!colourImage)
           { ui->txtXYnew->appendPlainText("no frames");
            return;}

        if(first)
                {
                        difference = cvCloneImage(colourImage);
                        temp = cvCloneImage(colourImage);
                        cvConvertScale(colourImage, movingAverage, 1.0, 0.0);

                    first = false;
                }

        cvConvertScale(movingAverage, temp, 1.0, 0.0);
        cvAbsDiff(colourImage,temp,difference);


        cvCvtColor(difference, greyImage, CV_RGB2GRAY);

        cvThreshold(greyImage,greyImage, 70, 255, CV_THRESH_BINARY);


        newImage = cvCloneImage(colourImage);

        cvDilate(greyImage, greyImage, 0, 18);
        cvErode(greyImage, greyImage, 0, 10);

       CvMemStorage* storage = cvCreateMemStorage(0);
       CvSeq* contour = 0;

         ui->txtXYnew->appendPlainText("contour");
         printf("******\n");

  cvFindContours( greyImage, storage, &contour, sizeof(CvContour), CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE);


              for( ; contour != 0; contour = contour->h_next )

                {
                    bndRect = cvBoundingRect(contour, 0);

                    ui->txtXYnew->appendPlainText("one contour");
                    pt1.x = bndRect.x;
                    pt1.y = bndRect.y;
                    pt2.x = bndRect.x + bndRect.width;
                    pt2.y = bndRect.y + bndRect.height;

                    printf("--------------------\n");
                 cvRectangle(newImage, pt1, pt2, CV_RGB(255,0,0), 1);


                }

     printf("here\n");

    cvCvtColor(colourImage, colourImage, CV_BGR2RGB);
    QImage qimgOriginal((uchar*)colourImage->imageData,colourImage->width, colourImage->height, colourImage->widthStep, QImage::Format_RGB888);
    QImage qimgProcessed((uchar*)newImage->imageData,newImage->width, newImage->height, newImage->widthStep, QImage::Format_RGB888);

    ui->label->setPixmap(QPixmap::fromImage(qimgOriginal));
    ui->label->resize(ui->label->pixmap()->size());
    ui->txtXYnew->appendPlainText("one frame");

    ui->label_2->setPixmap(QPixmap::fromImage(qimgProcessed));
    ui->label_2->resize(ui->label_2->pixmap()->size());

    cvReleaseImage(&temp);
    cvReleaseImage(&difference);
    cvReleaseImage(&greyImage);
    cvReleaseImage(&movingAverage ...
(more)
edit retag flag offensive close merge delete

Comments

just as a sidenote: using opencv's deprecated c-api (IplImages, cv*functions) is a bad idea, they moved to c++ (cv::Mat, cv::functions) some years ago, so should you.

berak gravatar imageberak ( 2014-08-26 02:03:37 -0500 )edit

thanks for your interest sir. yes i have compiled my code with c++ api also. As mention above loop written in c++ also skipped in qt gui application. but both c and c++ codes work fine with visual studio and qt console application with desired output. plz any help here would be greatly helpful.

dazzi gravatar imagedazzi ( 2014-08-26 10:14:09 -0500 )edit