某些图像的 OpenCV 中的双边滤波器错误

Bilateral Filter Error In OpenCV for Some Images

我有一个模因 URL 数据集,我想从中提取它们的文本。我有这个功能:

def image2text(path_x):
    with requests.get(path_x, stream=True) as r:
        request_x=r.content
        r.close()
    img=Image.open(BytesIO(request_x))
    bilat_x=cv2.bilateralFilter(np.array(img),5, 55,60)
    img.close()
    request_x=None
    cv2_x=cv2.cvtColor(bilat_x, cv2.COLOR_BGR2GRAY)
    _,img = cv2.threshold(cv2_x, 240, 255, 1)
    meme_text=pytesseract.image_to_string(img, lang='eng')
    return meme_text

image2text('https://i.redd.it/r9lw184zky881.png')

我收到以下错误:

error: OpenCV(4.1.2) /io/opencv/modules/imgproc/src/bilateral_filter.dispatch.cpp:166: error: (-215:Assertion failed) (src.type() == CV_8UC1 || src.type() == CV_8UC3) && src.data != dst.data in function 'bilateralFilter_8u'

BTW,代码主要是从this源码中复制过来的

我通过在打开图像后添加以下行来修复它:

img = cv2.cvtColor(np.array(img), cv2.COLOR_BGRA2BGR)