def robert(img):
    img1 = rgbToGray(img)
    img2 = rgbToGray(img)
    # img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    r, c = img1.shape
    # r_sunnzi = [[-1, -1], [1, 1]]
    r_sunnzi_x = [[-1, 0], [0, 1]]
    r_sunnzi_y = [[0, -1], [1, 0]]
    for x in range(r):
        for y in range(c):
            if (y + 2 <= c) and (x + 2 <= r):
                imgChild = img1[x:x + 2, y:y + 2]
                list_robert = r_sunnzi_x * imgChild
                img1[x, y] = abs(list_robert.sum())  # 求和加绝对值
    for x in range(r):
        for y in range(c):
            if (y + 2 <= c) and (x + 2 <= r):
                imgChild = img2[x:x + 2, y:y + 2]
                list_robert = r_sunnzi_y * imgChild
                img2[x, y] = abs(list_robert.sum())  # 求和加绝对值
    # 转uint8
    absX = convertScaleAbs(img1)
    absY = convertScaleAbs(img2)
    result = addWeighted(absX, 0.5, absY, 0.5, 0)
    # result = cv2.addWeighted(img1, 0.5, img2, 0.5, 0)
    # cv2.imshow("result",result)
    # cv2.waitKey(0)
    return result
Beispiel #2
0
def idealFilter(img, r, kind):
    '''
    理想滤波器
    :param img: 输入图像
    :param r: 滤波器半径
    :param kind: 滤波器类型
    :return: 滤波后的图像
    '''
    img = cv2.cvtColor(img, cv2.COLOR_RGBA2BGR)  # 四维转三维
    img = rgbToGray(img)  # 灰度化
    f = np.fft.fft2(img)  # 傅里叶变换
    fshift = np.fft.fftshift(f)  # 将低频部分移到中心
    # 取绝对值:将复数变化成实数
    # 取对数的目的为了将数据变化到0-255
    s1 = np.log(np.abs(fshift))
    # d1 = make_transform_matrix(r, fshift, s1)
    if kind == 0:  # 理想低通滤波
        d1 = make_transform_matrix(r, fshift, s1)
    elif kind == 3:  # 理想高通滤波
        d1 = 1 - make_transform_matrix(r, fshift, s1)
    img_d1 = np.abs(np.fft.ifft2(np.fft.ifftshift(fshift * d1)))
    img_d1 = img_d1 / img_d1.max()
    img_d1 = img_d1 * 255
    img_d1 = img_d1.astype(np.uint8)
    return img_d1
Beispiel #3
0
 def __call__(self, img):
     if self._kind == 0:
         img = rgbToGray(img)
     elif self._kind == 1:
         img = image_reverse(img)
     elif self._kind == 2:
         img = binarization(img)
     elif self._kind == 3:
         img = gammaTranform(self._c_value, self._γ_value, img)
     return img
def GaussianFilter(image, d, kind):
    image = cv2.cvtColor(image, cv2.COLOR_RGBA2BGR)
    image = rgbToGray(image)
    f = np.fft.fft2(image)
    fshift = np.fft.fftshift(f)
    s1 = np.log(np.abs(fshift))
    if kind == 2:
        d_matrix = make_transform_matrix(d, image, s1)
    elif kind == 5:
        d_matrix = 1 - make_transform_matrix(d, image, s1)
    img_d1 = np.abs(np.fft.ifft2(np.fft.ifftshift(fshift * d_matrix)))
    img_d1 = img_d1 / img_d1.max()
    img_d1 = img_d1 * 255
    img_d1 = img_d1.astype(np.uint8)
    return img_d1
def butterworthSelectFilter(image, d, n, W, kind):
    image = cv2.cvtColor(image, cv2.COLOR_RGBA2BGR)
    image = rgbToGray(image)  # 图像灰度化
    f = np.fft.fft2(image)  # 图像的傅里叶变换
    fshift = np.fft.fftshift(f)  # 将低频移动到中心
    s1 = np.log(np.abs(fshift))
    if kind == 1:  # 巴特沃斯带阻滤波器
        d_matrix = ButterworthBand(image, W, d, n)
    elif kind == 4:  # 巴特沃斯带通滤波器
        d_matrix = 1 - ButterworthBand(image, W, d, n)
    #  与模板相乘后再傅里叶逆变换
    img_d1 = np.abs(np.fft.ifft2(np.fft.ifftshift(fshift * d_matrix)))
    img_d1 = img_d1 / img_d1.max()
    img_d1 = img_d1 * 255
    img_d1 = img_d1.astype(np.uint8)
    return img_d1
