Click here to Skip to main content
15,867,594 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
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
#include
#include
#include
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);


}


///////////////////////////////////////////////////////////////////////////////////////
void Dialog::on_pushButton_clicked()
{

if(tmrTimer->isActive()==true)
{
tmrTimer->stop();
ui->pushButton->setText("resume");
}
else
{
tmrTimer->start(25);
ui->pushButton->setText("pause");
}
}
******************************************************
Below is shown the dialog.h (header file) code.
****************************************************

XML
#ifndef DIALOG_H
#define DIALOG_H
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
using namespace std;
using namespace cv;

//////////////////////////////////////////////////////////////
#include <QDialog>

namespace Ui {
class Dialog;
}
/////////////////////////////////////////////////////////////////////////
class Dialog : public QDialog
{
Q_OBJECT


public:
explicit Dialog(QWidget *parent = 0);
~Dialog();


public slots:
void processedframesandudateGUI();

private slots:

void on_pushButton_clicked();

private:
Ui::Dialog *ui;

QImage qimgOriginal;
QImage qimgProcessed;

IplImage* greyImage ;
IplImage* colourImage;
IplImage* newImage;
IplImage* movingAverage ;
IplImage* difference;
IplImage* temp;
CvCapture* inputMovie;
QTimer *tmrTimer;



};

#endif // DIALOG_H


i changed the above code with c++ api as below and still that loop is skipped.. any help would be great.

C#
//this is our containers
        std::vector<std::vector<cv::Point> > contours; // Vector for storing contour
        std::vector<cv::Vec4i> hierarchy;
        cv::Rect rect;

//now we try to find contours on the grayscale image
        findContours( greyImage, contours, hierarchy,CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE );

        for( int i = 0; i< contours.size(); i++ ) // iterate through each contour.
         {
          double a = contourArea( contours[i],false);  //  Find the area of contour

          if(a > 30)//you can delete this lines if you don't want search big contours
          {
          rect = boundingRect(contours[i]); // Find the bounding rectangle
          cv::Point2f centerCircle;
          float rad;
          cv::minEnclosingCircle(contours[i],centerCircle,rad);//search circle
          cv::rectangle(newImage, rect,  cv::Scalar(255,0,0),1, 8,0);//draw rect
          cv::circle(newImage, centerCircle,rad,  cv::Scalar(255,0,0),1, 8,0);//draw circle

         }
         }
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900