将坐标边 OpenCV 元组保存为 txt

Save Coordinates Edges OpenCV tuples to txt

提问人:PacoMI 提问时间:11/14/2023 最后编辑:PacoMI 更新时间:11/14/2023 访问量:27

问:

我正在尝试保存从边缘检测中获得的数据。我想将边缘的坐标 (X,Y) 保存在 txt 文件中。

我试图创建文件,但我只获得此数据 -> [255 255 255 ...255 255 255].

边缘

import cv2
import numpy as np
# read image
img = cv2.imread("SPIF.png")

# convert to gray
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

# threshold
#thresh = cv2.threshold(gray, 180, 255, cv2.THRESH_BINARY)[1]
thresh = cv2.threshold(gray, 210, 255, cv2.THRESH_BINARY)[1]

# morphology edgeout = dilated_mask - mask
# morphology dilate
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (5,5))
dilate = cv2.morphologyEx(thresh, cv2.MORPH_DILATE, kernel)

# get absolute difference between dilate and thresh
diff = cv2.absdiff(dilate, thresh)

# invert
edges = 255 - diff

indices = np.where(diff != [0])
coordinates = zip(indices[0], indices[1])
print(coordinates)

# write result to disk
cv2.imwrite("cartoon_diff.jpg", diff)
cv2.imwrite("cartoon_edges.jpg", edges)

# display it
cv2.imshow("diff", diff)
cv2.imshow("edges", edges)
cv2.waitKey(0)
opencv 元组 contour edges canny-operator

评论


答: 暂无答案