获取图像的轮廓和点

时间:2018-10-23 04:31:28

标签: python python-3.x opencv opencv3.0 opencv-contour

我有这样的代码:

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()

印刷品显示一个列表列表,该列表必须编号每个列表,我不知道这是轮廓和cv2的点(x,y)。仅显示灰色屏幕,而不显示轮廓图片。

1 个答案:

答案 0 :(得分:0)

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”功能获取所有白点并将其压缩到变量中,代码的最后部分是获取特定格式的点以使用它用另一种语言。