MATLAB Answers

Getting many outputs using blockproc

21 views (last 30 days)
Youssra
Youssra on 18 Jul 2015
Answered: Eric Chadwick on 21 Mar 2018
I want to apply SVD for each 4*4 blocks using the "blockproc" and get 3 outputs: U, S and V so I can reconstract all blocks together again but Matlab gets me "too many ouput arguments". How can I solve this problem?
fun = @(block_struct) svd(block_struct.data);
[U S V] = blockproc(a, [4 4], fun)
This is the error that I have:
Error using blockproc
Too many output arguments.

  0 Comments

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 18 Jul 2015
No, this is not possible with blockproc().
You can use something like
function r = blocksvd(block_struct)
[U, S, V] = svd(block_struct.data);
Spad = nan(size(U));
Vpad = Spad;
Spad(1:size(S,1),1:size(S,2)) = S;
Vpad(1:size(U,1),1:size(U,2)) = U;
r = [U; Spad; Vpad];
end
this returns a 12 x 4 array that can be broken up again into U, S, V.

  3 Comments

Youssra
Youssra on 21 Jul 2015
Thank you @Walter Roberson for your answer, it actually works and as you said it returns 12*4 array. But my objective is to modify a specific singular values of each block and then return back to my original image, and I added this line to your function before the "end"
img_back=U*Spad*Vpad' %%as defintion of singular values decomposition
It didn't give me the original values of the image ( I didn't modify the singular values yet). Do you know why or if I have to add something? Thank you in advance
Walter Roberson
Walter Roberson on 21 Jul 2015
Did you change the
function r =
to
function img_back =
?
Youssra
Youssra on 21 Jul 2015
No, I didn't. I just add this line to be sure if I will get the original values from returned U, Spad and Vpad. just like that:
function r = blocksvd(block_struct)
[U, S, V] = svd(block_struct.data);
Spad = nan(size(U));
Vpad = Spad;
Spad(1:size(S,1),1:size(S,2)) = S;
Vpad(1:size(U,1),1:size(U,2)) = U;
r = [U; Spad; Vpad];
img_back=U*Spad*Vpad'
end
I get a matrix with the same size as the original but not the same values. is this wrong?

Sign in to comment.

More Answers (2)

Brett Shoelson
Brett Shoelson on 15 Aug 2017
Edited: Walter Roberson on 15 Aug 2017
Walter is correct that blockproc returns a single output. For clarity, though, it's worth pointing out that you can make n separate calls to blockproc, requesting args 1--n. That's tricky to do with a one-line function handle, but easy if you create a single-output function for each output that you want to generate, and call them sequentially for the image. (Not ideal, but maybe helpful.)
For instance, if you wanted to calculate the thresholdOutput of the edge(img,'LOG') function:
[~,threshOut] = edge(img,'LOG');
you could create a function called, for example, calculateEdgeThresh, with a syntax like:
function thresh = calculateEdgeThresh(img)
[~,thresh] = edge(img,'LOG');
then (in pseudo-code):
localEdgeFcn = @(x) edge(x.data,'LOG');
threshFcn = @(x) calculateEdgeThresh(x.data);
localEdge = blockproc(img,localEdgeFcn); %This one is a one-liner, via function handle
localThresh = blockproc(img,threshFcn); %This one requires the separate calculateEdgeThresh function
Cheers,
Brett

  3 Comments

Eric Chadwick
Eric Chadwick on 20 Mar 2018
Hi Brett,
I have a similar problem to the original question and I do not understand your solution or the one above for that matter. I have a function:
function count_cells(Im,segmented_images,res,ECM_area);
I want to break my input image into many 100x100 blocks, run the function on them and then stitch it back together. The first answer seems to only work for 3 output images. Mine will be many so I'd rather be able to use blockproc and store each image in a 3D array or something.
Your solution just seems to perform blockproc on the image for two different function, but does not seem to stitch it back together. Is that correct? If so how can I achieve my goal?
Walter Roberson
Walter Roberson on 20 Mar 2018
Eric, your function does not return anything, and certainly not multiple outputs for each block. Your function name suggests that it just counts, rather than it calculating a revised block?
Image Analyst
Image Analyst on 20 Mar 2018
Eric, blockproc() stitches it back together automatically. See my attached demos.

Sign in to comment.


Eric Chadwick
Eric Chadwick on 21 Mar 2018
Hello, thank you both for responding so quickly! My function does have outputs, I just did not show them when I showed my function to you. However when I went to copy and paste it just now, I realized I wasn't telling it to output the image my function normally outputs. Adding the image as an output has solved the problem.
Thank you both!
Cheers,
Eric

  0 Comments

Sign in to comment.