图像噪声滤除,增加

文章目录

  • 图像噪声滤除,增加
    • 灰度图像
    • 彩色图像
    • 图像仿射变换

图像噪声滤除,增加

灰度图像

import cv2
import numpy as np
from matplotlib import pyplot as plt
from skimage import util,color
import math
import random
import datetime
import pywt

## for color image show with plt
def img_plt(img):
    b,g,r = cv2.split(img)
    img = cv2.merge([r, g, b])
    return img
######  add salt & peckle noise
def sp_noise(img,prob=0.02):
    dst_img = np.array(img,np.uint8)
    row = img.shape[0]
    col = img.shape[1]
    mn = row*col
    rp_num = math.floor(row*col*prob)
    rp = random.sample(range(0, mn), rp_num)
    for i in range(rp_num):
        pixel_v = 255 * np.random.randint(0, 2, size=1)
        m = math.floor(rp[i]/col)
        n = rp[i]%col
        if len(img.shape)>2:
            dst_img[m,n,:] = pixel_v
        else:
            dst_img[m,n] = pixel_v
    return dst_img
###### add gaussian noise
def gauss_noise(img, mean=0, var=0.001):
    image = np.array(img/255, dtype=float)
    noise = np.random.normal(mean, var ** 0.5, img.shape)
    dst_img = image + noise
    if dst_img.min() < 0:
        low_clip = -1.
    else:
        low_clip = 0.
    dst_img = np.clip(dst_img, low_clip, 1.0)
    dst_img = np.uint8(dst_img*255)
    return dst_img

#####  Inverse modulation and mean filter
###    the parameters' meaning are:first-img data, second-K
###    third-Statistics area size
def Inverse_modulation(img,k,h1,v1):
    r = img.shape[0]
    c = img.shape[1]
    h = math.floor(h1 / 2)  ## threshold of row
    v = math.floor(v1 / 2)  ## threshold of Column
    dst_img = np.array(img)
    for i in range(h,r-h):
        for j in range(v,c-v):
            if len(img.shape)==2:
                sum_k1 = np.sum(np.float32(img[i-h:i+h+1,j-v:j+v+1]) **(k+1))
                sum_k = np.sum(np.float32(img[i - h:i + h + 1, j - v:j + v + 1]) ** (k))
                dst_img[i][j] =abs(sum_k1/sum_k)
            else:
                for t in range(img.shape[3]):
                    sum_k1 = np.sum(np.float32(img[i-h:i+h+1,j-v:j+v+1,t]) **(k+1))
                    sum_k = np.sum(np.float32(img[i - h:i + h + 1, j - v:j + v + 1,t]) ** (k))
                    dst_img[i,j,t] =abs(sum_k1/sum_k)
    return dst_img


