image processing loop to calculate median value in window / kernel

1 visualización (últimos 30 días)
Yas Rebecca
Yas Rebecca el 15 de Mzo. de 2021
Editada: DGM el 21 de Mzo. de 2021
I want to slide through an image matrix and evaluate the median at each frame. The structuring element needs to be 5x5. This is what i have so far:
img = imread('img.jpg');
length = 5;
half = round(length/2);
Len = size(greyscale_Gaussian);
rows = Len(1);
cols = Len(2);
kernel = zeros(5,5);
rowend = (rows - 1);
colend = (col - 1);
for row = 3 : rows -3;
for column = 3: num_cols -3;
up = row + half;
down = rows - half;
right = column + half;
left = cols - half;
startrow = max(up), 1;
endrow = min(down), rowend;
startcolumn = max(left),1;
endcolumn = min(right),colend;
window = W(startrow:endrow, startcolumn:endcolumn); %??
window = ([up:down,right:left]); %??
B(row, column) = median(window);
end
end
Im not sure how to define the window or how to end this script? i dont want to use any inbuilt functions such as imfilter etc.

Respuesta aceptada

DGM
DGM el 16 de Mzo. de 2021
Editada: DGM el 21 de Mzo. de 2021
This smells like homework, but I'll bite. It's unclear what the variables you've defined mean. What is the undefined variable W? What is 'length'? How does that differ from 'Len'? I'm not inclined to try to deciper indexing problems, but I'll offer an example.
This is an edited excerpt from a routine I used for grayscale dilation. In its original form, this replicates the behavior of imdilate(). It can be adapted to be a median or range filter. Maybe it will be of use.
% you'll have to figure out how you want to handle class dependency
% i'm just going to work in the input class
inclass=class(inpict);
% we need to know the size of the implied rectangular strel
sse=[5 5];
% i'm going to handle edges by padding them
% that way the output image is the same size.
% if you don't want to pad, you'll have to handle your indexing accordingly
padsize=floor(sse/2);
% if you don't want to use padarray, you can do this by
% generating padding blocks and concatenating them to the image
% if you don't want padding, you'll have to adjust your subscript ranges
s0=size(inpict);
inpict=padarray(inpict,padsize,'replicate','both');
s=size(inpict);
% preallocate the output array so you have somewhere to put the result
outpict=zeros(s0,inclass);
% specify offsets based on size of strel
osm=sse(1)-1;
osn=sse(2)-1;
% i'm assuming that the image is only 2D
for n=1:s0(2)
for m=1:s0(1)
% extract the sample region from the image
sample=inpict(m:(m+osm),n:(n+osn));
% since i'm doing dilation, this is essentially a maximum filter
% if you want to make a median filter, take the median instead
outpict(m,n)=max(sample(:));
end
end
% there really isn't anything else to do
  1 comentario
DGM
DGM el 16 de Mzo. de 2021
P.S. if you really want to avoid padarray(), you can do the padding by building the right subscript vectors:
mm=[ones([1 padsize(1)]) 1:s0(1) ones([1 padsize(1)])*s0(1)];
inpict=inpict(mm,:);
mm=[ones([1 padsize(2)]) 1:s0(2) ones([1 padsize(2)])*s0(2)];
inpict=inpict(:,mm);

Iniciar sesión para comentar.

Más respuestas (1)

Image Analyst
Image Analyst el 21 de Mzo. de 2021
Why not use the build-in function medfilt2()?
  3 comentarios
Image Analyst
Image Analyst el 21 de Mzo. de 2021
Then can you please accept DGM's answer to give him reputation points?
And it's almost always better to use built-in methods because they've been extensively tested for robustness and validated for accuracy, rather than your own method, which might not think of all the things that could go wrong or have bugs.
DGM
DGM el 21 de Mzo. de 2021
I second Image Analyst's note about using existing tools. Not only are they typically more robust, but they're often faster. I should've added that the only reason I wrote that sliding window filter was as a fallback for use only when imdilate() wasn't available.

Iniciar sesión para comentar.

Etiquetas

Productos


Versión

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by