Ask Your Question
0

Detect Closed loop through from the image

asked 2019-07-01 01:32:06 -0600

varul gravatar image

updated 2019-07-01 21:41:23 -0600

supra56 gravatar image

Hi, I am trying to detect the closed loop within the attached images and mentioned below code I am able to draw contours of closed loop and I need to detect the closed-loop contour Image 1 Without any Pattern image description

Image 2 With the pattern

image description

Image 3 Closed Contour

image description

My major Question is how can I detect the coutour drawn in the image is closed loop or open loop

Note: The pattern can anything if its not enclosed then it will give Flase in output else it will give true

Code:

    import cv2, imutils
    import numpy as np

    image = cv2.imread("mask5.jpg")
    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

    frame = cv2.imread("mask4.jpg")
    static_back = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

    while(True):

        diff_frame = cv2.absdiff(static_back, gray)

        thresh_frame = cv2.threshold(diff_frame, 10, 255, cv2.THRESH_BINARY)[1])

        thresh_frame = cv2.dilate(thresh_frame, None, iterations = 1)

        (cnts, h) = cv2.findContours(thresh_frame.copy(), cv2.RETR_CCOMP, 
        cv2.CHAIN_APPROX_NONE)
        for contour in cnts:
            if cv2.contourArea(contour) > 10000:
                cv2.drawContours(frame, contour, -1, (0, 255, 0), 5)    
        result = imutils.resize(frame, width=320)
        cv2.imshow("Frame", result)
        cv2.imwrite("Frame.jpg", result)
        key = cv2.waitKey(1)
        if key == 27:
            break
edit retag flag offensive close merge delete

Comments

1
sturkmen gravatar imagesturkmen ( 2019-07-01 02:28:33 -0600 )edit

any possible suggestion in python and will it work on enclosed loops??

varul gravatar imagevarul ( 2019-07-01 03:16:05 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
0

answered 2019-07-02 05:17:30 -0600

VxW gravatar image

Hi,

you can check if the contour area is bigger than the perimeter:

 for contour in contours:
        if cv2.contourArea(contour) > cv2.arcLength(contour, True):
            cv2.drawContours(gray, contour, -1, (0, 255, 0), 5)

If the contour is open, the area should be zero

Best

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2019-07-01 01:32:06 -0600

Seen: 2,938 times

Last updated: Jul 02 '19