# ########## noise addition and filtering
# if __name__ == '__main__':
#     img = cv2.imread('lena-color.jpg',-1)
#     img = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
#     noise_sp_img = sp_noise(img,0.01)
#     noise_gs_img = gauss_noise(img)
#     # img = img_plt(img)
#     plt.subplot(331), plt.imshow(img,cmap='gray'), plt.title('Original'),plt.xticks([]), plt.yticks([])
#     # add s&p noise
#     # noise_sp_img = img_plt(noise_sp_img)
#     plt.subplot(332), plt.imshow(noise_sp_img,cmap='gray'), plt.title('s&p'),plt.xticks([]), plt.yticks([])
#     # add gaussian noise
#     # noise_gs_img = img_plt(noise_gs_img)
#     plt.subplot(333),plt.imshow(noise_gs_img,cmap='gray'),plt.title('gaussian'),plt.xticks([]), plt.yticks([])
#
#     # noise_gs_img=util.random_noise(img,mode='s&p')
#     # noise_sp_img=noise_gs_img*255  #由于输出是[0,1]的浮点型,先转成灰度图(我的输入就是灰度图)
#     # noise_sp_img=noise_gs_img.astype(np.int)   #再变成整型数组
#     # cv2.imwrite(os.path.join(save_dir,image_name),noise_sp_img)  #保存到新建的文件夹
#
#     # the fft result of original image
#     f2 = np.fft.fft2(img)
#     fshift = np.fft.fftshift(f2)
#     original_spectrum = 20*np.log(np.abs(fshift))
#     plt.subplot(334),plt.imshow(original_spectrum,cmap='gray'),plt.title('original_spectrum')
#     plt.xticks([]), plt.yticks([])
#     # the fft result of the image with s&p noise
#     f2 = np.fft.fft2(noise_sp_img)
#     fshift = np.fft.fftshift(f2)
#     sp_spectrum = 20*np.log(np.abs(fshift))
#     plt.subplot(335),plt.imshow(sp_spectrum,cmap='gray'),plt.title('s&p_spectrum')
#     plt.xticks([]), plt.yticks([])
#
#     # the fft result of the image with gaussian noise
#     f2 = np.fft.fft2(noise_gs_img)
#     fshift = np.fft.fftshift(f2)
#     gaussian_spectrum = 20*np.log(np.abs(fshift))
#     plt.subplot(336),plt.imshow(gaussian_spectrum,cmap='gray'),plt.title('gaussian_spectrum')
#     plt.xticks([]), plt.yticks([])
#     #
#     # #####  use the Inverse modulation
#     img_inver = Inverse_modulation(img,1,5,5)
#     noise_sp_img = Inverse_modulation(noise_sp_img,1,5,5)
#     noise_gs_img = Inverse_modulation(noise_gs_img,1, 5, 5)
#     plt.subplot(337),plt.imshow(img_inver,cmap='gray'),plt.title('orig_modul')
#     plt.xticks([]), plt.yticks([])
#     plt.subplot(338),plt.imshow(noise_sp_img,cmap='gray'),plt.title('sp_modul')
#     plt.xticks([]), plt.yticks([])
#     plt.subplot(339), plt.imshow(noise_gs_img, cmap='gray'), plt.title('gs_modul')
#     plt.xticks([]), plt.yticks([])
#     plt.show()



# 仿真运动模糊
# 第一个参数为图像大小,
# 第二个参数为模糊的角度,
# 第三参数为运动像素点个数
def motion_process(image_size, motion_angle, motion_dis):
    PSF = np.zeros(image_size)
    x_center = (image_size[0] - 1) / 2
    # y_center = (image_size[0] - 1) / 2
    slope_tan = math.tan(motion_angle * math.pi / 180)
    slope_cot = 1 / slope_tan
    if slope_tan <= 1:
        for i in range(motion_dis):
            offset = round(i * slope_tan)
            if int(x_center + offset)<image_size[0] and int(x_center - offset)>0:
                PSF[int(x_center + offset), int(x_center - offset)] = 1
        return PSF / PSF.sum()
    else:
        for i in range(motion_dis):
            offset = round(i * slope_cot)
            if int(x_center + offset) < image_size[0] and int(x_center - offset) > 0:
                PSF[int(x_center - offset), int(x_center + offset)] = 1
        return PSF / PSF.sum()


# 对图片进行运动模糊
def make_blurred(input, PSF, eps):
    input_fft = np.fft.fft2(input)  # 进行二维数组的傅里叶变换
    PSF_fft = np.fft.fft2(PSF) + eps
    blurred = np.fft.ifft2(input_fft * PSF_fft)
    blurred = np.abs(np.fft.fftshift(blurred))
    return blurred


def inverse(input, PSF, eps):  # 逆滤波
    input_fft = np.fft.fft2(input)
    PSF_fft = np.fft.fft2(PSF) + eps  # 噪声功率,这是已知的
    result = np.fft.ifft2(input_fft / PSF_fft)  # 计算F(u,v)的傅里叶反变换
    result = np.abs(np.fft.fftshift(result))
    return result


def wiener(input, PSF, eps, K=0.01):  # 维纳滤波,K=0.01
    input_fft = np.fft.fft2(input)
    PSF_fft = np.fft.fft2(PSF) + eps
    PSF_fft_1 = np.conj(PSF_fft) / (np.abs(PSF_fft) ** 2 + K)
    result = np.fft.ifft2(input_fft * PSF_fft_1)
    result = np.abs(np.fft.fftshift(result))
    return result

