# Read Access Violation from opencv\_cored.dll when using .converTo()

**URL:** <https://forum.opencv.org/t/read-access-violation-from-opencv-cored-dll-when-using-converto/4071>\
**Category:** C++\
**Tags:** core\
**Created:** [June 29, 2021, 2:00am UTC](https://forum.opencv.org/t/read-access-violation-from-opencv-cored-dll-when-using-converto/4071 "2021-06-29T02:00:25Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Marc\_P](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/marc_p/32/2533_2.png) [@Marc\_P](https://forum.opencv.org/u/Marc_P)\
**Post date:** [June 29, 2021, 2:00am UTC](https://forum.opencv.org/t/read-access-violation-from-opencv-cored-dll-when-using-converto/4071/1 "2021-06-29T02:00:26Z")

</div>

Hello!  
I am a beginner and running into some trouble with the surface matching in opencv.  
I am attempting to get depth from a depth camera and give it to a surface match. When I attempt to convert the Mat to CV\_32F, which the detector.trainModel() needs, I get a Read Access Violation.

> Exception thrown: read access violation.  
> **ptr** was 0x8A88000.

```auto
Mat imageD(Size(wD, hD), CV_8UC3, (void*)depth.get_data(), Mat::AUTO_STEP);

imageD.convertTo(imageD, CV_32F);

ppf_match_3d::PPF3DDetector detector(0.03, 0.05);
detector.trainModel(imageD);

```

imageD is the depth image I get from the camera.  
wD is the width of the depth image.  
hD is the height of the depth image.

Also, I am using Visual Studio 2019 with OpenCV version 4.3.0 through vcpkg.

Thanks for the help!

---

<div class="post-metadata">

**Author:** ![crackwitz](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/crackwitz/32/14_2.png) [@crackwitz](https://forum.opencv.org/u/crackwitz)\
**Post date:** [June 29, 2021, 9:02am UTC](https://forum.opencv.org/t/read-access-violation-from-opencv-cored-dll-when-using-converto/4071/2 "2021-06-29T09:02:47Z")

</div>

you’re gonna need to show more code. `depth` isn’t shown.

---

<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:** [June 29, 2021, 10:46am UTC](https://forum.opencv.org/t/read-access-violation-from-opencv-cored-dll-when-using-converto/4071/3 "2021-06-29T10:46:17Z")

</div>

assuming, your input is CV\_8UC3, you cannot convert it _in-place_ to a single float channel (you would need to re-allocate the output) using that “borrowed pointer”.

convertTo also does not change the number of channels

probably the easiest solution is to use a new, empty Mat for the result:

```auto
Mat ImageG;
cvtColor(imageD, imageG, COLOR_BGR2GRAY);

Mat imageF;
imageG.convertTo(imageF, CV_32F);

ppf_match_3d::PPF3DDetector detector(0.03, 0.05);
detector.trainModel(imageF);

```

p.s.  
i think, this is all the wrong approach. instead (lossy) conversion to float (what do you think, you gain here, even ?), you should find out, if it is possible to get a better image format from your cam directly
