%pylab inline
import cv2
%matplotlib inline
from PIL import Image
from __future__ import print_function
from __future__ import division
from scipy.io import wavfile
import numpy as np
Populating the interactive namespace from numpy and matplotlib
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
cWeightTable = buildDistanceWeightTable(10, 6)
imshow(cWeightTable)
<matplotlib.image.AxesImage at 0x11dbb5210>
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
sWeightTable = buildSimilarityWeightTable(60)
plot(sWeightTable)
[<matplotlib.lines.Line2D at 0x116d49810>]
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) <height:
rowOffset = row + semirow
else:
rowOffset = 0
if (col+semicol)>= 0 and (col+semicol) <width:
colOffset = col + semicol
else:
colOffset = 0
tr2 = img[colOffset,rowOffset,0]
tg2 = img[colOffset,rowOffset,1]
tb2 = img[colOffset,rowOffset,2]
csRedWeight = cWeightTable[semirow+radius,semicol+radius] * sWeightTable[(abs(tr2 - tr))]
csGreenWeight = cWeightTable[semirow+radius,semicol+radius] * sWeightTable[(abs(tg2 - tg))]
csBlueWeight = cWeightTable[semirow+radius,semicol+radius] * sWeightTable[(abs(tb2 - tb))]
#print(csRedWeight, csGreenWeight, csBlueWeight)
# Do the Normalization
csSumRedWeight += csRedWeight
csSumGreenWeight += csGreenWeight
csSumBlueWeight += csBlueWeight # the sum of all weight
redSum += (csRedWeight * tr2) # the sum of all color's value
greenSum += (csGreenWeight * tg2)
blueSum += (csBlueWeight * tb2)
# make sure the index won't out of array
tr = floor(redSum/ csSumRedWeight)
tg = floor(greenSum/ csSumGreenWeight)
tb = floor(blueSum/ csSumBlueWeight)
# set RGB value for the destination image
dest[col,row,0]=tr
dest[col,row,1]=tg
dest[col,row,2]=tb
return dest
imgbowl= imread('bowl.png')
imgbowl = imgbowl*255;
imgbowl = imgbowl.astype(int)
imgbowl_afterbi = BIfilter(imgbowl,10, 6,60);
figure(figsize(15,15))
subplot(1,2,1)
imshow(imgbowl/255)
xlabel("Original Image")
subplot(1,2,2)
imshow(imgbowl_afterbi /255)
xlabel("Bilateral Filter")
<matplotlib.text.Text at 0x12ea86290>
imggirl= imread('girl.png')
imggirl2 = imggirl*255;
imggirl2 = imggirl2.astype(int)
imggirl2 = filter(imggirl2,10, 6,60)
imggirl3 = cv2.GaussianBlur(imggirl, (11,11), 20)
figure(figsize=(15,15))
subplot(1,3,1)
imshow(imggirl)
xlabel("Original Image")
subplot(1,3,2)
imshow(imggirl2 /256)
title("Comparison of Bilateral Filter and GaussianBlur Filter")
xlabel("Bilateral Filter")
subplot(1,3,3)
imshow(imggirl3)
xlabel("GaussianBlur Filter")
<matplotlib.text.Text at 0x12bb8ce10>
def clamp(p, min,max ):
if p<min: p=min
elif p>max: p=max
return p
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
imgboard = imread('board.png')
imgboard2 = imgboard*255
imgboard2 = imgboard2.astype(int)
imgboard2_afterbi = BIfilter(imgboard2,10, 6,60)
imgboard3 = Glow(imgboard2,10, 6,60)
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")
<matplotlib.text.Text at 0x12e928090>
imgpeo = imread('peo.png')
imgpeo2 = imgpeo*255
imgpeo2 = imgpeo2.astype(int)
imgpeo2_afterbi = BIfilter(imgpeo2,10, 10,80)
imgpeo3 = Glow(imgpeo2,10, 10,80)
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")
<matplotlib.text.Text at 0x11766c610>
def buildSVKWeightTable(imggg):
width=imggg.shape[0]
height=imggg.shape[1]
SVKWeightTable=zeros((width,height))
SVKWeightTable=imggg
return SVKWeightTable
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) <height:
rowOffset = row + semirow
else:
rowOffset = 0
if (col+semicol)>= 0 and (col+semicol) <width:
colOffset = col + semicol
else:
colOffset = 0
tr2 = img[colOffset,rowOffset,0]
tg2 = img[colOffset,rowOffset,1]
tb2 = img[colOffset,rowOffset,2]
#csRedWeight = cWeightTable[semirow+radius,semicol+radius] * sWeightTable[(abs(tr2 - tr))] * SVKWeightTable[colOffset, rowOffset, 0]
#csGreenWeight = cWeightTable[semirow+radius,semicol+radius] * sWeightTable[(abs(tg2 - tg))]* SVKWeightTable[colOffset, rowOffset, 0]
#csBlueWeight = cWeightTable[semirow+radius,semicol+radius] * sWeightTable[(abs(tb2 - tb))]* SVKWeightTable[colOffset, rowOffset, 0]
csRedWeight = cWeightTable[semirow+radius,semicol+radius] * SVKWeightTable[colOffset, rowOffset, 0]
csGreenWeight = cWeightTable[semirow+radius,semicol+radius] * SVKWeightTable[colOffset, rowOffset, 0]
csBlueWeight = cWeightTable[semirow+radius,semicol+radius] * SVKWeightTable[colOffset, rowOffset, 0]
#print(csRedWeight, csGreenWeight, csBlueWeight)
# Do the Normalization
csSumRedWeight += csRedWeight
csSumGreenWeight += csGreenWeight
csSumBlueWeight += csBlueWeight # the sum of all weight
redSum += (csRedWeight * tr2) # the sum of all color's value
greenSum += (csGreenWeight * tg2)
blueSum += (csBlueWeight * tb2)
# make sure the index won't out of array
if csSumRedWeight > 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
imggg=imread("spacial2.png")
imggg2=imread("spacial3.png")
spacial=imread("spacial1.png")
SVK=SVKWeightTable(imggg)
spacial_trans=spacial*255
spacial_trans=spacial_trans.astype(int)
spacial_after=SVKfilter(spacial_trans,10, 6,60, SVK)
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")
<matplotlib.text.Text at 0x142f52a50>
SVK2=SVKWeightTable(imggg2)
spacial_trans=spacial*255
spacial_trans=spacial_trans.astype(int)
spacial_after2=SVKfilter(spacial_trans,10, 6,60, SVK2)
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")
<matplotlib.text.Text at 0x141b80d90>
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) <height:
rowOffset = row + semirow
else:
rowOffset = 0
if (col+semicol)>= 0 and (col+semicol) <width:
colOffset = col + semicol
else:
colOffset = 0
tr2 = img[colOffset,rowOffset,0]
tg2 = img[colOffset,rowOffset,1]
tb2 = img[colOffset,rowOffset,2]
csRedWeight = cWeightTable[semirow+radius,semicol+radius] * sWeightTable[(abs(tr2 - tr))]
csGreenWeight = cWeightTable[semirow+radius,semicol+radius] * sWeightTable[(abs(tr2 - tr))]
csBlueWeight = cWeightTable[semirow+radius,semicol+radius] * sWeightTable[(abs(tr2 - tr))]
#print(csRedWeight, csGreenWeight, csBlueWeight)
# Do the Normalization
csSumRedWeight += csRedWeight
csSumGreenWeight += csGreenWeight
csSumBlueWeight += csBlueWeight # the sum of all weight
redSum += (csRedWeight * tr2) # the sum of all color's value
greenSum += (csGreenWeight * tg2)
blueSum += (csBlueWeight * tb2)
# make sure the index won't out of array
tr = floor(redSum/ csSumRedWeight)
tg = floor(greenSum/ csSumGreenWeight)
tb = floor(blueSum/ csSumBlueWeight)
# set RGB value for the destination image
dest[col,row,0]=tr
dest[col,row,1]=tg
dest[col,row,2]=tb
return dest
imgrcolor=imread("color1.png")
figure(figsize=(5,5))
imshow(imgrcolor)
xlabel("Original Image")
<matplotlib.text.Text at 0x115c03d10>
imgrcolor_trans=imgrcolor*255
imgrcolor_trans=imgrcolor_trans.astype(int)
imgrcolor_out = ESfilter(imgrcolor_trans, 10, 6,60);
figure(figsize=(5,5))
imshow(imgrcolor_out/255)
xlabel("Edge Selection Image")
<matplotlib.text.Text at 0x115d210d0>
img= imread("girl.png")
blur=cv2.bilateralFilter(img, 9, 30,5 )
figure(figsize=(10,10))
imshow(blur)
subplot(1,2,1)
imshow(img)
subplot(1,2,2)
imshow(blur)
<matplotlib.image.AxesImage at 0x114e7f090>
cap = cv2.VideoCapture(0)
ret,frame=cap.read()
#src: Source image
#dst: Destination image
#d: The diameter of each pixel neighborhood.
#sigma_{Color}: Standard deviation in the color space.
#sigma_{Space}: Standard deviation in the coordinate space (in pixel terms)
while(True):
ret,frame=cap.read()
pos = cv2.bilateralFilter(frame, 20, 512, 6)
cv2.imshow('frame',pos)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
r1= imread("R1.png")
r2= imread("R2.png")
figure(figsize=(10,10))
subplot(1,2,1)
imshow(r1)
xlabel("Original Image")
subplot(1,2,2)
imshow(r2)
xlabel("Test Image")
<matplotlib.text.Text at 0x11e9c6610>