dailylog 4-17-20

1 minute read

# CLICK ON OUTPUT and THEN ctrl C to close 

from __future__ import print_function
import sys
import time
import numpy as np
import cv2 as cv

def is_grayscale(my_image):
    return len(my_image.shape) < 3
def saturated(sum_value):
    # STARTING POINT:
    # if sum_value > 255:
    if sum_value > 100:
        sum_value = 255
    # STARTING POINT:
    # if sum_value < 0:
    if sum_value < 50:
        sum_value = 0
    return sum_value
def make_mask(my_image):
    if is_grayscale(my_image):
        height, width = my_image.shape
    else:
        my_image = cv.cvtColor(my_image, cv.CV_8U)
        height, width, n_channels = my_image.shape
    result = np.zeros(my_image.shape, my_image.dtype)
    
    for j in range(1, height - 1):
        for i in range(1, width - 1):
            if is_grayscale(my_image):
                sum_value = 5 * my_image[j, i] - my_image[j + 1, i] - my_image[j - 1, i] \
                            - my_image[j, i + 1] - my_image[j, i - 1]
                result[j, i] = saturated(sum_value)
            else:
                for k in range(0, n_channels):
                    sum_value = 5 * my_image[j, i, k] - my_image[j + 1, i, k]  \
                                - my_image[j - 1, i, k] - my_image[j, i + 1, k]\
                                - my_image[j, i - 1, k]
                    result[j, i, k] = saturated(sum_value)
    
    return result


def main(argv):
    filename = 'lena.jpg'
    img_codec = cv.IMREAD_COLOR
    if argv:
        filename = sys.argv[1]
        if len(argv) >= 2 and sys.argv[2] == "G":
            img_codec = cv.IMREAD_GRAYSCALE
    src = cv.imread(cv.samples.findFile(filename), img_codec)
    cv.namedWindow("Input", cv.WINDOW_AUTOSIZE)
    cv.namedWindow("Output", cv.WINDOW_AUTOSIZE)
    cv.imshow("Input", src)
    t = round(time.time())
    dst0 = make_mask(src)
    t = (time.time() - t)
    print("Time passed in seconds: %s" % t)
    cv.imshow("Output", dst0)
    cv.waitKey()
    cv.destroyAllWindows()
    return 0


if __name__ == "__main__":
    main(sys.argv[1:])