block processing of a matrix
1 visualizzazione (ultimi 30 giorni)
Mostra commenti meno recenti
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 Commenti
Risposte (3)
Ryan
il 8 Giu 2012
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 Commenti
Ryan
il 8 Giu 2012
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
il 9 Giu 2012
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
il 9 Giu 2012
1 Commento
Ryan
il 9 Giu 2012
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.
Vedere anche
Categorie
Scopri di più su Image Processing Toolbox in Help Center e File Exchange
Prodotti
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!