我正在使用精明的算法来寻找边缘。
接下来,我想将区域保留在闭合曲线内。
我的代码示例是:
import cv2
import numpy as np
from matplotlib import pyplot as plt
import scipy.ndimage as nd
from skimage.morphology import watershed
from skimage.filters import sobel
img1 = cv2.imread('coins.jpg')
img = cv2.imread('coins.jpg',0)
edges= cv2.Canny(img,120,200)
markers = np.zeros_like(img)
markers[edges<50] = 0
markers[edges==255] = 1
img1[markers == 1] = [0,0,255]
img1[markers == 0] = [255,255,255]
cv2.imshow('Original', img)
cv2.imshow('Canny', img1)
#Wait for user to press a key
cv2.waitKey(0)
我想显示硬币内的原始像素值。这可能吗?
请您参考如下方法:
我建议您使用联合查找结构来获取 img1 的白色像素的连通分量。 (您可能想在维基百科上找到此算法的详细信息:https://en.wikipedia.org/wiki/Disjoint-set_data_structure)。
一旦你有了连接的组件,我最好的想法是考虑在你的图片边界上不包含任何点的连接组件(它们应该对应于你的硬币的内部)并将它们着色为 img 的颜色.
当然,您的硬币之间可能有某种三角形,它们仍然会被着色,但您可以手动移除相应的连接组件。
