CVBpy 15.0
All Classes Namespaces Functions Variables Enumerations Enumerator Properties Modules Pages
foundation/QmlBlobSearch
1# @brief This example shows the blob search of CVB Foundation package.
2
3import os, sys
4
5import cvb
6import cvb.ui
8
9
10if sys.version_info >= (3, 11):
11 from PySide6.QtQml import qmlRegisterType
12 from PySide6.QtWidgets import QApplication
13 from PySide6.QtQuick import QQuickView
14 from PySide6.QtGui import QIcon
15else:
16 from PySide2.QtQml import qmlRegisterType
17 from PySide2.QtWidgets import QApplication
18 from PySide2.QtQuick import QQuickView
19 from PySide2.QtGui import QIcon
20
21
22class BlobResultModel(QAbstractListModel):
23
24 Center = Qt.UserRole
25 BoundingBox = Qt.UserRole + 1
26 Size = Qt.UserRole + 2
27
28 def __init__(self, blob_results, parent=None):
29 super(BlobResultModel, self).__init__(parent)
30 self._blob_results = blob_results
31
32 def roleNames(self):
33 roles = dict()
34 roles[BlobResultModel.Center] = b"blobCenter"
35 roles[BlobResultModel.BoundingBox] = b"boundingBox"
36 roles[BlobResultModel.Size] = b"blobSize"
37 return roles
38
39 def rowCount(self, parent = QModelIndex()):
40 return len(self._blob_results)
41
42 def data(self, index, role = Qt.DisplayRole):
43 if not index.isValid():
44 return None
45 item = self._blob_results[index.row()]
46 if role == BlobResultModel.Center:
47 return cvb.ui.cvb_to_qt_point(item.center)
48 elif role == BlobResultModel.Size:
49 return item.size
50 elif role == BlobResultModel.BoundingBox:
51 return cvb.ui.cvb_to_qt_rect(item.bounding_box)
52 else:
53 return None
54
55
56if __name__ == "__main__":
57
58 app = QApplication([])
59 app.setOrganizationName('STEMMER IMAGING')
60 app.setOrganizationDomain('https://www.stemmer-imaging.com/')
61 app.setApplicationName('BlobSearch Python tutorial')
62
63 # tell Windows the correct AppUserModelID for this process (shows icon in the taskbar)
64 if sys.platform == 'win32':
65 import ctypes
66 myappid = u'stemmerimaging.commonvisionblox.pyblobsearch.0'
67 ctypes.windll.shell32.SetCurrentProcessExplicitAppUserModelID(myappid)
68
69 app.setWindowIcon(QIcon('Tutorial-Python_32x32.png'))
70
71 with cvb.Image(os.path.join(cvb.install_path(),
72 "tutorial", "foundation", "images", "blob", "Microswitch.bmp")) as image:
74 filter[cvb.foundation.BlobRangeFilter.Size] = cvb.NumberRange(100, 100000)
76 plane = image.planes[0],
77 binarization_threshold = cvb.NumberRange(0, 62),
78 filter = filter)
79
80 blob_result_model = BlobResultModel(blob_results)
81
82 image_controller = cvb.ui.ImageController()
85
86 view = QQuickView()
87 view.setResizeMode(QQuickView.SizeRootObjectToView)
88 context = view.rootContext()
89 context.setContextProperty("mainImage", image_controller)
90 context.setContextProperty("blobResultModel", blob_result_model)
91 view.setSource(QUrl.fromLocalFile("main.qml"))
92
93
94 image_controller.refresh(image)
95
96 view.resize(640, 480)
97 view.show()
98
99 app.exec_()
100
The Common Vision Blox image.
Definition: __init__.py:2097
Container for number range definitions.
Definition: __init__.py:3878
Class to build a filter for the blob search.
Definition: __init__.py:202
Controller object for the QML image view item.
Definition: __init__.py:16
None register(cls, str uri="CvbQuick", int version_major=1, int version_minor=0, str qml_name="ImageLabel")
Basically just calls qmlRegisterType(...).
Definition: __init__.py:126
None register(cls, str uri="CvbQuick", int version_major=1, int version_minor=0, str qml_name="ImageView")
Convenience method to register this type or a derived type in QML.
Definition: __init__.py:198
Common Vision Blox Foundation module for Python.
Definition: __init__.py:1
List[cvb.foundation.BlobResult] binarize_and_search_blobs(cvb.ImagePlane plane, cvb.NumberRange binarization_threshold, Optional[cvb.Rect] aoi, Optional[int] filter)
Searches for all blobs in the given image plane.
Definition: __init__.py:2054
Common Vision Blox UI module for Python.
Definition: __init__.py:1
Union[PySide2.QtCore.QPointF, PySide6.QtCore.QPointF] cvb_to_qt_point(cvb.Point2D point)
Convenience converter for points.
Definition: __init__.py:383
Union[PySide2.QtCore.QRectF, PySide6.QtCore.QRectF] cvb_to_qt_rect(cvb.RectLT rect)
Convenience converter for rectangles.
Definition: __init__.py:396
str install_path()
Directory Common Vision Blox has been installed to.
Definition: __init__.py:8318
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}
1import QtQuick 2.3
2import CvbQuick 1.0 as CvbQuick
3
4CvbQuick.ImageLabel
5{
6 id: label
7 property var size : 0.0
8 rotation : -45
9 opacity : 0.65
10
11 Rectangle
12 {
13 id: contend
14 width : childrenRect.width + 8
15 height: childrenRect.height + 6
16 color : "red"
17 border.color : "black"
18 border.width : 1
19
20 Text
21 {
22 x: 4
23 y: 3
24 text: "Size: " + String(label.size)
25 font.pointSize : 10
26 }
27 }
28
29}
1import QtQuick 2.3
2import CvbQuick 1.0 as CvbQuick
3
4CvbQuick.ImageLabel
5{
6 id : label
7 property var boxWidth : 10
8 property var boxHeight : 10
9
10 labelScale : 1
11
12
13 Rectangle
14 {
15 id: contend
16 width : label.boxWidth
17 height: label.boxHeight
18 color : "transparent"
19 border.color : "green"
20 border.width : 1
21
22 }
23
24
25}