OpenCV rendering low quality video - opencv

I am trying to make something like a video player using Python. Quick google search showed me how to play a video using OpenCV. But video rendered using OpenCV is not as crisp as the video played by VLC media player. The images of both players are shown below.
OpenCV rendering
Video in VLC media player
I have checked the width and height of the images rendered by OpenCV and it is 1080p. But somehow the video is not as crisp as it should be. Here is the code used to render the images.
def start_slideshow_demo(video_file_path: str):
cap = cv2.VideoCapture(video_file_path)
cv2.namedWindow(video_file_path, cv2.WINDOW_GUI_EXPANDED)
cv2.setWindowProperty(video_file_path, cv2.WND_PROP_FULLSCREEN, cv2.WINDOW_FULLSCREEN)
while(cap.isOpened()):
ret, frame = cap.read()
if ret == True:
cv2.imshow(video_file_path, frame)
if cv2.waitKey(25) & 0xFF == ord('q'):
break
else:
break
cap.release()
cv2.destroyAllWindows()
Any help is appreciated. Thank you.

i don think it's Opencv issue. I used your code as it is in my desktop.
(left : opencv - right VLC player)

Related

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.
But how It should look like:
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?
(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).

Open CV Frame does not show up for Laptop camera

I am trying to do face detection in Open CV , however when i run my code i see camera light in laptop on , but the frame does not show up on system .
Can anyone help ?
below is the code
print("[INFO] loading model .....")
net = cv2.dnn.readNetFromCaffe(args["prototxt"],args["model"])
initialize video stream
print("[INFO] starting video stream ....")
vs =VideoStream(src=0).start()
time.sleep(1.0)
while True:
frame = vs.read()
frame =imutils.resize(frame,width=400)
(h,w)=frame.shape[:2]
blob = cv2.dnn.blobFromImage(cv2.resize(frame,(300,300)),1.0,(300,300),(104.0,177.0,123.0))
Try changing src=1 instead of src=0
vs =VideoStream(src=1).start() time.sleep(1.0)

OpenCV - Generate adaptive background for video

I want to achieve something like this-
Reference Video
Say I have a video which is a vertical video(Dimension- 720x1280). I want to create a horizontal video with adaptive background like the video I've shown.
I have written some code for reading and writing to a file.
video_index = 0
cap = cv2.VideoCapture(videofiles[0])
# video resolution: 1920x1080 px
out = cv2.VideoWriter("video.mp4v",
cv2.VideoWriter_fourcc(*'MP4V'),
30, (1920, 1080), 1)
What is the effect of having background to the video which smudges on the sides called in opencv/ffmpeg or otherwise?
How do I achieve this effect using code or tools(I am open to using OSS desktop tools)?
That effect is simply realized by scaling the initial video to the desired size, bluring it, and overlaying the original video on top at the center.
For the blur, I suggest starting with Gaussian blur, available in OpenCV.
As suggested by Gyan, I used the following piece of code-
ffmpeg -i video720x1280.mp4
-filter_complex
"[0]scale=hd1080,setsar=1,boxblur=20:20[b];
[0]scale=-1:1080[v];[b][v]overlay=(W-w)/2" video1920x1080.mp4
It worked like charm!
Link to original answer.

video capture card (webcam like) with OpenCV

I want to use video capture card to capture my screen display, and process the image by OpenCV/C++.
I have heard that there's some video capture card which is webcam like.(i.e. I can get the screen display by VideoCapture in OpenCV.)
Can someone tell me which video capture card should I buy?
Thanks !!!
I do not know if there some way to achieve that directly using OpenCV. However, a simple workaround could be like this:
Using this software you can create new webcam that stream your screen: https://sparkosoft.com/how-to-stream-desktop-as-webcam-video
Using OpenCV you can start capture the stream using this code:
cv::VideoCapture cap;
if(!cap.open(0)) // Use the new webcam Id instead of 0
return 0;
while(true){
cv::Mat frame;
cap >> frame;
if(frame.empty()) break;
cv::imshow("Screen", frame);
if( waitKey(10) == 27 ) break;
}
return 0;
I don't know if this helps now. But i found a way using opencv.
In linux and python, we achieve this using the following piece of code.
import cv2
cap = cv2.VideoCapture('/dev/video0')

How to select a video codec when capturing with DSPack?

I need to create an application that captures a video from a webcam. Later on I need to load the video and be able to navigate around in the video freely.
When I use the videocap-demo that comes with the Sources of DSPack, the captured video is encoded in H264. When I navigate around in that video, the picture becomes blurry, with wrong colors and you can't recognize the picture. (I tried with VLC and Windows media player)
...playing the video works, but after jumping to any position in the video, it looks like this...
How can I tell DSPack to capture the video e.g. in the old MJPEG-format?
(I tried with old videos from my camera. In MJpeg the navigation seems to work flawless)
Thanks in advance, R.

Resources