in "fun" have access to the center values of the "colfilt" kernels?

2 Ansichten (letzte 30 Tage)
Marco
Marco am 2 Apr. 2014
Bearbeitet: Marco am 2 Apr. 2014
Would anybody know, how I can in "fun" have access to the center values of the "colfilt" kernels?
The kernels of the "colfilt" function are the columns in the array passed to "fun" by the "colfilt" function, and if for the m x n kernel the values for m and n have been odd numbers, then the value of the center pixel can be determined easily. But I did not find how to determine the values of the center pixels of the kernels of the colfilt function, if even numbers have been used to span the kernel.
I know that using the "nlfilter" function I can easily receive in "fun" the value of the center pixel from the kernel matrix passed to "fun" (according to the documentation it is to be found by "floor(([m n]+1)/2)" ). But, as a kernel of the "colfilt" function is not received in "fun" as an array, but as a column, I here did not find a valid formula to determine the position of the kernel´s center pixel, from which I could read out the value, as soon as an even m x n kernel dimension comes in.
Any idea how to always get the kernel´s center pixel´s value also from the "colfilt" function, also if an even dimension defining the kernel size has been used?

Akzeptierte Antwort

Anand
Anand am 2 Apr. 2014
You're going to have to store the linear index of where you expect the pixel to be and access that element of the array passed into fun. Here's an example showing how to replace each 5x5 block with the center pixel in that block.
I = imread('tire.tif');
blk = [5 5];
% Find linear index of center pixel
center = floor((blk+1)/2);
centerind = sub2ind(blk,center(1),center(2));
% Define anonymous function that replaces the 5x5 block with the center pixel.
% The center pixel is accessed as x(centerind), in this case, x(13). repmat
% is used to create a 5x5 block of this.
fun = @(x)repmat(x(centerind),size(x,1),size(x,2));
out = colfilt(I,blk,'distinct',fun);
You can just as easily replace 5x5 with another block size (odd or even).
Hope this helps.
  1 Kommentar
Marco
Marco am 2 Apr. 2014
Bearbeitet: Marco am 2 Apr. 2014
Thanks, it works excellent! I took from your example the following lines, being the key to solve my problem,
blk = [5 5];
center = floor((blk+1)/2);
centerind = sub2ind(blk,center(1),center(2));
and implemented my own little example code. It is attached to this comment. Hopefully it helps other beginners to get quicker started with the colfilt function, than it took for me. Thanks again, Anand!
PS: I should comment, that my wish to use the colfilt function, which at a first glance does not really seem to make sense in my tiny example, is motivated by the need to now implement a bigger code which in the filter function has to proceed a more complicated calculation with the value of the center pixel of the kernel scaling offsets applied to the further neighborhood operation.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Just for fun finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by