Exemplo n.º 1
0
__author__ = "huzhenhong@2020-03-27"

import cv2 as cv
import numpy as np
from include import tool


@tool.time_cost_calc
def reduce_color(src):
    """

    :param src:
    :return:
    """
    dst = np.copy(src)

    dst = dst // 64 * 64 + 32

    return dst


src = cv.imread("../test.jpg")
assert src.shape

dst = reduce_color(src)
tool.cvshow("reduce_color", dst)

cv.waitKey()
cv.destroyAllWindows()
Exemplo n.º 2
0
    else:
        kernel = [[0, 0, 0], [-1, 1, 0], [0, 0, 0]]

    h, w = src.shape
    # padding
    dst = np.zeros((h + 2, w + 2), dtype=np.float)
    dst[1:1 + h, 1:1 + w] = src.copy().astype(np.float)

    for y in range(h):
        for x in range(w):

            dst[y][x] = np.sum(kernel * dst[y:y + 3, x:x + 3])

    dst = dst[:h, :w]
    dst = np.clip(dst, 0, 255).astype(np.uint8)

    return dst


src = cv.imread('../lena.jpg')
assert src.shape

gray = cv.cvtColor(src, cv.COLOR_BGR2GRAY)

vertical = differential_filter(gray)
horizontial = differential_filter(gray, True)
tool.cvshow("manual", np.hstack((gray, vertical, horizontial)))

cv.waitKey()
cv.destroyAllWindows()
Exemplo n.º 3
0
    h, w = src.shape

    # 边界补零
    padding = k_size // 2
    dst = np.zeros((h + padding * 2, w + padding * 2), dtype=np.float)
    dst[padding:h + padding, padding:w + padding] = src.copy().astype(np.float)

    # 滤波
    for y in range(h):
        for x in range(w):
            max_val = np.max(dst[y:y + k_size, x:x + k_size])
            min_val = np.min(dst[y:y + k_size, x:x + k_size])
            dst[y, x] = max_val - min_val

    # 剪切
    dst = dst[0:h, 0:w]

    return dst.astype(np.uint8)


src = cv.imread('../lena.jpg')
assert src.shape

gray = cv.cvtColor(src, cv.COLOR_BGR2GRAY)

manual = maxmin_filter_manual(gray, 3)
tool.cvshow('maxmin_filter_manual', np.hstack((gray, manual)))

cv.waitKey()
cv.destroyAllWindows()
Exemplo n.º 4
0
    dst = np.zeros_like(src)
    dst[src > 128] = 255

    return dst


@tool.time_cost_calc
def thresholding_by_threshold(src):
    """

    :param src:
    :return:
    """
    thresh, dst = cv.threshold(src, 128, 255, cv.THRESH_BINARY)
    return dst


src = cv.imread("lena.jpg")
assert src.shape

gray = cv.cvtColor(src, cv.COLOR_BGR2GRAY)

dst1 = thresholding_by_manual(gray)
tool.cvshow("thresholding_by_manual", dst1)

dst2 = thresholding_by_threshold(gray)
tool.cvshow("thresholding_by_threshold", dst2)

cv.waitKey()
cv.destroyAllWindows()
Exemplo n.º 5
0
    return dst.astype(np.uint8)


@tool.time_cost_calc
def mean_filter_opencv(src, ksize=3):
    """

    :param src:
    :param ksize:
    :return:
    """
    return cv.blur(src, (ksize, ksize))


src = cv.imread('../lena.jpg')
assert src.shape



manual = mean_filter_manual(src, 3)
tool.cvshow('mean_filter_manual', np.hstack((src, manual)))

opencv = mean_filter_opencv(src, 3)
tool.cvshow('mean_filter_opencv', np.hstack((src, opencv)))

cv.waitKey()
cv.destroyAllWindows()



Exemplo n.º 6
0
    :param src:
    :return:
    """
    dst = src.copy()

    h, w, ch = src.shape

    if ksize > h or ksize > w or ksize < 1:
        return dst

    h1 = int(h / ksize)
    w1 = int(w / ksize)

    for y in range(h1):
        for x in range(w1):
            for c in range(ch):
                dst[y*ksize : (y+1)*ksize, x*ksize : (x+1)*ksize, c] \
                    = np.mean(dst[y*ksize : (y+1)*ksize, x*ksize : (x+1)*ksize, c]).astype(np.uint8)

    return dst


src = cv.imread('../test.jpg')
assert src.shape

manual = average_pooling_mauanl(src)
tool.cvshow('average_pooling_mauanl', manual)

cv.waitKey()
cv.destroyAllWindows()
Exemplo n.º 7
0
    :param src:
    :return:
    """
    dst = src.copy()

    h, w, ch = src.shape

    if ksize > h or ksize > w or ksize < 1:
        return dst

    h1 = int(h / ksize)
    w1 = int(w / ksize)

    for y in range(h1):
        for x in range(w1):
            for c in range(ch):
                dst[y*ksize : (y+1)*ksize, x*ksize : (x+1)*ksize, c] \
                    = np.max(dst[y*ksize : (y+1)*ksize, x*ksize : (x+1)*ksize, c])

    return dst


src = cv.imread('../test.jpg')
assert src.shape

manual = max_pooling_mauanl(src)
tool.cvshow('max_pooling_mauanl', manual)

