在使用超像素语义分割时会遇到的错误:
AttributeError: partially initialized module ‘cv2‘ has no attribute ‘gapi_wip_gst_GStreamerPipeli
AttributeError: module ‘cv2’ has no attribute ‘ximgproc’
cv2.error: OpenCV(4.7.0) D:\a\opencv-python\opencv-python\opencv\modules\ml\src\svm.cpp:1630: error: (-5:Bad argument) in the case of classification problem the responses must be categorical; either specify varType when creating TrainData, or pass integer responses in function ‘cv::ml::SVMImpl::train’
这些错误的原因是opencv-contrib-python和opencv-python两个库之间的版本不兼容所造成的,如果你按照某一个错误原因去查询解决方案,那么你大概率会在这几次个错误之间反复跳转,并且无法解决。
解决办法如下
先对两个包进行卸载:
pip uninstall opencv-contrib-python
pip uninstall opencv-python
然后选择相对应的版本进行安装:
pip install opencv-contrib-python==3.4.2.16
pip install opencv-python==3.4.2.16
目前能够使用的版本号如下:
version = 3.4.0.14, 3.4.10.37, 3.4.11.39, 3.4.11.41, 3.4.11.43, 3.4.11.45, 3.4.13.47, 3.4.14.51, 3.4.14.53, 3.4.15.55, 3.4.16.57, 3.4.16.59, 3.4.17.61, 3.4.17.63, 3.4.18.65, 4.3.0.38, 4.4.0.40, 4.4.0.42, 4.4.0.44, 4.4.0.46, 4.5.1.48, 4.5.2.52, 4.5.2.54, 4.5.3.56, 4.5.4.58, 4.5.4.60, 4.5.5.62, 4.5.5.64, 4.6.0.66, 4.7.0.68, 4.7.0.72
SLIC分割的代码如下:
import skimage.segmentation as seg
import skimage.color as color
import matplotlib.pyplot as plt
# 读取图像
img = plt.imread('00002.jpg')
# SLIC超像素分割
num_segments = 2000
compactness = 10
segments = seg.slic(img, n_segments=num_segments, compactness=compactness)
# 显示结果
fig, ax = plt.subplots(1, 2, figsize=(10, 5))
ax[0].imshow(img)
ax[1].imshow(color.label2rgb(segments, img, kind='avg'))
plt.show()
在使用这段代码时可能会提示没有‘six’模块,直接 pip install 就行了