首先先解决opencv报错问题,一上来就报错 头很疼
cv2.error: OpenCV(4.5.3) C:\Users\runneradmin\AppData\Local\Temp\pip-req-build-z4706ql7\opencv\modules\highgui\src\window.cpp:1274: error: (-2:Unspecified error) The function is not implemented. Rebuild the library with Windows, GTK+ 2.x or Cocoa support. If you are on Ubuntu or Debian, install libgtk2.0-dev and pkg-config, then re-run cmake or configure script in function 'cvShowImage'
这个估计是版本没下载好,删了重来
pip uninstall opencv-python
然后下载命令
pip3 install opencv-contrib-python
nice 成功解决 当然报错还有可能是路径带有中文或空格有时也会报错。
废话不多说,直接上代码
import cv2 as cv
import numpy as np
def fill_color_demo(image): #定义一键填充颜色的函数
Img2 = image.copy() #对输入的图片进行复制
h, w = image.shape[:2] #获得图片的长宽
mask = np.zeros([h+2, w+2],np.uint8) #mask必须行和列都加2,且必须为uint8单通道阵列,填充边缘需要多2个像素,否则会报错
cv.floodFill