Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't convert video to grayscale

I'm trying to convert a video from my camera feed, which has low fps to gray. I have successfully fetched the video now I want to convert it to grayscale.

I've tried basic opencv operations and it isn't working. I get a video file when I open it there is no video.

import cv2
import time

cap = cv2.VideoCapture('output.avi')
fourcc = cv2.VideoWriter_fourcc(*'XVID')
print(fourcc)
out = cv2.VideoWriter('grey.avi',fourcc, 30.0, (800,600))
while True:    
    ret, frame = cap.read()
    time.sleep(0.1)
    cv2.imshow('frame1',frame)
    frame = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
    out.write(frame)
    cv2.imwrite('img.jpg',frame)
    cv2.imshow('frame',frame)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break
cap.release()
out.release()
cv2.destroyAllWindows()
like image 708
High On Math Avatar asked Sep 13 '25 08:09

High On Math


1 Answers

You need to change the isColor flag in cv2.VideoWriter. Currently the video writer setting is set to color instead of gray scale. You're incorrectly attempting to save a 3-channel color image (OpenCV default is BGR) as a gray scale image.

Change

out = cv2.VideoWriter('grey.avi',fourcc, 30.0, (800,600))

to

out = cv2.VideoWriter('grey.avi',fourcc, 30.0, (800,600), isColor=False)

Also your overall goal seems to capture video from a stream/camera feed and save the captured video in gray scale format. Here's an 'all in one' widget that reads frames from a camera stream link (RTSP), converts each frame to gray scale, and saves it as a video. Change video_src to your camera stream link.

RTSP video stream to grayscale video

from threading import Thread
import cv2

class VideoToGrayscaleWidget(object):
    def __init__(self, src=0):
        # Create a VideoCapture object
        self.capture = cv2.VideoCapture(src)

        # Default resolutions of the frame are obtained (system dependent)
        self.frame_width = int(self.capture.get(3))
        self.frame_height = int(self.capture.get(4))

        # Set up codec and output video settings
        self.codec = cv2.VideoWriter_fourcc('X','V','I','D')
        self.output_video = cv2.VideoWriter('output.avi', self.codec, 30, (self.frame_width, self.frame_height), isColor=False)

        # Start the thread to read frames from the video stream
        self.thread = Thread(target=self.update, args=())
        self.thread.daemon = True
        self.thread.start()

    def update(self):
        # Read the next frame from the stream in a different thread
        while True:
            if self.capture.isOpened():
                (self.status, self.frame) = self.capture.read()

    def show_frame(self):
        # Convert to grayscale and display frames
        if self.status:
            self.gray = cv2.cvtColor(self.frame, cv2.COLOR_BGR2GRAY)
            cv2.imshow('grayscale frame', self.gray)

        # Press 'q' on keyboard to stop recording
        key = cv2.waitKey(1)
        if key == ord('q'):
            self.capture.release()
            self.output_video.release()
            cv2.destroyAllWindows()
            exit(1)

    def save_frame(self):
        # Save grayscale frame into video output file
        self.output_video.write(self.gray)

if __name__ == '__main__':
    video_src = 'Your video stream link!'
    video_stream_widget = VideoToGrayscaleWidget(video_src)
    while True:
        try:
            video_stream_widget.show_frame()
            video_stream_widget.save_frame()
        except AttributeError:
            pass
like image 127
nathancy Avatar answered Sep 15 '25 21:09

nathancy