# Stroke Width Transform Unknown C++ Exception

**URL:** <https://forum.opencv.org/t/stroke-width-transform-unknown-c-exception/5525>\
**Category:** Python\
**Tags:** text\
**Created:** [October 8, 2021, 12:38am UTC](https://forum.opencv.org/t/stroke-width-transform-unknown-c-exception/5525 "2021-10-08T00:38:38Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jesse\_Whitten](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/jesse_whitten/32/3129_2.png) [@Jesse\_Whitten](https://forum.opencv.org/u/Jesse_Whitten)\
**Post date:** [October 8, 2021, 12:38am UTC](https://forum.opencv.org/t/stroke-width-transform-unknown-c-exception/5525/1 "2021-10-08T00:38:38Z")

</div>

Hi,  
I am trying to implement the new cv2.text.detectTextSWT() algorithm described here in the opencv docs: [OpenCV: cv::text Namespace Reference](https://docs.opencv.org/master/d8/de7/namespacecv_1_1text.html#a9370f4e7849c94fb418eebd915a6839d),  
But I am seeing an error with the underlying C++ code.  
Here is my code (python):

```auto
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:

```auto
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.

---

<div class="post-metadata">

**Author:** ![berak](https://avatars.discourse-cdn.com/v4/letter/b/85f322/32.png) [@berak](https://forum.opencv.org/u/berak)\
**Post date:** [October 8, 2021, 8:22am UTC](https://forum.opencv.org/t/stroke-width-transform-unknown-c-exception/5525/2 "2021-10-08T08:22:54Z")

</div>

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

(cant reproduce your problem on 4.5.3-dev)

![crnn](https://us1.discourse-cdn.com/flex020/uploads/opencv/original/2X/d/dd66d680894d2b124782d0ab87c69120167e3c7a.png)

```auto
>>> 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)

```

---

<div class="post-metadata">

**Author:** ![matti.vuori](https://avatars.discourse-cdn.com/v4/letter/m/e79b87/32.png) [@matti.vuori](https://forum.opencv.org/u/matti.vuori)\
**Post date:** [October 8, 2021, 8:34am UTC](https://forum.opencv.org/t/stroke-width-transform-unknown-c-exception/5525/3 "2021-10-08T08:34:52Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Jesse\_Whitten](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/jesse_whitten/32/3129_2.png) [@Jesse\_Whitten](https://forum.opencv.org/u/Jesse_Whitten)\
**Post date:** [October 13, 2021, 8:23pm UTC](https://forum.opencv.org/t/stroke-width-transform-unknown-c-exception/5525/5 "2021-10-13T20:23:10Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Jesse\_Whitten](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/jesse_whitten/32/3129_2.png) [@Jesse\_Whitten](https://forum.opencv.org/u/Jesse_Whitten)\
**Post date:** [October 13, 2021, 8:42pm UTC](https://forum.opencv.org/t/stroke-width-transform-unknown-c-exception/5525/6 "2021-10-13T20:42:12Z")

</div>

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.
