??? Error using ==> plus Matrix dimensions must agree.

2 views (last 30 days)
I am trying to add two matrices, as below, size of Cs, d_int_outside, d_speed_outside is same. Also w1 and w2 are constants. Can someone help me what's wrong here.
Cs=w1.*d_int_outside+ w2.*d_speed_outside;
Main code:
clear all; close all;
clc;
% include max-flow solver
addpath(['..', filesep, 'maxflow']);
addpath(['..', filesep, 'lib']);
% 1. Load an image to segment
img =imread('/home/image.jp2');
% 2. Normalize the image intensity to [0,1]:
img = single(img);
img_n = (img - min(img(:))) / (max(img(:)) - min(img(:)));
% 3. Initialize a region as initialization for the zero level set
% This can be done by mouse selecting method
display('mark object stroke')
ROIPosition_o=point_selection(img_n);
%clear all windows
size_img=size(img_n);
region = zeros(size_img(1),size_img(2));
roio_size=size(ROIPosition_o);
num_points=roio_size(1);
for i=1:num_points
region(ceil(ROIPosition_o(i,2)),ceil(ROIPosition_o(i,1)))=1;
region(ceil(ROIPosition_o(i,2)),floor(ROIPosition_o(i,1)))=1;
region(floor(ROIPosition_o(i,2)),ceil(ROIPosition_o(i,1)))=1;
region(floor(ROIPosition_o(i,2)),floor(ROIPosition_o(i,1)))=1;
end
% % clear all previous figures
% delete(findall(0,'Type','figure'));
% % visualize initial region
figure();
imshow(img_n,[]);
hold on; contour(region,'r'); hold off;
title('Marked region');
% clf
% 4. Construct an s-t graph:
[sx, sy,channels] = size(img_n);
Cs = zeros(sx,sy);
Ct = zeros(sx,sy);
% allocate alpha(x), the regularization weight at each node x
alpha = zeros(sx,sy);
% 5. Set up parameters and start level set iterations:
maxLevelSetIterations = 20; % number of maximum time steps
tau = 50; % speed parameter
w1 = 0.6; % weight parameter for intensity data term
w2 = 0.4; % weight parameter for the speed data term
for t=1:maxLevelSetIterations
% 6. Compute a speed data term based on the current region
d_speed_inside = bwdist(region == 1,'Euclidean');
d_speed_outside = bwdist(region == 0,'Euclidean');
% 7. Compute a intensity data term based on the L1 distance to the
% mean
m_int_inside = mean(mean(img_n(region == 1)));
m_int_outside = mean(mean(img_n(region == 0)));
d_int_inside = abs(img_n - m_int_inside);
d_int_outside = abs(img_n - m_int_outside);
% 8. Compute speed data term as in Tutorial 01:
d_speed_inside = ((1-region).*d_speed_inside)./tau;
d_speed_outside = (region.*d_speed_outside)./tau;
% 7. Weight the contribution of both costs and assign them as source
% and sink capacities Cs, Ct in the graph
Cs = w1.*d_int_outside + w2.*d_speed_outside;
Ct = w1.*d_int_inside + w2.*d_speed_inside;
% Assign a regularization weight (equivalent to pairwise terms) for each
% node x. Here we employ a constant regularization weight alpha. The higher
% alpha is, the more smoothness penalty is assigned.
alpha = 1.5.*ones(sx,sy);
% 6. Set up the parameters for the max flow optimizer:
% [1] graph dimension 1
% [2] graph dimension 2
% [3] number of maximum iterations for the optimizer (default 200)
% [4] an error bound at which we consider the solver converged (default
% 1e-5)
% [5] c parameter of the multiplier (default 0.2)
% [6] step size for the gradient descent step when calulating the spatial
% flows p(x) (default 0.16)
pars = [sx; sy; 200; 1e-5; 0.2; 0.16];
% 7. Call the binary max flow optimizer with Cs, Ct, alpha and pars to obtain
% the continuous labelling function u, the convergence over iterations
% (conv), the number of iterations (numIt) and the run time (time);
[u, conv, numIt, time] = asetsBinaryMF2D(Cs, Ct, alpha, pars);
% 8. Threshold the continuous labelling function u to obtain a discrete
% segmentation result
region = u > 0.5;
% visualize the costs
subplot(2,2,2); imshow(Cs-Ct,[]); title('Cs-Ct');
subplot(2,2,3); imshow(img,[]); title(['r(',num2str(t),')']); hold on; contour(region,'r'); hold off;
subplot(2,2,4); loglog(conv); title(['convergence(',num2str(t),')']);
drawnow();
end
%
%
  1 Comment
Geoff Hayes
Geoff Hayes on 19 Jun 2015
Mojo - put a breakpoint at the line of code that is causing the problem and use the MATLAB debugger to step through the code to verify the sizes of the two matrices that you are trying to sum. See http://blogs.mathworks.com/videos/2010/09/02/using-debugger-to-walk-through-code/ for details on how to debug.

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 19 Jun 2015
d_int_inside and d_int_outside are constructed from img_n which is the normalized image. d_speed_inside and d_speed_outside are constructed from region which is number of rows of normalized image by number of columns of normalized image. Those two groups will be the same size only if the image that is read in is 2 dimensional. But we do not know that to be true: it is read from a .jp2 file, which might be grayscale or indexed but would more commonly be RGB and so be 3D.

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!