#!/usr/bin/env python # coding: utf-8 # # Bilateral Filter # ## 201 A Final Project # ### MAT LU LIU # # In[11]: get_ipython().run_line_magic('pylab', 'inline') import cv2 get_ipython().run_line_magic('matplotlib', 'inline') from PIL import Image from __future__ import print_function from __future__ import division from scipy.io import wavfile import numpy as np # ## Basic Bilateral Filter Function # #### Build the Distance Weight Table # In[12]: def buildDistanceWeightTable(radius, ds): size = radius*2 + 1 cWeightTable = zeros((size, size)) for semirow in range(-int(radius), int(radius)): for semicol in range(-int(radius),int(radius) ): # calculate Euclidean distance between center point and close pixels delta= math.sqrt(semirow * semirow + semicol * semicol)/ds deltaDelta = delta*delta cWeightTable[semirow+radius,semicol+radius]= math.exp(deltaDelta * -0.5) return cWeightTable # In[13]: cWeightTable = buildDistanceWeightTable(10, 6) imshow(cWeightTable) # #### Build the Range Weight Table # In[31]: def buildSimilarityWeightTable(rs): sWeightTable = zeros(256) # since the color scope is 0 ~ 255 for i in range(0,256): delta = math.sqrt(i*i)/rs deltaDelta= delta*delta sWeightTable[i] = math.exp(deltaDelta * -0.5) return sWeightTable # In[32]: sWeightTable = buildSimilarityWeightTable(60) plot(sWeightTable) # #### Build the Main Function # In[33]: def BIfilter(img,radius, ds, rs): width = img.shape[0] height= img.shape[1] radius= max(ds, radius) cWeightTable = buildDistanceWeightTable(radius, ds) sWeightTable = buildSimilarityWeightTable(rs) dest = zeros([width,height,3]) # do the convolution for row in range(0,height-1): for col in range(0,width-1): # get RGB value image tr=img[col,row,0] tg=img[col,row,1] tb=img[col,row,2] # clean value for next time redSum = greenSum = blueSum = 0 csRedWeight = csGreenWeight= csBlueWeight = 0 csSumRedWeight = csSumGreenWeight = csSumBlueWeight = 0 for semirow in range(- int(radius), int(radius)): for semicol in range(-int(radius), int(radius)): if (row+semirow)>= 0 and (row+semirow) = 0 and (col+semicol) max: p=max return p # In[36]: def Glow(img_orig,radius, ds, rs): width = img_orig.shape[0] height= img_orig.shape[1] img= BIfilter(img_orig,radius,ds,rs) dest = zeros([width,height,3]) for row in range(0,height-1): for col in range(0,width-1): # get RGB value image after bilateral filter and original image tr_orig=img_orig[col,row,0] tg_orig=img_orig[col,row,1] tb_orig=img_orig[col,row,2] tr=img[col,row,0] tg=img[col,row,1] tb=img[col,row,2] r=clamp((tr_orig+tr),0,255) g=clamp((tg_orig+tg),0,255) b=clamp((tb_orig+tb),0,255) dest[col,row,0]=r dest[col,row,1]=g dest[col,row,2]=b return dest # In[ ]: imgboard = imread('board.png') imgboard2 = imgboard*255 imgboard2 = imgboard2.astype(int) imgboard2_afterbi = BIfilter(imgboard2,10, 6,60) imgboard3 = Glow(imgboard2,10, 6,60) # In[56]: figure(figsize=(15,15)) subplot(1,3,1) imshow(imgboard) xlabel("Original Image") subplot(1,3,2) imshow(imgboard2 /256) title("Comparison of Bilateral Filter and Glow Function") xlabel("Bilateral Filter") subplot(1,3,3) imshow(imgboard3 /256) xlabel("Glow Function") # In[37]: imgpeo = imread('peo.png') imgpeo2 = imgpeo*255 imgpeo2 = imgpeo2.astype(int) imgpeo2_afterbi = BIfilter(imgpeo2,10, 10,80) imgpeo3 = Glow(imgpeo2,10, 10,80) # In[38]: figure(figsize=(15,15)) subplot(1,3,1) imshow(imgpeo) xlabel("Original Image") subplot(1,3,2) imshow(imgpeo2 /256) title("Comparison of Bilateral Filter and Glow Function") xlabel("Bilateral Filter") subplot(1,3,3) imshow(imgpeo3 /256) xlabel("Glow Function") # ### 2. Spacial Varying Kernel # In[116]: def buildSVKWeightTable(imggg): width=imggg.shape[0] height=imggg.shape[1] SVKWeightTable=zeros((width,height)) SVKWeightTable=imggg return SVKWeightTable # In[149]: def SVKfilter(img,radius, ds, rs, imggg): width = img.shape[0] height= img.shape[1] radius= max(ds, radius) cWeightTable = buildDistanceWeightTable(radius, ds) sWeightTable = buildSimilarityWeightTable(rs) SVKWeightTable = buildSVKWeightTable(imggg) dest = zeros([width,height,3]) # do the convolution for row in range(0,height-1): for col in range(0,width-1): # get RGB value image tr=img[col,row,0] tg=img[col,row,1] tb=img[col,row,2] # clean value for next time redSum = greenSum = blueSum = 0 csRedWeight = csGreenWeight= csBlueWeight = 0 csSumRedWeight = csSumGreenWeight = csSumBlueWeight = 0 kerrad = int(SVKWeightTable[row, col, 0] * radius) for semirow in range(- int(kerrad), int(kerrad)): for semicol in range(-int(kerrad), int(kerrad)): if (row+semirow)>= 0 and (row+semirow) = 0 and (col+semicol) 0 : tr = floor(redSum/ csSumRedWeight) if csSumGreenWeight > 0 : tg = floor(greenSum/ csSumGreenWeight) if csSumBlueWeight > 0 : tb = floor(blueSum/ csSumBlueWeight) #print(tr,tg,tb) # set RGB value for the destination image dest[col,row,0]=tr dest[col,row,1]=tg dest[col,row,2]=tb return dest # In[150]: imggg=imread("spacial2.png") imggg2=imread("spacial3.png") spacial=imread("spacial1.png") # In[151]: SVK=SVKWeightTable(imggg) spacial_trans=spacial*255 spacial_trans=spacial_trans.astype(int) spacial_after=SVKfilter(spacial_trans,10, 6,60, SVK) # In[154]: figure(figsize=(15,15)) subplot(1,3,1) imshow(spacial) xlabel("Original Image") subplot(1,3,2) imshow(imggg) title("Spacial Varying Kernel Filter") xlabel("Affect Image") subplot(1,3,3) imshow(spacial_after /256) xlabel("SVK Filter") # In[147]: SVK2=SVKWeightTable(imggg2) spacial_trans=spacial*255 spacial_trans=spacial_trans.astype(int) spacial_after2=SVKfilter(spacial_trans,10, 6,60, SVK2) # In[148]: figure(figsize=(15,15)) subplot(1,3,1) imshow(spacial) xlabel("Original Image") subplot(1,3,2) imshow(imggg2) title("Spacial Varying Kernel Filter") xlabel("Bilateral Filter") subplot(1,3,3) imshow(spacial_after2 /256) xlabel("SVK Filter") # ## 3. Edge Selection # In[16]: def ESfilter(img,radius, ds, rs): width = img.shape[0] height= img.shape[1] radius= max(ds, radius) cWeightTable = buildDistanceWeightTable(radius, ds) sWeightTable = buildSimilarityWeightTable(rs) dest = zeros([width,height,3]) # do the convolution for row in range(0,height-1): for col in range(0,width-1): # get RGB value image tr=img[col,row,0] tg=img[col,row,1] tb=img[col,row,2] # clean value for next time redSum = greenSum = blueSum = 0 csRedWeight = csGreenWeight= csBlueWeight = 0 csSumRedWeight = csSumGreenWeight = csSumBlueWeight = 0 for semirow in range(- int(radius), int(radius)): for semicol in range(-int(radius), int(radius)): if (row+semirow)>= 0 and (row+semirow) = 0 and (col+semicol)