# After using the detectTextSWT function, the program crashes

**URL:** <https://forum.opencv.org/t/after-using-the-detecttextswt-function-the-program-crashes/6350>\
**Category:** Uncategorized\
**Created:** [November 22, 2021, 8:53am UTC](https://forum.opencv.org/t/after-using-the-detecttextswt-function-the-program-crashes/6350 "2021-11-22T08:53:10Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mahdi](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/mahdi/32/3459_2.png) [@Mahdi](https://forum.opencv.org/u/Mahdi)\
**Post date:** [November 22, 2021, 8:53am UTC](https://forum.opencv.org/t/after-using-the-detecttextswt-function-the-program-crashes/6350/1 "2021-11-22T08:53:10Z")

</div>

**problem :** hi, why After using the detectTextSWT function, the program crashes? Where am I wrong?

**my codes :**

```auto
// read image
cv::Mat imageInputBgr = OpenImage("imageInputBgr", cv::ImreadModes::IMREAD_COLOR); // read as 3-channel-BGR
if (imageInputBgr.empty())
{
	qDebug() << "Could not load the image file";
	return;
}

cv::Mat imageOutputBgr1 = imageInputBgr.clone();
std::vector<cv::Rect> components;
bool dark_on_light = false;
cv::Mat out;
std::vector<cv::Rect> regions;
cv::text::detectTextSWT(imageOutputBgr1, components, dark_on_light, out, regions);

return;

// other codes
// .
// .
// .

```

**Application output :**  
12:21:02: The program has unexpectedly finished.  
12:21:02: The process was ended forcefully.  
12:21:02: C:\Users\mahdi\Documents\build-untitled-Desktop\_Qt\_5\_12\_11\_MSVC2017\_64bit-Debug\debug\untitled.exe crashed.

---

<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:** [November 22, 2021, 9:34am UTC](https://forum.opencv.org/t/after-using-the-detecttextswt-function-the-program-crashes/6350/2 "2021-11-22T09:34:35Z")

</div>

similar :

> [@Stroke Width Transform Unknown C++ Exception](https://forum.opencv.org/t/stroke-width-transform-unknown-c-exception/5525):
>
> 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): 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) …

> [@Stroke Width Transform Error](https://forum.opencv.org/t/stroke-width-transform-error/6279):
>
> Hi, I’m using the SWT function inside an image preprocessing pipeline I have in Python, and I’m finding that it is throwing some strange errors. The crash code is: Unknown C++ Exception. I also posted about this a few weeks ago. When I input my images, if I have resized them more than around 2x, then it will crash maybe 1 in 5 times. If I don’t resize the images then it doesn’t seem to crash. It also seems to throw the error more often when the Dark\_On\_Light parameter is True while the actual i…

(probably not your fault, but a bug)

---

<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:** [November 22, 2021, 10:07am UTC](https://forum.opencv.org/t/after-using-the-detecttextswt-function-the-program-crashes/6350/3 "2021-11-22T10:07:46Z")

</div>

can you add the image, please ?

---

<div class="post-metadata">

**Author:** ![Mahdi](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/mahdi/32/3459_2.png) [@Mahdi](https://forum.opencv.org/u/Mahdi)\
**Post date:** [November 22, 2021, 12:04pm UTC](https://forum.opencv.org/t/after-using-the-detecttextswt-function-the-program-crashes/6350/4 "2021-11-22T12:04:06Z")

</div>

yes sure, for example this image :

![scenetext_segmented_word03](https://us1.discourse-cdn.com/flex020/uploads/opencv/original/2X/2/21c8f4e66f763bba7582fd98d245276fab0239c8.jpeg)

or

![scenetext_segmented_word01](https://us1.discourse-cdn.com/flex020/uploads/opencv/original/2X/6/613cea6053b7494c87cd63eebf6fb09fd2d94bba.jpeg)

---

<div class="post-metadata">

**Author:** ![Mahdi](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/mahdi/32/3459_2.png) [@Mahdi](https://forum.opencv.org/u/Mahdi)\
**Post date:** [November 22, 2021, 2:39pm UTC](https://forum.opencv.org/t/after-using-the-detecttextswt-function-the-program-crashes/6350/5 "2021-11-22T14:39:18Z")

</div>

**Test1**

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

```auto
cv::Mat imageOutputBgr1 = imageInputBgr.clone();
std::vector<cv::Rect> components;
bool dark_on_light = false;
cv::Mat out;
std::vector<cv::Rect> regions;
cv::text::detectTextSWT(imageOutputBgr1, components, dark_on_light, out, regions);
//---
// Show Result
AddImage(out, "Letter Candidates");

```

I tested with the image above, When the value of the variable [dark\_on\_light] is false, I have no problem and no error occurs! BUT!!! When the value of the variable [dark\_on\_light] is true, it shows the following errors [**WHY???**] :

OpenCV(4.5.4) Error: Assertion failed (channels() == CV\_MAT\_CN(dtype)) in cv::Mat::copyTo, file F:…\4.5.4\opencv\modules\core\src\copy.cpp, line 320

OpenCV: terminate handler is called! The last OpenCV error is:

OpenCV(4.5.4) Error: Assertion failed (channels() == CV\_MAT\_CN(dtype)) in cv::Mat::copyTo, file F:…\4.5.4\opencv\modules\core\src\copy.cpp, line 320

* * *

* * *

* * *

**Test2**

when use this image, app crash [dark\_on\_light == true /// and /// dark\_on\_light == false, in both, app crash]

![scenetext_segmented_word03](https://us1.discourse-cdn.com/flex020/uploads/opencv/original/2X/2/21c8f4e66f763bba7582fd98d245276fab0239c8.jpeg)

error message :

18:12:49: The program has unexpectedly finished.  
18:12:49: The process was ended forcefully.  
18:12:49: C:…\build-untitled-Desktop\_Qt\_5\_12\_11\_MSVC2017\_64bit-Debug\debug\untitled.exe crashed.

* * *

* * *

* * *

**Test3**

![scenetext_word01](https://us1.discourse-cdn.com/flex020/uploads/opencv/original/2X/b/b4bbf0cdbcf79111868da67be7a916d9ed1789ce.jpeg)

dark\_on\_light == true ::: no error, no crash, show best result  
dark\_on\_light == false ::: no error, no crash, The output is not the same as the previous test and is poor.

* * *

* * *

* * *

---

<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:** [November 23, 2021, 9:38am UTC](https://forum.opencv.org/t/after-using-the-detecttextswt-function-the-program-crashes/6350/6 "2021-11-23T09:38:32Z")

</div>

> [@Mahdi](#):
>
> When the value of the variable [dark\_on\_light] is true, it shows the following errors [**WHY???**] :
> 
> OpenCV(4.5.4) Error: Assertion failed (channels() == CV\_MAT\_CN(dtype)) in cv::Mat::copyTo, file F:…\4.5.4\opencv\modules\core\src\copy.cpp, line 320

outTextRegions is empty here:

> <https://github.com/opencv/opencv_contrib/blob/4.x/modules/text/src/text_detector_swt.cpp#L850>

i think it must be checked (bug)
