Rya*_*att 6 opencv vision image-segmentation dbscan scikit-learn
我正在尝试使用来自scikitlearn的DBSCAN来基于颜色分割图像。我得到的结果是。如您所见,有3个集群。我的目标是将图片中的浮标分成不同的群集。但是很明显,它们显示为同一群集。我尝试了广泛的eps值和min_samples,但是这两件事总是聚在一起。我的代码是:
img= cv2.imread("buoy1.jpg)
labimg = cv2.cvtColor(img, cv2.COLOR_BGR2LAB)
n = 0
while(n<4):
labimg = cv2.pyrDown(labimg)
n = n+1
feature_image=np.reshape(labimg, [-1, 3])
rows, cols, chs = labimg.shape
db = DBSCAN(eps=5, min_samples=50, metric = 'euclidean',algorithm ='auto')
db.fit(feature_image)
labels = db.labels_
plt.figure(2)
plt.subplot(2, 1, 1)
plt.imshow(img)
plt.axis('off')
plt.subplot(2, 1, 2)
plt.imshow(np.reshape(labels, [rows, cols]))
plt.axis('off')
plt.show()
Run Code Online (Sandbox Code Playgroud)
我假设这是欧氏距离,因为在实验室空间中,欧氏距离在不同颜色之间会有所不同。如果有人可以给我指导,我将不胜感激。
更新:以下答案有效。由于DBSCAN要求数组的尺寸不能超过2维,因此我将这些列连接到原始图像,并进行了整形以生成anx 5矩阵,其中n是x维乘以y维。这似乎为我工作。
indices = np.dstack(np.indices(img.shape[:2]))
xycolors = np.concatenate((img, indices), axis=-1)
np.reshape(xycolors, [-1,5])
Run Code Online (Sandbox Code Playgroud)
小智 5
\n\n\n您能否在答案中添加完整代码?我无法理解在哪里添加对你有用的那三行 \xe2\x80\x93 user8306074 Sep 4 at 8:58
\n
让我来回答你,这是完整版的代码:
\n\nimport numpy as np\nimport cv2\nimport matplotlib.pyplot as plt\nfrom sklearn.cluster import DBSCAN\n\nimg= cv2.imread('your image') \nlabimg = cv2.cvtColor(img, cv2.COLOR_BGR2LAB)\n\nn = 0\nwhile(n<4):\n labimg = cv2.pyrDown(labimg)\n n = n+1\n\nfeature_image=np.reshape(labimg, [-1, 3])\nrows, cols, chs = labimg.shape\n\ndb = DBSCAN(eps=5, min_samples=50, metric = 'euclidean',algorithm ='auto')\ndb.fit(feature_image)\nlabels = db.labels_\n\nindices = np.dstack(np.indices(labimg.shape[:2]))\nxycolors = np.concatenate((labimg, indices), axis=-1) \nfeature_image2 = np.reshape(xycolors, [-1,5])\ndb.fit(feature_image2)\nlabels2 = db.labels_\n\nplt.figure(2)\nplt.subplot(2, 1, 1)\nplt.imshow(img)\nplt.axis('off')\n\n# plt.subplot(2, 1, 2)\n# plt.imshow(np.reshape(labels, [rows, cols]))\n# plt.axis('off')\n\nplt.subplot(2, 1, 2)\nplt.imshow(np.reshape(labels2, [rows, cols]))\nplt.axis('off')\nplt.show()\n
Run Code Online (Sandbox Code Playgroud)\n
归档时间: |
|
查看次数: |
3037 次 |
最近记录: |