block processing of a matrix

4 Ansichten (letzte 30 Tage)
Filipe
Filipe am 8 Jun. 2012
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

Antworten (3)

Ryan
Ryan am 8 Jun. 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 Kommentare
Ryan
Ryan am 8 Jun. 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
Image Analyst am 9 Jun. 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.

Melden Sie sich an, um zu kommentieren.


Filipe
Filipe am 9 Jun. 2012
Hi again,
first of all, thank you very much for the good answers provided.
I've been testing the supplied code and the second one is the closest to what I pretended. However, it seems my problem cannot be solved the way I explained.
Imagine you have the matrix provided in the following link: https://dl.dropbox.com/u/3545452/matrix.mat
and you want to get rid of those noisy points... My intention was to filter them, attributing them the median value of their 3x3 neighborhood, only if they differed more than a predefined threshold from any of the 3x3 neighbours.
What I've found from my experiments is that the supplied code (even being very good) does not completely solve my problem, because, apparently, I only need to filter those points if they are also the maximum points in their 3x3 neighborhood, otherwise I should not filter them.
Imagine the following matrix: [1 2 3; 5 67 9; 2 4 5], I just wanted to filter the value 67 because the it is the maximum value in that 3x3 neighborhood and the maximum difference between it and any other value is the same 3x3 neighborhood is bigger than 50 (50 is just an hypotetic value)...
I think this way I will be able to filter these noisy points... What do you think? If you think there are better way of cleaning these points, please, let me know.
Thank you very much in advance!
Best regards, Filipe.

Filipe
Filipe am 9 Jun. 2012
Hi again,
I think I've found the solution. Here is the code:
threshold = 50; matrix = rand(32,32) * 255; %you can also use the matrix I've indicated in my previous message
figure; imagesc(matrix, [0 255]); axis off; axis square; colormap gray
median_matrix = medfilt2(matrix,[3 3]);
max_matrix = ordfilt2(matrix,9,ones(3,3));
min_matrix = ordfilt2(matrix,1,ones(3,3));
diff_matrix = abs(min_matrix - median_matrix);
idx = diff_matrix >= threshold;
matrix(idx) = median_matrix(idx);
max_matrix = ordfilt2(matrix,9,ones(3,3));
diff_matrix = max_matrix - median_matrix;
idx = diff_matrix >= threshold;
matrix(idx) = median_matrix(idx);
figure; imagesc(matrix, [0 255]); axis off; axis square; colormap gray
Thank you very much for your help!
  1 Kommentar
Ryan
Ryan am 9 Jun. 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.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Image Processing Toolbox finden Sie in Help Center und File Exchange

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by