Stroke Width Transform Unknown C++ Exception

Hi,
I am trying to implement the new cv2.text.detectTextSWT() algorithm described here in the opencv docs: OpenCV: cv::text Namespace Reference,
But I am seeing an error with the underlying C++ code.
Here is my code (python):

img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
rects, draw, chainBBs = cv2.text.detectTextSWT(img, True)
for rect in rects:
    cv2.rectangle(vis, rect, (255, 255, 255), 2)
cv2.imshow("", img)
cv2.waitKey(0)

And the error is:

rects = cv2.text.detectTextSWT(vis, True)
    cv2.error: Unknown C++ exception from OpenCV code

Any ideas? My image is a numpy array, and changing it to BGR, GRAY or RGB doesn’t seem to help.

os ? opencv version ? how did you install that ?
maybe you can add the image ?

(cant reproduce your problem on 4.5.3-dev)

crnn

>>> im = cv2.imread("img/crnn.png")
>>> rects, draw, chainBBs = cv2.text.detectTextSWT(im, False)
>>> rects
array([[143,  20,  12,  40],
       [103,  37,  19,  22],
       [ 10,  11,  29,  42],
       [ 79,  16,  23,  41],
       [ 37,  33,  20,  21],
       [155,  41,  18,  21],
       [122,  18,  21,  42],
       [ 58,  35,  19,  21]], dtype=int32)

Note that on your code snippets you call the function with img and vis… Perhaps your real code just has such silly slip.

2 Likes

Cheers, that’s because it was originally vis in my code, but I changed the name in the example for clarity :slight_smile: Must have missed one.

For some reason running img = cv2.adaptiveThreshold(img, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 15, 35) before the code example avoids the error? I’m using uint8 as the array dtype, int32 and uint32 didn’t seem to help.

Edit: It seems to only work for me when the image is binarised AND in RGB format, which is very strange.