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

1 回表示 (過去 30 日間)
Marco 2014 年 4 月 2 日

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?

サインインしてコメントする。

### 採用された回答

Anand 2014 年 4 月 2 日
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.
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 件のコメント-1 件の古いコメントを表示-1 件の古いコメントを非表示
Marco 2014 年 4 月 2 日

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.

サインインしてコメントする。

### カテゴリ

Help Center および File ExchangeStartup and Shutdown についてさらに検索

### Community Treasure Hunt

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

Start Hunting!

Translated by