Hello guys ,
I have a problem.I have an image file and you also know it is matrix.I want to implement 2x2 maximum fill as an image that i upload. I want to search the all 2x2 pixels , finding the maximum value and fill 2x2 area (that we searched) with the maximum value.Can anyone help me. Thank you

 採用された回答

Stephen23
Stephen23 2017 年 3 月 11 日
編集済み: Stephen23 2017 年 3 月 11 日

1 投票

Method One: blockproc:
>> ipt = [128,25,12,26;240,200,15,25;128,128,255,15;35,35,35,155]
ipt =
128 25 12 26
240 200 15 25
128 128 255 15
35 35 35 155
>> blk = [2,2];
>> fun = @(s)max(s.data(:))*ones(blk);
>> otp = blockproc(ipt,blk,fun)
otp =
240 240 26 26
240 240 26 26
128 128 255 255
128 128 255 255
Method Two: mat2cell, cellfun, and cell2mat:
>> blk = [2,2];
>> spl = size(ipt)./blk;
>> col = repmat(blk(1),1,spl(1));
>> row = repmat(blk(2),1,spl(2));
>> C = mat2cell(ipt,col,row);
>> foo = @(m)max(m(:))*ones(blk);
>> otp = cell2mat(cellfun(foo,C,'Uni',0))
otp =
240 240 26 26
240 240 26 26
128 128 255 255
128 128 255 255
Method Three: kron and accumarray:
>> blk = [2,2];
>> idx = size(ipt)./blk;
>> idx = reshape(1:prod(idx),idx);
>> idx = kron(idx,ones(blk)); % (repelem also works)
>> vec = accumarray(idx(:),ipt(:),[],@max);
>> otp = vec(idx)
otp =
240 240 26 26
240 240 26 26
128 128 255 255
128 128 255 255
Method Four: reshape, permute, and kron:
>> idx = size(ipt)./blk;
>> tmp = reshape(ipt,blk(1),idx(1),blk(2),idx(2));
>> tmp = permute(tmp,[2,4,1,3]);
>> tmp = max(max(tmp,[],4),[],3);
>> otp = kron(tmp,ones(blk))
otp =
240 240 26 26
240 240 26 26
128 128 255 255
128 128 255 255

3 件のコメント

Fabio Corres
Fabio Corres 2017 年 3 月 11 日
編集済み: Fabio Corres 2017 年 3 月 11 日
in blockproc: method i had and error like :
Function BLOCKPROC encountered an error while evaluating the user supplied function handle, FUN.
The cause of the error was:
Error using .* Integers can only be combined with integers of the same class, or scalar doubles.
Error in @(s)max(s.data(:))*ones(block)
Error in blockprocFunDispatcher (line 14) output_block = fun(block_struct);
Error in blockprocInMemory (line 81) [ul_output fun_nargout] = blockprocFunDispatcher(fun,block_struct,...
Error in blockproc (line 237) result_image = blockprocInMemory(source,fun,options);
Error in bitirmetest1 (line 24) otp = blockproc(img_backup_ycbcr,block,fun);
///////////////// then i use im2double function image and it compiled.Did i make the true addition ?
Thank you so much :)
Stephen23
Stephen23 2017 年 3 月 11 日
編集済み: Stephen23 2017 年 3 月 11 日
This error occurs if the input matrix is not double. It can be fixed by specifying the class of the ones in fun:
>> ipt = uint8([128,25,12,26;240,200,15,25;128,128,255,15;35,35,35,155]);
>> blk = [2,2];
>> fun = @(s)max(s.data(:))*ones(blk,class(s.data));
>> otp = blockproc(ipt,blk,fun)
otp =
240 240 26 26
240 240 26 26
128 128 255 255
128 128 255 255
Fabio Corres
Fabio Corres 2017 年 3 月 11 日
It works thank you

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

その他の回答 (0 件)

Community Treasure Hunt

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

Start Hunting!

Translated by