Home > Mobile >  Get vertices in a image using Python / Computer Vision , etc ? ( Images Attached )
Get vertices in a image using Python / Computer Vision , etc ? ( Images Attached )

Time:02-27

Hello all I need to find the vertices (x & y coordinates) of the given shape in image, after doing segmentation and edge extraction following is the image obtained : enter image description here

And following are the vertices whose coordinates I need to find : enter image description here

CodePudding user response:

You could try to find the contours and then once you have the contours, you could from there look if one contour has a x and y position that matches the x and y position of that contour plus the width and height of the previous contour.

Example: Say that one contour has position (0,0) and width 10 and height 20. Say that another contour has position (10,20) and width x and height y Here you should be able to search the contours found and match any contours that have the x width and y height positions. This would match the first and second contour in this example and from there you know that if you have a match, that you found a vertex.

CodePudding user response:

I think you may want to use Hough Line Transform to find the lines first. Then, you can get the intersections from the lines detected. You may find the tutorial of OpenCV about Hough Line Transform enter image description here

Code:

import numpy as np
import cv2 as cv2
import math

img_path = 'hSAdf.png'

# Read the original image
img = cv2.imread(img_path) 

# Convert to graycsale
img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
dst = cv2.threshold(img_gray, 50, 255, cv2.THRESH_BINARY)[1]

cdst = cv2.cvtColor(dst, cv2.COLOR_GRAY2BGR)

lines = cv2.HoughLines(dst, 1, np.pi / 180, 180, None, 0, 0)

# Drawing the lines
if lines is not None:
    for i in range(0, len(lines)):
        rho = lines[i][0][0]
        theta = lines[i][0][1]
        a = math.cos(theta)
        b = math.sin(theta)
        x0 = a * rho
        y0 = b * rho
        pt1 = (int(x0   10000*(-b)), int(y0   10000*(a)))
        pt2 = (int(x0 - 10000*(-b)), int(y0 - 10000*(a)))
        cv2.line(cdst, pt1, pt2, (0,0,255), 3, cv2.LINE_AA)

cv2.imshow("Detected Lines (in red) - Standard Hough Line Transform", cdst)
cv2.imwrite("output.png", cdst)
cv2.waitKey(0)

Here I did not use a Canny Edge Detection because I think the image itself is very line-clear, which make an edge detection redundant.

The function HoughLines() returns the rho in pixels and theta in radians of the line, which correspond to the line equation:

enter image description here

Edit 1: A simple convertion between rho, theta and m, c:

m = tan(theta PI/2)

c = rho / sin(theta)

enter image description here Image from Socret Lee

I think that you may continue on adjusting the line detection function. You can manually adjust the threshold, even limit the line's gradient in the function. Then, you can target one line by cropping and limiting the gradient.

Or you can reject the intersections of lines having ~90 degrees difference. Then, you will get the points you need.

  • Related