CVB++ 15.0
Foundation/QmlBlobSearch

This example requires Qt5 >= 5.9 setup for building.

This example requires Qt5 >= 5.9 setup for building.

You may build it with Ubuntu 18.04's default Qt5 after installing:

1// ---------------------------------------------------------------------------
3// ---------------------------------------------------------------------------
4
5#include <iostream>
6
7
8
9
10
11
12
13
14
15#include <QApplication>
16#include <QQuickView>
17#include <QQmlContext>
18#include <QIcon>
19
20
21
22
23#include <cvb/ui/image_view_item.hpp>
24#include <cvb/foundation/blob.hpp>
25#include <cvb/image.hpp>
26
27#include "blob_result_model.hpp"
28
29int main(int argc, char* argv[])
30{
31 try
32 {
33
34 QApplication app(argc, argv);
35
36 Cvb::String path(Cvb::InstallPath() + CVB_LIT("tutorial/Foundation/Images/Blob/Microswitch.bmp"));
37 // expand environment variables in path
38 path = Cvb::ExpandPath(path);
39 auto image = Cvb::Image::Load(path);
40
42 filter[Cvb::Foundation::BlobRangeFilter::Size] = Cvb::ValueRange<int>(100, 100000);
43 auto blobResults = Cvb::Foundation::BinarizeAndSearchBlobs(image->Plane(0), Cvb::ValueRange<int>(0, 62), filter);
44
45 BlobResultModel blobResultModel(blobResults);
46
47
48 // register QML components for an image display
51
52 Cvb::UI::ImageController imageController;
53
54 QQuickView view;
55 view.setResizeMode(QQuickView::SizeRootObjectToView);
56 view.setIcon(QIcon(":/qttutorial.png"));
57 auto context = view.rootContext();
58 // create a controller object to communicate with QML
59 context->setContextProperty("mainImage", &imageController);
60 context->setContextProperty("blobResultModel", &blobResultModel);
61
62 // load main QML file
63 view.setSource(QUrl::fromLocalFile("../main.qml"));
64
65
66 // refresh the image display through the controller object
67 imageController.Refresh(std::move(image));
68
69 view.resize(640, 480);
70 view.show();
71
72
73
74 return app.exec();
75 }
76 catch (const std::exception& error)
77 {
78 std::cout << error.what() << std::endl;
79 }
80}
Class to build a filter for the blob search.
Definition: blob.hpp:387
static std::unique_ptr< Image > Load(const String &fileName)
Loads an image with the given file name.
Definition: detail_image.hpp:32
Controller object for the QML image view item.
Definition: image_view_item.hpp:252
void Refresh(const ImagePtr &image, AutoRefresh autoRefresh=AutoRefresh::Off)
Share the image and refresh the view.
Definition: image_view_item.hpp:294
static void Register()
Convenience method to register this type in QML.
Definition: image_view_item.hpp:90
static void Register()
Convenience method to register this type in QML.
Definition: image_view_item.hpp:667
Container for range definitions.
Definition: value_range.hpp:17
void setContextProperty(const QString &name, QObject *value)
void setResizeMode(QQuickView::ResizeMode)
QQmlContext * rootContext() const const
void setSource(const QUrl &url)
QUrl fromLocalFile(const QString &localFile)
void resize(const QSize &newSize)
void setIcon(const QIcon &icon)
void show()

UI definition using QML:

1import QtQuick 2.3
2import CvbQuick 1.0 as CvbQuick
3
4
5Rectangle
6{
7
8 CvbQuick.ImageView
9 {
10 id: view
11 image : mainImage
12 anchors.fill : parent
13
14 Repeater
15 {
16 model : blobResultModel
17 delegate: BoxLabel
18 {
19 imageView : view
20 imageX : boundingBox.x
21 imageY : boundingBox.y
22 boxWidth : boundingBox.width
23 boxHeight : boundingBox.height
24
25 }
26 }
27 Repeater
28 {
29 model : blobResultModel
30 delegate: CenterLabel
31 {
32 imageView : view
33 imageX : blobCenter.x
34 imageY : blobCenter.y
35 size : blobSize
36
37 }
38 }
39
40 }
41}