Ask Your Question
1

To find the coordinates of corners detected by Harris corner detection

asked 2018-03-12 10:58:13 -0600

csthakur gravatar image

updated 2018-03-12 14:24:46 -0600

berak gravatar image

I have applied harris corner detection on the following image Input Image

and obtained the result as Image after Harris corner detection

and now i want to find the coordinates of these corner pixels marked in red how do i do so?

Code:

import numpy as np

import cv2

img=cv2.imread('trial1.jpg')

h,w,l=img.shape

res=cv2.resize(img,(w/4,h/4),interpolation=cv2.INTER_LINEAR)

gray = cv2.cvtColor(res,cv2.COLOR_BGR2GRAY)

gray = np.float32(gray)

dst = cv2.cornerHarris(gray,2,3,0.04)

cv2.imwrite('dsttest.jpg',dst)


dst = cv2.dilate(dst,None)


res[dst>0.01*dst.max()]=[0,0,255]

points=np.unravel_index(dst.argmax(),dst.shape)

print list(points)

cv2.imwrite('dst.jpg',res)

cv2.waitKey(0)

cv2.destroyAllWindows()
edit retag flag offensive close merge delete

Comments

I want to be able to find out the coordinates of the corner points, the points in red aren't going to give me a single pixel value since each of them comprise of several pixels.I need this information so that i can automatically separate the image into it's constituent squares by using numpy array slicing

csthakur gravatar imagecsthakur ( 2018-03-12 14:32:34 -0600 )edit

first of all, don't dilate the dst image. from there on it's basically non-maximum-suppression

(and probably you need a higher threshold value)

or use goodFeaturesToTrack() instead ?

berak gravatar imageberak ( 2018-03-12 15:20:36 -0600 )edit

Hi @csthakur Were you able to solve this issue. I am also working on similar problem where i am trying to find the corners of a grid and then slice it. I was trying to find the lines using HoughTransformP, seperate horizontal and vertical line, and then detect the point of intersections. Do let me know if you were able to solve this problem.

sai teja gravatar imagesai teja ( 2018-12-11 09:57:19 -0600 )edit

It will of great help for me & others like me if you add your solution in the answers below and accept it as a right answer.

sai teja gravatar imagesai teja ( 2018-12-11 09:59:54 -0600 )edit

3 answers

Sort by ยป oldest newest most voted
0

answered 2018-12-14 14:09:16 -0600

supra56 gravatar image

Try this:

import cv2
import numpy as np

img = cv2.imread('matrix_box.jpg')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
gray = np.float32(gray)
dst = cv2.cornerHarris(gray,5,3,0.04)
ret, dst = cv2.threshold(dst,0.1*dst.max(),255,0)
dst = np.uint8(dst)
ret, labels, stats, centroids = cv2.connectedComponentsWithStats(dst)
criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 100, 0.001)
corners = cv2.cornerSubPix(gray,np.float32(centroids),(5,5),(-1,-1),criteria)
for i in range(1, len(corners)):
    print(corners[i])
img[dst>0.1*dst.max()]=[0,0,255]
cv2.imshow('image', img)
cv2.waitKey(0)
cv2.destroyAllWindows
edit flag offensive delete link more

Comments

Sorry. I can't post output. I got message of spamming.

supra56 gravatar imagesupra56 ( 2018-12-14 14:12:47 -0600 )edit
0

answered 2018-12-14 02:51:31 -0600

sai teja gravatar image

https://medium.com/coinmonks/a-box-de...

The above link has a solution i have tried it and its working.

edit flag offensive delete link more

Comments

link-only answers are not really helpful here.

berak gravatar imageberak ( 2018-12-14 03:06:32 -0600 )edit
0

answered 2018-03-13 00:39:03 -0600

saideepthik gravatar image

updated 2018-12-14 03:05:49 -0600

berak gravatar image

.

import cv2
import numpy as np

filename = 'chessboard2.jpg'
img = cv2.imread(filename)
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)

# find Harris corners
gray = np.float32(gray)
dst = cv2.cornerHarris(gray,2,3,0.04)
dst = cv2.dilate(dst,None)
ret, dst = cv2.threshold(dst,0.01*dst.max(),255,0)
dst = np.uint8(dst)

# find centroids
ret, labels, stats, centroids = cv2.connectedComponentsWithStats(dst)

# define the criteria to stop and refine the corners
criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 100, 0.001)
corners = cv2.cornerSubPix(gray,np.float32(centroids),(5,5),(-1,-1),criteria)
print corners    #here u can get corners check for more information follow the link...........http://opencv-python-tutroals.readthedocs.io/en/latest/py_tutorials/py_feature2d/py_features_harris/py_features_harris.html
# Now draw them
res = np.hstack((centroids,corners))
res = np.int0(res)
img[res[:,1],res[:,0]]=[0,0,255]
img[res[:,3],res[:,2]] = [0,255,0]

cv2.imwrite('subpixel5.png',img)
edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2018-03-12 10:58:13 -0600

Seen: 9,527 times

Last updated: Dec 14 '18