How to work the loop in Qt GUI application and display the qimage
-
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);
}
///////////////////////////////////////////////////////////////////////////////////////
void Dialog::on_pushButton_clicked()
{if(tmrTimer->isActive()==true)
{
tmrTimer->stop();
ui->pushButton->setText("resume");
}
else
{
tmrTimer->start(25);
ui->pushButton->setText("pause");
}
}@i changed the above code with c++ api as below and still that loop is skipped..
@ std::vector<std::vectorcv::Point > contours; // Vector for storing contour
std::vectorcv::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::rectangle(newImage, rect, cv::Scalar(255,0,0),1, 8,0);//draw rect
printf("==============v======");
}
printf("=v=");
}@
any help to work the code above would be greatly appreciated. bear with me for my bad grammar. English is not my native language. god bless u all. -
@for( ; contour != 0; contour = contour->h_next )@
It may not be compilation issue. It must be logic issue in the above loop. Just check it. What is the initial value of contour ? What is next value of contour. Can you just print and check them ?
-
yes sir i printed those values
initial value of contour=0
and when i try to print the value of contour->h_next it crashes.
i think it contains a null value. can someone correct my logic.
thanx in advance sir for your kind attention. i waited till someone reply.