Matrix dimensions must agree

1 view (last 30 days)
Quan Ly Duong
Quan Ly Duong on 21 Feb 2019
Answered: Cris LaPierre on 5 Apr 2019
I tried to make a gui about LSB watermarking
% --- Executes on button press in btnChonAnhGoc2.
function btnChonAnhGoc2_Callback(hObject, eventdata, handles)
% hObject handle to btnChonAnhGoc2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
[tenAnh, duongDanAnh] = uigetfile( ...
{'*.jpg;*.png;*.bmp;*.jpeg;*.tif', 'All MATLAB Files (*.jpg, *.png, *.bmp, *.jpeg, *.tif)'; ...
'*.*', 'All Files (*.*)'}, ...
'Chon Hinh Anh De Mo');
full = strcat(duongDanAnh, tenAnh);
global anhGoc2;
anhGoc2 = imread(full);
anhGoc2=imresize(anhGoc2,[512,512]);
axes(handles.axes1);
imshow(anhGoc2);
% --- Executes on button press in btnChonWatermark2.
function btnChonWatermark2_Callback(hObject, eventdata, handles)
% hObject handle to btnChonWatermark2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
[tenAnh, duongDanAnh] = uigetfile( ...
{'*.jpg;*.png;*.bmp;*.jpeg;*.tif', 'All MATLAB Files (*.jpg, *.png, *.bmp, *.jpeg, *.tif)'; ...
'*.*', 'All Files (*.*)'}, ...
'Chon Hinh Anh De Mo');
f = strcat(duongDanAnh, tenAnh);
global anhmo;
anhmo = imread(f);
anhmo=imresize(anhmo,[512,512]);
axes(handles.axes2);
imshow(anhmo);
% --- Executes on button press in btnXuatketqua2.
function btnXuatketqua2_Callback(hObject, eventdata, handles)
% hObject handle to btnXuatketqua2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global anhGoc2;
global anhmo;
k= rgb2gray(anhGoc2);
z= imbinarize(anhmo);
z= double(z);
r= double(k - mod(k,2));
l= uint8(z + r);
axes(handles.axes3);
imshow(l)
when i start the code i got Matrix dimensions must agree error at line l= uint8(z + r);
Can anyone know how to fix it?
  4 Comments
Quan Ly Duong
Quan Ly Duong on 21 Feb 2019
i forgot to remove it out, sorry
Quan Ly Duong
Quan Ly Duong on 21 Feb 2019
z size = anhmo
and r size = double(k - mod(k,2)) ??

Sign in to comment.

Answers (1)

Cris LaPierre
Cris LaPierre on 5 Apr 2019
What version of MATLAB are you using? If pre 16b, the issue might be the 3rd dimension.
k= rgb2gray(anhGoc2); makes k a 512x512x1 matrix. As far as I can tell, anhmo remains a 512x512x3, which would cause a matrix dimension error when adding z and r in a version of MATLAB that does not support implicit expansion.
The simplest fix is probably to turn anhmo into a grayscale image as well.

Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!