The first blog, some of the teachers usually stay in the job, although very simple, but also want to save it, Liugejinian it.
I understand KNN average filter, select a pixel, the pixel values within whichever M * M center, the front mold according to the value of K pixel values are averaged, the K pixel includes a center point. KNN is to take the median value of the first K values. May understand there is an error, the result of the filtering is not very satisfactory.
Before always felt very weak ability, just the work is not difficult, took python tried, can be considered small to overcome the fear of the dictionary. Under attached python code, and the Internet to find the matlab code.
import cv2
import numpy as np
from PIL import Image
import matplotlib.pyplot as plt
def KNNfliter(str,M,K):
img = cv2.imread(str,0).astype(np.float32)
matrix = np.asarray(img)
size = img.shape
mid=(M-1)//2
value=0.0
#dic=np.arange(M*M+1)
for i in range(mid,size[0]-mid):
for j in range(mid,size[1]-mid):
order = matrix[i-mid:i+mid+1,j-mid:j+mid+1]
order=order.flatten()
order=order-matrix[i][j]
order1=abs(order)
dict1=dict(zip(order,order1))
a=sorted(dict1.items(),key = lambda kv:(kv[1],kv[0]))
for s in range(K):
value=value+a[s][0]
value=value+K*matrix[i][j]
matrix[i,j]=round(value/K)
image = Image.fromarray(matrix)
return image
def KNNMfliter(str,M,K):
img = cv2.imread(str,0).astype(np.float32)
matrix = np.asarray(img)
size = img.shape
mid=(M-1)//2
value=0.0
for i in range(mid,size[0]-mid):
for j in range(mid,size[1]-mid):
order = matrix[i-mid:i+mid+1,j-mid:j+mid+1]
order=order.flatten()
order = order - matrix[i][j]
order1 = abs(order)
dict1 = dict(zip(order, order1))
a = sorted(dict1.items(), key=lambda kv: (kv[1], kv[0]))
matrix[i,j]=matrix[i][j]+value[(k-1)/2]
image = Image.fromarray(matrix)
return image
img='gaus.jpg'
image=cv2.imread(img,0)
image1=KNNfliter(img,5,3)
image2=KNNMfliter(img,5,3)
plt.imshow(image1)
plt.imshow(image2)
plt.show()
#KNNMF
function[ J ]=knnmf(I)
[l,w]=size(I);
M=5;K=9;
MM=M*M;
weighMM=ceil(M/2);
lweighMM=fix(M/2);
lhalfMM=fix(M*M/2);
uhalfMM=ceil(M*M/2);
iend=l-weighMM;
jend=w-weighMM;
J=I;
for i=weighMM:iend
for j=weighMM:jend
I1=I(i-weighMM+1:i+weighMM-1,j-weighMM+1:j+weighMM-1);
A=reshape(I1,1,MM);
intA=int16(A);
for k=1:MM
B(1,k)=int16(intA(uhalfMM)-intA(k));
end
BB=abs(B);
[BBB,id]=sort(BB);
for k=1:K;
C(1,k)=A(id(k));
end
J(i,j)=round(median(C));
end
end
end
#KNNF
function[ J ]=K_neardealnoise(I)
[l,w]=size(I);
M=5;K=9;
MM=M*M;
weighMM=ceil(M/2);
lweighMM=fix(M/2);
lhalfMM=fix(M*M/2);
uhalfMM=ceil(M*M/2);
iend=l-weighMM;
jend=w-weighMM;
J=I;
for i=weighMM:iend
for j=weighMM:jend
I1=I(i-weighMM+1:i+weighMM-1,j-weighMM+1:j+weighMM-1);
A=reshape(I1,1,MM);
intA=int16(A);
for k=1:MM
B(1,k)=int16(intA(uhalfMM)-intA(k));
end
BB=abs(B);
[BBB,id]=sort(BB);
for k=1:K;
C(1,k)=A(id(k));
end
J(i,j)=round(mean(C));
end
end
end
#均方滤波
function[y]=grayminvariance(x)
[m,n]=size(x);
x=double(x);
y=x;
for i=3:m-2
for j=3:n-2
z1=[x(i-1,j+2),x(i,j+2),x(i+1,j+2),x(i-1,j+1),x(i,j+1),x(i+1,j+1),x(i,j)];
z2=[x(i-1,j-1),x(i,j-1),x(i+1,j-1),x(i-1,j-2),x(i,j-2),x(i+1,j-2),x(i,j)];
z3=[x(i-2,j+1),x(i-1,j+1),x(i-2,j),x(i-1,j),x(i-2,j-1),x(i-1,j-1),x(i,j)];
z4=[x(i+1,j+1),x(i+2,j+1),x(i+1,j),x(i+2,j),x(i+1,j-1),x(i+2,j-1),x(i,j)];
z5=[x(i-2,j+2),x(i-1,j+2),x(i-2,j+1),x(i-1,j+1),x(i,j+1),x(i-1,j),x(i,j)];
z6=[x(i+1,j+2),x(i+2,j+2),x(i,j+1),x(i+1,j+1),x(i+2,j+1),x(i+1,j),x(i,j)];
z7=[x(i-1,j),x(i-2,j-1),x(i-1,j-1),x(i,j-1),x(i-2,j-2),x(i-1,j-2),x(i,j)];
z8=[x(i+1,j),x(i,j-1),x(i+1,j-1),x(i+2,j-1),x(i+1,j-2),x(i+2,j-2),x(i,j)];
z9=[x(i-1,j+1),x(i,j+1),x(i+1,j+1),x(i-1,j-1),x(i,j-1),x(i+1,j-1),x(i-1,j),x(i,j),x(i+1,j)];
z=[z1,z2,z3,z4,z5,z6,z7,z8,z9];
h=[var(z1),var(z2),var(z3),var(z4),var(z5),var(z6),var(z7),var(z8),var(z9)];
%对9个方差进行冒泡排序法
for i1=1:1:8
for j1=1:1:9-i1
if h(1,j1) > h(1,j1+1)
z0=z(1,j1);%z0为临时变量
z(1,j1)=z(1,j1+1);
z(1,j1+1)=z0;
end
end
end
%最小方差已经求出,即h(1,1),对应z(1,1)这个向量,即对应的方差最小的模板
y(i,j)=mean( z(1,1) );%中心像素获得最小方差模板对应的均值
%y(i,j)=median( z(1,1) );%中心像素获得最小方差模板对应的中值
end
end