#include "dialog.h"
#include "ui_dialog.h"
//////////////////////////////////////////////////////////////////////////
/// \brief -> this is the Constructor
/// \param parent
//////////////////////////////////////////////////////////////////////////
{
ui->setupUi(this);
connect(timer1, SIGNAL(timeout()), this, SLOT(ShowCamera()));
ch1_min = ch2_min = ch3_min = 0;
ch1_max = ch2_max = ch3_max = 255;
rmin = gmin = bmin = 0;
rmax = gmax = bmax = 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!!");
}*/
//////////////////////////////////////////////////////////////////////////////////////////////
/// \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.");
thresholdFeed.release();
ui->outputView->setText("Your output view will be available here.");
}
else
{
ui->dispButton->setText("Stop camView");
timer1->start(30);
Camera.open(1); ///// 1 -> usb webcam
}
}
//////////////////////////////////////////////////////////////////////////////////////////////
/// \brief all sliderMoved and spinBox value changed to change channel min and max
/// \param ch1_min ch2_min ch3_min ch1_max ch2_max ch3_max
/// \param rmin gmin bmin rmax gmax bmax
/////////////////////////////////////////////////////////////////////////////////////////////
void Dialog::on_ch1min_sliderMoved(int ch1_min)
{
rmin = ch1_min;
}
void Dialog::on_ch1minspinBox_valueChanged(int ch1_min)
{
rmin = ch1_min;
}
void Dialog::on_ch2min_valueChanged(int ch2_min)
{
gmin = ch2_min;
}
void Dialog::on_ch2minspinBox_valueChanged(int ch2_min)
{
gmin = ch2_min;
}
void Dialog::on_ch3min_valueChanged(int ch3_min)
{
bmin = ch3_min;
}
void Dialog::on_ch3minspinBox_valueChanged(int ch3_min)
{
bmin = ch3_min;
}
void Dialog::on_ch1max_valueChanged(int ch1_max)
{
rmax = ch1_max;
}
void Dialog::on_ch1maxspinBox_valueChanged(int ch1_max)
{
rmax = ch1_max;
}
void Dialog::on_ch2max_valueChanged(int ch2_max)
{
gmax = ch2_max;
}
void Dialog::on_ch2maxspinBox_valueChanged(int ch2_max)
{
gmax = ch2_max;
}
void Dialog::on_ch3max_valueChanged(int ch3_max)
{
bmax = ch3_max;
}
void Dialog::on_ch3maxspinBox_valueChanged(int ch3_max)
{
bmax = ch3_max;
}
////////////////////////////////////////////////////////////////////////////////////////////////
/// \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); ////////////////it should be cameraFeed otherwise.
if(cameraFeed.empty() == true) return;
cv::inRange(cameraFeed,cv::Scalar(rmin,gmin,bmin),cv::Scalar(rmax,gmax,bmax),thresholdFeed);
morphOps(thresholdFeed);
trackLP(x, y, thresholdFeed);
////////////////// 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);
///////////////// show
ui
->camView
->setPixmap
(QPixmap::fromImage(qimgOriginal
));
ui
->outputView
->setPixmap
(QPixmap::fromImage(qimgProcessed
));
}
//////////////////////////////////////////////////////////////////////////////////////
/// \brief Dialog::morphOps -> morphological operations
/// \param thresh -> passed image from inRange to this
///////////////////////////////////////////////////////////////////////////////////////
void Dialog::morphOps(cv::Mat &threshMorph)
{
///create structuring element that will be used to "dilate" and "erode" image.
///the element chosen here is a 3px by 3px rectangle
cv::Mat erodeElement = getStructuringElement( cv::MORPH_RECT, cv::Size(3,3));
///dilate with larger element so make sure object is nicely visible
cv::Mat dilateElement = getStructuringElement( cv::MORPH_RECT,cv::Size(8,8));
cv::erode(threshMorph,threshMorph,erodeElement);
cv::erode(threshMorph,threshMorph,erodeElement);
cv::dilate(threshMorph,threshMorph,dilateElement);
cv::dilate(threshMorph,threshMorph,dilateElement);
}
/////////////////////////////////////////////////////////////////////////////////////////////
/// \brief trackLP -> this tracks the x,y coordinate from binary image
/// \param x -> x coordinate
/// \param y -> y coordinate
/// \param threshold -> binary image
/// \param cameraFeed -> acquired image from camera
//////////////////////////////////////////////////////////////////////////////////////////////
void Dialog::trackLP(int &x, int &y, cv::Mat &threshTrack) //////// cv::Mat &cameraFeed
{
threshTrack.copyTo(temp1);
cv::vector< cv::vector<cv::Point> > contours;
cv::vector<cv::Vec4i> hierarchy;
//find contours of filtered image using openCV findContours function
findContours(temp,contours,hierarchy,CV_RETR_CCOMP,CV_CHAIN_APPROX_SIMPLE );
double refArea = 0;
bool objectFound = false;
if (hierarchy.size() > 0)
{
int numObjects = hierarchy.size();
//if number of objects greater than MAX_NUM_OBJECTS we have a noisy filter
if(numObjects<MAX_NUM_OBJECTS)
{
for (int index = 0; index >= 0; index = hierarchy[index][0])
{
cv::Moments moment = cv::moments((cv::Mat)contours[index]);
double area = moment.m00;
//if the area is less than 5 px by 5 px then it is probably just noise
//if the area is the same as the 3/2 of the image size, probably just a bad filter
//we only want the object with the largest area so we safe a reference area each
//iteration and compare it to the area in the next iteration.
if(area>MIN_OBJ_AREA && area<MAX_OBJ_AREA && area>refArea)
{
x = moment.m10/area;
y = moment.m01/area;
objectFound = true;
refArea = area;
}
else objectFound = false;
}
//let user know you found an object
if(objectFound ==true)
{
}
}
else
{
ui
->plainTextEdit
->appendPlainText
(QString("still too much noise!"));
}
}
}
Bookmarks