Something strange happens when substracting 2 images (mat type) in opencv

Hi!
I have 2 images, image 1 and image 2.
I use a loop to substract that works like this (element1fromimage1=element1fromimage1-element1fromimage2)
The loop works, no sign of crash or error.
From what I have investigated, my problem comes from image2 (I have changed and used a different image and my problem dissapeares)
Im gonna show the problem with the result image.
To make it clear, i have to say that image 1 and image 2 are exactly the same but image2 HAS NO Ant in there.
So as you can see in the image here, the strange curve that appears in the middle of the image is clearly coming from image2 and it shouldnt, as image1 and image2 have suposedly the same dimesions.
I hope I made it clear. If not please let me know, is difficult only being able to post 1 picture

Are you sure that image size are equal?

Don’t use a loop to substract images use subtract

I tried to use functions like subtract or absdiff but the program crashed. I was recomended to use a loop and there it is. Where could I check the size or format? I mean i check outside of the program manually the dimensions of the images and they are both 671x648. Size in kilobytes is true that is a bit diferent.

Show us your program code

uchar tmp;
	for (int i = 0; i < arenafondo.rows; i++) {
		for (int j = 0; j < arenafondo.cols; j++) {
			tmp = imageDiff.at<uchar>(i, j);
			tmp = abs(tmp - arenafondo.at<uchar>(i, j));

			imageDiff.at<uchar>(i, j) = tmp;
		}
	}

in which image 1 is imageDiff and image 2 arenafondo, i think arenafondo has the problem

try absdiff
How do you check image size, number of channel and image type in your code?

Absdiff as I said, crashes the program.
And I am new in opencv… can you help me checking those parameters? Dont know how

What’s error message using absdiff?

it was an exception I remember.
It said.
ucrtbase.pdb wasnt loaded, and also said exception not controlled in memory address (ucrtbase.dll). Pleople recomended me what I was doing now with the loop

about loop it’s here OpenCV: How to scan images, lookup tables and time measurement with OpenCV

May be you should start reading and trying all tutorials

you should post the original source images. we can’t tell what is going on from just the output picture.

Image 2 and Image 1 i am using image 2 as background , detecting just the ant as shown in the result image. But as you saw there, I am also getting that strange thing.

Size are differents 666 != 668 and 642!=638

apart from that, I think one of these pictures is grayscale while the other is RGB, and you’re accessing the RGB one (<vec3b>) as if it was grayscale (<uchar>).
that explains the horizontal stretch by a factor of 3.

your snippet of code isn’t enough to diagnose that properly. this is what I figured out from the little bit of info you did share. be forthcoming. don’t make this like pulling teeth.

Oh god, sorry, big confusion just send you 2 pictures that were screenshots. Here are the real images

Sorry, I now sent the correct two images, the ones sent before where just screenshots… your reply will still have sense?

Solved… Thanks for all your answers.