block processing of a matrix
    10 views (last 30 days)
  
       Show older comments
    
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 Comments
Answers (3)
  Ryan
      
 on 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 Comments
  Ryan
      
 on 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
      
      
 on 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.
  Filipe
 on 9 Jun 2012
        1 Comment
  Ryan
      
 on 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.
See Also
Categories
				Find more on Data Distribution Plots in Help Center and File Exchange
			
	Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!