######### inverse filter and wiener filter
if __name__ == '__main__':
    image = cv2.imread('lena-color.jpg')
    image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    img_h = image.shape[0]
    img_w = image.shape[1]
    plt.figure(1)
    plt.xlabel("Original Image")
    plt.gray()
    plt.imshow(image)  # 显示原图像

    plt.figure(2)
    plt.gray()
    # 进行运动模糊处理
    PSF = motion_process((img_h, img_w), 60,15)
    blurred = np.abs(make_blurred(image, PSF, 1e-3))
    plt.subplot(231),plt.xlabel("Motion blurred"),plt.imshow(blurred)
    # 逆滤波
    result = inverse(blurred, PSF, 1e-3)
    plt.subplot(232),plt.xlabel("inverse deblurred"),plt.imshow(result)
    # 维纳滤波
    result = wiener(blurred, PSF, 1e-3)
    plt.subplot(233),plt.xlabel("wiener deblurred(k=0.01)"),plt.imshow(result)

    # 添加噪声,standard_normal产生随机的函数
    blurred_noisy = blurred + 0.1 * blurred.std() * \
                    np.random.standard_normal(blurred.shape)
    plt.subplot(234),plt.xlabel("motion & noisy blurred")
    plt.imshow(blurred_noisy)  # 显示添加噪声且运动模糊的图像
    # 对添加噪声的图像进行逆滤波
    result = inverse(blurred_noisy, PSF, 0.1 + 1e-3)
    plt.subplot(235),plt.xlabel("inverse deblurred"),plt.imshow(result)
    # 对添加噪声的图像进行维纳滤波
    result = wiener(blurred_noisy, PSF, 0.1 + 1e-3)
    plt.subplot(236),plt.xlabel("wiener deblurred(k=0.01)"),plt.imshow(result)
    plt.show()


彩色图像

import cv2
import numpy as np
from matplotlib import pyplot as plt
import math
from skimage import util,color
import random
import datetime
import pywt

## for color image show with plt
def img_plt(img):
    b,g,r = cv2.split(img)
    img = cv2.merge([r, g, b])
    return img
def img_translation(img,tx,ty):
    dst_img = np.zeros((img.shape),dtype='uint8')
    for i in range(img.shape[0]):
        for j in range(img.shape[1]):
            if i+tx<dst_img.shape[0] and j+ty<dst_img.shape[1]:
               dst_img[i+tx][j+ty] = img[i][j]
    return dst_img
def img_resize(img,sx,sy):
    if len(img.shape)<=2:
        dst_img = np.zeros((round(img.shape[0]*sx),round(img.shape[1]*sy)),dtype='uint8')
    else:
        dst_img = np.zeros((round(img.shape[0] * sx), round(img.shape[1] * sy),img.shape[2]), dtype='uint8')
    for i in range(dst_img.shape[0]):
        for j in range(dst_img.shape[1]):
            if round(i/sx) < img.shape[0] and round(j/sy) < img.shape[1]:
                dst_img[i][j] = img[round(i/sx)][round(j/sy)]
    return dst_img
def img_rotation(img,th):
    dst_img = np.zeros((img.shape), dtype='uint8')
    row = img.shape[0]
    col = img.shape[1]
    # x = x'cos(theta)-y'sin(theta) + m/2*(1-cos(theta))+n/2*sin(theta)
    # y = x'sin(theta)+y'cos(theta) + n/2*(1-cos(theta))-m/2*sin(theta)
    for i in range(row):
        for j in range(col):
            m = i*math.cos(th)-j*math.sin(th)+row/2*(1-math.cos(th))+col/2*math.sin(th)
            n = i*math.sin(th)+j*math.cos(th)+col/2*(1-math.cos(th))-row/2*math.sin(th)
            if m >=0 and m < row and n >=0 and n<col:
                dst_img[i][j] = img[math.floor(m)][math.floor(n)]
    return dst_img
