Answers for "Python Convolution"

0

Python Convolution

# please use python 3.x
import numpy as np

# def convolve(image, kernel, bias, stride, padding):

"""
please implement convolution on an image
arguments:
    image: input color image,  numpy array of shape (in_height, in_width, in_channel)
    kernel: weigths, numpy array of shape (kernel_size, kernel_size, in_channel, out_channel)
    bias: biases, numpy array of shape (1,1,1,out_channel)
    stride: stride, scalar
    padding: the number of zero padding along image boundary, scalar
returns:
    result: results of convolution, numpy array of shape (out_height, out_width, out_channel)
"""

# return result

import matplotlib.pyplot as plt
import cv2
import os


def processImage(image):
    # image = cv2.imread(image)
    plt.imshow(image)
    image = cv2.cvtColor(src=image, code=cv2.COLOR_BGR2GRAY)
    return image


def convolve(image, kernel, bias, strides, padding):
    # Cross Correlation
    kernel = np.flipud(np.fliplr(kernel))

    # Gather Shapes of Kernel + Image + Padding
    xKernShape = kernel.shape[0]
    yKernShape = kernel.shape[1]
    xImgShape = image.shape[0]
    yImgShape = image.shape[1]

    # Shape of Output Convolution
    xOutput = int(((xImgShape - xKernShape + padding) / strides) + 1)
    yOutput = int(((yImgShape - yKernShape + padding) / strides) + 1)
    result = np.zeros((xOutput, yOutput))

    # Apply Equal Padding to All Sides
    if padding != 0:
        imagePadded = np.zeros((image.shape[0] + padding * 2, image.shape[1] + padding * 2))
        imagePadded[int(padding):int(-1 * padding), int(padding):int(-1 * padding)] = image
        print(imagePadded)
    else:
        imagePadded = image

    # Iterate through image
    for y in range(image.shape[1]):
        # Exit Convolution
        if y > image.shape[1] - yKernShape:
            break
        # Only Convolve if y has gone down by the specified Strides
        if y % strides == 0:
            for x in range(image.shape[0]):
                # Go to next row once kernel is out of bounds
                if x > image.shape[0] - xKernShape:
                    break
                try:
                    # Only Convolve if x has moved by the specified Strides
                    if x % strides == 0:
                        result[x, y] = (kernel * imagePadded[x: x + xKernShape, y: y + yKernShape]).sum()
                except:
                    break

    return result


# image = processImage('images.jpg')
# print(image.shape)
# plt.imshow(image)
# plt.show()
# kernel = np.array([[-1, -1, -1], [-1, 8, -1], [-1, -1, -1]])
# image_conv = convolve(image, kernel, 1, 1)


if __name__ == '__main__':

    kernel = np.array([[1, 1, 1], [1, 1, 1], [1, 1, 1]])

    video = cv2.VideoCapture(r'video.avi')
    try:
        if not os.path.exists('pet'):
            os.makedirs('pet')
    except OSError:
        print('Error')
    currentframe = 0
    while (True):
        ret, frame = video.read()

        if ret:
            image = processImage(frame)
            # name = './pet/frame' + str(currentframe) + '.jpg'
            # print('Captured...' + name)
            # cv2.imwrite(name, frame)
            cv2.imshow("original", frame)
            output = convolve(image, kernel, 0, 1, 0)
            cv2.imshow("convert", output)
            if cv2.waitKey(27) & 0xFF == ord('q'):
                break

        else:
            break
    video.release()
    cv2.destroyAllWindows()

    # Grayscale Image
    image = processImage('images.jpg')

    # Edge Detection Kernel

    # Convolve and Save Output
    # output = convolve(image, kernel, 0, 1, 15)
    # plt.imshow(output)
    # plt.show()
    # cv2.imwrite('2DConvolved.jpg', output)
Posted by: Guest on September-30-2021

Python Answers by Framework

Browse Popular Code Answers by Language