i have a problem with my codings.It does not seem to execute the program and it gave me an error
??? Error using ==> zeros
Maximum variable size allowed by the program is exceeded.
Error in ==> pigsfinal1 at 125
K1 = zeros(Npts_P11,Npts_P2);
Can someone tell me how do i solve the problem so that i can run this program.
this is my codings,
function pigs();
rgb1=imread('aa.jpeg');
BW = im2bw(rgb1,0.4);
figure,imshow(BW);
rgb2=imread('bb.jpeg');
BW2 = im2bw(rgb2,0.4);
figure,imshow(BW2);
rgb3=imread('cc1.jpeg');
BW3 = im2bw(rgb3,0.4);
figure,imshow(BW3);
%figure;
%BWforimg1 = bwareaopen(BW,500,8);
% to remove unwanted or some area from the image
se = strel('disk',35);
BWforimg1 = imopen(BW,se);
figure, imshow(BWforimg1), title('Img1 After opening')
BWforimg2 = imopen(BW2,se);
figure, imshow(BWforimg2), title('Img2 After opening')
BWforimg3 = imopen(BW3,se);
figure, imshow(BWforimg3), title('Img3 After opening')
% two find number of object as cluster
[L1,num1]=bwlabel(BWforimg1,8);
num1
[L2,num2]=bwlabel(BWforimg2,8);
num2
[L3,num3]=bwlabel(BWforimg3,8);
num3
%L1 = watershed(bw2,8);
%[D,L] = bwdist(L1,'euclidean');
% to get the indices of first pig in img
[r,c]=find(L2==1);%change
%P=[r , c ];
P1 = struct('x', r, 'y', c);
tf1 = isstruct(P1)
% to get the indices of second pig in img
[m,n]=find(L2==2); %change
P2 = struct('x', m, 'y', n);
tf2 = isstruct(P2)
%P2 = [m n];
%[m2,n2] = size(P2);
%to view the boundary of the pigs in image
figure;
[C1,h1]=imcontour(BWforimg1);
figure;
[C2,h2]=imcontour(BWforimg2,3);
figure;
[C3,h3]=imcontour(BWforimg3);
%[D1,L1] = bwdist(BWforimg1,'euclidean');
%[D2,L2] = bwdist(BWforimg2,'euclidean');
%[D3,L3] = bwdist(BWforimg3,'euclidean');
%calculate minumin distace
Npts_P1=numel(P1.x);
Npts_P2=numel(P2.x);
if Npts_P1>1 && Npts_P2>1
% Bounding box test
minP1x = min(P1.x);
maxP1x = max(P1.x);
minP1y = min(P1.y);
maxP1y = max(P1.y);
minP2x = min(P2.x);
maxP2x = max(P2.x);
minP2y = min(P2.y);
maxP2y = max(P2.y);
% if ((minP1x < maxP2x) && (maxP1x > minP2x) && ...
% (minP1y < maxP2y) && (maxP1y > minP2y))
% Checking if any of the segments of the 2 polygons are crossing
% S. H?lz function
%[x,y] = curveintersect(P1.x,P1.y,P2.x,P2.y);
% if ~isempty(x)
% min_d = 0;
% figure
% hold on
% box on
% axis equal
% plot(P1.x,P1.y,P2.x,P2.y);
% plot(x,y,'ro');
% return;
%end
%end
end
% To compute the minimum distance between two polygons, we compute the
% minimum distance between all vertices and all edges.
% To compute this distance, one needs to compute the minimum distance
% between a vertex I (xi,yi) and an edge [A B] of
% coordinates (xa,ya) and (xb,yb)
% To compute this distance, one computes the projected point of the vertex
% I on the line passing through the vertices of the edge [A B].
% The projected point P corresponds to the minimum distance between the
% vertex and the line.
% The coordiantes of point P are computed in a parametric way with the line
% created by points A and B:
if Npts_P1 >= 3
dP1x = diff(P1.x);
dP1y = diff(P1.y);
sdP12 = dP1x.^2 + dP1y.^2;
if any(sdP12 == 0)
warning('min_dist_between_two_pigs:w1',...
'Two successive points of P1 are indentical.')
end
% there are bug in matlab when create zeros matrix with high dimention
K1 = zeros(Npts_P11,Npts_P2);
D1 = zeros(Npts_P11,Npts_P2);
for i = 1:numel(P1.x)1
% Compute the closest distance between a point and a segment
% Computation of the parameter k
k = (dP1x(i) * (P1.x(i)  P2.x) +...
dP1y(i) * (P1.y(i)  P2.y)) / sdP12(i);
I1 = k < 0;
I2 = k > 1;
I =~(I1I2);
% Computation of the minimum squared distances between all points of
% P2 and the ith segment of P1.
D1(i,I1) = (P2.x(I1) P1.x(i )).^2+(P2.y(I1)P1.y(i )).^2;
D1(i,I2) = (P2.x(I2) P1.x(i+1)).^2+(P2.y(I2)P1.y(i+1)).^2;
D1(i,I ) = (P2.x(I )(P1.x(i)+k(I)*dP1x(i))).^2+...
(P2.y(I )(P1.y(i)+k(I)*dP1y(i))).^2;
K1(i,:) = k;
end
% One computes the minimum distance for pig 1
[min_d1,ind_seg1] = min(D1);
[min_D1,ind_pt1 ] = min(min_d1);
end
%%%%%%%%%%%%%%%%%%%%%%%%
% The same operations are performed for all segments of P2.
if Npts_P2 >= 3
dP2x = diff(P2.x);
dP2y = diff(P2.y);
sdP22 = dP2x.^2 + dP2y.^2;
if any(sdP22 == 0)
warning('min_dist_between_two_pig:w2',...
'Two successive points of P1 are indentical.')
end
% there are bug in matlab when create zeros matrix with high dimention
K2 = zeros(Npts_P21,Npts_P1);
D2 = zeros(Npts_P21,Npts_P1);
for i = 1:Npts_P21
k = (dP2x(i) * (P2.x(i)  P1.x) + ...
dP2y(i) * (P2.y(i)  P1.y))/sdP22(i);
I1 = k < 0;
I2 = k > 1;
I =~(I1I2);
D2(i,I1) = (P1.x(I1)P2.x(i )).^2+(P1.y(I1)P2.y(i )).^2;
D2(i,I2) = (P1.x(I2)P2.x(i+1)).^2+(P1.y(I2)P2.y(i+1)).^2;
D2(i,I ) = (P1.x(I )(P2.x(i)+k(I)*dP2x(i))).^2+...
(P1.y(I )(P2.y(i)+k(I)*dP2y(i))).^2;
K2(i,:) = k;
end
[min_d2,ind_seg2] = min(D2);
[min_D2,ind_pt2 ] = min(min_d2);
end
if (Npts_P1 >= 3) && (Npts_P2 >= 3)
[min_d,ind_tot] = min([min_D1,min_D2]);
min_d = sqrt(min_d);
elseif Npts_P1 < 3
ind_tot = 2;
min_d = sqrt(min_D2);
elseif Npts_P2 < 3
ind_tot = 1;
min_d = sqrt(min_D1);
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
figure
% hold on
% box on
axis equal
plot(P1.x,P1.y,P2.x,P2.y);
plot([p1x p2x],[p1y p2y],'ro');
title('Computation of the minimum distance between two pigs P1 and P2')
hleg = legend('P1','P2','min_dist(P1,P2)');
set(hleg,'Interpreter','none');
end
Thank you.
