Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

First of all: don't use the old python interface, the cv2-interace is much cooler since you can use the mighty numpy-arrays --> i.e. use cv2 instead of cv.

You don't draw the lines in a correct way, thus they don't show up. For the Harris-corner-detector you just don't use the correct parameters, furthermore as suggested in the documentation a non-maximum suppression step should be used, to remove too close corners.

My solution, still not perfect though:

import cv2
import numpy as np

img = cv2.imread('draw.jpg', cv2.CV_LOAD_IMAGE_GRAYSCALE)
edges = cv2.Canny(img, 50, 200)

# find lines
lines = cv2.HoughLinesP(np.copy(edges), 1, np.pi/180.0, 10, 0, 10)
# draw found lines        
img_dst = cv2.cvtColor(img, cv2.cv.CV_GRAY2BGR)
for line in lines[0]:
    cv2.line(img_dst, (line[0], line[1]), (line[2], line[3]), (0,255,0), 2)

# find corners
dst = cv2.cornerHarris(img, 15, 5, 0.04)
# non-maximum suppression via dilation
kernel = np.ones((10,10))
max_dst = cv2.dilate(dst, kernel)   
dst = dst * (dst == max_dst)
# sort points by strength, find their positions and take 5 highest ones
sortIdx = np.argsort(dst.flatten())[::-1][:5]
# draw them
w = img.shape[1]
for idx in sortIdx:
   cv2.circle(img_dst, (idx % w, idx / w) , 3, (0,0,255), -1)

Result:

image description


For the lines I just took your Hough-lines values, guess you could find better ones. Furthermore, Canny() produces two edges at each side of the lines which also results in two found lines, maybe other Canny-Parameters will work here better, too. For cornerHarris, I just draw the 5 most "corner-like" pixels according to the strength returned. However note that they are not sub-pixel accurate (maybe they could be refined w. cv2.cornerSubPix()).