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

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

blob_search.py:

# @brief This example shows the blob search of CVB Foundation package.
import os, sys
import cvb
import cvb.ui
if sys.version_info >= (3, 11):
from PySide6.QtQml import qmlRegisterType
from PySide6.QtWidgets import QApplication
from PySide6.QtQuick import QQuickView
from PySide6.QtGui import QIcon
else:
from PySide2.QtQml import qmlRegisterType
from PySide2.QtWidgets import QApplication
from PySide2.QtQuick import QQuickView
from PySide2.QtGui import QIcon
class BlobResultModel(QAbstractListModel):
Center = Qt.UserRole
BoundingBox = Qt.UserRole + 1
Size = Qt.UserRole + 2
def __init__(self, blob_results, parent=None):
super(BlobResultModel, self).__init__(parent)
self._blob_results = blob_results
def roleNames(self):
roles = dict()
roles[BlobResultModel.Center] = b"blobCenter"
roles[BlobResultModel.BoundingBox] = b"boundingBox"
roles[BlobResultModel.Size] = b"blobSize"
return roles
def rowCount(self, parent = QModelIndex()):
return len(self._blob_results)
def data(self, index, role = Qt.DisplayRole):
if not index.isValid():
return None
item = self._blob_results[index.row()]
if role == BlobResultModel.Center:
return cvb.ui.cvb_to_qt_point(item.center)
elif role == BlobResultModel.Size:
return item.size
elif role == BlobResultModel.BoundingBox:
return cvb.ui.cvb_to_qt_rect(item.bounding_box)
else:
return None
if __name__ == "__main__":
app = QApplication([])
app.setOrganizationName('STEMMER IMAGING')
app.setOrganizationDomain('https://www.stemmer-imaging.com/')
app.setApplicationName('BlobSearch Python tutorial')
# tell Windows the correct AppUserModelID for this process (shows icon in the taskbar)
if sys.platform == 'win32':
import ctypes
myappid = u'stemmerimaging.commonvisionblox.pyblobsearch.0'
ctypes.windll.shell32.SetCurrentProcessExplicitAppUserModelID(myappid)
app.setWindowIcon(QIcon('Tutorial-Python_32x32.png'))
with cvb.Image(os.path.join(cvb.install_path(),
"tutorial", "foundation", "images", "blob", "Microswitch.bmp")) as image:
filter[cvb.foundation.BlobRangeFilter.Size] = cvb.NumberRange(100, 100000)
plane = image.planes[0],
binarization_threshold = cvb.NumberRange(0, 62),
filter = filter)
blob_result_model = BlobResultModel(blob_results)
image_controller = cvb.ui.ImageController()
view = QQuickView()
view.setResizeMode(QQuickView.SizeRootObjectToView)
context = view.rootContext()
context.setContextProperty("mainImage", image_controller)
context.setContextProperty("blobResultModel", blob_result_model)
view.setSource(QUrl.fromLocalFile("main.qml"))
image_controller.refresh(image)
view.resize(640, 480)
view.show()
app.exec_()
None register(cls, str uri="CvbQuick", int version_major=1, int version_minor=0, str qml_name="ImageLabel")
None register(cls, str uri="CvbQuick", int version_major=1, int version_minor=0, str qml_name="ImageView")
List[cvb.foundation.BlobResult] binarize_and_search_blobs(cvb.ImagePlane plane, cvb.NumberRange binarization_threshold, Optional[cvb.Rect] aoi, Optional[int] filter)
Union[PySide2.QtCore.QPointF, PySide6.QtCore.QPointF] cvb_to_qt_point(cvb.Point2D point)
Union[PySide2.QtCore.QRectF, PySide6.QtCore.QRectF] cvb_to_qt_rect(cvb.RectLT rect)
str install_path()