获取图像的轮廓和点

Get contours and points of an image

我的代码是这样的:

import numpy as np
import cv2
im = cv2.imread('snorlax.jpg')
imgray = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)
ret, thresh = cv2.threshold(imgray, 127, 255, 0)
im2, contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
print(contours)
cv2.drawContours(im, contours, -1, (0, 255, 0), 3)
cv2.imshow("imagen", im)
input()

打印显示列表列表,必须为每个列表编号我不知道那是否是等高线的点 (x,y),cv2.show 只显示灰色屏幕而不是显示图像的轮廓。

import numpy as np
import cv2

img = cv2.imread("snorlax.jpg", cv2.IMREAD_GRAYSCALE)

canny = cv2.Canny(img, 100, 150)

cv2.imshow("Image", img)
cv2.imshow("Canny", canny)

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

coordinates_list = ""
for coordinate in coordinates:
    x = "'('{}, {}')', ".format(coordinate[1] / 100, -coordinate[0] / 100)
    coordinates_list += x

coordinates_list = "'('{}')'".format(coordinates_list)
coordinates_list = coordinates_list.replace("'('", "{")
coordinates_list = coordinates_list.replace("')'", "}")

print(coordinates_list)

cv2.waitKey(0)
cv2.destroyAllWindows()

我用canny来解决问题然后用numpy的"where"函数我得到所有的白点并将其压缩到一个变量中,最后一段代码是获取特定的点格式以在其他语言中使用它。