cv.waitKey()
cv.destroyAllWindows()
Exemplo n.º 8
0
def prewitt_filter_opencv(src, b_horizontial=False):
    """

    :param src:
    :param b_horizontial:
    :return:
    """
    if b_horizontial:
        kernel = np.array([[1, 0, -1], [1, 0, -1], [1, 0, -1]])
    else:
        kernel = np.array([[1, 1, 1], [0, 0, 0], [-1, -1, -1]])

    return cv.filter2D(src, -1, kernel)


src = cv.imread('../lena.jpg')
assert src.shape

gray = cv.cvtColor(src, cv.COLOR_BGR2GRAY)

vertical_manual = prewitt_filter_maual(gray)
horizontial_manual = prewitt_filter_maual(gray, True)
tool.cvshow("manual", np.hstack((gray, vertical_manual, horizontial_manual)))

vertical_opencv = prewitt_filter_opencv(gray)
horizontial_opencv = prewitt_filter_opencv(gray, True)
tool.cvshow("opencv", np.hstack((gray, vertical_opencv, horizontial_opencv)))

cv.waitKey()
cv.destroyAllWindows()
Exemplo n.º 9
0
    return dst


@tool.time_cost_calc
def emboss_filter_opencv(src):
    """

    :param src:
    :return:
    """
    kernel = np.array([[-2, -1, 0], [-1, 1, 1], [0, 1, 2]])

    return cv.filter2D(src, -1, kernel)


src = cv.imread('../lena.jpg')
# src = cv.imread('../imori.jpg')
assert src.shape

gray = cv.cvtColor(src, cv.COLOR_BGR2GRAY)

manual = emboss_filter_maual(gray)

opencv = emboss_filter_opencv(gray)

tool.cvshow("opencv", np.hstack((gray, manual, opencv)))

cv.waitKey()
cv.destroyAllWindows()
Exemplo n.º 10
0
@tool.time_cost_calc
def hsv2bgr_by_cvtcolor(src):
    """

    :param src:
    :return:
    """
    return cv2.cvtColor(src, cv2.COLOR_HSV2BGR)


src = cv2.imread("../test.jpg", cv2.IMREAD_COLOR)
assert src.shape

hsv_manual = bgr2hsv_by_manual(src)
tool.cvshow("bgr2hsv_by_manual", hsv_manual)

inverse_hue = inverse_hue_by_manual(hsv_manual)
tool.cvshow("inverse_hue_by_manual", inverse_hue)

bgr_manual = hsv2bgr_by_manual(inverse_hue)
tool.cvshow("hsv2bgr_by_manual", bgr_manual)

##################################

hsv_opencv = bgr2hsv_by_cvtcolor(src)
tool.cvshow("bgr2hsv_by_cvtcolor", hsv_opencv)

inverse_hue_opencv = inverse_hue_for_opencv(hsv_opencv)
tool.cvshow("inverse_hue_for_opencv", inverse_hue_opencv)
Exemplo n.º 11
0
    channels = cv2.split(src)
    channels[0], channels[2] = channels[2], channels[0]
    return cv2.merge(channels)


@tool.time_cost_calc
def switch_by_mixchannels(src):
    """

    :param src:
    :return:
    """
    dst = np.zeros_like(src)
    cv2.mixChannels([src], [dst], fromTo=[0, 2, 1, 1, 2, 0])
    return dst


src = cv2.imread("lena.jpg")

dst1 = switch_by_manual(src)
tool.cvshow("switch_by_manual", np.hstack((src, dst1)))

dst2 = switch_by_split_and_merge(src)
tool.cvshow("switch_by_split_and_merge", np.hstack((src, dst2)))

dst3 = switch_by_mixchannels(src)
tool.cvshow("switch_by_mixChannels", np.hstack((src, dst3)))

cv2.waitKey()
cv2.destroyAllWindows()
Exemplo n.º 12
0
    """

    :param src:
    :return:
    """
    dst = 0.2126 * src[:, :, 2] + 0.7152 * src[:, :, 1] + 0.072 * src[:, :, 0]
    return dst.astype(np.uint8)


@tool.time_cost_calc
def scale_by_cvtcolor(src):
    """

    :param src:
    :return:
    """
    return cv.cvtColor(src, cv.COLOR_BGR2GRAY)


src = cv.imread('lena.jpg')
assert src.shape

dst1 = scale_by_manual(src)
tool.cvshow('scale_by_manual', dst1)

dst2 = scale_by_cvtcolor(src)
tool.cvshow('scale_by_cvtcolor', dst2)

cv.waitKey()
cv.destroyAllWindows()
Exemplo n.º 13
0
            best_thresh = thresh

    dst[src > best_thresh] = 255

    return dst


@tool.time_cost_calc
def otsu_thresholding_by_threshold(src):
    """

    :param src:
    :return:
    """
    _, dst = cv.threshold(src, 0, 255, cv.THRESH_OTSU)
    return dst


src = cv.imread("lena.jpg")
assert src.shape

gray = cv.cvtColor(src, cv.COLOR_BGR2GRAY)

dst1 = otsu_thresholding_by_manual(gray)
tool.cvshow("otsu_thresholding_by_manual", np.hstack((gray, dst1)))

dst2 = otsu_thresholding_by_threshold(gray)
tool.cvshow("otsu_thresholding_by_threshold", np.hstack((gray, dst2)))

cv.waitKey()
cv.destroyAllWindows()