Ask Your Question

Python-openCV: Extracting (x,y) coordinates of point features on an image

asked 2016-05-25 07:47:04 -0500

zarray gravatar image

updated 2016-05-26 05:53:11 -0500

Hi all, I am trying to extract the (x,y) coordinates of the the four corners of a wooden rectangular plank image and apply that to a real-time video feed.

What I have in mind is: 1) read image and apply Harris Corner Dectection(HCD) to mark out 4 red points. 2) Search for red points on the image and output an array giving the (x,y) coordinates

I have no idea how to implement step 2 at the moment, and with regard to step 1, I have no idea how the HCD knows which pixel to mark out on. Is there a way I can extract the coordinate information?

image description

image description

image description

image description

edit retag flag offensive close merge delete


we suppose! that We want extract to X-Y coordinate of an image.(Whole image)?

sapna gravatar imagesapna ( 2020-10-12 02:31:03 -0500 )edit

2 answers

Sort by ยป oldest newest most voted

answered 2016-05-26 01:01:03 -0500

berak gravatar image

i'd go another way for this.

rather use findContours(), filter down to the largest, then use cv2.minAreaRect(), and cv2.boxPoints(), to find the 4 corner points:

import cv2
import numpy as np

im = cv2.imread("plank.jpg")
gray = cv2.cvtColor(im,cv2.COLOR_BGR2GRAY);
gray = cv2.GaussianBlur(gray, (5, 5), 0)
_, bin = cv2.threshold(gray,120,255,1) # inverted threshold (light obj on dark bg)
bin = cv2.dilate(bin, None)  # fill some holes
bin = cv2.dilate(bin, None)
bin = cv2.erode(bin, None)   # dilate made our shape larger, revert that
bin = cv2.erode(bin, None)
bin, contours, hierarchy = cv2.findContours(bin, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)

rc = cv2.minAreaRect(contours[0])
box = cv2.boxPoints(rc)
for p in box:
    pt = (p[0],p[1])
    print pt,pt,5,(200,0,0),2)
cv2.imshow("plank", im)


(182.5, 736.50006)
(53.0, 607.00006)
(618.99994, 41.000122)
(748.49994, 170.50012)

image description

edit flag offensive delete link more



Hi berak !

that was very helpful! thank you so much. My concern is with the live feed video stream ouputting a 'not-so-perfect' plank' and what if the case where there actually isn't only 4 corners(could be more or less) in the image frame. I have edited the original question with extra images that I took in the environment we will be testing in.

zarray gravatar imagezarray ( 2016-05-26 05:50:39 -0500 )edit

Hi berak, Thank you so much for making the code. It is exactly what i need for a project im working on

OutOfMyElement gravatar imageOutOfMyElement ( 2017-10-16 23:02:30 -0500 )edit

@berak I have a similar issue . Can you reply please. Thanks

spratyush02 gravatar imagespratyush02 ( 2018-05-28 09:39:28 -0500 )edit

i have a question, that how it can be applicable for a moving object in a video to find the coordinates

pavan sai gravatar imagepavan sai ( 2019-03-07 08:01:03 -0500 )edit

answered 2019-10-08 12:40:06 -0500

updated 2020-07-08 07:00:19 -0500

supra56 gravatar image

The code above wasn't buiding with Python3. Just in case if this is useful, see the code below with minor corrections.

import cv2 as cv
import numpy as np

im = cv.imread("plank.jpg")
gray = cv.cvtColor(im, cv.COLOR_BGR2GRAY)
gray = cv.GaussianBlur(gray, (5, 5), 0)
_, bin = cv.threshold(gray,120,255,1) # inverted threshold (light obj on dark bg)
bin = cv.dilate(bin, None)  # fill some holes
bin = cv.dilate(bin, None)
bin = cv.erode(bin, None)   # dilate made our shape larger, revert that
bin = cv.erode(bin, None)
contours, hierarchy = cv.findContours(bin, cv.RETR_LIST, cv.CHAIN_APPROX_SIMPLE)

rc = cv.minAreaRect(contours[0])
box = cv.boxPoints(rc)
for p in box:
    pt = (p[0],p[1])
cv.imshow("plank", im)
edit flag offensive delete link more

Question Tools



Asked: 2016-05-25 07:47:04 -0500

Seen: 46,488 times

Last updated: Jul 08 '20