OpenStreetMap

HeBri's Diary

Recent diary entries

Extracting geo-referenced images from video-recordings

Material:
  • Video file with known recording times / places
  • Track recording (gpx-file) for the video camera position
Tools:
  • mapillary_tools.exe (or ffmpeg) to extract the images from the video
  • exiftool.exe for manipulation of the image meta data information
  • JOSM (Openstreetmap-editor) for controlling the image positions (Alternatively you can use Garmin Basecamp for this task)

1st step: Extract images from the video (with mapillary_tools)

Since for me the video timestamp could not be extracted and writing the images with giving a start time did not work as well, the time is corrected later.

mapillary_tools.exe sample_video --video_import_path c:\VideofileOrDirectory --import_path c:\TargetDir --advanced --video_sample_interval 0.5

This step creates a subfolder with the extracted images for each video file.

With a maximum speed of 100 km/h (near 28 m/s) and 0.5 seconds sample time the distance between two consecutive images is at most about 14 meters. With slower movement the spacial distance between the images is smaller.

If the images are to be uploaded to Mapillary the distance should not be selected much larger. If the images are used for working in JOSM only an sampling interval of 3 to 5 seconds should be sufficient, depending on the speed at recording.

The timestamps in the image files correspond to the sampling interval where the first file in every subfolder starts at 1970/01/01 at 00:00:00 (Midnight) and increases with the sampling time in the sampled file sequence.

For the time correction test you should select and copy a file with distinct content. (Reaching a crossing, passing a bridge or similar)

2nd step: First correction of the timestamp

The video may contain a time overlay which can be used for a first correction. Otherwise you can use the recording time or file creation timestamp.

The timestamp in the EXIF data of the first image is found with

exiftool.exe ImageFileName_0000001,jpg

in the row

Date/Time Original : 1970:01:01 00:00:00.000

Example: The recording time of the copied image is 2019/02/16 13:56:30 The read time stamp from the copied image is

Date/Time Original : 1970:01:01 00:05:20.000

Correction: Time difference to the read 1970:01:01 00:05:20.000

You have to consider, that the time shown in the image or the found video start time may be the local time. The correction however has to be done in UTC time. For my location during winter the time is CET + 1 hour. So in my case the time difference to be corrected would calculate to 49 years, 1 month, 15 days, 12(!) hours, 51 minutes and 10 seconds.

With this we can try a first correction of the image file time entry.

exiftool.exe -DateTimeOriginal+="49:01:15 12:51:10" Imagecopy.jpg

3rd step: First entry of position data

With the timestamp now corrected in the image data we can add a position from the track recorded for the camera position.

exiftool.exe -geotag=Trackfile.gpx Imagecopy.jpg

4th step: Check of the image position

Start JOSM (or Basecamp) and import the image by drag and drop. Now verify the image position by comparing it with the features you see in the image and on the underlayed map or aerial image. You can import the track as well.

5th step: Correction loop

If the position of the image is not exactly correct the timestamp in the image has to be corrected and the position data has to be updated accordingly. This is repeated until the image position is right. The sum of all these corrections has to be used for setting the timestamp in all images later on. So take down notes of all changes made in this loop.

Example: The image is shown slightly too late on the track -> the timestamp has to be somewhat earlier, lets assume 5 seconds:

exiftool.exe -DateTimeOriginal-="00:00:00 00:00:05" Imagecopy.jpg

Overwrite the position according to new timestamp:

xiftool.exe -geotag=Trackfile.gpx Imagecopy.jpg

Reload image to JOSM (or Basecamp) and check position. Repeat 5th step until position is correct.

Example: After first correction the image is now slightly too early on the track -> the timestamp must be slightly later, lets say 1 second.

exiftool.exe -DateTimeOriginal+="00:00:00 00:00:01" Imagecopy.jpg

Overwrite the position according to new timestamp again:

exiftool.exe -geotag=Trackfile.gpx Imagecopy.jpg

For the total shift correction we get the sum -5+1 = -4 Seconds. After this evaluation the copied image can be deleted.

6th step: Insert all timestamps and positions

The time correction value estimated in the 2nd step is corrected by he difference found in loop of the 5th step and can now be used for timestamping all images.

From the example with the correction of -4 seconds:

exiftool.exe -DateTimeOriginal+="49:01:15 12:51:06" PathToTheImages

Then insert geotags to all images in the subfolder

xiftool.exe -geotag=Trackfile.gpx PathToTheImages

For the following videos the first steps can be omitted if you know that e.g. every 2 minutes the recording is continued in a next file. In this case you add these 2 minutes when entering the timestamps for the images subfolder of this video and the images should be located correctly.


Extrahieren von georeferenzierten Bildern aus Video-Aufzeichnungen

Material:
  • Videodatei mit bekannten Aufnahmezeiten / Orten
  • Trackaufzeichnung (GPX-Datei) für die Video-Kameraposition
Tools:
  • mapillary_tools.exe (oder ffmpeg) um die Bilder zu extrahieren
  • exiftool.exe für die Bearbeitung der Bildinformationen
  • JOSM (Openstreetmap-Editor) zur Kontrolle der Bildposition (Alternativ zu JOSM kann auch Garmin Basecamp benutzt werden)

1. Schritt: Bilder aus dem Video extrahieren (mit mapillary_tools)

Da bei mir der Zeitstempel nicht aus dem Video gelesen werden kann und das Schreiben mit Angabe einer Startzeit auch nicht funktioniert, wird die Zeit später korrigiert.

mapillary_tools.exe sample_video --video_import_path c:\VideofileOrDirectory --import_path c:\TargetDir --advanced --video_sample_interval 0.5

