Line segmentation detection line draw by hand

63 views Asked by At

This is my image.
I have an image of hand-drawn straight lines. My goal is to convert them to straight lines and connect them together in a logical way and draw them again to a new white background image. I have used HoughLineP and also switched to using cv2.createLineSegmentation, but I am currently having a few problems with this issue.And here is my result this is my result

I am trying to connect lines together and draw them at right angles, but the results are very bad. Do you have any solutions? This is my solution but it's not good This is my solution but it's not good.
Can you imagine what I wish for through the image above, right? I really look forward to your help.
Thank you sincerely.
Here is my code

lsd = cv2.createLineSegmentDetector(0)

image = cv2.imread("Image/IMG_8764.jpg", 0)

lines, width, prec, nfa = lsd.detect(image)

print(len(lines))

white_background = np.ones_like(image) * 0

for line in lines:
    x1, y1, x2, y2 = map(int, line[0])
    cv2.line(white_background, (x1, y1), (x2, y2), (255, 255, 255), 3)

My solution but...

def angle_cos(p0, p1, p2):
    d1, d2 = (p0-p1).astype('float'), (p2-p1).astype('float')
    return abs( np.dot(d1, d2) / np.sqrt( np.dot(d1, d1)*np.dot(d2, d2) ) )
def makebin(gray):
    bin = cv2.adaptiveThreshold(gray, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 5, 2)
    return cv2.bitwise_not(bin)
def find_squares(img):
    img = cv2.GaussianBlur(img, (5, 5), 0)
    squares = []
    points = []
    for gray in cv2.split(img):
        bin = makebin(gray)
        contours, hierarchy = cv2.findContours(bin, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
        corners = cv2.goodFeaturesToTrack(gray,len(contours)*4,0.2,15)
        cv2.cornerSubPix(gray,corners,(9,9),(-1,-1),(cv2.TERM_CRITERIA_MAX_ITER | cv2.TERM_CRITERIA_EPS,10, 0.1))
        for cnt in contours:
            cnt_len = cv2.arcLength(cnt, True)
            if len(cnt) >= 4 and cv2.contourArea(cnt) > 500:
                rect = cv2.boundingRect(cnt)
                if rect not in squares:
                    squares.append(rect)
    return squares, corners, contours
if __name__ == '__main__':
    for fn in glob('Test.jpg'):
        img = cv2.imread(fn)
        squares, corners, contours = find_squares(img)
        for p in corners:
            cv2.circle(img, (int(p[0][0]), int(p[0][1])), 3, (0, 0, 255), 2)
        squares = sorted(squares,key=itemgetter(1,0,2,3))
        areas = []
        moments = []
        centers = []
        for s in squares:
            areas.append(s[2]*s[3])
            cv2.rectangle( img, (s[0],s[1]),(s[0]+s[2],s[1]+s[3]),(0,255,0),5)
        for c in contours:
            moments.append(cv2.moments(np.array(c)))
        for m in moments:
            if m["m00"] != 0:
                centers.append((int(m["m10"] // m["m00"]), int(m["m01"] // m["m00"])))
        for cent in centers:
            cv2.circle(img, (cent[0],cent[1]), 3, (0,255,0),2)
        cv2.imshow('squares', ResizeWithAspectRatio(img,800,800))
        ch = 0xFF & cv2.waitKey()
        if ch == 27:
            break
    cv2.destroyAllWindows()
0

There are 0 answers