MATLAB: How can i use dct2 function in blockproc to avoid the following error ‘cant convert struct from double’

> I = rgb2gray(image1.png);
> myfun= @dct2;
> B = blockproc(I,[8 8],myfun);
These are the errors I get:
> Error using double
> Conversion to double from struct is not possible.
> Error in dct (line 28)
> a = double(a);
> Error in dct2 (line 68)
> b = dct(a, mpad);
> 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);
Can someone explain why these errors??

Best Answer

I was able to re-create your issue however the first step
I = rgb2gray(image1.png);
I was not able to convert directly from a png file with rgb2gray but rather I had to do
J = imread(image1.png)
I = rgb2gray(J);
Assuming your first 2 inputs are formatted how you want them, looking at the blockproc documentation:
It seems your issue is with the function handle input for dct2, according to the documentation the function input for blockproc should be a “Function handle to a function that accepts a block struct as input and returns a matrix, vector, or scalar.” Looking at the first example on the blockproc documentation page you should try something like this.
myfun = @(block_struct) dct2(block_struct.data);
Using this format fixed the ‘can’t convert struct from double’ error on my end.