def GaussianSelectFilter(image, d, W, kind):
    image = cv2.cvtColor(image, cv2.COLOR_RGBA2BGR)
    image = rgbToGray(image)
    f = np.fft.fft2(image)
    fshift = np.fft.fftshift(f)
    s1 = np.log(np.abs(fshift))
    if kind == 2:  # 高斯带阻滤波器
        # d_matrix = make_select_matrix(d,image,s1,W)
        d_matrix = GaussianBand(image, W, d)
    elif kind == 5:  # 高斯带通滤波器
        # d_matrix = 1-make_select_matrix(d,image,s1,W)
        d_matrix = 1 - GaussianBand(image, W, d)
    img_d1 = np.abs(np.fft.ifft2(np.fft.ifftshift(fshift * d_matrix)))
    img_d1 = img_d1 / img_d1.max()
    img_d1 = img_d1 * 255
    img_d1 = img_d1.astype(np.uint8)
    return img_d1
Beispiel #7
0
def laplacian_filter(img, K_size=3):
    H, W, C = img.shape
    gray = rgbToGray(img)
    # zero padding
    pad = K_size // 2
    out = zeros((H + pad * 2, W + pad * 2), dtype=float)
    out[pad:pad + H, pad:pad + W] = gray.copy().astype(float)
    tmp = out.copy()
    # laplacian kernle
    K = [[0., 1., 0.], [1., -4., 1.], [0., 1., 0.]]
    # filtering
    for y in range(H):
        for x in range(W):
            out[pad + y, pad + x] = sum(K * (tmp[y:y + K_size, x:x + K_size]))
    out = clip(out, 0, 255)
    out = out[pad:pad + H, pad:pad + W].astype(uint8)
    return out
Beispiel #8
0
def idealSelectFilter(img, r, W, kind):
    img = cv2.cvtColor(img, cv2.COLOR_RGBA2BGR)
    img = rgbToGray(img)
    # 傅里叶变换
    f = np.fft.fft2(img)
    # 将低频部分移到中心
    fshift = np.fft.fftshift(f)
    # 取绝对值:将复数变化成实数
    # 取对数的目的为了将数据变化到0-255
    s1 = np.log(np.abs(fshift))
    if kind == 0:  # 理想带阻滤波器
        d1 = make_select_matrix(r, fshift, s1, W)
    elif kind == 3:  # 理想带通滤波器
        d1 = 1 - make_select_matrix(r, fshift, s1, W)
    # 与模板相乘后再傅里叶逆变换
    img_d1 = np.abs(np.fft.ifft2(np.fft.ifftshift(fshift * d1)))
    img_d1 = img_d1 / img_d1.max()
    img_d1 = img_d1 * 255
    img_d1 = img_d1.astype(np.uint8)
    return img_d1
Beispiel #9
0
def idealNotchFilter(img, r, kind):
    img = cv2.cvtColor(img, cv2.COLOR_RGBA2BGR)
    img = rgbToGray(img)
    # 傅里叶变换
    f = np.fft.fft2(img)
    # 将低频部分移到中心
    fshift = np.fft.fftshift(f)
    # fshift = fshift.astype(np.uint8)
    # 取绝对值:将复数变化成实数
    # 取对数的目的为了将数据变化到0-255
    s1 = np.log(np.abs(fshift))
    if kind == 6:
        d1 = make_NotchFilter_matrix(r, fshift, s1)
    elif kind == 7:
        d1 = 1 - make_NotchFilter_matrix(r, fshift, s1)
    img_d1 = np.abs(np.fft.ifft2(np.fft.ifftshift(fshift * d1)))
    img_d1 = img_d1 / img_d1.max()
    img_d1 = img_d1 * 255
    img_d1 = img_d1.astype(np.uint8)
    return img_d1