##########   color image process  ###########
###   basic process transform   ###
# if __name__ == '__main__':
    # img = cv2.imread('lena-color.jpg')
    # # img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    # if len(img.shape) > 2:
    #     m,n,t = img.shape
    # else:
    #     m,n = img.shape
    # Resize_res = cv2.resize(img, None, fx=2, fy=0.5, interpolation=cv2.INTER_CUBIC)
    # M = cv2.getRotationMatrix2D((m / 2, n / 2), 45, 1)
    # Rotation_res = cv2.warpAffine(img, M, (n, m))
    # Translation_res = cv2.warpAffine(img, np.float32([[1, 0, 50], [0, 1, 100]]), (n, m))
    # my_trans = img_translation(img,100,50)
    # my_resiz = img_resize(img,0.5,2)
    # my_rotation = img_rotation(img,-45)
    # if len(img.shape) > 2:
    #     img = img_plt(img)
    #     Resize_res = img_plt(Resize_res)
    #     Rotation_res = img_plt(Rotation_res)
    #     Translation_res = img_plt(Translation_res)
    #     my_trans = img_plt(my_trans)
    #     my_resiz = img_plt(my_resiz)
    #     my_rotation = img_plt(my_rotation)

    #     plt.subplot(241),plt.imshow(img)
    #     plt.title('original'), plt.xticks([]), plt.yticks([])
    #     plt.subplot(242),plt.imshow(Resize_res)
    #     plt.title('resize'), plt.xticks([]), plt.yticks([])
    #     plt.subplot(243),plt.imshow(Rotation_res)
    #     plt.title('rotation'), plt.xticks([]), plt.yticks([])
    #     plt.subplot(244),plt.imshow(Translation_res)
    #     plt.title('translation'), plt.xticks([]), plt.yticks([])
    #     plt.subplot(246),plt.imshow(my_resiz)
    #     plt.title('my resize'), plt.xticks([]), plt.yticks([])
    #     plt.subplot(247),plt.imshow(my_rotation)
    #     plt.title('my rotation'), plt.xticks([]), plt.yticks([])
    #     plt.subplot(248),plt.imshow(my_trans)
    #     plt.title('my resize'), plt.xticks([]), plt.yticks([])
    #     plt.show()

### if the img is color, then process each channel
def template_conv(img, temp):
    h1 = temp.shape[0]
    v1 = temp.shape[1]
    new_img = np.zeros(img.shape,dtype='uint8')
    if len(img.shape) != len(temp.shape):
        if len(img.shape) > 2 and len(temp.shape)<=2:
            temp_tmp = np.zeros(((h1,v1,img.shape[2])))
            for k in range(temp_tmp.shape[2]):
                temp_tmp[:,:,k] = temp
            temp = temp_tmp
        else:
            print("error: the img or template is not valuable")
            return
    if len(img.shape) != len(temp.shape):
        print("error: the img or template is not valuable")
        return
    if len(img.shape)>2:
        for k in range(img.shape[2]):
            new_img[:,:,k] = template_convolution(img[:,:,k],temp[:,:,k])
    else:
        new_img = template_convolution(img, temp)
    return new_img
def template_convolution(img,temp):
    r,c = img.shape
    h1,v1 = temp.shape
    h = math.floor(h1 / 2)  ## threshold of row
    v = math.floor(v1 / 2)  ## threshold of Column
    N = sum(sum(temp))
    dst_img = np.zeros((r,c))
    for i in range(h,r-h):
        for j in range(v,c-v):
            if N !=0:
                dst_img[i][j] = abs(np.sum(img[i-h:i+h+1,j-v:j+v+1] * temp))/N
            else:
                dst_img[i][j] = abs(np.sum(img[i-h:i+h+1,j-v:j+v+1] * temp))
    return dst_img

