import cv2
import numpy as np
# load image
img = cv2.imread("pink_blob.png")
hh, ww = img.shape[:2]
# convert to gray
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# threshold
thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY+cv2.THRESH_OTSU)[1]
# center coords
center = (115,82)
cy = center[1]
# crop row at y center
row = thresh[cy:cy+1, 0:ww]
# get coordinates along row where it is white
# swap x and y between numpy and opencv coords
coords = np.argwhere(row==255)
num_coords = len(coords)
start = coords[0]
end = coords[num_coords-1]
start_pt = (start[1],cy)
end_pt = (end[1],cy)
print(start_pt)
print(end_pt)
# draw line from start to end coordinates on input
result = img.copy()
cv2.line(result, start_pt, end_pt, (255,255,255), 1)
# save result
cv2.imwrite('pink_blob_line.png', result)
# show result
cv2.imshow('result', result)
cv2.waitKey(0)
Start and End Coordinates:
(67, 82)
(160, 82)
下面是使用Python/OpenCV和Numpy的一种方法
输入:
输入图像上的行:
首先,找到图像中粉红色区域的轮廓。您可以首先对图像应用Otsu的阈值,然后使用cv2.findOntours()查找轮廓
然后在轮廓边界点中,找到与中心像素具有相同y坐标的点
其中,x坐标最大的点为右侧点,x坐标最小的点为左侧点
相关问题 更多 >
编程相关推荐