visionpro获取Blob最小外接矩形,然后创建与输入图像等大的图像,中间画矩形并填充颜色。最后做图像差
时间: 2024-03-08 16:47:37 浏览: 212
图像处理库文档——VisionPro中文教程
5星 · 资源好评率100%
以下是使用VisionPro获取Blob最小外接矩形,然后创建与输入图像等大的图像,间画矩形并填充颜色,最后做图像差的代码:
```
Sub ProcessImage()
' Load image
Dim img As CogImage8Grey
Set img = New CogImage8Grey
img.Load("C:\Images\input.jpg")
' Threshold image
Dim thresholdTool As CogThresholdTool
Set thresholdTool = New CogThresholdTool
thresholdTool.InputImage = img
thresholdTool.Run
Set img = thresholdTool.OutputImage
' Find blobs
Dim blobTool As CogBlobTool
Set blobTool = New CogBlobTool
blobTool.InputImage = img
blobTool.Run
Dim blobs As CogBlobResults
Set blobs = blobTool.Results
' Get minimum bounding rectangles
Dim mbrTool As CogMBRTool
Set mbrTool = New CogMBRTool
mbrTool.InputImage = img
mbrTool.Results = blobs
mbrTool.Run
' Create output image
Dim outputImg As CogImage8Grey
Set outputImg = New CogImage8Grey
outputImg.Create img.Width, img.Height
' Draw rectangles and fill with color
Dim rect As CogRectangle
For Each rect In mbrTool.Results
outputImg.FillRect rect.CenterX - rect.Width / 2, rect.CenterY - rect.Height / 2, rect.Width, rect.Height, 255
Next rect
' Compute image difference
Dim diffTool As CogImage8InvertTool
Set diffTool = New CogImage8InvertTool
diffTool.InputImage = img
diffTool.Run
Set img = diffTool.OutputImage
img.Combine outputImg, cogImageOperationSubtract
' Display result
Dim disp As CogDisplay
Set disp = New CogDisplay
disp.Image = img
disp.Fit(True)
End Sub
```
这个代码首先加载图像,然后对其进行阈值处理,找到Blob,获取最小外接矩形,并创建一个与输入图像大小相同的输出图像。然后,它循环遍历每个最小外接矩形,绘制矩形和填充颜色。最后,它使用CogImage8InvertTool计算输入图像与输出图像的差异,并将其显示在CogDisplay对象中。你可以根据自己的需求修改代码来实现不同的功能。
阅读全文