image processing loop to calculate median value in window / kernel

1 次查看(过去 30 天)
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.

采纳的回答

DGM
DGM 2021-3-16
编辑:DGM 2021-3-21
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 个评论
DGM
DGM 2021-3-16
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);

请先登录,再进行评论。

更多回答(1 个)

Image Analyst
Image Analyst 2021-3-21
Why not use the build-in function medfilt2()?
  3 个评论
Image Analyst
Image Analyst 2021-3-21
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 2021-3-21
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.

请先登录,再进行评论。

产品


版本

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by