Dieser Schritt erzeugt für jede Videodatei einen Unterordner mit den extrahierten Bildern aus dem jeweiligen Video.

Bei einer max. Geschwindigkeit von 100 km/h (knapp 28 m/s) ist bei 0.5 s Bildabstand die max. Strecke zwischen 2 Bildern etwa 14 Meter. Bei langsamerer Fahrt werden die räumlichen Abstände zwischen den Bildern kleiner.

Sollen die Bilder zu Mapillary hochgeladen werden, sollte der Abstand nicht wesentlich größer gewählt werden. Werden die Bilder nur für die Bearbeitung in JOSM verwendet, reicht auch ein Intervall von 3 bis 5 Sekunden, je nach der bei der Aufnahme gefahrenen Geschwindigkeit.

Die Zeitstempel für die Bilddateien entsprechen dem Zeitintervall, wobei die erste Datei im Ordner mit dem 01.01.1970 um 00:00:00 Uhr beginnt und für die folgenden Dateien um das video_sample_interval ansteigt.

Für die Testkorrektur sollte ein Bild mit erkennbaren Merkmalen ausgewählt und kopiert werden. (Ankunft an Kreuzung / Querung einer Brücke o.ä.)

2. Schritt: Erste Korrektur des Zeitstempels.

Im Bild ist evtl. die eingeblendete Aufnahmezeit zu sehen. Diese kann für eine erste Korrektur verwendet werden. (Alternativ z.B. die Video-Datei-Erstellungszeit.)

Den im EXIF eingetragenen Zeitstempel der ersten Datei findet man mit

exiftool.exe Bilddateiname_000001.jpg

in der Zeile

Date/Time Original : 1970:01:01 00:00:00.000

Beispiel: Richtige Zeit für das kopierte Bild ist 2019/02/16 13:56:30 Ausgelesene Zeit für das kopierte Bild ist

Date/Time Original : 1970:01:01 00:05:20.000

Korrektur: Zeitdifferenz zum ausgelesenen 01.01.1970 00:05:20.

Dabei ist zu beachten, dass die eingeblendete Zeit üblicherweise die lokale Zeit, die Korrektur aber auf UTC durchzuführen ist. Für das Winterhalbjahr ergibt sich für meine Position eine Lokalzeit CET + 1 Stunde. Damit ergibt sich also 49 Jahre, 1 Monat, 15 Tage, 12(!) Stunden, 51 Minuten und 10 Sekunden als zu korrigierende Differenz.

Damit kann das kopierte Bild probehalber korrigiert werden.

exiftool.exe -DateTimeOriginal+="49:01:15 12:51:10" Bildkopie.jpg

Damit sollte sich der Eintrag für die Bild-Erstellungszeit geändert haben:

Date/Time Original : 2019:02:16 12:56:30.000

3. Schritt: Erster Eintrag einer Position

Mit der im Bild eingetagenen Zeit lässt sich jetzt eine Position aus dem parallel zum Video aufgezeichneten Track in das Bild eintragen.

` exiftool.exe -geotag=Trackdatei.gpx Bildkopie.jpg`

4. Schritt: Kontrolle der Position

JOSM (oder Basecamp) starten und das Bild per Drag and Drop in JOSM importieren. Jetzt anhand von Merkmalen im Bild und auf der unterliegenden Karte oder dem Luftbild die Position des Bildes verifizieren. Auch der Track lässt sich hier importieren.

5. Schritt: Korrekturschleife

Wenn die Position des Bildes nicht exakt richtig ist, muss der Zeitstempel im Bild korrigiert und die Position neu eingetragen werden. Dies wird solange wiederholt, bis die Bildposition korrekt ist. Die Summe aller Korrekturen wird später für das Eintragen in alle Bilder verwendet, also alle Korrekturzeiten in dieser wiederholten Aktion zusammenzählen!

Beispiel: Bild ist etwas “zu spät” auf dem Track -> Zeitstempel muss etwas früher sein, erste Schätzung 5 Sekunden:

exiftool.exe -DateTimeOriginal-="00:00:00 00:00:05" Bildkopie.jpg

Position neu eintragen:

exiftool.exe -geotag=Trackdatei.gpx Bildkopie.jpg

Bild erneut in JOSM (oder Basecamp) laden und kontrollieren. Schritt 5 wiederholen, bis es passt.

Beispiel Bild ist jetzt etwas “zu früh” auf dem Track -> Zeitstempel muss jetzt etwas später sein, geschätzt, 1 Sekunde.

exiftool.exe -DateTimeOriginal+="00:00:00 00:00:01" Bildkopie.jpg

Position neu eintragen:

exiftool.exe -geotag=Trackdatei.gpx Bildkopie.jpg

Für die Gesamtkorrektur ergibt sich damit eine Korrektur von -5+1 = -4 Sekunden. Nach dieser Auswertung kann das kopierte Bild gelöscht werden.

6. Schritt: Alle Zeitstempel und Positionen eintragen

Der in Schritt 2 ermittelte Korrekturwert für die Zeit wird um den in Schritt 5 ermittelten Wert korrigiert und nun für alle Bilder verwendet.

Aus dem Beispiel mit der Korrektur um -4 Sekunden:

exiftool.exe -DateTimeOriginal+="49:01:15 12:56:26" Bilddateipfad

Anschließend alle Bilder aus den Unterverzeichnissen mit Geotags verssehen

exiftool.exe -geotag=Trackdatei.gpx Bilddateipfad

Für die weiteren Videos können die ersten Schritte entfallen, wenn z.B. bekannt ist, dass alle 2 Minuten die Aufzeichnung in einer neuen Datei fortgesetzt wurde. Dann erfolgt die Eintragung des Zeitstempels mit dieser Differenz und die Bilder müssten richtig positioniert sein.