Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
distance calculation

Subject: distance calculation

From: Bernard

Date: 6 Jan, 2009 02:15:04

Message: 1 of 3

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_P1-1,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_P1-1,Npts_P2);
    D1 = zeros(Npts_P1-1,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 =~(I1|I2);
        % Computation of the minimum squared distances between all points of
        % P2 and the i-th 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_P2-1,Npts_P1);
    D2 = zeros(Npts_P2-1,Npts_P1);
    for i = 1:Npts_P2-1
        k = -(dP2x(i) * (P2.x(i) - P1.x) + ...
              dP2y(i) * (P2.y(i) - P1.y))/sdP22(i);
        I1 = k < 0;
        I2 = k > 1;
        I =~(I1|I2);
        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.

Subject: distance calculation

From: Walter Roberson

Date: 6 Jan, 2009 04:17:04

Message: 2 of 3

Bernard wrote:
> 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_P1-1,Npts_P2);
>
> Can someone tell me how do i solve the problem so that i can run this program.

Well, at that point in the program, what are the values of Npts_P1 and Npts_P2 ?

Tracing your code, the values should be the number of locations labeled "1"
or "2" respectively in image bb.jpeg. But we don't have your images so we can't
run the code for ourself to look at the numbers.

> % To compute the minimum distance between two polygons, we compute the
> % minimum distance between all vertices and all edges.

What is known about the polygons? If they can be triangulated into convex
regions, then you can find the minimum distances by considering the
vertices of each bounded region instead of considering the distance
between every point and every other.

> K1 = zeros(Npts_P1-1,Npts_P2);
> for i = 1:numel(P1.x)-1

Why are you being inconsistent in your usage here? In the K1 assignment
statement you called the first quantity Npts_P1 - 1, and in the for loop
you are calling that same quantity as numel(P1.x) - 1 . You are doing
unnecessary work and confusing the reader by using two representations of
the same value.



--
.signature note: I am now avoiding replying to unclear or ambiguous postings.
Please review questions before posting them. Be specific. Use examples of what you mean,
of what you don't mean. Specify boundary conditions, and data classes and value
relationships -- what if we scrambled your data or used -Inf, NaN, or complex(rand,rand)?

Subject: distance calculation

From: Roger Stafford

Date: 6 Jan, 2009 05:06:02

Message: 3 of 3

"Bernard " <mohinder_lah@yahoo.com> wrote in message <gjuer8$fv1$1@fred.mathworks.com>...
> 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_P1-1,Npts_P2);
>
> Can someone tell me how do i solve the problem so that i can run this program.
>
> this is my codings,
> .........

  As far as I can make out, your algorithm looks basically correct, Bernard. However, with very large numbers of points in each polygon it looks as though you will have to forego the luxury of storing the distance between each vertex of the one polygon and each line segment of the other. You simply will not have enough memory for all such pairings. It is not a matlab "bug"; it is a limitation of your basic computer storage resources.

  Based on your final output of 'min_d' (or 'min_dist' - which is it?) you apparently have no real need for such immense storage. Just store your minima and their indices as you go through the segments of each polygon and use only single dimensional arrays in the process. That will avoid the necessity of using such huge amounts of memory. What other needs do you have for saving each possible combination of vertex-to-segment distance?

  Depending on the polygons, there may exist algorithms which make it possible to avoid the majority of the complex segment-to-vertex calculations with large numbers of vertices. Assuming you already know that there are distances as small as a value d, then for any given segment a rectangular box can be drawn about it having x and y coordinates a distance d beyond the segment coordinates in each direction. It can be quickly determined if any given vertex lies beyond such a box, and if so, the more complex distance calculation can be avoided. If, say, the x-coordinates of all the vertices of both polygons have a sorted version, the question of whether a vertex lies inside such a box can be even more efficiently answered.

Roger Stafford

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us