r/Python • • Sep 30 '19

Rolling Shutter Effect demonstrated with Python

2.2k Upvotes

58 comments sorted by

92

u/Swipecat Sep 30 '19

I watched the Youtube video Rolling Shutter Explained on the Cheap by Matt Parker where he shows that you don't need to visit an actual aeroplane with expensive slow-motion cameras to demonstrate the rolling shutter effect on aircraft propellors. He shows that you can do it with a model propellor connected to an electric drill — then some Python code to process the video to emulate the rolling shutter effect and to add a green line scanning down the image.

Well, I thought, let's do the whole thing in Python.

Here's 30 lines of Python, using the Numpy and Pillow libraries and ffmpeg to create the video:

https://github.com/dafarry/rolling-shutter-effect-python/blob/master/rolling-shutter-makevideo.py

Here's another version that uses Tkinter to display the video to your screen in real time rather than making a video. It needs a fast(ish) PC to display smoothly:

https://github.com/dafarry/rolling-shutter-effect-python/blob/master/rolling-shutter-tkinter.py

11

u/SamF111 Sep 30 '19

Any thoughts on how to undo it with the known shutter rate and the output of your analysis as an input?

13

u/s3cur1ty Sep 30 '19 edited Aug 08 '24

This post has been removed.

9

u/Synaps4 Oct 01 '19

Yep, you would need to match your shutter rate to the rotation of the object.

6

u/bradfordmaster Oct 01 '19

Hmm... Not necessarily. If you knew it was a constant rate, I think you should be able to approximate it from the input if you can also make some assumptions about the shape of the real propeller (e.g. radial symmetry). Now I'm curious how well it would work

4

u/Synaps4 Oct 01 '19

Good point. You can at least know a multiple of the rotation rate if you know the shutter speed. I wonder if 4 rotor objects are always discernable from 5-rotor ones?

1

u/bradfordmaster Oct 01 '19

Almost certainly based on the number of blobs you see and rate they move, at least I think. Also I think you can be pretty confident in the multiple by putting some bounds on the speed (propeller tip not going faster than speed of sound, blade not longer than a certain amount)

2

u/flashman Oct 01 '19

Well that's only going to get you a video with a single row in each frame, isn't it?

1

u/[deleted] Oct 01 '19

You can probably learn a lot about the propeller from that.

1

u/[deleted] Oct 07 '19

May you please explain the math on this? Especially the use of complex numbers for the propeller

3

u/Swipecat Oct 07 '19

If you have a complex plane Z, and plot the absolute value |Z| in the third dimension, what do you have? The further from the origin, the greater its height, so it's a cone.

Cut a section through the cone at a height of H: |Z| = H and you have a circle.

And |Z| < H is a solid disk in "boolean" terms.

A cone offset from the origin by A is |Z - A| .

Consider adding the heights of two cones together then cut through them: |Z - A| + |Z - B| < H ...it's perhaps not entirely obvious that they combine to form a solid ellipse, but they do.

Put the focus B back at the origin... |Z - A| + |Z| < H

Then spin the focus A around the origin in 6 steps and you have 6 ellipses.

Combine them with boolean logic and you have a propeller.

1

u/[deleted] Oct 07 '19

Thanks!

1

u/GickRick Oct 01 '19

Man you're the goat✊🙌🙌

5

u/ComplexColor Oct 01 '19

Very nice animation and demonstration.

But is this physically accurate? Light sensors are integration devices - simplified they count the number of photons over a period of time - this time is the exposure time. The demo above suggests that this integration is done line after line. Always only one line integrating for a short time. This would seem to be very inefficient, as the total exposure time would be much longer than the individual line exposure time.

Would it not make more sense, that the exposure times somehow overlap? For example, if one frame exposure ends when the green line hits the sensor, but the next frames exposure starts immediately. This would totally blur the image though. So how does this actually work?

3

u/Swipecat Oct 01 '19

I presume that the videos of this effect that are shown on Youtube, out of aircraft windows with smartphones etc, are in bright light conditions. I also presume that the camera reduces its sensitivity in bright light by integrating no more than a few lines.

2

u/ericonr Oct 01 '19

It is done line after line. That's why sometimes you have pictures with mirrors where the real thing and its reflection don't match. The sensor probably starts its integration in a reasonable time so that when it's read it's been exposed to light only for its exposure time.

