block processing of a matrix
1 次查看(过去 30 天)
显示 更早的评论
Hello all,
I'm dealing with a problem and perhaps someone can help me out.
I want to process a matrix in blocks of 3x3 and attribute the median value of that 3x3 block to the central pixel only if the difference between the central pixel and any other in the 3x3 pixels block is bigger than a predefined threshold.
Imagine the matrix is defined by: matrix = rand(32,32) * 255;
I've tried to use blkproc but the processed matrix does not end with the 32x32 initial dimensions as desired.
Any help would be greatly appreciated.
Thank you very much in advance!
Best regards, Filipe
0 个评论
回答(3 个)
Ryan
2012-6-8
threshold = 50;
matrix = rand(32,32)*255;
median_matrix = medfilt2(matrix,[3 3]);
diff_matrix = median_matrix - matrix;
idx = diff_matrix>=threshold;
matrix(idx) = median_matrix(idx);
I believe that is what you asked for. Basically it generates a matrix of the median value then notes where the difference with the original is greater than the threshold and replaces those values in the original matrix with the median value.
2 个评论
Ryan
2012-6-8
Just re-read your question and it appears that you want to replace the value of a pixel if any pixel in its 3 x 3 neighborhood is a certain amount away, not of it's a certain amount away from the median. This code should handle that (the ordfilt2 acts like a "maximum filter" in the 3x3 neighborhood).
threshold = 50;
matrix = rand(32,32)*255;
median_matrix = medfilt2(matrix,[3 3]);
max_matrix = ordfilt2(matrix,9,ones(3,3));
diff_matrix = max_matrix - matrix;
idx = diff_matrix>=threshold;
matrix(idx) = median_matrix(idx);
Image Analyst
2012-6-9
FYI, you can also use imdilate() to get the local max matrix. But this will do what he says, in a clever and compact yet straightforward and intuitive way.
Filipe
2012-6-9
1 个评论
Ryan
2012-6-9
So you wanted to replace with the median if there are values +/- a given threshold? Glad you figured out what you needed! As far as removing the random outlier values goes, look into image denoising techniques. If you know the type of noise that is effecting your data (Gaussian, salt and pepper, etc) then you can better design a filter to account for it.
另请参阅
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!