Home > Net >  Find edge in a liine OpenCV
Find edge in a liine OpenCV

Time:01-20

I want to find an edge(of an object) in a line. I know sobel and canny etc detection methods detect the edges, but I need to find edge points of an object as a (x,y) pixel coordinate only in a line.

NI Vision Builder have similar tool as Find Edge I attached example.

Is this possible in OpenCV or other image processing libraries?

Find Edge

enter image description here

import cv2
import numpy as np

# read image as grayscale
img = cv2.imread('03n3n.jpg', cv2.IMREAD_GRAYSCALE)
h, w = img.shape[:2]

# threshold
thresh = cv2.threshold(img, 0, 255, cv2.THRESH_BINARY cv2.THRESH_OTSU)[1]

# specify row
row = 620

# find min and max of white region at given row
data = np.where(thresh[row:row 1, 0:h] == 255)
x1, x2 = np.amin(data[1]), np.amax(data[1])
pt1 = (x1,row)
pt2 = (x2,row)
print("min:", x1)
print("max:", x2)

# draw line and 2 points
result = img.copy()
result = cv2.cvtColor(result, cv2.COLOR_GRAY2BGR)
cv2.line(result, (0, row), (w, row), (0,255,0), 1)
cv2.circle(result, pt1, 3, (0,0,255), 3)
cv2.circle(result, pt2, 3, (0,0,255), 3)

# save result
cv2.imwrite('03n3n_line_pts.jpg', result)

# show results
cv2.imshow('result', result)
cv2.waitKey(0)

Result:

enter image description here

min: 1240
max: 1643
  • Related