if __name__ == '__main__':
    img = cv2.imread('lena-color.jpg')
    # img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    if len(img.shape) > 2:
        m,n,t = img.shape
    else:
        m,n = img.shape
    t_11 = np.ones((11,11))
    t_Gaussian = np.array([[1,4,7,4,1],[4,16,26,16,4],[7,26,41,26,7],[4,16,26,16,4],[1,4,5,4,1]])
    t_Lap = np.array([[-1,-1,-1],[-1,8,-1],[-1,-1,-1]])
    new_img11 = template_conv(img,t_11)
    new_imgG = template_conv(img,t_Gaussian)
    new_imgL = template_conv(img,t_Lap)

    new_img11 = img_plt(new_img11)
    new_imgG = img_plt(new_imgG)
    new_imgL = img_plt(new_imgL)

    plt.subplot(131),plt.imshow(new_img11)
    plt.title('Conv 11*11 Average'), plt.xticks([]), plt.yticks([])
    plt.subplot(132),plt.imshow(new_imgG)
    plt.title('Conv Gaussian Average'), plt.xticks([]), plt.yticks([])
    plt.subplot(133),plt.imshow(new_imgL)
    plt.title('Conv Laplace sharp'), plt.xticks([]), plt.yticks([])
    plt.show()

# if __name__ == '__main__':
# #     img = cv2.imread('lena-color.jpg')
# #     img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# #     if len(img.shape) > 2:
# #         m,n,t = img.shape
# #     else:
# #         m,n = img.shape
# #     ### fft
# #     f = np.fft.fft2(img)
# #     fshift = np.fft.fftshift(f)
# #     np_spectrum = 20 * np.log(np.abs(fshift))
# #     ### inverse fft
# #     ishift = np.fft.ifftshift(fshift)
# #     iimg = np.fft.ifft2(ishift)
# #     iimg = np.abs(iimg)
# #
# #     ### low high pass filter ...
# #     fm = fshift.shape[0]
# #     fn = fshift.shape[1]
# #     dm, dn = fm/4, fn/4
    # high pass filter
    # high_mask = np.ones(img.shape,np.uint8)
    # high_mask[int(m/2-dm/2):int(m/2+dm/2),int(n/2-dn/2):int(n/2+dn/2)] = 0
    # # low pass filter
    # low_mask = np.zeros(img.shape,np.uint8)
    # low_mask[int(m/2-dm/2):int(m/2+dm/2),int(n/2-dn/2):int(n/2+dn/2)] = 1 #低通滤波
    # # Band pass filter
    # mask1 = np.ones(img.shape, np.uint8)
    # mask1[int(m/2-dm/4):int(m/2+dm/4), int(n/2-dn/4):int(n/2+dn/4)] = 0
    # mask2 = np.zeros(img.shape, np.uint8)
    # mask2[int(m/2-dm/2):int(m/2+dm/2), int(n/2-dn/2):int(n/2+dn/2)] = 1
    # mask = mask1 * mask2
