Platforms to show: All Mac Windows Linux Cross-Platform
/MacFrameworks/DetectDocumentSegmentationRequest
Required plugins for this example: MBS MacCF Plugin, MBS MacCG Plugin, MBS MacCI Plugin, MBS Main Plugin, MBS MacBase Plugin, MBS MacFrameworks Plugin
Last modified Tue, 21th Oct 2024.
You find this example project in your MBS Xojo Plugin download as a Xojo project file within the examples folder: /MacFrameworks/DetectDocumentSegmentationRequest
Download this example: DetectDocumentSegmentationRequest.zip
Project "DetectDocumentSegmentationRequest.xojo_binary_project"
Class App Inherits DesktopApplication
Const kEditClear = "&Delete"
Const kFileQuit = "&Quit"
Const kFileQuitShortcut = ""
EventHandler Sub DocumentCreated()
// we pick some image
Var file As FolderItem = GetOpenFolderItem(FileTypeGroup1.All)
If file <> Nil Then
Self.OpenDocument file
End If
End EventHandler
EventHandler Sub DocumentOpened(item As FolderItem)
// load the image
Var image As New CIImageMBS(Item)
// if needed, we rotate it
Var orientation As Integer = image.properties.Lookup("Orientation", 0)
If orientation <> 0 Then
image = image.imageByApplyingOrientation(orientation)
End If
// we ask the image request handler above to do the document detection
Var request As New VNDetectDocumentSegmentationRequestMBS
Var requests() As VNRequestMBS
requests.add request
// run it synchronously
Var error As NSErrorMBS
Var imageRequestHandler As VNImageRequestHandlerMBS = VNImageRequestHandlerMBS.RequestWithCIImage(image)
Var Success As Boolean = imageRequestHandler.performRequests(requests, error)
// check results to get observation
Var results() As VNObservationMBS = request.results
Var result As Variant = results(0)
Var r As VNRectangleObservationMBS = result
// now we need to scale the box to the image size as box is normalized 0 to 1
Var w As Double = image.Width
Var h As Double = image.Height
Var boundingBox As CGRectMBS = r.boundingBox.multiply(w,h)
Var cf As New CIFilterCropMBS
cf.inputImage = image
cf.inputRectangle = CIVectorMBS.vectorWithCGRect(boundingBox)
// now we strech the area found to the output size
Var f As New CIFilterPerspectiveCorrectionMBS
f.inputImage = cf.outputImage
f.inputBottomLeft = CIVectorMBS.vectorWithCGPoint(r.bottomLeft.multiply(w,h))
f.inputBottomRight = CIVectorMBS.vectorWithCGPoint(r.bottomRight.multiply(w,h))
f.inputTopLeft = CIVectorMBS.vectorWithCGPoint(r.topLeft.multiply(w,h))
f.inputTopRight = CIVectorMBS.vectorWithCGPoint(r.topRight.multiply(w,h))
// check output and show in window
Var outputImage As CIImageMBS = f.outputImage
Var pic As Picture = outputImage.RenderPictureWithAlpha
Var mw As New MainWindow
mw.pic = pic
End EventHandler
End Class
Class MainWindow Inherits DesktopWindow
EventHandler Sub Paint(g As Graphics, areas() As Rect)
// Calculate scale factor
If pic <> Nil Then
Var faktor As Double = Min( Height / Pic.Height, Width / Pic.Width)
// Calculate new size
Var w As Integer = Pic.Width * faktor
Var h As Integer = Pic.Height * faktor
// draw picture in the new size
g.DrawPicture Pic, 0, 0, w, h, 0, 0, Pic.Width, Pic.Height
End If
End EventHandler
Property pic As Picture
End Class
MenuBar MainMenuBar
MenuItem FileMenu = "&File"
MenuItem FileQuit = "#App.kFileQuit"
MenuItem EditMenu = "&Edit"
MenuItem EditUndo = "&Undo"
MenuItem EditSeparator1 = "-"
MenuItem EditCut = "Cu&t"
MenuItem EditCopy = "&Copy"
MenuItem EditPaste = "&Paste"
MenuItem EditClear = "#App.kEditClear"
MenuItem EditSeparator2 = "-"
MenuItem EditSelectAll = "Select &All"
MenuItem HelpMenu = "&Help"
End MenuBar
Sign
End Sign
FileTypeGroup1
Filetype Jpeg
Filetype Png
End FileTypeGroup1
End Project
Download this example: DetectDocumentSegmentationRequest.zip
The items on this page are in the following plugins: MBS MacFrameworks Plugin.