def pseudoColorTrans(img, H, S, V, type):
    if img.shape == 4:
        img = cv2.cvtColor(img, cv2.COLOR_RGBA2BGR)
    img_gray = rgbToGray(img)
    if type == 0:
        img_color = cv2.applyColorMap(img_gray, cv2.COLORMAP_AUTUMN)
    elif type == 1:
        img_color = cv2.applyColorMap(img_gray, cv2.COLORMAP_BONE)
    elif type == 2:
        img_color = cv2.applyColorMap(img_gray, cv2.COLORMAP_JET)
    elif type == 3:
        img_color = cv2.applyColorMap(img_gray, cv2.COLORMAP_WINTER)
    elif type == 4:
        img_color = cv2.applyColorMap(img_gray, cv2.COLORMAP_RAINBOW)
    elif type == 5:
        img_color = cv2.applyColorMap(img_gray, cv2.COLORMAP_OCEAN)
    elif type == 6:
        img_color = cv2.applyColorMap(img_gray, cv2.COLORMAP_SUMMER)
    elif type == 7:
        img_color = cv2.applyColorMap(img_gray, cv2.COLORMAP_SPRING)
    elif type == 8:
        img_color = cv2.applyColorMap(img_gray, cv2.COLORMAP_COOL)
    elif type == 9:
        img_color = cv2.applyColorMap(img_gray, cv2.COLORMAP_HSV)
    elif type == 10:
        img_color = cv2.applyColorMap(img_gray, cv2.COLORMAP_PINK)
    elif type == 11:
        img_color = cv2.applyColorMap(img_gray, cv2.COLORMAP_HOT)
    img_color = hsvProcess(img_color, H, S, V)
    return img_color


# img_gray = cv2.imread("../pic/beach.png",cv2.IMREAD_GRAYSCALE)
# img_color = cv2.applyColorMap(img_gray,cv2.COLORMAP_JET)
# img = cv2.imread('../pic/beach.png')
# img_gray = pseudoColorTrans(img,type)
# cv2.imshow('img_color',img_gray)
# cv2.waitKey(0)
# cv2.imshow('img_color',img_color)
# cv2.waitKey(0)
def butterworthFilter(image, d, n, kind):
    '''
    巴特沃斯低通滤波器
    :param image: 输入图像
    :param d: 滤波半径
    :param n: 阶数
    :return:
    '''
    image = cv2.cvtColor(image, cv2.COLOR_RGBA2BGR)
    image = rgbToGray(image)
    f = np.fft.fft2(image)
    fshift = np.fft.fftshift(f)
    s1 = np.log(np.abs(fshift))
    if kind == 1:
        d_matrix = make_transform_matrix(image, d, s1, n)
    elif kind == 4:
        d_matrix = 1 - make_transform_matrix(image, d, s1, n)
    img_d1 = np.abs(np.fft.ifft2(np.fft.ifftshift(fshift * d_matrix)))
    # 高通滤波
    # img_d1 = np.abs(np.fft.ifft2(np.fft.ifftshift(fshift * (1-d_matrix))))
    img_d1 = img_d1 / img_d1.max()
    img_d1 = img_d1 * 255
    img_d1 = img_d1.astype(np.uint8)
    return img_d1
Beispiel #12
0
def prewitt_filter(img, K_size=3):
    gray = rgbToGray(img)
    if len(img.shape) == 3:
        H, W, C = img.shape
    else:
        img = expand_dims(img, axis=-1)
        H, W, C = img.shape
    # 填充0
    pad = K_size // 2
    out = zeros((H + pad * 2, W + pad * 2), dtype=float)
    out[pad:pad + H, pad:pad + W] = gray.copy().astype(float)
    tmp = out.copy()
    out_v = out.copy()
    out_h = out.copy()
    ## prewitt 水平方向的核
    Kv = [[-1., -1., -1.], [0., 0., 0.], [1., 1., 1.]]
    ## prewitt 竖直方向的核
    Kh = [[-1., 0., 1.], [-1., 0., 1.], [-1., 0., 1.]]
    # filtering
    for y in range(H):
        for x in range(W):
            out_v[pad + y,
                  pad + x] = sum(Kv * (tmp[y:y + K_size, x:x + K_size]))
            out_h[pad + y,
                  pad + x] = sum(Kh * (tmp[y:y + K_size, x:x + K_size]))
    out_v = clip(out_v, 0, 255)
    out_h = clip(out_h, 0, 255)
    out_v = out_v[pad:pad + H, pad:pad + W].astype(uint8)
    out_h = out_h[pad:pad + H, pad:pad + W].astype(uint8)
    dst = addWeighted(out_v, 0.5, out_h, 0.5, 0)
    return dst


# 读取图像
# img = cv2.imread('../pic/boy.png')
# prewitt_filter(img)