#include "sample.h"
#include "ui_sample.h"
Sample::Sample(QWidget *parent) :
QWidget(parent),
ui(new Ui::Sample)
{
ui->setupUi(this);
ui->label->setText("");
ui->label_id->setAlignment(Qt::AlignCenter);
rect_state = NOT_SET;
img_name = "";
ui->radioButton_n->setEnabled(false);
ui->radioButton_p->setEnabled(false);
ui->pushButton->setEnabled(false);
ui->save_btn->setEnabled(false);
ui->pushButton_2->setEnabled(false);
}
Sample::Sample(QWidget *parent, int id) :
QWidget(parent),
ui(new Ui::Sample)
{
ui->setupUi(this);
sample_id = id;
ui->label->setText("");
ui->label_id->setText(QString::number(id));
ui->label_id->setAlignment(Qt::AlignCenter);
rect_state = NOT_SET;
}
bool Sample::SetImage(QString image_path, int idx)
{
ui->save_btn->setEnabled(true);
ui->pushButton->setEnabled(false);
ui->radioButton_n->setEnabled(true);
ui->radioButton_p->setEnabled(true);
ui->pushButton_2->setEnabled(true);
ui->radioButton_p->setChecked(true);
sample_id = idx;
ui->label_id->setText(QString::number(sample_id));
ui->label_id->setAlignment(Qt::AlignCenter);
img_name = image_path;
ui->label_img_path->setText("Uri: " + image_path);
string image_path_std = image_path.trimmed().toStdString();
Mat img = imread(image_path_std);
img_cur = img.clone();
cv::resize(img, img, cv::Size(691, 459));
if(!img.empty())
{
DisplayMat(img);
}
else
{
QMessageBox::information(NULL, "Read Failure", "Failed To Read " + image_path, QMessageBox::Ok);
}
SetResult();
return false;
}
void Sample::DisplayMat(Mat mat)
{
Mat rgb;
QImage img;
if(mat.channels()==3)
{
cvtColor(mat,rgb,CV_BGR2RGB);
img = QImage((const uchar*)(rgb.data),rgb.cols,rgb.rows,rgb.cols*rgb.channels(),QImage::Format_RGB888);
}
else
{
img = QImage((const uchar*)(mat.data),mat.cols,mat.rows,mat.cols*mat.channels(),QImage::Format_Indexed8);
}
ui->label->setPixmap(QPixmap::fromImage(img));
ui->label->resize(ui->label->pixmap()->size());
ui->label->show();
}
void Sample::Clear()
{
ui->label_id->setText("");
ui->label_img_path->setText("");
ui->label->clear();
}
void Sample::mousePressEvent(QMouseEvent *event)
{
//cout << "Called1 " << sample_id << img_name.toStdString() << endl;
if(event->button() == Qt::LeftButton)
{
Point point_tmp(event->x(), event->y());
RestrictPoint(point_tmp);
if(rect_state == NOT_SET)
{
rect.x = point_tmp.x;
rect.y = point_tmp.y;
rect_state = IN_PROCESS;
}
}
else if(event->button() == Qt::RightButton)
{
}
}
void Sample::mouseReleaseEvent(QMouseEvent *event)
{
//cout << "Called2 " << sample_id << img_name.toStdString() << endl;
if(event->button() == Qt::LeftButton)
{
Point point_tmp(event->x(), event->y());
RestrictPoint(point_tmp);
if(rect_state == IN_PROCESS)
{
rect.width = abs(point_tmp.x - rect.x);
rect.height = abs(point_tmp.y - rect.y);
rect_state = NOT_SET;
}
}
}
void Sample::mouseMoveEvent(QMouseEvent *event)
{
//cout << "Called3 " << sample_id << img_name.toStdString() << endl;
Point point_tmp(event->x(), event->y());
RestrictPoint(point_tmp);
//cout << rect_state << endl;
if(rect_state == IN_PROCESS)
{
//cout << "Good" << endl;
img_cur_copy.release();
img_cur_copy = img_cur.clone();
//img_cur.copyTo(img_cur_copy);
rectangle(img_cur_copy, Point(rect.x, rect.y), Point(point_tmp.x, point_tmp.y), Scalar(0, 255, 0), 2);
DisplayMat(img_cur_copy);
//imshow("test", img_cur);
}
}
void Sample::RestrictPoint(Point &_point)
{
_point.x -= valid_region.x;
_point.y -= valid_region.y;
_point.x = _point.x>0?_point.x:0;
_point.y = _point.y>0?_point.y:0;
_point.x = _point.x<valid_region.width?_point.x:valid_region.width;
_point.y = _point.y<valid_region.height?_point.y:valid_region.height;
}
void Sample::SetResult()
{
if(img_name != "")
{
if(img_name.contains(".jpg"))
{
QString result_name = img_name;
result_name = result_name.replace(".jpg", ".txt");
// Analysis
QFile file(result_name);
if(file.exists())
{
if(file.open(QIODevice::ReadOnly))
{
QString str_tmp = "";
str_tmp = file.readLine();
if(str_tmp.length() > 0)
{
QStringList str_list_tmp = str_tmp.split(",");
if(str_list_tmp.size() == 5)
{
QString state = str_list_tmp[0];
QString rect_x = str_list_tmp[1];
QString rect_y = str_list_tmp[2];
QString rect_w = str_list_tmp[3];
QString rect_h = str_list_tmp[4];
rect.x = rect_x.toInt();
rect.y = rect_y.toInt();
rect.width = rect_w.toInt();
rect.height = rect_h.toInt();
Mat mat_show = img_cur.clone();
rectangle(mat_show, rect, Scalar(0, 255, 0), 2);
DisplayMat(mat_show);
if(state == "1")
{
ui->radioButton_p->setChecked(true);
ui->radioButton_n->setChecked(false);
}
else
{
ui->radioButton_p->setChecked(false);
ui->radioButton_n->setChecked(true);
}
}
}
}
}
}
}
}
void Sample::paintEvent(QPaintEvent *event)
{
//cout << "Called4 " << sample_id << img_name.toStdString() << endl;
valid_region.x = ui->label->frameGeometry().x();
valid_region.y = ui->label->frameGeometry().y() + 10;
valid_region.height = ui->label->frameGeometry().height();
valid_region.width = ui->label->frameGeometry().width();
}
void Sample::timerEvent(QTimerEvent *event)
{
cout << "Called5 " << endl;
}
void Sample::on_save_btn_clicked()
{
QString label_name = img_name;
label_name.replace("jpg", "txt");
cout << label_name.toStdString() << endl;
ofstream fout(label_name.toStdString());
if(ui->radioButton_n->isChecked())
{
// negative
fout << 0;
}
else
{
// posetive
fout << 1;
}
fout <<"," << rect.x << "," << rect.y << "," << rect.width << "," << rect.height;
fout.close();
ui->save_btn->setEnabled(false);
ui->pushButton->setEnabled(true);
//cout << img_name.toStdString() << rect.x << "-" << rect.y << "-" << rect.width << "-" << rect.height << endl;
}
Sample::~Sample()
{
delete ui;
}
void Sample::on_pushButton_2_clicked()
{
QString label_name = img_name;
label_name.replace("jpg", "unknown");
ofstream fout(label_name.toStdString());
fout.close();
}
void Sample::on_pushButton_clicked()
{
ui->save_btn->setEnabled(true);
ui->pushButton->setEnabled(false);
}