VNContourObservations are outside Region of Interest

I have set the regional interest on my VNDetectContoursRequest but the observations I am getting back all seem to be outside of the region of interest. So my question is if the region of interest is set should I only get back observations in the region or should I still get back all of the contour observations for the image?

Accepted Reply

The contours are reported back in relation to the ROI. So if you draw them, you have to draw them within the ROI not the overall image. It looks to me that your drawing function might not do that.

Replies

In a playground now, I have the following code
Code Block let context = CIContext()
let orginalImage = UIImage.init(named: imageArray[0])
let inputImage = CIImage.init(cgImage: (orginalImage?.cgImage)!)
let contourRequest = VNDetectContoursRequest.init()
contourRequest.detectDarkOnLight = true
contourRequest.contrastAdjustment = 1.0
let areaOfIntrest = CGRect(x: 0, y: 0, width: 50, height: 50)
let roi = VNNormalizedRectForImageRect(areaOfIntrest, 3264, 2448)
contourRequest.regionOfInterest = roi
let requestHandler = VNImageRequestHandler(cgImage: (orginalImage?.cgImage)!, options: [:])
do{
    try requestHandler.perform([contourRequest])
}catch{
    print("error in perform")
}
let observations = contourRequest.results?.first as! VNContoursObservation
print(observations.contourCount)
let imageContours = drawContours(contoursObservation: observations, sourceImage: (orginalImage?.cgImage)!)
print(imageContours.size)

But I still get contours that our outside of the Region of Interest. Which I would think would be a rectangle that his 50 x 50 and starts in the lower left corner. Am I misunderstanding how the region of interest works.


The contours are reported back in relation to the ROI. So if you draw them, you have to draw them within the ROI not the overall image. It looks to me that your drawing function might not do that.