Search code examples
opencvvideo-capturempeg2-ts

Combing effect while reading interlaced video


all. I have a very strange issue, reading the VideoCapture in OpenCV. I have .MTS videos (MPEG2), and I read them in OpenCV using the next code:



cv2.namedWindow("frame", cv2.WINDOW_NORMAL)

cap = cv2.VideoCapture("00030.MTS")

while(cap.isOpened()):
    ret,frame = cap.read()
    cv2.imshow("frame", frame)
    cv2.waitKey(0)

And this shows me the corrupted frames (with bands on them). The same quality is kept, if I save the frame as the image, and look at It outside the OpenCV. enter image description here

But how It should look like: enter image description here I've never seen this before while working with .avi or .mp4

How can I get the same not-corrupted frames like in the media player?


Solution

  • (I edited the title of the question. Some of this information wasn't apparent originally.)

    Your file names suggest that this video material is a video camera's own recording, with no alterations to it.

    Your video seems to be "interlaced", i.e. not "progressive". Interlaced video consists of "fields" instead of complete frames. A field contains all the even or odd lines of an image. Even and odd fields follow each other. With that method, you can have "50i" video that updates at 50 Hz, yet requires only half the data of a full "50p" video (half the data means reduced vertical resolution).

    OpenCV (probably) uses ffmpeg to read your video file.

    Both ffmpeg and VLC know when a video file contains interlaced video data. VLC automatically applies a suitable filter to make this data nicer to look at. ffmpeg does not, because filtering costs processing time and changes the data.

    You should use ffmpeg to "de-interlace" your video files. I would suggest the yadif filter.

    Example:

    ffmpeg -i 00001.MTS -c copy -vf yadif -c:v libx264 -b:v 24M 00001_deinterlaced.MTS
    

    You should look at the settings/options of yadif. Maybe the defaults aren't to your liking. Try yadif=1 to get field-rate progressive video (50/60p).