我试图在Matlab中实现模糊脉冲噪声检测方法。我定义了一个3 * 3窗口,对于灰度图像的每个非边界像素,将计算中心像素的所有8个可能邻居中的不同梯度,检查模糊规则并检测该像素是否有噪声。但它只是通过第一个像素并正确计算;对于第二个像素,我得到以下错误。有人可以帮忙吗? 另外,我试图定义一个计算梯度的函数,这是否可以为所有方向定义这样的函数? 错误:
指数超出矩阵维度。
主要错误(第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);
答案 0 :(得分:0)
您正在为图像中的每个中心像素提取3 x 3像素的邻域。但是,在评估渐变时,您使用的坐标系相对于原始图像而不是提取的像素邻域本身。
因此,所有渐变计算都是错误的。这是显而易见的,因为r
和c
可能会超出3的值,这是您所在社区的行和列的最大维度。
最简单的解决方法是将所有索引操作纠正到3 x 3像素的邻域中,使它们相对于提取图像的坐标系而不是原始图像坐标系。
将所有r
和c
替换为2
和2
,将所有r-1
和c-1
替换为1
和{{ 1}},最后所有1
和r+1
与c+1
和3
。至于你的算法的准确性,这应该是你需要的唯一修复,如果我正确理解你的描述,这应该或多或少地给你你想要的。但是,有一些可疑变量正在被访问但未在您的脚本中定义。这些变量是3
,out
和output
。我是凭信心接受这些,并假设你在此代码之前声明了这些。有更多的矢量化方法可以做你想做的事情,但我会将其作为练习。
具体来说,改变:
temp
要:
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));
变化:
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));
要:
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));
...依此类推。有太多的东西要为你改变所以我假设你可以为其他受影响的代码块做到这一点。