# Automatically detect image flip and reverse

**URL:** <https://forum.opencv.org/t/automatically-detect-image-flip-and-reverse/2997>\
**Category:** Uncategorized\
**Created:** [April 26, 2021, 4:21pm UTC](https://forum.opencv.org/t/automatically-detect-image-flip-and-reverse/2997 "2021-04-26T16:21:34Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Vlad](https://avatars.discourse-cdn.com/v4/letter/v/3bc359/32.png) [@Vlad](https://forum.opencv.org/u/Vlad)\
**Post date:** [April 26, 2021, 4:21pm UTC](https://forum.opencv.org/t/automatically-detect-image-flip-and-reverse/2997/1 "2021-04-26T16:21:34Z")

</div>

Hello, I am trying to build an application using a camera that has an automatic 180 degrees flip feature when its physically rotated to more than 90 degrees. I am trying to overcome this feature by flipping the image back when the automatic flip is triggered and I need a robust way to detect the feed rotation automatically to be able to flip it back. Any ideas?

---

<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:** [April 26, 2021, 6:54pm UTC](https://forum.opencv.org/t/automatically-detect-image-flip-and-reverse/2997/2 "2021-04-26T18:54:46Z")

</div>

while it is next to impossible to say, if any image is a “flipped” one without further clues like flipped text or “i know, the streetlamp is to the right of my window”

you may still try to catch the moment, the flip happens, by comparing current / last frame (ranging from simple absdiff() to BackgroundSubtractors)),  
there should be a clear spike (also mirror symmetric, what was moved to the right, is missing on the left, and vice versa)

to check for flip symmetry (vs camera sweep), you could try to split your image in 2 halves along the sym axis, and reduce() both along that axis.  
those should be similar for flip, but not for sweep

---

<div class="post-metadata">

**Author:** ![Vlad](https://avatars.discourse-cdn.com/v4/letter/v/3bc359/32.png) [@Vlad](https://forum.opencv.org/u/Vlad)\
**Post date:** [April 26, 2021, 7:01pm UTC](https://forum.opencv.org/t/automatically-detect-image-flip-and-reverse/2997/3 "2021-04-26T19:01:13Z")

</div>

What do you mean by camera sweep?  
Also, I think I should have mentioned that the video feed will be subject to change. Meaning that the things the camera will see will vary. It is a fixed setup with a bed where people come and go on and off the bed.

---

<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:** [April 26, 2021, 7:13pm UTC](https://forum.opencv.org/t/automatically-detect-image-flip-and-reverse/2997/4 "2021-04-26T19:13:14Z")

</div>

> [@Vlad](#):
>
> What do you mean by camera sweep?

that the camera is moving in steps less than ± 90° (which would trigger a flip), so you probably have to deal with intermediate camera movement, too !)

> [@Vlad](#):
>
> Meaning that the things the camera will see will vary.

please explain (maybe we’re saying the same thing ?)

---

<div class="post-metadata">

**Author:** ![Vlad](https://avatars.discourse-cdn.com/v4/letter/v/3bc359/32.png) [@Vlad](https://forum.opencv.org/u/Vlad)\
**Post date:** [April 27, 2021, 4:13am UTC](https://forum.opencv.org/t/automatically-detect-image-flip-and-reverse/2997/5 "2021-04-27T04:13:34Z")

</div>

If you imagine the image the camera displays then the sides would be somewhat the same over time without much variation whilst the center of the image defined by a rectangle would be variable. This is what I mean.

---

<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:** [April 27, 2021, 6:08am UTC](https://forum.opencv.org/t/automatically-detect-image-flip-and-reverse/2997/6 "2021-04-27T06:08:06Z")

</div>

no idea, what you mean, sorry

---

<div class="post-metadata">

**Author:** ![FilipBaas](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/filipbaas/32/802_2.png) [@FilipBaas](https://forum.opencv.org/u/FilipBaas)\
**Post date:** [April 28, 2021, 8:10pm UTC](https://forum.opencv.org/t/automatically-detect-image-flip-and-reverse/2997/7 "2021-04-28T20:10:58Z")

</div>

I do not understand your problem either. Maybe photos of your problem would say more than just words.

---

<div class="post-metadata">

**Author:** ![fincherdavid](https://avatars.discourse-cdn.com/v4/letter/f/cdc98d/32.png) [@fincherdavid](https://forum.opencv.org/u/fincherdavid)\
**Post date:** [May 6, 2021, 4:03am UTC](https://forum.opencv.org/t/automatically-detect-image-flip-and-reverse/2997/8 "2021-05-06T04:03:50Z")

</div>

thanks a lot for your inspiring post! I struggling actually also with an rotation problem a bit different you have done:

I have a binary image with a white rectangle. The image dimensions are sufficient so rotating the image up to 90° doesnt cut off anything. So far so good!

At the starting point (0° rotation) I count the white pixels.  
Then I rotate the image using scipy.ndimage.rotate with reshape=False from 0°:1°:90° counting the white pixels of the rotated image and estimating the difference regarding no rotation.

What do I get: for some angles, the rotated rectangle has more and sometimes less white pixels than the rectangle without any rotation[.](https://cinemahdapk.info/)  
Do you know am algorithm or any hint to get a pixelinvariant rotation?? I´m struggling since days without any idea
