コード例 #1
0
# video io
from videoreader import VideoReader
from videowriter import VideoWriter

# OpenCV
import cv2

if __name__ == '__main__':
    parser = IOParser()
    reader = VideoReader(parser.input_file)
    writer = VideoWriter(parser.output_file, reader.fps, (reader.width, reader.height), color=False)

    '''
        OpenCV allows for you to easily switch between colorspaces using the cvtColor method
        Note - no video players will be able to play a single channel grayscale video so each channel, R,G,B are 
        given the same intensity values to represent grayscale.
    '''

    for frame in reader:
        gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
        '''
        if we didn't specify that our VideoWriter is grayscale, we would have to convert back to BGR to output a 
        playable version.
        '''
        # to_bgr = cv2.cvtColor(gray, cv2.COLOR_GRAY2BGR)
        writer.write_frame(gray)

    writer.close()
    reader.close()
コード例 #2
0
    writer = VideoWriter(parser.output_file, reader.fps, (reader.width, reader.height))

    ''' 
        many image processing operations involve 'convolution', where a small matrix is applied to all pixels 
        in a frame and acts as a filter. For an excellent example, look here: http://setosa.io/ev/image-kernels/
        try to experiment with the kernel size and see what kind of output you get. 
        
        a 3x3 kernel looks like this 
          _____
         |1 1 1|   
         |1 1 1|  
         |1 1 1|  
          -----
        to perform a simple blur, you would multiple every element in the 3x3 kernel by 1/9 and set
        the center pixel (the anchor) to that value
    '''
    # the data type can be anything you want, but a 32 bit float allows for more precision
    kernel = np.ones((3, 3), dtype=np.float32)/9

    for frame in reader:
        ''' here we use the cv2.filter2D method to do the convolution. the -1 parameter specifies that the depth of the 
            destination array is the same as the source (HxWx3)
        '''
        blurred = cv2.filter2D(frame, -1, kernel)
        writer.write_frame(blurred)

    writer.close()
    reader.close()


コード例 #3
0
ファイル: sharpen.py プロジェクト: rochester-rcl/dhsi
from io_parser import IOParser

# video io
from videoreader import VideoReader
from videowriter import VideoWriter

# OpenCV
import cv2

# numpy
import numpy as np

if __name__ == '__main__':
    parser = IOParser()
    reader = VideoReader(parser.input_file)
    writer = VideoWriter(parser.output_file, reader.fps,
                         (reader.width, reader.height))
    ''' See 'blur.py' for an explanation of kernels. Below is an example of how you can create a custom kernel.
        You can play around with the numbers and see what you get.
    '''

    sharpen_kernel = np.array(([0, -1, 0], [-1, 5, -1], [0, -1, 0]),
                              dtype=np.float32)

    for frame in reader:
        sharpened = cv2.filter2D(frame, -1, sharpen_kernel)
        writer.write_frame(sharpened)

    writer.close()
    reader.close()
コード例 #4
0
if __name__ == '__main__':
    parser = IOParser()
    reader = VideoReader(parser.input_file)
    writer = VideoWriter(parser.output_file, reader.fps,
                         (reader.width, reader.height))

    # initialize an array WxHx1 to act as a placeholder for the other 2 channels
    black_channel = np.zeros((reader.height, reader.width),
                             dtype=np.uint8)  # unsigned 8-bit integer
    slice_size = math.ceil(reader.width / 4)

    for frame in reader:
        # by default OpenCV arranges its matrices in BGR colorspace
        b, g, r = cv2.split(frame)
        output_red = cv2.merge([black_channel, black_channel, r])
        output_green = cv2.merge([black_channel, g, black_channel])
        output_blue = cv2.merge([b, black_channel, black_channel])

        # using numpy array indexing, we can swap sections of the original frame with our processed ones
        # frame[:(keep the height dimension the same), slice_start:slice_end]
        frame[:, slice_size:slice_size *
              2] = output_red[:, slice_size:slice_size * 2]
        frame[:, slice_size * 2:slice_size *
              3] = output_green[:, slice_size * 2:slice_size * 3]
        frame[:, slice_size * 3:slice_size * 4] = output_blue[:, slice_size *
                                                              3:slice_size * 4]
        writer.write_frame(frame)

    writer.close()
    reader.close()
コード例 #5
0
ファイル: threshold.py プロジェクト: rochester-rcl/dhsi
from io_parser import IOParser

# video io
from videoreader import VideoReader
from videowriter import VideoWriter

# OpenCV
import cv2

if __name__ == '__main__':
    parser = IOParser()
    reader = VideoReader(parser.input_file)
    writer = VideoWriter(parser.output_file,
                         reader.fps, (reader.width, reader.height),
                         color=False)
    '''
        Thresholding, or "binarization" operations are important for simplifying images in order to do analysis on them -
        for instance in OCR 
    '''

    for frame in reader:
        # we need to pass a grayscale matrix to the threshold function
        gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
        # if a pixel value is above 200, make it white - otherwise make it black
        ret, thresh = cv2.threshold(gray, 200, 255, cv2.THRESH_BINARY)
        writer.write_frame(thresh)

    writer.close()
    reader.close()