Q:

Lines connecting corners over hand drawn image in python with cv2

I am looking to detect lines connecting corners over a hand drawn image like this.  I am using Harris Corner Detection to find the corners of the image.  Next I am connecting all of the corners with lines and iterating though the points to see if they match the pixels from original image and setting a threshold for each line pixel cover to say what is acceptable to say it is a correct line connecting corners. Image of connected lines.  It works... but it is very slow.  Is there a better way to do this or different method I should use? ( Hough lines will not work because of the possibility of curved lines, I only want the lines connecting corners. 
for i in c_corners: #corners thru harris and coorected with subpix
    x1,y1 = i.ravel()
    for k in c_corners:
        x2,y2 = k.ravel()
        if x1 ! = x2 and y1 ! = y2: #ignore vertical lines 
            linePoints = line_points(x1,y1, x2,y2) # function to get line pnts
            totalLinePoints = len(linePoints)
            coverPoints = 0

########## This is where I think the slow down is happening and could be optimized

            for m in originalImage: #image is dialated to help detection
                for n in linePoints:
                    match = np.all(m == n)
                    if match == True:
                        coverPoints += 1 
            print("Line Cover = ", (coverPoints/totalLinePoints))
            if (coverPoints/totalLinePoints) > .65:
                good_lines.append([x1,y1,x2,y2])

Any help at all is appreciated, thank you! 

A:

My original approach was to create a blank image and draw each line on it, and then use cv2.bitwise_and() with the binary (dilated) image to count how many pixels were in agreement, and if they met a threshold, then draw those lines over the original image.  However setting a threshold for the number of pixels penalizes small lines.  A better indicator would be the ratio of the number of correct matches to incorrect matches (I realize now that's what you were actually doing).  Furthermore this is a little more robust towards dilation and the line thickness you choose to draw your lines. 
However the general method you're using is not very robust to issues in the drawing where, like this one, synthetic lines may be able to fit easily to lines they don't belong to, because many drawn curves may hit a line segment.  You can see this issue in the output of my code:

I simply hardcoded some corner estimates and went from there.  Note the use of itertools to help create all possible pairs of points to define line segments. 
import cv2
import numpy as np
import itertools

img = cv2.imread('drawing.png')
bin_inv = cv2.bitwise_not(img) # flip image colors
bin_inv = cv2.cvtColor(bin_inv, cv2.COLOR_BGR2GRAY) # make one channel
bin_inv = cv2.dilate(bin_inv, np.ones((5,5)))

corners = ((517, 170),
    (438, 316),
    (574, 315),
    (444, 436),
    (586, 436))

lines = itertools.combinations(corners,2) # create all possible lines
line_img = np.ones_like(img)*255 # white image to draw line markings on
for line in lines: # loop through each line
    bin_line = np.zeros_like(bin_inv) # create a matrix to draw the line in
    start, end = line # grab endpoints
    cv2.line(bin_line, start, end, color=255, thickness=5) # draw line
    conj = (bin_inv/255 + bin_line/255) # create agreement image
    n_agree = np.sum(conj==2)
    n_wrong = np.sum(conj==1)
    if n_agree/n_wrong > .05: # high agreements vs disagreements
        cv2.line(line_img, start, end, color=[0,200,0], thickness=5) # draw onto original img

# combine the identified lines with the image
marked_img = cv2.addWeighted(img, .5, line_img, .5, 1)
cv2.imwrite('marked.png', marked_img)

I tried a lot of different settings (playing with thickness, dilation, different ratios, etc) and couldn't get that spurious longer line from appearing.  It fits the original black pixels super well though, so I'm not sure how you would be able to get rid of it if you used this method.  It's got the curve from the top-right line going for it, as well as the middle line it crosses, and the curve at the bottom right which trends that direction for a bit.  Regardless, this only takes two seconds to run, so at least it's faster than your current code.