File "/media/linux/643A-4CF3/code/SelfDeblur-master/K-meanszengqiang.py", line 35, in <module> contrast = kmeans_contrast(image) File "/media/linux/643A-4CF3/code/SelfDeblur-master/K-meanszengqiang.py", line 10, in kmeans_contrast kmeans = cv2.KMeans(n_clusters=k, max_iter=100, n_init=10, random_state=42) AttributeError: module 'cv2' has no attribute 'KMeans'
时间: 2024-01-12 11:04:56 浏览: 345
runtime library [libssl.so.1.1] in /usr/lib/x86_64-linux-gnu may be hidden by files in:/home/
这个错误提示说在你的代码中调用了 `cv2.KMeans`,但是cv2模块中并没有这个属性。这是因为在OpenCV 4.0版本后,KMeans算法被移动到了ml模块中。你可以尝试修改代码中的这一行:
```
kmeans = cv2.KMeans(n_clusters=k, max_iter=100, n_init=10, random_state=42)
```
改为:
```
kmeans = cv2.ml.KMeans_create()
kmeans.setClustersNumber(k)
kmeans.setTermCriteria((cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 100, 1.0))
kmeans.setAttempts(10)
kmeans.setFlags(cv2.KMEANS_PP_CENTERS)
```
这样就可以使用ml模块中的KMeans算法了。
阅读全文