OpenCV: Optical Flow calculation in a pipe with Gunnar Farneback

34 views Asked by At

I would like to calculate the optical flow in a endoscopy video of a pipe. I would use the cv2 Farneback Optical FLow to calculate the optical flow. With the optical flow i would like to calculate the movement of the endoscope (forwards or backwards). My Video Frame looks like this: The pipe endoscope videp My results looks like this, the picture of optical flow is always black, any idea why? Results optical flow

import cv2
import tkinter as tk
from tkinter import filedialog
import numpy as np
import matplotlib.pyplot as plt

def select_video_file():
    root = tk.Tk()
    root.withdraw()
    file_path = filedialog.askopenfilename(title="Video auswählen", filetypes=[("Video Dateien", "*.mp4 *.avi *.mov *.mkv *.wmv *.flv *.mpeg")])
    return file_path

def horn_schunck(frame1, frame2):
    # Def Optical flow
    I1 = cv2.cvtColor(frame1, cv2.COLOR_BGR2GRAY).astype(np.float32) / 255.
    I2 = cv2.cvtColor(frame2, cv2.COLOR_BGR2GRAY).astype(np.float32) / 255.

    # Optical flow calculated with Gunner Farneback
    #flow = cv2.calcOpticalFlowFarneback(I1, I2, None, 0.5, 3, 15, 3, 5, 1.2, 0)
    flow = cv2.calcOpticalFlowFarneback(I1, I2, None, 0.5, 3, 15, 3, 5, 1.2, 0)

    return flow

def main():
    global flow
    # Choose file
    video_path = select_video_file()   

    # Check file
    if not video_path:
        print("Kein Video ausgewählt.")
        return

    # Video import
    cap = cv2.VideoCapture(video_path)

    # Cideo check
    if not cap.isOpened():
        print("Fehler beim Öffnen des Videos.")
        return

    ret, frame1 = cap.read()
    if not ret:
        print("Fehler beim Lesen des ersten Frames.")
        return

    # Frame size
    height, width, _ = frame1.shape
  

    while True:
        ret, frame2 = cap.read()

        if not ret:
            break

        # Optical flow is now calculated between two frames
        flow = horn_schunck(frame1, frame2)
        
        #draw optical flow in colors
        
        global hsv
        global rgb
         
        hsv=np.zeros_like(frame1)
        hsv[...,1]=255
         
        mag, ang =     (flow[...,0], flow[...,1])
         
        hsv[...,0]=ang*180/np.pi/2
        hsv[...,2]=cv2.normalize(mag,None,0,255,cv2.NORM_MINMAX)
        rgb=cv2.cvtColor(hsv, cv2.COLOR_HSV2BGR)
         
        plt.subplot(3,1,1)
        plt.imshow(frame1),plt.title('frame1'),plt.xticks([]),plt.yticks([])
        plt.subplot(3,1,2)
        plt.imshow(frame2),plt.title('frame2'),plt.xticks([]),plt.yticks([])
        plt.subplot(3,1,3)
        plt.imshow(rgb),plt.title('optical flow'),plt.xticks([]),plt.yticks([])
        plt.show()
        

        
        frame1 = frame2

    # Video-Stream release
    cap.release()

if __name__ == "__main__":
    main()

0

There are 0 answers