How can i find close curved line on an image in python

Viewed 153

community,

Is there any way you can find an close curved line on an image using numpy and opencv or anything else (sample images attached)

close curved line image: enter image description here or simply: enter image description here

unclose curved line image: enter image description here or simply: enter image description here

I need this function to search entire image and if there is one return me true else return false, thanks and happy coding

def check_close_curved_line_on_img(img):
    // logic here 
        if has_close_curved_line:
            return True
    return False
1 Answers

You can probably use a combination of floodFill and erode from opencv. The code below worked on the examples provided.

import cv2
import numpy as np

# load and convert to grayscale
img = cv2.imread('unclosed.png')
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

# floodFill
img = cv2.bitwise_not(img)
mask = np.zeros((np.array(img.shape)+2), np.uint8)
cv2.floodFill(img, mask, (0,0), (255))

# erode
img = cv2.bitwise_not(img)
img = cv2.erode(img, np.ones((3,3)))

# show image
cv2.imshow('show', img)
if img.sum() > 0:
    print('Closed curve detected')
else:
    print('Closed curve was not detected')
Related