CVBpy 14.1
foundation/QmlBlobSearch
1import os, sys
2
3import cvb
4import cvb.ui
6
7
8if sys.version_info >= (3, 11):
9 from PySide6.QtQml import qmlRegisterType
10 from PySide6.QtWidgets import QApplication
11 from PySide6.QtQuick import QQuickView
12 from PySide6.QtGui import QIcon
13else:
14 from PySide2.QtQml import qmlRegisterType
15 from PySide2.QtWidgets import QApplication
16 from PySide2.QtQuick import QQuickView
17 from PySide2.QtGui import QIcon
18
19
20class BlobResultModel(QAbstractListModel):
21
22 Center = Qt.UserRole
23 BoundingBox = Qt.UserRole + 1
24 Size = Qt.UserRole + 2
25
26 def __init__(self, blob_results, parent=None):
27 super(BlobResultModel, self).__init__(parent)
28 self._blob_results = blob_results
29
30 def roleNames(self):
31 roles = dict()
32 roles[BlobResultModel.Center] = b"blobCenter"
33 roles[BlobResultModel.BoundingBox] = b"boundingBox"
34 roles[BlobResultModel.Size] = b"blobSize"
35 return roles
36
37 def rowCount(self, parent = QModelIndex()):
38 return len(self._blob_results)
39
40 def data(self, index, role = Qt.DisplayRole):
41 if not index.isValid():
42 return None
43 item = self._blob_results[index.row()]
44 if role == BlobResultModel.Center:
45 return cvb.ui.cvb_to_qt_point(item.center)
46 elif role == BlobResultModel.Size:
47 return item.size
48 elif role == BlobResultModel.BoundingBox:
49 return cvb.ui.cvb_to_qt_rect(item.bounding_box)
50 else:
51 return None
52
53
54if __name__ == "__main__":
55
56 app = QApplication([])
57 app.setOrganizationName('STEMMER IMAGING')
58 app.setOrganizationDomain('https://www.stemmer-imaging.com/')
59 app.setApplicationName('BlobSearch Python tutorial')
60
61 # tell Windows the correct AppUserModelID for this process (shows icon in the taskbar)
62 if sys.platform == 'win32':
63 import ctypes
64 myappid = u'stemmerimaging.commonvisionblox.pyblobsearch.0'
65 ctypes.windll.shell32.SetCurrentProcessExplicitAppUserModelID(myappid)
66
67 app.setWindowIcon(QIcon('Tutorial-Python_32x32.png'))
68
69 with cvb.Image(os.path.join(cvb.install_path(),
70 "tutorial", "foundation", "images", "blob", "Microswitch.bmp")) as image:
72 filter[cvb.foundation.BlobRangeFilter.Size] = cvb.NumberRange(100, 100000)
74 plane = image.planes[0],
75 binarization_threshold = cvb.NumberRange(0, 62),
76 filter = filter)
77
78 blob_result_model = BlobResultModel(blob_results)
79
80 image_controller = cvb.ui.ImageController()
83
84 view = QQuickView()
85 view.setResizeMode(QQuickView.SizeRootObjectToView)
86 context = view.rootContext()
87 context.setContextProperty("mainImage", image_controller)
88 context.setContextProperty("blobResultModel", blob_result_model)
89 view.setSource(QUrl.fromLocalFile("main.qml"))
90
91
92 image_controller.refresh(image)
93
94 view.resize(640, 480)
95 view.show()
96
97 app.exec_()
98
The Common Vision Blox image.
Definition: __init__.py:2038
Container for number range definitions.
Definition: __init__.py:3817
Class to build a filter for the blob search.
Definition: __init__.py:202
Controller object for the QML image view item.
Definition: __init__.py:14
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:124
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:196
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:1997
Common Vision Blox UI module for Python.
Definition: __init__.py:1
PySide2.QtCore.QRectF cvb_to_qt_rect(cvb.RectLT rect)
Convenience converter for rectangles.
Definition: __init__.py:394
PySide2.QtCore.QPointF cvb_to_qt_point(cvb.Point2D point)
Convenience converter for points.
Definition: __init__.py:381
str install_path()
Directory Common Vision Blox has been installed to.
Definition: __init__.py:8257
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}