rcmandar
28th October 2014, 01:55
hello guys,
i want to make an app to track an Object.
I am using opencv. I want change color threshold values using QSliders. However, i am unable to do so.
Please see attached pic along with the code.
Notice all the zeros in plainTextedit. Even if i change the slider position my variables ch1_min and others change. However, i still have 0 numbers for this in inRange function in the code.
Please help.
#include "dialog.h"
#include "ui_dialog.h"
//////////////////////////////////////////////////////////////////////////
/// \brief -> this is the Constructor
/// \param parent
//////////////////////////////////////////////////////////////////////////
Dialog::Dialog(QWidget *parent) : QDialog(parent), ui(new Ui::Dialog)
{
ui->setupUi(this);
timer1 = new QTimer(this);
connect(timer1, SIGNAL(timeout()), this, SLOT(ShowCamera()));
ch1_min = ch2_min = ch3_min = 0;
ch1_max = ch2_max = ch3_max = 255;
}
//////////////////////////////////////////////////////////////////////////
/// \brief -> this is the Deconstructor
//////////////////////////////////////////////////////////////////////////
Dialog::~Dialog()
{
delete ui;
}
/////////////////////////////////////////////////////////////////////////////
/// \brief Dialog::Dialog::paintEvent -> putText function
/// \param e
/////////////////////////////////////////////////////////////////////////////
/*void Dialog::putText(QPaintEvent *e)
{
QPainter painter(&tempframe);
QPen ourpen(Qt::magenta);
ourpen.setWidth(2);
painter.setPen(ourpen);
painter.setFont(QFont("Times", 20, QFont::Bold));
painter.drawText(outputView.rect(), "THIS!!"); ////////////// doesn't work
}*/
//////////////////////////////////////////////////////////////////////////////////////////////
/// \brief Dialog::on_dispButton_clicked -> this function starts or stops the camera.
///
///
/////////////////////////////////////////////////////////////////////////////////////////////
void Dialog::on_dispButton_clicked()
{
if(timer1->isActive() == true)
{
ui->dispButton->setText("Start camView");
timer1->stop();
cameraFeed.release();
ui->camView->setText("Your camera view will be available here.");
}
else
{
ui->dispButton->setText("Stop camView");
timer1->start(30);
Camera.open(0);
}
}
//////////////////////////////////////////////////////////////////////////////////////////////
/// \brief all sliderMoved to change channel min and max
/// \param ch1_min ch2_min ch3_min ch1_max ch2_max ch3_max
/////////////////////////////////////////////////////////////////////////////////////////////
void Dialog::on_ch1min_sliderMoved(int ch1_min)
{
ui->ch1min->valueChanged(ch1_min);
if(ch1_min < ch1_max)
{
ch1_min_string = QString::number(ch1_min);
ui->dsp_ch1_min->clear();
ui->dsp_ch1_min->appendPlainText(ch1_min_string);
}
else
{
ch1_min = ch1_max-1;
ui->ch1min->valueChanged(ch1_min);
ch1_min_string = QString::number(ch1_min);
ui->dsp_ch1_min->clear();
ui->dsp_ch1_min->appendPlainText(ch1_min_string);
}
}
void Dialog::on_ch2min_sliderMoved(int ch2_min)
{
ui->ch2min->valueChanged(ch2_min);
if(ch2_min < ch2_max)
{
ch2_min_string = QString::number(ch2_min);
ui->dsp_ch2_min->clear();
ui->dsp_ch2_min->appendPlainText(ch2_min_string);
}
else
{
ch2_min = ch2_max-1;
ui->ch2min->valueChanged(ch2_min);
ch2_min_string = QString::number(ch2_min);
ui->dsp_ch2_min->clear();
ui->dsp_ch2_min->appendPlainText(ch2_min_string);
}
}
void Dialog::on_ch3min_sliderMoved(int ch3_min)
{
ui->ch3min->valueChanged(ch3_min);
if(ch3_min < ch3_max)
{
ch3_min_string = QString::number(ch3_min);
ui->dsp_ch3_min->clear();
ui->dsp_ch3_min->appendPlainText(ch3_min_string);
}
else
{
ch3_min = ch3_max-1;
ui->ch3min->valueChanged(ch3_min);
ch3_min_string = QString::number(ch3_min);
ui->dsp_ch3_min->clear();
ui->dsp_ch3_min->appendPlainText(ch3_min_string);
}
}
void Dialog::on_ch1max_sliderMoved(int ch1_max)
{
ui->ch1max->valueChanged(ch1_max);
if(ch1_max > ch1_min)
{
ch1_max_string = QString::number(ch1_max);
ui->dsp_ch1_max->clear();
ui->dsp_ch1_max->appendPlainText(ch1_max_string);
}
else
{
ch1_max= ch1_min+1;
ch1_max_string = QString::number(ch1_max);
ui->dsp_ch1_max->clear();
ui->dsp_ch1_max->appendPlainText(ch1_max_string);
}
}
void Dialog::on_ch2max_sliderMoved(int ch2_max)
{
ui->ch2max->valueChanged(ch2_max);
if(ch2_max>ch2_min)
{
ch2_max_string = QString::number(ch2_max);
ui->dsp_ch2_max->clear();
ui->dsp_ch2_max->appendPlainText(ch2_max_string);
}
else
{
ch2_max = ch2_min+1;
ch2_max_string = QString::number(ch2_max);
ui->dsp_ch2_max->clear();
ui->dsp_ch2_max->appendPlainText(ch2_max_string);
}
}
void Dialog::on_ch3max_sliderMoved(int ch3_max)
{
ui->ch3max->valueChanged(ch3_max);
if(ch3_max>ch3_min)
{
ch3_max_string = QString::number(ch3_max);
ui->dsp_ch3_max->clear();
ui->dsp_ch3_max->appendPlainText(ch3_max_string);
}
else
{
ch3_max=ch3_min+1;
ch3_max_string = QString::number(ch3_max);
ui->dsp_ch3_max->clear();
ui->dsp_ch3_max->appendPlainText(ch3_max_string);
}
}
////////////////////////////////////////////////////////////////////////////////////////////////
/// \brief Dialog::ShowCamera -> here we read frames and show them in label called camView
/// temp is used only to make use of visual representation of image in Qt using QImage.
///
////////////////////////////////////////////////////////////////////////////////////////////////
void Dialog::ShowCamera()
{
Camera.read(cameraFeed);
ui->plainTextEdit->appendPlainText((QString::number(ch1_min))); ////this is the reason of all the zeros in plainTextEdit. for this function changed values do not appear updated
cv::inRange(cameraFeed,cv::Scalar(ch1_min,ch2_min, ch3_min),cv::Scalar(ch1_max,ch2_max,ch3_max),thres holdFeed);
if(cameraFeed.empty() == true) return;
////////////////// visualization of image acquired from camera
cameraFeed.copyTo(temp);
cv::cvtColor(temp, temp, CV_BGR2RGB);
QImage qimgOriginal((uchar*) temp.data, temp.cols, temp.rows, temp.step, QImage::Format_RGB888);
////////////////// visualization of binary image
QImage qimgProcessed((uchar*) thresholdFeed.data, thresholdFeed.cols, thresholdFeed.rows, thresholdFeed.step, QImage::Format_Indexed8);
ui->camView->setPixmap(QPixmap::fromImage(qimgOriginal));
ui->outputView->setPixmap(QPixmap::fromImage(qimgProcessed));
}
i want to make an app to track an Object.
I am using opencv. I want change color threshold values using QSliders. However, i am unable to do so.
Please see attached pic along with the code.
Notice all the zeros in plainTextedit. Even if i change the slider position my variables ch1_min and others change. However, i still have 0 numbers for this in inRange function in the code.
Please help.
#include "dialog.h"
#include "ui_dialog.h"
//////////////////////////////////////////////////////////////////////////
/// \brief -> this is the Constructor
/// \param parent
//////////////////////////////////////////////////////////////////////////
Dialog::Dialog(QWidget *parent) : QDialog(parent), ui(new Ui::Dialog)
{
ui->setupUi(this);
timer1 = new QTimer(this);
connect(timer1, SIGNAL(timeout()), this, SLOT(ShowCamera()));
ch1_min = ch2_min = ch3_min = 0;
ch1_max = ch2_max = ch3_max = 255;
}
//////////////////////////////////////////////////////////////////////////
/// \brief -> this is the Deconstructor
//////////////////////////////////////////////////////////////////////////
Dialog::~Dialog()
{
delete ui;
}
/////////////////////////////////////////////////////////////////////////////
/// \brief Dialog::Dialog::paintEvent -> putText function
/// \param e
/////////////////////////////////////////////////////////////////////////////
/*void Dialog::putText(QPaintEvent *e)
{
QPainter painter(&tempframe);
QPen ourpen(Qt::magenta);
ourpen.setWidth(2);
painter.setPen(ourpen);
painter.setFont(QFont("Times", 20, QFont::Bold));
painter.drawText(outputView.rect(), "THIS!!"); ////////////// doesn't work
}*/
//////////////////////////////////////////////////////////////////////////////////////////////
/// \brief Dialog::on_dispButton_clicked -> this function starts or stops the camera.
///
///
/////////////////////////////////////////////////////////////////////////////////////////////
void Dialog::on_dispButton_clicked()
{
if(timer1->isActive() == true)
{
ui->dispButton->setText("Start camView");
timer1->stop();
cameraFeed.release();
ui->camView->setText("Your camera view will be available here.");
}
else
{
ui->dispButton->setText("Stop camView");
timer1->start(30);
Camera.open(0);
}
}
//////////////////////////////////////////////////////////////////////////////////////////////
/// \brief all sliderMoved to change channel min and max
/// \param ch1_min ch2_min ch3_min ch1_max ch2_max ch3_max
/////////////////////////////////////////////////////////////////////////////////////////////
void Dialog::on_ch1min_sliderMoved(int ch1_min)
{
ui->ch1min->valueChanged(ch1_min);
if(ch1_min < ch1_max)
{
ch1_min_string = QString::number(ch1_min);
ui->dsp_ch1_min->clear();
ui->dsp_ch1_min->appendPlainText(ch1_min_string);
}
else
{
ch1_min = ch1_max-1;
ui->ch1min->valueChanged(ch1_min);
ch1_min_string = QString::number(ch1_min);
ui->dsp_ch1_min->clear();
ui->dsp_ch1_min->appendPlainText(ch1_min_string);
}
}
void Dialog::on_ch2min_sliderMoved(int ch2_min)
{
ui->ch2min->valueChanged(ch2_min);
if(ch2_min < ch2_max)
{
ch2_min_string = QString::number(ch2_min);
ui->dsp_ch2_min->clear();
ui->dsp_ch2_min->appendPlainText(ch2_min_string);
}
else
{
ch2_min = ch2_max-1;
ui->ch2min->valueChanged(ch2_min);
ch2_min_string = QString::number(ch2_min);
ui->dsp_ch2_min->clear();
ui->dsp_ch2_min->appendPlainText(ch2_min_string);
}
}
void Dialog::on_ch3min_sliderMoved(int ch3_min)
{
ui->ch3min->valueChanged(ch3_min);
if(ch3_min < ch3_max)
{
ch3_min_string = QString::number(ch3_min);
ui->dsp_ch3_min->clear();
ui->dsp_ch3_min->appendPlainText(ch3_min_string);
}
else
{
ch3_min = ch3_max-1;
ui->ch3min->valueChanged(ch3_min);
ch3_min_string = QString::number(ch3_min);
ui->dsp_ch3_min->clear();
ui->dsp_ch3_min->appendPlainText(ch3_min_string);
}
}
void Dialog::on_ch1max_sliderMoved(int ch1_max)
{
ui->ch1max->valueChanged(ch1_max);
if(ch1_max > ch1_min)
{
ch1_max_string = QString::number(ch1_max);
ui->dsp_ch1_max->clear();
ui->dsp_ch1_max->appendPlainText(ch1_max_string);
}
else
{
ch1_max= ch1_min+1;
ch1_max_string = QString::number(ch1_max);
ui->dsp_ch1_max->clear();
ui->dsp_ch1_max->appendPlainText(ch1_max_string);
}
}
void Dialog::on_ch2max_sliderMoved(int ch2_max)
{
ui->ch2max->valueChanged(ch2_max);
if(ch2_max>ch2_min)
{
ch2_max_string = QString::number(ch2_max);
ui->dsp_ch2_max->clear();
ui->dsp_ch2_max->appendPlainText(ch2_max_string);
}
else
{
ch2_max = ch2_min+1;
ch2_max_string = QString::number(ch2_max);
ui->dsp_ch2_max->clear();
ui->dsp_ch2_max->appendPlainText(ch2_max_string);
}
}
void Dialog::on_ch3max_sliderMoved(int ch3_max)
{
ui->ch3max->valueChanged(ch3_max);
if(ch3_max>ch3_min)
{
ch3_max_string = QString::number(ch3_max);
ui->dsp_ch3_max->clear();
ui->dsp_ch3_max->appendPlainText(ch3_max_string);
}
else
{
ch3_max=ch3_min+1;
ch3_max_string = QString::number(ch3_max);
ui->dsp_ch3_max->clear();
ui->dsp_ch3_max->appendPlainText(ch3_max_string);
}
}
////////////////////////////////////////////////////////////////////////////////////////////////
/// \brief Dialog::ShowCamera -> here we read frames and show them in label called camView
/// temp is used only to make use of visual representation of image in Qt using QImage.
///
////////////////////////////////////////////////////////////////////////////////////////////////
void Dialog::ShowCamera()
{
Camera.read(cameraFeed);
ui->plainTextEdit->appendPlainText((QString::number(ch1_min))); ////this is the reason of all the zeros in plainTextEdit. for this function changed values do not appear updated
cv::inRange(cameraFeed,cv::Scalar(ch1_min,ch2_min, ch3_min),cv::Scalar(ch1_max,ch2_max,ch3_max),thres holdFeed);
if(cameraFeed.empty() == true) return;
////////////////// visualization of image acquired from camera
cameraFeed.copyTo(temp);
cv::cvtColor(temp, temp, CV_BGR2RGB);
QImage qimgOriginal((uchar*) temp.data, temp.cols, temp.rows, temp.step, QImage::Format_RGB888);
////////////////// visualization of binary image
QImage qimgProcessed((uchar*) thresholdFeed.data, thresholdFeed.cols, thresholdFeed.rows, thresholdFeed.step, QImage::Format_Indexed8);
ui->camView->setPixmap(QPixmap::fromImage(qimgOriginal));
ui->outputView->setPixmap(QPixmap::fromImage(qimgProcessed));
}