Common Vision Blox 15.0
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Friends Modules Pages
Foundation/Cvb++/QmlBlobSearch

This example program is located in your CVB installation under %CVB%Tutorial/Foundation/Cvb++/QmlBlobSearch.

blob_result_model.cpp:

#include "blob_result_model.hpp"
#include <QRect>
BlobResultModel::BlobResultModel(const std::vector<Cvb::Foundation::BlobResult>& blobResults)
: QAbstractListModel()
, blobResults_(blobResults)
{
}
QHash<int, QByteArray> BlobResultModel::roleNames() const
{
QHash<int, QByteArray> names;
names[Center] = "blobCenter";
names[BoundingBox] = "boundingBox";
names[Size] = "blobSize";
return names;
}
int BlobResultModel::rowCount(const QModelIndex &) const
{
return static_cast<int>(blobResults_.size());
}
QVariant BlobResultModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid())
return QVariant();
const auto & result = blobResults_[index.row()];
switch (role)
{
default:
return QVariant();
case Center:
return QVariant(QPointF(result.X(), result.Y()));
case BoundingBox:
return QVariant(QRect(result.BoundingBox().Left(),
result.BoundingBox().Top(),
result.BoundingBox().Width(),
result.BoundingBox().Height()));
case Size:
return QVariant(result.Size());
}
}

blob_result_model.hpp:

#pragma once
#include <QAbstractListModel>
#include <cvb/foundation/blob.hpp>
class BlobResultModel
: public QAbstractListModel
{
public:
explicit BlobResultModel(const std::vector<Cvb::Foundation::BlobResult>& blobResults);
private:
enum BlobResultRoles
{
Center = Qt::UserRole,
BoundingBox = Qt::UserRole + 1,
Size = Qt::UserRole + 2
};
QHash<int, QByteArray> roleNames() const override;
int rowCount(const QModelIndex &) const override;
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
std::vector<Cvb::Foundation::BlobResult> blobResults_;
};

main.cpp:

// The QmlBlobSearch example shows the **blob search** of CVB Foundation package.
// ---------------------------------------------------------------------------
// ---------------------------------------------------------------------------
#include <iostream>
#include <QApplication>
#include <QQuickView>
#include <QQmlContext>
#include <QIcon>
#include <cvb/ui/image_view_item.hpp>
#include <cvb/foundation/blob.hpp>
#include <cvb/image.hpp>
#include "blob_result_model.hpp"
int main(int argc, char* argv[])
{
try
{
QApplication app(argc, argv);
Cvb::String path(Cvb::InstallPath() + CVB_LIT("tutorial/Foundation/Images/Blob/Microswitch.bmp"));
// expand environment variables in path
path = Cvb::ExpandPath(path);
auto image = Cvb::Image::Load(path);
filter[Cvb::Foundation::BlobRangeFilter::Size] = Cvb::ValueRange<int>(100, 100000);
auto blobResults = Cvb::Foundation::BinarizeAndSearchBlobs(image->Plane(0), Cvb::ValueRange<int>(0, 62), filter);
BlobResultModel blobResultModel(blobResults);
// register QML components for an image display
Cvb::UI::ImageViewItem::Register();
Cvb::UI::ImageLabelItem::Register();
Cvb::UI::ImageController imageController;
QQuickView view;
view.setResizeMode(QQuickView::SizeRootObjectToView);
view.setIcon(QIcon(":/qttutorial.png"));
auto context = view.rootContext();
// create a controller object to communicate with QML
context->setContextProperty("mainImage", &imageController);
context->setContextProperty("blobResultModel", &blobResultModel);
// load main QML file
view.setSource(QUrl::fromLocalFile("../main.qml"));
// refresh the image display through the controller object
imageController.Refresh(std::move(image));
view.resize(640, 480);
view.show();
return app.exec();
}
catch (const std::exception& error)
{
std::cout << error.what() << std::endl;
}
}
static std::unique_ptr< Image > Load(const String &fileName)
std::vector< BlobResult > BinarizeAndSearchBlobs(const ImagePlane &plane, ValueRange< int > binarizationThreshold)
String ExpandPath(const String &path)
std::string String
String InstallPath()