Error in adMedianfilter_2D (line 12)
d3x3 = window(r3, r3);
Error in adMedian (line 18)
[pixel_value, pixel_valid] = adMedianfilter_2D(c_data, c_idx); %function
Here's my code:
clc; % Clear command window
clear all; % Delete all variables
close all; % Close all figure windows
I = imread('mdb001.pgm'); %image read
J = imnoise(I,'salt & pepper',0.02); %add noise to image
smax = 3; %spatial co-variance for least neighbouring pixels
[r, c] = size(I); %dimensions of the image
ll = ceil(smax/2); %lower-left
ul = floor(smax/2); %upper-left
for i = 2:r-smax
for j =2:c-smax
c_idx = i;
c_data = double(I(j:j+smax-1, i));
[pixel_value, pixel_valid] = adMedianfilter_2D(c_data, c_idx); %function
if pixel_valid
J(j,i) = pixel_value;
end
end
end
subplot( 1, 2, 1 );
imshow( I, [ ] );
subplot( 1, 2, 2 );
imshow( J, [ ] );
%function adMedianfilter_2D
function [pixel_value, pixel_valid ] = adMedianfilter_2D( c_data, c_idx );
smax = 3;
persistent window; %window tool
if isempty(window)
window = zeros(smax, smax);
end
cp = ceil(smax/2); % center pixel;
w3 = -2:2;
r3 = cp + w3;
d3x3 = window(r3, r3);
center_pixel = window(cp, cp);
outbuf = get_median_2d(d3x3);
[min3, med3, max3] = getMinMaxMed_2d(outbuf);
pixel_val = get_new_pixel(min3, med3, max3, ...
center_pixel);
persistent datavalid
if isempty(datavalid)
datavalid = false;
end
pixel_valid = datavalid;
datavalid = (c_idx >= smax);
end
%get_median_2d
function outbuf = get_median_2d(inbuf)
outbuf = inbuf;
[nrows ncols] = size(inbuf);
for ii=coder.unroll(1:ncols)
colData = outbuf(:, ii)';
colDataOut = get_median_1d(colData)';
outbuf(:, ii) = colDataOut;
end
for ii=coder.unroll(1:nrows)
rowData = outbuf(ii, :);
rowDataOut = get_median_1d(rowData);
outbuf(ii, :) = rowDataOut;
end
end

3 commentaires

KSSV
KSSV le 24 Fév 2022
You said you are getting error. But you have not specififed what error.
Pratiksha Joshi
Pratiksha Joshi le 24 Fév 2022
r3 starts from 0 which is not allowed as an index.
smax = 3;
cp = ceil(smax/2); % center pixel;
w3 = -2:2;
r3 = cp + w3
r3 = 1×5
0 1 2 3 4

Connectez-vous pour commenter.

 Réponse acceptée

KSSV
KSSV le 24 Fév 2022
It looks like r3 is not an integer, it has decimals. Try:
r3 = round(r3) ; % round r3 to the nearest integer
d3x3 = window(r3, r3);

Plus de réponses (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by