Even with mechanical shutters, cameras do something similar. What happens is that the shutter has two curtains, where one is responsible for exposing the sensor and the other is responsible for covering it back up. Fast shutter speeds require that the two curtains form a slit and move that slit across the sensor.

6

u/Tie_Good_Flies Sep 30 '19

Very cool! Can I ask why?

30

u/Swipecat Sep 30 '19

A few hours diversion on Sunday afternoon. And it was raining hard, here in the UK.

3

u/circle42 Sep 30 '19

What graphics package did you use?

12

u/Swipecat Sep 30 '19

No graphics package as such. Just array processing with Numpy to calculate the shapes. Pillow is an image library with some graphics capability, but I just used it to save the Numpy arrays to image files.

3

u/circle42 Sep 30 '19

Thanks, I'll look into that. I'm relatively inexperienced with python

12

u/14446368 Sep 30 '19

And it was raining hard, here in the UK.

Isn't that basically every day?

3

u/tunisia3507 Sep 30 '19

It's usually more of an unpleasant drizzle.

4

u/kaphi Sep 30 '19

Why not?

1

u/Tie_Good_Flies Sep 30 '19

Fair question I guess. This kind of stuff always amazed me and I had recently started wondering about it's practical application, since I do not have a background in this field

27

u/[deleted] Sep 30 '19

Nice animation, explains why propellers always appear like that on video

7

u/winowmak3r Sep 30 '19

The coolest demonstration of this is when a camera was filming a helicopter at the nearly same fps as the propellers were spinning. The helicopter looked like it was just floating in mid-air. It was really cool! Bonus secret Russian footage.

0

u/[deleted] Sep 30 '19

[deleted]

1

u/[deleted] Sep 30 '19

That’s correct

7

u/PcGamerSam Oct 01 '19

That would be a cool logo for an airline.

2

u/[deleted] Oct 01 '19

Fun fact, check the Malaysian Airlines logo. It looks quite similar.

1

u/PcGamerSam Oct 01 '19

Oh yeah good spot

2

u/[deleted] Oct 01 '19

Lol it's cos I'm malaysian

0

u/[deleted] Oct 01 '19

[deleted]

0

u/PcGamerSam Oct 01 '19

To be honest the world is a big place and it probably already is.

2

u/penguin-bonks Oct 01 '19

sweet man. posted on r/generative?

1

u/aitorp6 Sep 30 '19

awesome, good work

1

u/lyacdi Sep 30 '19

Yeah that's just a squid

1

u/dgube1 Oct 01 '19

Very nice demo

1

u/TheLeftyMan Oct 01 '19

Thats so cool

1

u/DDFoster96 Oct 01 '19

Spinning flower becomes a bird

1

u/imbaczek Oct 01 '19

went to https://en.wikipedia.org/wiki/Rolling_shutter, didn't find anything remotely as good. maybe put it there?

1

u/WikiTextBot Oct 01 '19

Rolling shutter

Rolling shutter is a method of image capture in which a still picture (in a still camera) or each frame of a video (in a video camera) is captured not by taking a snapshot of the entire scene at a single instant in time but rather by scanning across the scene rapidly, either vertically or horizontally. In other words, not all parts of the image of the scene are recorded at exactly the same instant. (Though, during playback, the entire image of the scene is displayed at once, as if it represents a single instant in time.) This produces predictable distortions of fast-moving objects or rapid flashes of light. This is in contrast with "global shutter" in which the entire frame is captured at the same instant.


[ ^PM | Exclude ^me | Exclude from ^subreddit | FAQ / ^Information | ^Source ] Downvote to remove | v0.28

1

u/jinchuika Oct 01 '19

The Uchiha clan wants to know your location

1

u/CaptainRogers1226 Oct 01 '19

Lol, when this video started playing, I was like, “Hey, the way that pattern is forming looks just like rolling shutter!” Then I read the caption

1

u/Shakaka88 Oct 01 '19

That’s how company logos are made

1

u/Sylorak Oct 01 '19

Could you repeat and post the video with a vertical line instead of the horizontral, please?

1

u/MadMemeingShotgunMan Oct 01 '19

That's a dope-ass logo for something

5

