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