# #
# #
# #     low_pass_spectrum = np_spectrum*low_mask
# #     ### the general method
# #     low_pass_filter = fshift*low_mask
# #     ## ifft
# #     ishift = np.fft.ifftshift(low_pass_filter)
# #     iimg_low = np.fft.ifft2(ishift)
# #     iimg_low = np.abs(iimg_low)
# #
# #     ### FFT and IFFT of each channel
# #     # iimg_low = np.zeros(img.shape, np.uint8)
# #     # fshiftr = np.fft.fftshift(np.fft.fft2(img[:,:,0]))
# #     # fshiftg = np.fft.fftshift(np.fft.fft2(img[:, :, 1]))
# #     # fshiftb = np.fft.fftshift(np.fft.fft2(img[:, :, 2]))
# #     # low_pass_filter = fshiftr * low_mask[:, :, 0]
# #     # ishift= np.fft.ifftshift(low_pass_filter)
# #     # r = np.fft.ifft2(ishift)
# #     # r = r.astype(np.uint8)
# #     # low_pass_filter = fshiftg * low_mask[:, :, 1]
# #     # ishift = np.fft.ifftshift(low_pass_filter)
# #     # g = np.fft.ifft2(ishift)
# #     # g = g.astype(np.uint8)
# #     # low_pass_filter = fshiftb * low_mask[:, :, 2]
# #     # ishift = np.fft.ifftshift(low_pass_filter)
# #     # b = np.fft.ifft2(ishift)
# #     # b = b.astype(np.uint8)
# #
# #     # iimg_low = cv2.merge([r, g, b])
# #     # iimg_low = np.abs(iimg_low)
# #
# #     if len(img.shape>2):   ### the bgr to rgb
# #         img = img_plt(img)
# #         np_spectrum = img_plt(np_spectrum)
# #         iimg = img_plt(iimg)
# #         iimg_low = img_plt(iimg_low)
# #
# #
# #     plt.figure().suptitle('Spatial domain image enhancement')
# #     plt.subplot(231),plt.imshow(img)
# #     plt.title('original'), plt.xticks([]), plt.yticks([])
# #     plt.subplot(232),plt.imshow(np_spectrum.astype(np.uint8))
# #     plt.title('np_spectrum'), plt.xticks([]), plt.yticks([])
# #     plt.subplot(233),plt.imshow(iimg.astype(np.uint8))
# #     plt.title('inverse'), plt.xticks([]), plt.yticks([])
# #     plt.subplot(235),plt.imshow(low_pass_spectrum.astype(np.uint8))
# #     plt.title('low pass spectrum'), plt.xticks([]), plt.yticks([])
# #     plt.subplot(236),plt.imshow(iimg_low.astype(np.uint8))
# #     plt.title('low pass inverse '), plt.xticks([]), plt.yticks([])
# #     plt.show()
if __name__ == '__main__':
    img = cv2.imread('lena-color.jpg')

    # show BGR lena
    # plt.subplot(3, 3, 1)
    # plt.imshow(img)
    # plt.axis('off')
    # plt.title('lena_BGR')
    #
    # # BGR to RGB
    # lena_RGB = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
    # plt.subplot(3, 3, 2)
    # plt.imshow(lena_RGB)
    # plt.axis('off')
    # plt.title('lena_RGB')
    #
    # # BGR to GRAY
    # lena_GRAY = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    # plt.subplot(3, 3, 3)
    # plt.imshow(lena_GRAY,cmap = 'gray')
    # plt.axis('off')
    # plt.title('lena_GRAY')
    #
    # # BGR to CIE XYZ
    # lena_CIEXYZ = cv2.cvtColor(img, cv2.COLOR_BGR2XYZ)
    # plt.subplot(3, 3, 4)
    # plt.imshow(lena_CIEXYZ)
    # plt.axis('off')
    # plt.title('lena_CIEXYZ')
    #
    # # BGR to YCrCb
    # lena_YCrCb = cv2.cvtColor(img, cv2.COLOR_BGR2YCrCb)
    # plt.subplot(3, 3, 5)
    # plt.imshow(lena_YCrCb)
    # plt.axis('off')
    # plt.title('lena_YCrCb')
    #
    # # BGR to HSV
    # lena_HSV = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
    # plt.subplot(3, 3, 6)
    # plt.imshow(lena_HSV)
    # plt.axis('off')
    # plt.title('lena_HSV')
    #
    # # BGR to HLS
    # lena_HLS = cv2.cvtColor(img, cv2.COLOR_BGR2HLS)
    # plt.subplot(3, 3, 7)
    # plt.imshow(lena_HLS)
    # plt.axis('off')
    # plt.title('lena_HLS')
    #
    # # BGR to CIE L*a*b
    # lena_Lab = cv2.cvtColor(img, cv2.COLOR_BGR2Lab)
    # plt.subplot(3, 3, 8)
    # plt.imshow(lena_Lab)
    # plt.axis('off')
    # plt.title('lena_Lab')
    #
    # # BGR to CIE L*u*v
    # lena_Luv = cv2.cvtColor(img, cv2.COLOR_BGR2Luv)
    # plt.subplot(3, 3, 9)
    # plt.imshow(lena_Luv)
    # plt.axis('off')
    # plt.title('lena_Luv')
    #
    # plt.show()

图像仿射变换



你可能感兴趣的:(数字图像处理)