Я пытаюсь реализовать метод обнаружения нечеткого импульсного шума в Matlab. Я определяю окно размером 3*3, которое для каждого неграничного пикселя изображения в градациях серого будет вычислять различные градиенты во всех 8 возможных соседях центрального пикселя, проверять нечеткие правила и определять, является ли этот пиксель шумным или нет. Но он просто проходит через первый пиксель и правильно его вычисляет; для второго пикселя я получаю следующую ошибку. Кто-нибудь может помочь, пожалуйста? Кроме того, я пытаюсь определить функцию для расчета градиентов, возможно ли определить такую функцию для всех направлений? ошибка :
Показатель превышает размеры матрицы.
Ошибка в Main (строка 29) g2 = double(img_temp(r, c+1) - img_temp(r,c));
и вот мой код:
close all
clc
[file, path] = uigetfile('*.*' , 'Open an image');
filename = strcat(path, file);
img = (imread(filename));
dim = ndims(img);
if (dim==3)
img = rgb2gray(img);
end
figure, imshow(img);
k = 1;
[row , col] = size(img);
for r=2:row-1
largeCount = 0;
for c=2:col-1
img_temp = img(r-1:r+1, c-1:c+1);
%% Gradient Calculation in Direction : N
g0 = double(img_temp(r-1,c) - img_temp(r,c));
g1 = double(img_temp(r, c-1) - img_temp(r,c));
g2 = double(img_temp(r, c+1) - img_temp(r,c));
g_weight = max (max ( evalfis([g0 g1 0],out) , evalfis([g0 0 g2],out)), evalfis([g0 g1 g2],out) );
if g_weight >128
largeCount = largeCount+1;
end
%% Gradient Calculation in Direction : NE
g0 = double(img_temp(r-1 , c+1) - img_temp(r,c));
g1 = double(img_temp(r-1, c-1) - img_temp(r,c));
g2 = double(img_temp(r+1 , c+1) - img_temp(r,c));
g_weight = max (max ( evalfis([g0 g1 0],out) , evalfis([g0 0 g2],out)), evalfis([g0 g1 g2],out) );
if g_weight >128
largeCount = largeCount+1;
end
%% Gradient Calculation in Direction : E
g0 = double(img_temp(r,c+1) - img_temp(r,c));
g1 = double(img_temp(r-1,c) - img_temp(r,c));
g2 = double(img_temp(r+1 ,c) - img_temp(r,c));
g_weight = max (max ( evalfis([g0 g1 0],out) , evalfis([g0 0 g2],out)), evalfis([g0 g1 g2],out) );
if g_weight >128
largeCount = largeCount+1;
end
%% Gradient Calculation in Direction : SE
g0 = double(img_temp(r+1, c+1) - img_temp(r,c));
g1 = double(img_temp(r-1 , c+1) - img_temp(r,c));
g2 = double(img_temp(r+1 , c-1) - img_temp(r,c));
g_weight = max (max ( evalfis([g0 g1 0],out) , evalfis([g0 0 g2],out)), evalfis([g0 g1 g2],out) );
if g_weight >128
largeCount = largeCount+1;
end
%% Gradient Calculation in Direction : S
g0 = double(img_temp(r+1, c) - img_temp(r,c));
g1 = double (img_temp(r , c+1) - img_temp(r,c));
g2 = double(img_temp(r , c-1) - img_temp(r,c));
g_weight = max (max ( evalfis([g0 g1 0],out) , evalfis([g0 0 g2],out)), evalfis([g0 g1 g2],out) );
if g_weight >128
largeCount = largeCount+1;
end
%% Gradient Calculation in Direction : SW
g0 = double(img_temp(r+1, c-1) - img_temp(r,c));
g1 = double(img_temp(r+1 , c+1) - img_temp(r,c));
g2 = double(img_temp(r-1, c-1) - img_temp(r,c));
g_weight = max (max ( evalfis([g0 g1 0],out) , evalfis([g0 0 g2],out)), evalfis([g0 g1 g2],out) );
if g_weight >128
largeCount = largeCount+1;
end
%% Gradient Calculation in Direction : W
g0 = double(img_temp(r,c-1) - img_temp(r,c));
g1 = double(img_temp(r+1, c) - img_temp(r,c));
g2 = double(img_temp(r-1, c) - img_temp(r,c));
g_weight = max (max ( evalfis([g0 g1 0],out) , evalfis([g0 0 g2],out)), evalfis([g0 g1 g2],out) );
if g_weight >128
largeCount = largeCount+1;
end
%% Gradient Calculation in Direction : NW
g0 = double(img_temp(r-1,c-1) - img_temp(r,c));
g1 = double(img_temp(r+1 , c-1) - img_temp(r,c));
g2 = double(img_temp(r-1 , c+1) - img_temp(r,c));
g_weight = max (max ( evalfis([g0 g1 0],out) , evalfis([g0 0 g2],out)), evalfis([g0 g1 g2],out) );
if g_weight >128
largeCount = largeCount+1;
end
k = k+1;
end
%% if largeCount > 4 then the pixel is noisy for sure
if largeCount> 4
%% Add the pixel value to histogram
out(r,c)= 0;
else
%% Don't change pixel value
output(r,c) = (temp(r,c));
end
end
figure ; imshow(output);
РЕДАКТИРОВАТЬ :
Однако я изменил свой код, пока я запускаю следующий код, он приостанавливается (стек вызовов функций: ismemeber) и не показывает мне img_out. Я получаю эту ошибку:
69 [sortuAuB,IndSortuAuB] = sort([uA;uB]);
и вот мой отредактированный код:
close all
clc
[file, path] = uigetfile('*.*' , 'Open an image');
filename = strcat(path, file);
img = (imread(filename));
dim = ndims(img);
if (dim==3)
img = rgb2gray(img);
end
figure, imshow(img);
k = 1;
out = readfis('NoiseDetection.fis');
[row , col] = size(img);
img_out = zeros(row , col , 'uint8');
for r=2:row-1
largeCount = 0;
for c=2:col-1
img_temp = img(r-1:r+1, c-1:c+1);
%% Gradient Calculation in Direction : N
g0 = double(img_temp(1,2) - img_temp(2,2));
g1 = double(img_temp(2,1) - img_temp(2,2));
g2 = double(img_temp(2,3) - img_temp(2,2));
g_weight = max (max ( evalfis([g0 g1 0],out) , evalfis([g0 0 g2],out)), evalfis([g0 g1 g2],out) );
if g_weight >128
largeCount = largeCount+1;
end
%% Gradient Calculation in Direction : NE
g0 = double(img_temp(1,3) - img_temp(2,2));
g1 = double(img_temp(1,1) - img_temp(2,2));
g2 = double(img_temp(3,3) - img_temp(2,2));
g_weight = max (max ( evalfis([g0 g1 0],out) , evalfis([g0 0 g2],out)), evalfis([g0 g1 g2],out) );
if g_weight >128
largeCount = largeCount+1;
end
%% Gradient Calculation in Direction : E
g0 = double(img_temp(2,3) - img_temp(2,2));
g1 = double(img_temp(1,2) - img_temp(2,2));
g2 = double(img_temp(3,2) - img_temp(2,2));
g_weight = max (max ( evalfis([g0 g1 0],out) , evalfis([g0 0 g2],out)), evalfis([g0 g1 g2],out) );
if g_weight >128
largeCount = largeCount+1;
end
%% Gradient Calculation in Direction : SE
g0 = double(img_temp(3,3) - img_temp(2,2));
g1 = double(img_temp(1,3) - img_temp(2,2));
g2 = double(img_temp(3,1) - img_temp(2,2));
g_weight = max (max ( evalfis([g0 g1 0],out) , evalfis([g0 0 g2],out)), evalfis([g0 g1 g2],out) );
if g_weight >128
largeCount = largeCount+1;
end
%% Gradient Calculation in Direction : S
g0 = double(img_temp(3, 2)- img_temp(2,2));
g1 = double(img_temp(2, 3)- img_temp(2,2));
g2 = double(img_temp(2 ,1)- img_temp(2,2));
g_weight = max (max ( evalfis([g0 g1 0],out) , evalfis([g0 0 g2],out)), evalfis([g0 g1 g2],out) );
if g_weight >128
largeCount = largeCount+1;
end
%% Gradient Calculation in Direction : SW
g0 = double(img_temp(3,1) - img_temp(2,2));
g1 = double(img_temp(3,3) - img_temp(2,2));
g2 = double(img_temp(1,1) - img_temp(2,2));
g_weight = max (max ( evalfis([g0 g1 0],out) , evalfis([g0 0 g2],out)), evalfis([g0 g1 g2],out) );
if g_weight >128
largeCount = largeCount+1;
end
%% Gradient Calculation in Direction : W
g0 = double(img_temp(2,1)- img_temp(2,2));
g1 = double(img_temp(3,2)- img_temp(2,2));
g2 = double(img_temp(1,2)- img_temp(2,2));
g_weight = max (max ( evalfis([g0 g1 0],out) , evalfis([g0 0 g2],out)), evalfis([g0 g1 g2],out) );
if g_weight >128
largeCount = largeCount+1;
end
%% Gradient Calculation in Direction : NW
g0 = double(img_temp(1,1)- img_temp(2,2));
g1 = double(img_temp(3,1)- img_temp(2,2));
g2 = double(img_temp(1,3)- img_temp(2,2));
g_weight = max (max ( evalfis([g0 g1 0],out) , evalfis([g0 0 g2],out)), evalfis([g0 g1 g2],out) );
if g_weight >128
largeCount = largeCount+1;
end
%% if largeCount > 4 then the pixel is noisy for sure
if largeCount> 4
%% Add the pixel value to histogram
img_out(r,c)=0;
else
%% Don't change pixel value
img_out(r,c) = img_temp(2,2);
end
end
k = k+1;
end
figure ; imshow(img_out);