1.
您希望创建类似的:
基本步骤包括:
-
读取并转换为灰色,
-
二值图像阈值
-
做
findContours
关于二进制图像
-
然后
drawContours
,你会明白的。
2.
如果你想得到这样的想法:
基本步骤包括:
-
读取并转换为灰色
-
模糊并执行
Canny
-
查找方式
connectedComponents
-
然后将标签设置为不同的颜色
-
你会变成那样的。
生成最后图像的代码:
#!/usr/bin/python3
# 2018.01.23 11:25:35 CST
# 2018.01.23 11:45:22 CST
import numpy as np
import cv2
import myutils
colors = [(0, 0, 255), (0, 43, 255), (0, 85, 255), (0, 128, 255), (0, 170, 255), (0, 213, 255), (0, 255, 255), (0, 255, 212), (0, 255, 170), (0, 255, 127), (0, 255, 85), (0, 255, 42), (0, 255, 0), (43, 255, 0), (85, 255, 0), (128, 255, 0), (170, 255, 0), (213, 255, 0), (255, 255, 0), (255, 212, 0), (255, 170, 0), (255, 127, 0), (255, 85, 0), (255, 42, 0), (255, 0, 0)]
img = cv2.imread("leaf.jpg")
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
gray = cv2.medianBlur(img, 3)
edged = cv2.Canny(gray, 50, 200)
## Find connected edges
ret, labels = cv2.connectedComponents(edged)
## Draw(set to different colors)
canvas = np.zeros_like(img, np.uint8)
for i in range(1,ret):
pts = labels == i
canvas[pts] = colors[i%len(colors)]
## Display
cv2.imshow("res", canvas);cv2.waitKey();cv2.destroyAllWindows()
cv2.imwrite("res.png", canvas)
但是
Notice
我
设置
具有不同颜色的边
the points of different colors maybe not the real path
.