# Subtract operation but in c++ code

**URL:** <https://forum.opencv.org/t/subtract-operation-but-in-c-code/736>\
**Category:** Uncategorized\
**Created:** [January 8, 2021, 1:12pm UTC](https://forum.opencv.org/t/subtract-operation-but-in-c-code/736 "2021-01-08T13:12:21Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Javier\_Dopico](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/javier_dopico/32/453_2.png) [@Javier\_Dopico](https://forum.opencv.org/u/Javier_Dopico)\
**Post date:** [January 8, 2021, 1:12pm UTC](https://forum.opencv.org/t/subtract-operation-but-in-c-code/736/1 "2021-01-08T13:12:21Z")

</div>

Hello, I am completely new in opencv and c++ programming and this is my case.  
I have Mat image 1 and Mat image2  
Want to subtract element by element image 1-image2 and get image 3.  
Just as if I was using subtract(image1, image2, image3);  
Does anybody know how?

By the way. I am doing this because I tried of course to use subtract function and the program crashed. Dont know why so I am tryng to do this operation a bit more “raw”  
Thanks!

---

<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:** [January 8, 2021, 1:18pm UTC](https://forum.opencv.org/t/subtract-operation-but-in-c-code/736/2 "2021-01-08T13:18:42Z")

</div>

> [@Javier\_Dopico](#):
>
> I tried of course to use subtract function and the program crashed.

please show us the code you tried, and ´the resulting error msg

(also be aware, that those functions require 2 Mats of exactly the same type and size

---

<div class="post-metadata">

**Author:** ![Javier\_Dopico](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/javier_dopico/32/453_2.png) [@Javier\_Dopico](https://forum.opencv.org/u/Javier_Dopico)\
**Post date:** [January 8, 2021, 1:55pm UTC](https://forum.opencv.org/t/subtract-operation-but-in-c-code/736/3 "2021-01-08T13:55:10Z")

</div>

![Captura](https://us1.discourse-cdn.com/flex020/uploads/opencv/original/1X/1ae1f06e06c37225f9c5e73c362ffae965b40017.png)  
Debug Error!

Program: …xtr\Desktop\Toxtrac-Git\toxtrac\x64\ToxTrac\Debug\ToxTrac.exe

abort() has been called

The code is simply what I said:

```auto
image1 = Mat::zeros(Size(mapX.cols, mapX.rows), CV_8UC1);
image2 = Mat::zeros(Size(mapX.cols, mapX.rows), CV_8UC1);

Mat imageDiff;
imageDiff = Mat::zeros(Size(mapX.cols, mapX.rows), CV_8UC1);
subtract(arena, arenafondo, imageDiff);

```

---

<div class="post-metadata">

**Author:** ![Javier\_Dopico](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/javier_dopico/32/453_2.png) [@Javier\_Dopico](https://forum.opencv.org/u/Javier_Dopico)\
**Post date:** [January 8, 2021, 4:34pm UTC](https://forum.opencv.org/t/subtract-operation-but-in-c-code/736/4 "2021-01-08T16:34:21Z")

</div>

uP! Please I need help creating this function 😃

---

<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:** [January 8, 2021, 5:06pm UTC](https://forum.opencv.org/t/subtract-operation-but-in-c-code/736/5 "2021-01-08T17:06:01Z")

</div>

> [@Javier\_Dopico](#):
>
> ```auto
> image1 = Mat::zeros(Size(mapX.cols, mapX.rows), CV_8UC1);
> image2 = Mat::zeros(Size(mapX.cols, mapX.rows), CV_8UC1);
> 
> Mat imageDiff;
> imageDiff = Mat::zeros(Size(mapX.cols, mapX.rows), CV_8UC1);
> subtract(arena, arenafondo, imageDiff);
> 
> ```

read that code carefully. image1, image2 aren’t used. arena and arenafondo aren’t defined.

---

<div class="post-metadata">

**Author:** ![Javier\_Dopico](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/javier_dopico/32/453_2.png) [@Javier\_Dopico](https://forum.opencv.org/u/Javier_Dopico)\
**Post date:** [January 8, 2021, 5:17pm UTC](https://forum.opencv.org/t/subtract-operation-but-in-c-code/736/6 "2021-01-08T17:17:34Z")

</div>

Well sorry Arena and arenafondo are really image 1 and image2, in my code is written correctly haha SORRY again.

---

<div class="post-metadata">

**Author:** ![Javier\_Dopico](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/javier_dopico/32/453_2.png) [@Javier\_Dopico](https://forum.opencv.org/u/Javier_Dopico)\
**Post date:** [January 8, 2021, 7:45pm UTC](https://forum.opencv.org/t/subtract-operation-but-in-c-code/736/7 "2021-01-08T19:45:49Z")

</div>

UP! please help me with the funtion ☹ Im sure it is over the internet already solved but coudnt find it.

---

<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:** [January 8, 2021, 10:42pm UTC](https://forum.opencv.org/t/subtract-operation-but-in-c-code/736/8 "2021-01-08T22:42:26Z")

</div>

what is “UP!” supposed to mean?

---

<div class="post-metadata">

**Author:** ![Javier\_Dopico](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/javier_dopico/32/453_2.png) [@Javier\_Dopico](https://forum.opencv.org/u/Javier_Dopico)\
**Post date:** [January 8, 2021, 10:56pm UTC](https://forum.opencv.org/t/subtract-operation-but-in-c-code/736/9 "2021-01-08T22:56:54Z")

</div>

Just a way to make my thread be seen in the forum…

---

<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:** [January 8, 2021, 11:31pm UTC](https://forum.opencv.org/t/subtract-operation-but-in-c-code/736/10 "2021-01-08T23:31:08Z")

</div>

that won’t help you at all. in fact it might even do the opposite.

you need to present relevant information. correct code would be a start. a “[minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example)”.
