Shifted alignment when opening video

When opening a video in Python with OpenCV, the video is shown incorrectly.

Wrong result in OpenCV:

The face in above picture is covered because of privacy.

import numpy as np
import cv2 as cv

cap = cv.VideoCapture('video.avi')
while cap.isOpened():
    ret, frame = cap.read()

    # gray = cv.cvtColor(frame, cv.COLOR_BGR2GRAY)
    cv.imshow('frame', frame)

    if cv.waitKey(1) & 0xFF == ord('q'):
        break

cap.release()
cv.destroyAllWindows()

Terminal shows the following error

(python:xxxx): GStreamer-CRITICAL **: xx:xx:xx.xxx: gst_query_set_position: assertion 'format == g_value_get_enum (gst_structure_id_get_value (s, GST_QUARK (FORMAT)))' failed
[ WARN:0] global /builddir/build/BUILD/opencv-4.3.0/modules/videoio/src/cap_gstreamer.cpp (935) open OpenCV | GStreamer warning: Cannot query video position: status=1, value=-1, duration=3000
QSocketNotifier: Can only be used with threads started with QThread

OS: Fedora 33
Python: 3.9.1
OpenCV: 4.3.0
Gstreamer: 1.18.2

This is a screenshot of the original video

you’ll have to upload a sample video somehow.

other points:

  • cap.isOpened() is supposed to be called and checked once
  • ret from cap.read() must be checked and the loop ended if it is false

Thanks for the reply.

Unfortunately, I am not allowed to upload the video publicly.

Do you know what causes the error shown in the terminal?
It occurs at cap = cv.VideoCapture('video.avi')
It seems to be a gstreamer problem, but Totem can play the video correctly.

well yes, the video has a weird horizontal resolution, and whatever decodes the video doesn’t handle that properly.

it’s definitely a bug, but you decide if you want to fix your video to have a size that’s divisible by a certain number (powers of two are popular), or if you want to dig into some source code.

if you want to submit it as an issue for others to deal with, you HAVE TO provide data to reproduce the issue. a screenshot is not enough.

Like crackwitz has pointed out rightly, the horizontal resolution (image width) is not correct. It must be divisible by 4 for OpenCV to process it correctly.

OpenCV has no such restriction in general. it’s the video reading library that happens to have been picked, or its usage.