u/auto-xkcd37 Oct 01 '19

dope ass-logo


Bleep-bloop, I'm a bot. This comment was inspired by xkcd#37

0

u/[deleted] Sep 30 '19

[deleted]

25

u/Swipecat Oct 01 '19

I've now posted a README file on Github for installing the necessary Python libraries, in order to make this work, because there is a "gotcha" or two.

Regarding Numpy, I'll repost a comment that I made a month back in reply to somebody asking if it was usual to find that learning Numpy techniques was slow going:

I remember running PASCAL back in the '80s on an 8-bit home computer (Amstrad CPC6128) and being really impressed with the concept of structured programming rather than the GOTOs and GOSUBs that I'd been using. So that was my first computing paradigm shift, which I found really easy to adjust to, probably because I was young then, and I've remained with that paradigm, whatever the programming language, until just recently. So yes, lots of loops.

When I learned Python, I did the same thing, of course. Here's a Cosine "Mandelbrot Set" that I made to test my knowledge of Python structures and graphical display (using Tkinter).

https://pastebin.com/znkqNDZV

Creates this image.

https://i.imgur.com/0dp2qGK.jpg

I'd been aware of numpy, but I assumed that there were many types of nested loop structures that were impossible to "vectorize", certainly in the case of the mandelbrot set, so numpy would only be useable in a few specific cases.

I would discover that I was wrong about that in time. The change was gradual. No sudden change of thinking. I just learned more about numpy with time, learning new numpy commands when I did happen to write something that used numpy. Then I'd think can such-and-such be done in numpy? I'd do a Google search of the docs and Stack Overflow, and find that yes it can.

I've now rewritten that Mandelbrot Set script using Numpy. It creates the same image as the previous script, except a whole lot faster.

https://pastebin.com/usF7tgF6

One problem with numpy scripts is that they are almost unreadable to anybody that's not familiar with numpy. There is a certain elegance with "vectorizing" an algorithm that way, but there are pros and cons.

Anyway, in my opinion you'll get there if you want to, but just expect it to take a bit of time.

9

u/WikiTextBot Oct 01 '19

Structured programming

Structured programming is a programming paradigm aimed at improving the clarity, quality, and development time of a computer program by making extensive use of the structured control flow constructs of selection (if/then/else) and repetition (while and for), block structures, and subroutines.

It emerged in the late 1950s with the appearance of the ALGOL 58 and ALGOL 60 programming languages, with the latter including support for block structures. Contributing factors to its popularity and widespread acceptance, at first in academia and later among practitioners, include the discovery of what is now known as the structured program theorem in 1966, and the publication of the influential "Go To Statement Considered Harmful" open letter in 1968 by Dutch computer scientist Edsger W. Dijkstra, who coined the term "structured programming".Structured programming is most frequently used with deviations that allow for clearer programs in some particular cases, such as when exception handling has to be performed.


[ ^PM | Exclude ^me | Exclude from ^subreddit | FAQ / ^Information | ^Source ] Downvote to remove | v0.28

1

u/SuspiciousScript Oct 02 '19

Hello! Quick question about this code — where does the j in line 11 (plane = x + 1j * y) come from? It's definitely not something I've seen before.

3

u/Swipecat Oct 02 '19

The "imaginary unit". Mathematics uses "i" for the imaginary unit, but electrical engineers use "j" to avoid confusing it with "I" for current. Python uses the electrical engineering convention. There was a "bug" logged to change it to "i", but that didn't happen. Python's creator, Guido van Rossum, said:

This will not be fixed. For one thing, the letter 'i' or upper case 'I' look too much like digits. The way numbers are parsed either by the language parser (in source code) or by the built-in functions (int, float, complex) should not be localizable or configurable in any way; that's asking for huge disappointments down the road. If you want to parse complex numbers using 'i' instead of 'j', you have plenty of solutions available already.

So the "plane" in   plane = x + 1j * y

...means the "complex plane".

1

u/SuspiciousScript Oct 02 '19

Huh, I never knew that was a feature in the language. Thanks!

33

u/24mitcher Oct 01 '19

good to know it's numpy

0

u/[deleted] Sep 30 '19

[deleted]

1

u/tylor_durden Oct 01 '19

I see why you are being downvoted. Rick and morty fans are not allowed to be dumb.