Error while using blockproc. Matrix dimensions must agree.

Hi,
I'm trying to use blockproc to compress a watermark that I want to embed. I'm using discrete cosine transform. I'm using blockproc for the first time, so I just copied the code off a youtube video to see how it works.
This is the code I'm using.
A=imread('C:\Users\hanuman\Documents\studies sheep\sem 7\FINAL YEAR PROJECT\project\house.jpg');
abc=imread('C:\Users\hanuman\Documents\studies sheep\sem 7\FINAL YEAR PROJECT\project\ABC watermark.jpg'); %reading the watermark
abc=imresize(abc, [96, 96]); %resizing the watermark to a number divisible by 8
f1= @(block_struct) dct2(block_struct.data);
f2= @(block_struct) idct2(block_struct.data);
J=blockproc(abc, [8 8], f1);
depth= find(abs(J)<150);
J(depth)= zeros(size(depth));
K= blockproc(J, [8 8], f2)/255;
This is the error matlab is showing:
BLOCKPROC encountered an error while evaluating the user-supplied function handle, FUN.
The cause of the error was:
Matrix dimensions must agree.
Error in dct (line 76)
b = W .* fft(y);
Error in dct2 (line 49)
b = dct(dct(arg1).').';
Error in Trial2>@(block_struct)dct2(block_struct.data)
Error in blockprocFunDispatcher (line 13)
output_block = fun(block_struct);
Error in blockprocInMemory (line 80)
[ul_output fun_nargout] = blockprocFunDispatcher(fun,block_struct,...
Error in blockproc (line 243)
result_image = blockprocInMemory(source,fun,options);
Error in Trial2 (line 8)
J=blockproc(abc, [8 8], f1);
If you know where I'm going wrong, please help.
Thank you for your time!

 Risposta accettata

Matt J
Matt J il 13 Nov 2019
Modificato: Matt J il 13 Nov 2019
Matrix dimensions must agree.
Error in dct (line 76)
b = W .* fft(y);
It would appear that W and y are not the same size.

8 Commenti

Ummm... the only matrices I've entered are A and abc. I didn't enter W or y. And, I din't know if I can change their dimensions.
Do you still get an error when you replace abc with a random 96x96 matrix? I don't.
abc=rand(96);
f1= @(block_struct) dct2(block_struct.data);
J=blockproc(abc, [8 8], f1);
You're right!
I didn't convert the image to a grayscale... I took a black and white image, so in my head it was already in grayscale. Of course matlab won't consider it that way!
as soon as I converted it, the code started working fine!
thank you so much!!!!!!!!:)
You're welcome, but please Accept-click the answer to signify that we've solved it.
Incidentally, blockproc is often not the fastest option. You can download mat2tiles from here
abc=rand(4000);
tic;
J=cell2mat( cellfun(@dct2, mat2tiles(abc,[8,8]),'uni',0) );
toc;
%Elapsed time is 12.124396 seconds.
tic;
f1= @(block_struct) dct2(block_struct.data);
J=blockproc(abc, [8 8], f1);
toc;
%Elapsed time is 15.340144 seconds.
Also, this
depth= find(abs(J)<150);
J(depth)= zeros(size(depth));
is more efficiently implemented,
J(abs(J)<150)=0;
ooh! Did come across that before... didn't know that it was more efficient.. Thank you for all the useful tips!:) :)
I meant mat2cell, this one is new!

Accedi per commentare.

Più risposte (0)

Community Treasure Hunt

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

Start Hunting!

Translated by