Closed contour algorithm detection

Hello! A circle of radius 5, using this circle as a brush (red), Outlines a track, when this track appears closed, these closed areas become red areas, that is, changed to brush areas. How to implement this algorithm?

AARZ85~%%452NIOOLK7EB