This example requires Qt5 >= 5.9 setup for building.
4 #include "main_widget.hpp" 11 #include <QMessageBox> 12 #include <QElapsedTimer> 13 #include <QGraphicsSceneMouseEvent> 14 #include <QRubberBand> 15 #include <QGraphicsRectItem> 17 #include <cvb/image.hpp> 18 #include <cvb/device_factory.hpp> 19 #include <cvb/device.hpp> 20 #include <cvb/async/single_stream_handler.hpp> 21 #include <cvb/driver/driver.hpp> 22 #include <cvb/shapefinder2/classifier_factory.hpp> 24 MainWidget::MainWidget()
27 imageScene_ =
new CustomImageScene(
this);
32 imageView_->
resize(800, 600);
34 imageView_->
setDragMode(QGraphicsView::RubberBandDrag);
37 ctrlWidget_ =
new ControlWidget(
this);
38 addWidget(imageView_);
39 addWidget(ctrlWidget_);
45 streamHandler_ = CustomStreamHandler::Create(device_->Stream(), *
this);
47 ctrlWidget_->EnableGrabCheckBox(
true);
49 QObject::connect(
this, SIGNAL(SearchDone()),
this, SLOT(ShowSearchResult()));
54 device_->Stream()->GetSnapshot();
58 void MainWidget::HandleLoad()
60 imageScene_->ClearScene();
63 ,
Cvb::UI::CvbToQt(Cvb::ExpandPath(Cvb::InstallPath() + CVB_LIT(
"tutorial/ShapeFinder/Images/SF2/Lusterterminal")))
64 ,
"CVB Emulator Image Lists (*.emu);;CVB Drivers (*.vin);;Images (*.bmp *.png *.tif *.tiff *.jpg)");
65 if (fileName.isEmpty())
68 if (fileName.contains(
".emu", Qt::CaseInsensitive))
69 fileName = fileName.replace(
"/",
"\\");
74 if (fileName.contains(
".bmp", Qt::CaseInsensitive) || fileName.contains(
".png", Qt::CaseInsensitive) || fileName.contains(
".tif", Qt::CaseInsensitive) || fileName.contains(
".jpg", Qt::CaseInsensitive))
79 ctrlWidget_->EnableGrabCheckBox(
false);
87 streamHandler_ = CustomStreamHandler::Create(device_->Stream(), *
this);
89 ctrlWidget_->EnableGrabCheckBox(
true);
99 void MainWidget::HandleGrab(
int state)
101 if (state == Qt::CheckState::Checked)
103 imageScene_->ClearScene();
106 streamHandler_->Run();
115 streamHandler_->Finish();
122 void MainWidget::HandleSnap()
124 device_->Stream()->GetSnapshot();
125 if (continuedSearch_)
128 imageScene_->ClearScene();
132 void MainWidget::HandleSave()
136 ,
"Bitmap File (*.bmp);;CVB Emulator Image Lists (*.emu);;JPEG (*.jpg, *.jpeg);;Portable Network Graphic (*.png);;Tagged Image File Format (*.tif, *.tiff);;All Files (*.*)");
137 if (fileName.isEmpty())
143 void MainWidget::HandleLoadModel()
145 imageScene_->ClearScene();
148 ,
Cvb::UI::CvbToQt(Cvb::ExpandPath(Cvb::InstallPath() + CVB_LIT(
"tutorial/ShapeFinder/Images/SF2/Lusterterminal")))
149 ,
"ShapeFinder2 models (*.sf2)");
150 if (fileName.isEmpty())
157 void MainWidget::HandleSaveModel()
160 ,
Cvb::UI::CvbToQt(Cvb::ExpandPath(Cvb::InstallPath() + CVB_LIT(
"tutorial/ShapeFinder/Images/SF2/Lusterterminal")))
161 ,
"ShapeFinder2 models (*.sf2)");
162 if (fileName.isEmpty())
169 void MainWidget::HandleTrain()
173 ctrlWidget_->SetTrainView(*selection_);
174 imageScene_->ClearScene();
179 ctrlWidget_->SetTrainView(*imageView_->
Image());
180 auto aoi = imageView_->
Image()->Bounds();
181 selectionRect_ =
Cvb::Rect<double>(aoi.Left(), aoi.Top(), aoi.Right(), aoi.Bottom());
186 double centerX = selectionRect_.
Left() + ((selectionRect_.
Right() - selectionRect_.
Left()) / 2);
187 double centerY = selectionRect_.
Top() + ((selectionRect_.
Bottom() - selectionRect_.
Top()) / 2);
189 Cvb::Rect<int> teachWindow(selectionRect_.
Left() - centerX, selectionRect_.
Top() - centerY, selectionRect_.
Right() - centerX, selectionRect_.
Bottom() - centerY);
201 void MainWidget::HandleSearch()
205 QMessageBox::warning(
this,
"SF2 Search",
"Please create (train) or load a model/classifier first.");
213 void MainWidget::HandleContSearch(
int state)
215 imageScene_->ClearScene();
217 if (state == Qt::Checked)
218 continuedSearch_ =
true;
220 continuedSearch_ =
false;
224 void MainWidget::HandleSelection(
QRectF selection)
228 auto convertedArea = imageView_->
Image()->ImageToPixelCoordinates(area);
235 selectionRect_ = boundingRect;
239 void MainWidget::Search()
245 double relativeThreshold = 0.55;
246 int minimumThreshold = 20;
247 int coarseLocality = 10;
252 searchResult_ = classifier_->SearchAll(imageView_->
Image()->Plane(0), aoi, mode, relativeThreshold, minimumThreshold, coarseLocality);
256 searchResult_ = classifier_->SearchAll(imageView_->
Image()->Plane(0), imageView_->
Image()->Bounds(), mode, relativeThreshold, minimumThreshold, coarseLocality);
263 void MainWidget::ShowSearchResult()
266 ctrlWidget_->SetResultInfo(searchResult_.size());
267 imageScene_->SetSearchResult(*imageView_->
Image(), searchResult_);
271 ControlWidget::ControlWidget(
QWidget* parent)
281 QObject::connect(openImgBtn, SIGNAL(clicked()), parent, SLOT(HandleLoad()));
283 grabCheckBox_ =
new QCheckBox(tr(
"Grab"));
284 QObject::connect(grabCheckBox_, SIGNAL(stateChanged(
int)), parent, SLOT(HandleGrab(
int)));
292 contSearchCheckBox_ =
new QCheckBox(tr(
"Continued Search"));
293 QObject::connect(contSearchCheckBox_, SIGNAL(stateChanged(
int)), parent, SLOT(HandleContSearch(
int)));
298 QObject::connect(loadModelBtn, SIGNAL(clicked()), parent, SLOT(HandleLoadModel()));
301 QObject::connect(saveModelBtn, SIGNAL(clicked()), parent, SLOT(HandleSaveModel()));
307 QObject::connect(searchBtn, SIGNAL(clicked()), parent, SLOT(HandleSearch()));
309 resultLabel_ =
new QLabel(tr(
"Number of search results: 0"));
316 gbLayout->
addWidget(resultLabel_, 2, 0, 1, -1);
319 mainLayout->addWidget(trainImageView_, 0, 0, 1, -1);
320 mainLayout->addWidget(openImgBtn, 1, 0, 1, 1);
321 mainLayout->addWidget(grabCheckBox_, 1, 1, 1, 1);
322 mainLayout->addWidget(snapBtn, 1, 2, 1, 1);
323 mainLayout->addWidget(saveBtn, 2, 0, 1, 1);
324 mainLayout->addWidget(contSearchCheckBox_, 2, 1, 1, -1);
325 mainLayout->addWidget(groupBox, 3, 0, 1, -1);
327 setLayout(mainLayout);
330 void ControlWidget::EnableGrabCheckBox(
bool value)
332 grabCheckBox_->setEnabled(value);
335 void ControlWidget::SetResultInfo(
size_t numResults)
338 resultLabel_->setText(count);
341 void ControlWidget::SetTrainView(
const Cvb::Image & image)
344 trainImageView_->Refresh(image);
348 CustomImageScene::CustomImageScene(
QWidget* parent)
349 :
Cvb::UI::ImageScene(parent)
357 searchResult_ = result;
360 for (
int i = 0; i < searchResult_.size(); i++)
375 textLabel->setBrush(
QBrush(Qt::red));
376 this->addItem(textLabel);
378 text->
setPos(point.X() + 5, point.Y() + 2.5);
383 void CustomImageScene::ClearScene()
392 this->ImageView()->setDragMode(QGraphicsView::RubberBandDrag);
395 pointStart_ =
event->scenePos();
401 pointEnd_ =
event->scenePos();
403 this->addRect(
QRectF(pointStart_, pointEnd_),
QPen(Qt::blue, 0));
405 emit SelectionDone(
QRectF(pointStart_, pointEnd_));
Cvb::String QtToCvb(const QString text) noexcept
Convenience converter for strings.
Definition: ui.hpp:239
void SetRenderEngine(Cvb::UI::RenderEngine renderEngine)
Set the current render engine.
Definition: detail_image_view.hpp:238
Structure that represents an area of interest in the image.
Definition: area_2d.hpp:20
T Top() const noexcept
Gets first row of the rectangle.
Definition: rect.hpp:111
Search result as returned by the classifier.
Definition: search_result.hpp:23
void SetUploadMode(Cvb::UI::UploadMode uploadMode)
Set the current upload mode.
Definition: decl_image_view.hpp:354
Point2D< double > PixelToImageCoordinates(Point2D< double > point) const noexcept
Convert a point from pixel to image coordinates.
Definition: decl_image.hpp:470
In the CorrelationFine mode, after the initial edge model search a correlation and hill climbing will...
static std::unique_ptr< Image > Load(const String &fileName)
Loads an image with the given file name.
Definition: detail_image.hpp:32
bool IsWaitForRepaintEnabled() const noexcept
Checks if waiting for repaint is set.
Definition: decl_image_view.hpp:273
Rectangle object.
Definition: rect.hpp:25
T Bottom() const noexcept
Gets bottom row of the rectangle (still inside the rectangle).
Definition: rect.hpp:151
Angle Rotation() const noexcept
Rotation angle of the result.
Definition: search_result.hpp:97
T Left() const noexcept
Gets first column of the rectangle.
Definition: rect.hpp:91
double X() const noexcept
X position at which the object has been found.
Definition: search_result.hpp:57
static std::unique_ptr< Classifier > Create(const String &fileName)
Creates a classifier object loading a classifier file.
Definition: classifier.hpp:64
QString number(int n, int base)
Root namespace for the Image Manager interface.
Definition: version.hpp:11
The Common Vision Blox image.
Definition: decl_image.hpp:44
void setPos(const QPointF &pos)
void Refresh(Cvb::UI::RefreshMode refreshMode)
Refresh the view using a specified mode.
Definition: detail_image_view.hpp:68
T Right() const noexcept
Gets rightmost column of the rectangle (still inside the rectangle).
Definition: rect.hpp:131
QString CvbToQt(const Cvb::String &text) noexcept
Convenience converter for strings.
Definition: ui.hpp:254
ImagePtr Image() const noexcept
Get the currently shared image.
Definition: decl_image_view.hpp:365
PrecisionMode
Controls precision over accuracy for ShapeFinder 1 type searches.
Definition: shapefinder2.hpp:46
View to display an image.
Definition: decl_image_view.hpp:70
QString getSaveFileName(QWidget *parent, const QString &caption, const QString &dir, const QString &filter, QString *selectedFilter, QFileDialog::Options options)
QMessageBox::StandardButton critical(QWidget *parent, const QString &title, const QString &text, QMessageBox::StandardButtons buttons, QMessageBox::StandardButton defaultButton)
static std::shared_ptr< T > Open(const String &provider, AcquisitionStack acquisitionStack=AcquisitionStack::PreferVin)
Opens a device with the given provider with its default board and port (if applicable).
Definition: decl_device_factory.hpp:50
void setDragMode(QGraphicsView::DragMode mode)
double Deg() const noexcept
Get the value in degrees.
Definition: angle.hpp:87
QMessageBox::StandardButton warning(QWidget *parent, const QString &title, const QString &text, QMessageBox::StandardButtons buttons, QMessageBox::StandardButton defaultButton)
double Quality() const noexcept
Quality measure of the result.
Definition: search_result.hpp:77
QString getOpenFileName(QWidget *parent, const QString &caption, const QString &dir, const QString &filter, QString *selectedFilter, QFileDialog::Options options)
void setBrush(const QBrush &brush)
void setDefaultTextColor(const QColor &col)
std::unique_ptr< Classifier > Learn(const ImagePlane &plane, Point2D< double > position, Rect< int > teachWindow, const std::vector< Point2D< int >> &dontCarePoints) const
Creates a ShapeFinder2 classifier from plane 0 of the input image.
Definition: classifier_factory.hpp:261
ShapeFinder2 classifier factory object.
Definition: classifier_factory.hpp:26
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
double Y() const noexcept
Y position at which the object has been found.
Definition: search_result.hpp:67
void SetWaitForRepaintEnabled(bool enabled)
Enable and disables waiting for repaints.
Definition: detail_image_view.hpp:125