Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/matlab/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Matlab 如何指定节点周围的邻域,然后搜索该邻域内的点?_Matlab_Image Processing - Fatal编程技术网

Matlab 如何指定节点周围的邻域,然后搜索该邻域内的点?

Matlab 如何指定节点周围的邻域,然后搜索该邻域内的点?,matlab,image-processing,Matlab,Image Processing,这可能有点长,但如果你能锻炼一些耐心,那会很有趣 假设我有下面的图片。 Allvectors是一个单元数组,包含每个内部节点的北(n)、南(s)、东(e)和西(w)面周围的向量点(边)(请参见有箭头或“X”标记的点) 触点(以红点显示)是包含1、2和3个像素标签作为其邻居的节点 xpoint1是一个单元阵列,包含所有接触点每个面的所有向量 x点1是具有标签1和2的相邻像素的面,因此矢量方向为“a+”或“a-”,具体取决于标签2的方向。这与像素标签3和2相同,因此向量方向为“B+”或“B-” 到

这可能有点长,但如果你能锻炼一些耐心,那会很有趣

假设我有下面的图片。

Allvectors是一个单元数组,包含每个内部节点的北(n)、南(s)、东(e)和西(w)面周围的向量点(边)(请参见有箭头或“X”标记的点)

触点(以红点显示)是包含1、2和3个像素标签作为其邻居的节点

xpoint1是一个单元阵列,包含所有接触点每个面的所有向量

x点1是具有标签1和2的相邻像素的面,因此矢量方向为“a+”或“a-”,具体取决于标签2的方向。这与像素标签3和2相同,因此向量方向为“B+”或“B-”

到目前为止我所尝试的:

clc; clear; close all;

Img = [3 3 3 2 2 
       3 3 3 2 2 
       3 1 2 2 2 
       1 1 2 2 2 
       1 1 1 1 2];

%%%%%% Plot the image
figure, imshow(Img, [], 'InitialMagnification','fit')   
grid on; 
hold on;

%# Find the size of the image   
[row, col] = size(Img);

%# Obtain total no. of internal x and y node points
ny = row - 1;  nx = col - 1;

%%%%%%%%%%% Obtain full output grids 
[gridy, gridx] = meshgrid(1:ny, 1:nx); 
coords = [gridy(:), gridx(:)];

%%%%%%%% Obtain node counts
nodecount = nx*(coords(:,1)-1) + coords(:,2);

%%%%%%%%%%% Compute the vectors around each node
%%%%% firstly create a cell array to store your vectors
Allvectors = cell(ny*nx, 4);
for nodecount = 1:ny*nx;

    %%%%%% determine the x and y-coords of each node
    [node_y, node_x] = get_xycoord_from_nodecount(nx, nodecount);

    %%%%%% compute vectors at each face around each node
    [n, s, e, w] = find_vectors(node_y, node_x, Img);

    Allvectors{nodecount,1} = n;
    Allvectors{nodecount,2} = s;
    Allvectors{nodecount,3} = e;
    Allvectors{nodecount,4} = w;

end

%%%%%%% Determine the contactpoint nodes
cpNodes = find(( any(strcmp('A+', Allvectors),2) | any(strcmp('A-', Allvectors),2)) & (any(strcmp('B+', Allvectors),2) | any(strcmp('B-', Allvectors),2)))


%%%%%%% find the node as well as coordinates of the contactpoints
nodecount        = cpNodes;
[node_y, node_x] = get_xycoord_from_nodecount(nx, nodecount);


%%%%%%% find all other x-points around cp
%# extract vectors for all x-points for pixel label 1
xpoint1 = Allvectors(cpNodes,:);  

%# create an empty cell array wherein the vectors around the faces of each new node will be stored
xpt2_labelA = cell(numel(cpNodes), 4);
xpt3_labelA = cell(numel(cpNodes), 4);

for ii = 1: numel(cpNodes)

    %# COMPUTATIONS FOR pixel label 1
    %%%%%% Determine the face where the 1st x-point (i.e. x-point1) lies
    Idx1_labelA(ii,:) = find(strcmp('A+', xpoint1(ii,:)) | strcmp('A-', xpoint1(ii,:)))


    %%%%%% Determine the face where the 2nd x-point (i.e. x-point2) lies
    %# firstly find the next Node from the previous   
    [xpt2_labelA, a, node_Anext] = find_node_next(ii, nodecount, nx, Idx1_labelA, xpt2_labelA, Allvectors);

    %%%%%% find all possible face(s) where x-point2 lies for both Solid phase
    if find(strcmp('A+', xpt2_labelA(ii,:)) | strcmp('A-', xpt2_labelA(ii,:))) > 0
         Idx2_labelA(ii,:)   = find(strcmp('A+', xpt2_labelA(ii,:)) | strcmp('A-', xpt2_labelA(ii,:)));
    else
        continue  
    end


    %%%%%% Determine the face where the 3rd x-point (i.e. x-point2) lies
         % repeat the steps for x-point2, however, rename the indices and cell
         % arrays with 2 or 3 where appropriate



    %# COMPUTATIONS FOR pixel label 3
    %%%%%% Determine the face where the 1st x-point (i.e. x-point1) lies
    Idx1_labelB(ii,:) = find(strcmp('B+', xpoint1(ii,:)) | strcmp('B-', xpoint1(ii,:)));



    %%%%%%%%%%%%%%%%%%%%%%%%%%%% COLLATE vectors from all X-Points found:
    %# Average unit vector 'A'
    Va = [(ya1 + ya2 +... yan)/Na, (xa1 + xa2 +... xan)/Na];  % Na=total no. of X-points collated for vector A within the specified neighborhood.

    %# Average unit vector 'B'
    Vb = [(yb1 + yb2 +... ybn)/Nb, (xb1 + xb2 +... xbn)/Nb];  % Nb=total no. of X-points collated for vector B within the specified neighborhood.
     %where: ya, yb, xa, xb are components of the vectors in the directions observed
     %as an example: from the arrow shown, ya1=0, and xa1=1

    %%%%%%%%% Compute angle between average unit vectors A and B
    angle(ii) = 180 - ( acos( dot(Va(ii,:), Vb(ii,:)) / (norm(Va(ii,:)) *    norm(Vb(ii,:)) ) ) * 180/pi );
    angle(:, ii) = angle(ii); 


end
function[node_y, node_x] = get_xycoord_from_nodecount(nx, nodecount)
%This function obtains the x and y coordinates from nodecount

node_x = mod(nodecount - 1, nx) + 1;
node_y = (nodecount - node_x)/nx + 1;

end





function [n, s, e, w] = find_vectors(node_y, node_x, Img)
%This function determines if a vector exists at the north, south,
%east and western faces around each node. 

%If a normal vector exists at any of these faces, 
%the function determines if it is an 'A+', 'A-', 'B+', or a 'B-'
%
%Where '+/-' is for +ve or -ve x or y-direction, and 'A' or 'B' tells if it
%is a vector resulting from a pixel labels 1|2,  or 3|2

n = NaN; s = NaN; e = NaN; w = NaN;

%# NORTH FACE
if Img(node_y, node_x + 1) == 2
    if Img(node_y, node_x) == 1
        n = 'A+'; 
    elseif Img(node_y, node_x) == 3
        n = 'B+';    
    end
elseif Img(node_y, node_x) == 2
    if Img(node_y, node_x + 1) == 1
        n = 'A-';
    elseif Img(node_y, node_x + 1) == 3
        n = 'B-';
    end
end


%# SOUTH FACE
if Img(node_y + 1, node_x + 1) == 2
    if Img(node_y + 1, node_x) == 1
        s = 'A+';
    elseif Img(node_y + 1, node_x) == 3
        s = 'B+'; 
    end
elseif Img(node_y + 1, node_x) == 2
    if Img(node_y + 1, node_x + 1) == 1
        s = 'A-';
    elseif Img(node_y + 1, node_x + 1) == 3
        s = 'B-';
    end
end


%# EAST FACE
if Img(node_y + 1, node_x + 1) == 2
    if Img(node_y, node_x + 1) == 1
        e = 'A+';
    elseif Img(node_y, node_x + 1) == 3
        e = 'B+';
    end
elseif Img(node_y, node_x + 1) == 2
    if Img(node_y + 1, node_x + 1) == 1
        e = 'A-';
    elseif Img(node_y + 1, node_x + 1) == 3
        e = 'B-';
    end
end


%# WEST FACE
if Img(node_y + 1, node_x) == 2
    if Img(node_y, node_x) == 1
        w = 'A+';
    elseif Img(node_y, node_x) == 3
        w = 'B+';
    end
elseif Img(node_y, node_x) == 2
    if Img(node_y + 1, node_x) == 1
        w = 'A-';
    elseif Img(node_y + 1, node_x) == 3
        w = 'B-';
    end
end



end





function [xpt2_labelA, a, node_Anext] = find_node_next(ii, nodecount, nx, Idx1_labelA, xpt2_labelA, Allvectors)


if Idx1_labelA(ii,:) == 1    %# xpoint1 lies on the north face
    %%%%% Find the node at the northern end of the face which contains xpoint1
    node_Anext(ii,:) =  nodecount(ii,:) - nx;

    %%%%%% extract the n, s, e, w faces from AllNormalvectors
    a = Allvectors(node_Anext(ii,:),:);

    %%%%%% store the new results (exclude the existing result at the southern face of the node northofCP)
    xpt2_labelA{ii,1} = a{1};
    xpt2_labelA{ii,2} = NaN;
    xpt2_labelA{ii,3} = a{3};
    xpt2_labelA{ii,4} = a{4};

elseif Idx1_labelA(ii,:) == 2    %# xpoint1 lies on the south face
    %%%%% Find the node at the southern end of the face which contains xpoint1
    node_Anext(ii,:) = nodecount(ii,:) + nx;

    %%%%%% extract n, s, e, w faces from AllNormalvectors
    a = Allvectors(node_Anext(ii,:),:);

    %%%%%% store the new results (exclude the node at the northern face of the node southofCP)
    xpt2_labelA{ii,1} = NaN;
    xpt2_labelA{ii,2} = a{2};
    xpt2_labelA{ii,3} = a{3};
    xpt2_labelA{ii,4} = a{4};

elseif Idx1_labelA(ii,:) == 3      %# xpoint1 lies on the east face
    %%%%% Find the node at the eastern end of the face which contains xpoint1
    node_Anext(ii,:)  = nodecount(ii,:) + 1;

    %%%%%% extract n, s, e, w faces from AllNormalvectors
    a = Allvectors(node_Anext(ii,:),:);

    %%%%%% store the new results (exclude the node at the western face of the node eastofCP)
    xpt2_labelA{ii,1} = a{1};
    xpt2_labelA{ii,2} = a{2};
    xpt2_labelA{ii,3} = a{3};
    xpt2_labelA{ii,4} = NaN;

elseif Idx1_labelA(ii,:) == 4      %# xpoint1 lies on the west face
    %%%%% Find the node at the western end of the face which contains xpoint1
    node_Anext(ii,:)  = nodecount(ii,:) - 1;

    %%%%%% extract n, s, e, w faces from AllNormalvectors
    a = Allvectors(node_Anext(ii,:),:);

    %%%%%% store the new results (exclude the node at the eastern face of the node westofCP)
    xpt2_labelA{ii,1} = a{1};
    xpt2_labelA{ii,2} = a{2};
    xpt2_labelA{ii,3} = NaN;
    xpt2_labelA{ii,4} = a{4};

end
end
我已经能够找到尽可能多的x点,我想和代码运行成功。然而,我这样做看起来相当麻烦,因为我一直在为要找到的每个新x点重新编写过程,这会导致太多的函数和变量。此外,这使我发现“A”和“B”向量的x点数量相同,然而,我意识到x点的数量不一定相同

请参见下面的代码:

clc; clear; close all;

Img = [3 3 3 2 2 
       3 3 3 2 2 
       3 1 2 2 2 
       1 1 2 2 2 
       1 1 1 1 2];

%%%%%% Plot the image
figure, imshow(Img, [], 'InitialMagnification','fit')   
grid on; 
hold on;

%# Find the size of the image   
[row, col] = size(Img);

%# Obtain total no. of internal x and y node points
ny = row - 1;  nx = col - 1;

%%%%%%%%%%% Obtain full output grids 
[gridy, gridx] = meshgrid(1:ny, 1:nx); 
coords = [gridy(:), gridx(:)];

%%%%%%%% Obtain node counts
nodecount = nx*(coords(:,1)-1) + coords(:,2);

%%%%%%%%%%% Compute the vectors around each node
%%%%% firstly create a cell array to store your vectors
Allvectors = cell(ny*nx, 4);
for nodecount = 1:ny*nx;

    %%%%%% determine the x and y-coords of each node
    [node_y, node_x] = get_xycoord_from_nodecount(nx, nodecount);

    %%%%%% compute vectors at each face around each node
    [n, s, e, w] = find_vectors(node_y, node_x, Img);

    Allvectors{nodecount,1} = n;
    Allvectors{nodecount,2} = s;
    Allvectors{nodecount,3} = e;
    Allvectors{nodecount,4} = w;

end

%%%%%%% Determine the contactpoint nodes
cpNodes = find(( any(strcmp('A+', Allvectors),2) | any(strcmp('A-', Allvectors),2)) & (any(strcmp('B+', Allvectors),2) | any(strcmp('B-', Allvectors),2)))


%%%%%%% find the node as well as coordinates of the contactpoints
nodecount        = cpNodes;
[node_y, node_x] = get_xycoord_from_nodecount(nx, nodecount);


%%%%%%% find all other x-points around cp
%# extract vectors for all x-points for pixel label 1
xpoint1 = Allvectors(cpNodes,:);  

%# create an empty cell array wherein the vectors around the faces of each new node will be stored
xpt2_labelA = cell(numel(cpNodes), 4);
xpt3_labelA = cell(numel(cpNodes), 4);

for ii = 1: numel(cpNodes)

    %# COMPUTATIONS FOR pixel label 1
    %%%%%% Determine the face where the 1st x-point (i.e. x-point1) lies
    Idx1_labelA(ii,:) = find(strcmp('A+', xpoint1(ii,:)) | strcmp('A-', xpoint1(ii,:)))


    %%%%%% Determine the face where the 2nd x-point (i.e. x-point2) lies
    %# firstly find the next Node from the previous   
    [xpt2_labelA, a, node_Anext] = find_node_next(ii, nodecount, nx, Idx1_labelA, xpt2_labelA, Allvectors);

    %%%%%% find all possible face(s) where x-point2 lies for both Solid phase
    if find(strcmp('A+', xpt2_labelA(ii,:)) | strcmp('A-', xpt2_labelA(ii,:))) > 0
         Idx2_labelA(ii,:)   = find(strcmp('A+', xpt2_labelA(ii,:)) | strcmp('A-', xpt2_labelA(ii,:)));
    else
        continue  
    end


    %%%%%% Determine the face where the 3rd x-point (i.e. x-point2) lies
         % repeat the steps for x-point2, however, rename the indices and cell
         % arrays with 2 or 3 where appropriate



    %# COMPUTATIONS FOR pixel label 3
    %%%%%% Determine the face where the 1st x-point (i.e. x-point1) lies
    Idx1_labelB(ii,:) = find(strcmp('B+', xpoint1(ii,:)) | strcmp('B-', xpoint1(ii,:)));



    %%%%%%%%%%%%%%%%%%%%%%%%%%%% COLLATE vectors from all X-Points found:
    %# Average unit vector 'A'
    Va = [(ya1 + ya2 +... yan)/Na, (xa1 + xa2 +... xan)/Na];  % Na=total no. of X-points collated for vector A within the specified neighborhood.

    %# Average unit vector 'B'
    Vb = [(yb1 + yb2 +... ybn)/Nb, (xb1 + xb2 +... xbn)/Nb];  % Nb=total no. of X-points collated for vector B within the specified neighborhood.
     %where: ya, yb, xa, xb are components of the vectors in the directions observed
     %as an example: from the arrow shown, ya1=0, and xa1=1

    %%%%%%%%% Compute angle between average unit vectors A and B
    angle(ii) = 180 - ( acos( dot(Va(ii,:), Vb(ii,:)) / (norm(Va(ii,:)) *    norm(Vb(ii,:)) ) ) * 180/pi );
    angle(:, ii) = angle(ii); 


end
function[node_y, node_x] = get_xycoord_from_nodecount(nx, nodecount)
%This function obtains the x and y coordinates from nodecount

node_x = mod(nodecount - 1, nx) + 1;
node_y = (nodecount - node_x)/nx + 1;

end





function [n, s, e, w] = find_vectors(node_y, node_x, Img)
%This function determines if a vector exists at the north, south,
%east and western faces around each node. 

%If a normal vector exists at any of these faces, 
%the function determines if it is an 'A+', 'A-', 'B+', or a 'B-'
%
%Where '+/-' is for +ve or -ve x or y-direction, and 'A' or 'B' tells if it
%is a vector resulting from a pixel labels 1|2,  or 3|2

n = NaN; s = NaN; e = NaN; w = NaN;

%# NORTH FACE
if Img(node_y, node_x + 1) == 2
    if Img(node_y, node_x) == 1
        n = 'A+'; 
    elseif Img(node_y, node_x) == 3
        n = 'B+';    
    end
elseif Img(node_y, node_x) == 2
    if Img(node_y, node_x + 1) == 1
        n = 'A-';
    elseif Img(node_y, node_x + 1) == 3
        n = 'B-';
    end
end


%# SOUTH FACE
if Img(node_y + 1, node_x + 1) == 2
    if Img(node_y + 1, node_x) == 1
        s = 'A+';
    elseif Img(node_y + 1, node_x) == 3
        s = 'B+'; 
    end
elseif Img(node_y + 1, node_x) == 2
    if Img(node_y + 1, node_x + 1) == 1
        s = 'A-';
    elseif Img(node_y + 1, node_x + 1) == 3
        s = 'B-';
    end
end


%# EAST FACE
if Img(node_y + 1, node_x + 1) == 2
    if Img(node_y, node_x + 1) == 1
        e = 'A+';
    elseif Img(node_y, node_x + 1) == 3
        e = 'B+';
    end
elseif Img(node_y, node_x + 1) == 2
    if Img(node_y + 1, node_x + 1) == 1
        e = 'A-';
    elseif Img(node_y + 1, node_x + 1) == 3
        e = 'B-';
    end
end


%# WEST FACE
if Img(node_y + 1, node_x) == 2
    if Img(node_y, node_x) == 1
        w = 'A+';
    elseif Img(node_y, node_x) == 3
        w = 'B+';
    end
elseif Img(node_y, node_x) == 2
    if Img(node_y + 1, node_x) == 1
        w = 'A-';
    elseif Img(node_y + 1, node_x) == 3
        w = 'B-';
    end
end



end





function [xpt2_labelA, a, node_Anext] = find_node_next(ii, nodecount, nx, Idx1_labelA, xpt2_labelA, Allvectors)


if Idx1_labelA(ii,:) == 1    %# xpoint1 lies on the north face
    %%%%% Find the node at the northern end of the face which contains xpoint1
    node_Anext(ii,:) =  nodecount(ii,:) - nx;

    %%%%%% extract the n, s, e, w faces from AllNormalvectors
    a = Allvectors(node_Anext(ii,:),:);

    %%%%%% store the new results (exclude the existing result at the southern face of the node northofCP)
    xpt2_labelA{ii,1} = a{1};
    xpt2_labelA{ii,2} = NaN;
    xpt2_labelA{ii,3} = a{3};
    xpt2_labelA{ii,4} = a{4};

elseif Idx1_labelA(ii,:) == 2    %# xpoint1 lies on the south face
    %%%%% Find the node at the southern end of the face which contains xpoint1
    node_Anext(ii,:) = nodecount(ii,:) + nx;

    %%%%%% extract n, s, e, w faces from AllNormalvectors
    a = Allvectors(node_Anext(ii,:),:);

    %%%%%% store the new results (exclude the node at the northern face of the node southofCP)
    xpt2_labelA{ii,1} = NaN;
    xpt2_labelA{ii,2} = a{2};
    xpt2_labelA{ii,3} = a{3};
    xpt2_labelA{ii,4} = a{4};

elseif Idx1_labelA(ii,:) == 3      %# xpoint1 lies on the east face
    %%%%% Find the node at the eastern end of the face which contains xpoint1
    node_Anext(ii,:)  = nodecount(ii,:) + 1;

    %%%%%% extract n, s, e, w faces from AllNormalvectors
    a = Allvectors(node_Anext(ii,:),:);

    %%%%%% store the new results (exclude the node at the western face of the node eastofCP)
    xpt2_labelA{ii,1} = a{1};
    xpt2_labelA{ii,2} = a{2};
    xpt2_labelA{ii,3} = a{3};
    xpt2_labelA{ii,4} = NaN;

elseif Idx1_labelA(ii,:) == 4      %# xpoint1 lies on the west face
    %%%%% Find the node at the western end of the face which contains xpoint1
    node_Anext(ii,:)  = nodecount(ii,:) - 1;

    %%%%%% extract n, s, e, w faces from AllNormalvectors
    a = Allvectors(node_Anext(ii,:),:);

    %%%%%% store the new results (exclude the node at the eastern face of the node westofCP)
    xpt2_labelA{ii,1} = a{1};
    xpt2_labelA{ii,2} = a{2};
    xpt2_labelA{ii,3} = NaN;
    xpt2_labelA{ii,4} = a{4};

end
end
功能:

clc; clear; close all;

Img = [3 3 3 2 2 
       3 3 3 2 2 
       3 1 2 2 2 
       1 1 2 2 2 
       1 1 1 1 2];

%%%%%% Plot the image
figure, imshow(Img, [], 'InitialMagnification','fit')   
grid on; 
hold on;

%# Find the size of the image   
[row, col] = size(Img);

%# Obtain total no. of internal x and y node points
ny = row - 1;  nx = col - 1;

%%%%%%%%%%% Obtain full output grids 
[gridy, gridx] = meshgrid(1:ny, 1:nx); 
coords = [gridy(:), gridx(:)];

%%%%%%%% Obtain node counts
nodecount = nx*(coords(:,1)-1) + coords(:,2);

%%%%%%%%%%% Compute the vectors around each node
%%%%% firstly create a cell array to store your vectors
Allvectors = cell(ny*nx, 4);
for nodecount = 1:ny*nx;

    %%%%%% determine the x and y-coords of each node
    [node_y, node_x] = get_xycoord_from_nodecount(nx, nodecount);

    %%%%%% compute vectors at each face around each node
    [n, s, e, w] = find_vectors(node_y, node_x, Img);

    Allvectors{nodecount,1} = n;
    Allvectors{nodecount,2} = s;
    Allvectors{nodecount,3} = e;
    Allvectors{nodecount,4} = w;

end

%%%%%%% Determine the contactpoint nodes
cpNodes = find(( any(strcmp('A+', Allvectors),2) | any(strcmp('A-', Allvectors),2)) & (any(strcmp('B+', Allvectors),2) | any(strcmp('B-', Allvectors),2)))


%%%%%%% find the node as well as coordinates of the contactpoints
nodecount        = cpNodes;
[node_y, node_x] = get_xycoord_from_nodecount(nx, nodecount);


%%%%%%% find all other x-points around cp
%# extract vectors for all x-points for pixel label 1
xpoint1 = Allvectors(cpNodes,:);  

%# create an empty cell array wherein the vectors around the faces of each new node will be stored
xpt2_labelA = cell(numel(cpNodes), 4);
xpt3_labelA = cell(numel(cpNodes), 4);

for ii = 1: numel(cpNodes)

    %# COMPUTATIONS FOR pixel label 1
    %%%%%% Determine the face where the 1st x-point (i.e. x-point1) lies
    Idx1_labelA(ii,:) = find(strcmp('A+', xpoint1(ii,:)) | strcmp('A-', xpoint1(ii,:)))


    %%%%%% Determine the face where the 2nd x-point (i.e. x-point2) lies
    %# firstly find the next Node from the previous   
    [xpt2_labelA, a, node_Anext] = find_node_next(ii, nodecount, nx, Idx1_labelA, xpt2_labelA, Allvectors);

    %%%%%% find all possible face(s) where x-point2 lies for both Solid phase
    if find(strcmp('A+', xpt2_labelA(ii,:)) | strcmp('A-', xpt2_labelA(ii,:))) > 0
         Idx2_labelA(ii,:)   = find(strcmp('A+', xpt2_labelA(ii,:)) | strcmp('A-', xpt2_labelA(ii,:)));
    else
        continue  
    end


    %%%%%% Determine the face where the 3rd x-point (i.e. x-point2) lies
         % repeat the steps for x-point2, however, rename the indices and cell
         % arrays with 2 or 3 where appropriate



    %# COMPUTATIONS FOR pixel label 3
    %%%%%% Determine the face where the 1st x-point (i.e. x-point1) lies
    Idx1_labelB(ii,:) = find(strcmp('B+', xpoint1(ii,:)) | strcmp('B-', xpoint1(ii,:)));



    %%%%%%%%%%%%%%%%%%%%%%%%%%%% COLLATE vectors from all X-Points found:
    %# Average unit vector 'A'
    Va = [(ya1 + ya2 +... yan)/Na, (xa1 + xa2 +... xan)/Na];  % Na=total no. of X-points collated for vector A within the specified neighborhood.

    %# Average unit vector 'B'
    Vb = [(yb1 + yb2 +... ybn)/Nb, (xb1 + xb2 +... xbn)/Nb];  % Nb=total no. of X-points collated for vector B within the specified neighborhood.
     %where: ya, yb, xa, xb are components of the vectors in the directions observed
     %as an example: from the arrow shown, ya1=0, and xa1=1

    %%%%%%%%% Compute angle between average unit vectors A and B
    angle(ii) = 180 - ( acos( dot(Va(ii,:), Vb(ii,:)) / (norm(Va(ii,:)) *    norm(Vb(ii,:)) ) ) * 180/pi );
    angle(:, ii) = angle(ii); 


end
function[node_y, node_x] = get_xycoord_from_nodecount(nx, nodecount)
%This function obtains the x and y coordinates from nodecount

node_x = mod(nodecount - 1, nx) + 1;
node_y = (nodecount - node_x)/nx + 1;

end





function [n, s, e, w] = find_vectors(node_y, node_x, Img)
%This function determines if a vector exists at the north, south,
%east and western faces around each node. 

%If a normal vector exists at any of these faces, 
%the function determines if it is an 'A+', 'A-', 'B+', or a 'B-'
%
%Where '+/-' is for +ve or -ve x or y-direction, and 'A' or 'B' tells if it
%is a vector resulting from a pixel labels 1|2,  or 3|2

n = NaN; s = NaN; e = NaN; w = NaN;

%# NORTH FACE
if Img(node_y, node_x + 1) == 2
    if Img(node_y, node_x) == 1
        n = 'A+'; 
    elseif Img(node_y, node_x) == 3
        n = 'B+';    
    end
elseif Img(node_y, node_x) == 2
    if Img(node_y, node_x + 1) == 1
        n = 'A-';
    elseif Img(node_y, node_x + 1) == 3
        n = 'B-';
    end
end


%# SOUTH FACE
if Img(node_y + 1, node_x + 1) == 2
    if Img(node_y + 1, node_x) == 1
        s = 'A+';
    elseif Img(node_y + 1, node_x) == 3
        s = 'B+'; 
    end
elseif Img(node_y + 1, node_x) == 2
    if Img(node_y + 1, node_x + 1) == 1
        s = 'A-';
    elseif Img(node_y + 1, node_x + 1) == 3
        s = 'B-';
    end
end


%# EAST FACE
if Img(node_y + 1, node_x + 1) == 2
    if Img(node_y, node_x + 1) == 1
        e = 'A+';
    elseif Img(node_y, node_x + 1) == 3
        e = 'B+';
    end
elseif Img(node_y, node_x + 1) == 2
    if Img(node_y + 1, node_x + 1) == 1
        e = 'A-';
    elseif Img(node_y + 1, node_x + 1) == 3
        e = 'B-';
    end
end


%# WEST FACE
if Img(node_y + 1, node_x) == 2
    if Img(node_y, node_x) == 1
        w = 'A+';
    elseif Img(node_y, node_x) == 3
        w = 'B+';
    end
elseif Img(node_y, node_x) == 2
    if Img(node_y + 1, node_x) == 1
        w = 'A-';
    elseif Img(node_y + 1, node_x) == 3
        w = 'B-';
    end
end



end





function [xpt2_labelA, a, node_Anext] = find_node_next(ii, nodecount, nx, Idx1_labelA, xpt2_labelA, Allvectors)


if Idx1_labelA(ii,:) == 1    %# xpoint1 lies on the north face
    %%%%% Find the node at the northern end of the face which contains xpoint1
    node_Anext(ii,:) =  nodecount(ii,:) - nx;

    %%%%%% extract the n, s, e, w faces from AllNormalvectors
    a = Allvectors(node_Anext(ii,:),:);

    %%%%%% store the new results (exclude the existing result at the southern face of the node northofCP)
    xpt2_labelA{ii,1} = a{1};
    xpt2_labelA{ii,2} = NaN;
    xpt2_labelA{ii,3} = a{3};
    xpt2_labelA{ii,4} = a{4};

elseif Idx1_labelA(ii,:) == 2    %# xpoint1 lies on the south face
    %%%%% Find the node at the southern end of the face which contains xpoint1
    node_Anext(ii,:) = nodecount(ii,:) + nx;

    %%%%%% extract n, s, e, w faces from AllNormalvectors
    a = Allvectors(node_Anext(ii,:),:);

    %%%%%% store the new results (exclude the node at the northern face of the node southofCP)
    xpt2_labelA{ii,1} = NaN;
    xpt2_labelA{ii,2} = a{2};
    xpt2_labelA{ii,3} = a{3};
    xpt2_labelA{ii,4} = a{4};

elseif Idx1_labelA(ii,:) == 3      %# xpoint1 lies on the east face
    %%%%% Find the node at the eastern end of the face which contains xpoint1
    node_Anext(ii,:)  = nodecount(ii,:) + 1;

    %%%%%% extract n, s, e, w faces from AllNormalvectors
    a = Allvectors(node_Anext(ii,:),:);

    %%%%%% store the new results (exclude the node at the western face of the node eastofCP)
    xpt2_labelA{ii,1} = a{1};
    xpt2_labelA{ii,2} = a{2};
    xpt2_labelA{ii,3} = a{3};
    xpt2_labelA{ii,4} = NaN;

elseif Idx1_labelA(ii,:) == 4      %# xpoint1 lies on the west face
    %%%%% Find the node at the western end of the face which contains xpoint1
    node_Anext(ii,:)  = nodecount(ii,:) - 1;

    %%%%%% extract n, s, e, w faces from AllNormalvectors
    a = Allvectors(node_Anext(ii,:),:);

    %%%%%% store the new results (exclude the node at the eastern face of the node westofCP)
    xpt2_labelA{ii,1} = a{1};
    xpt2_labelA{ii,2} = a{2};
    xpt2_labelA{ii,3} = NaN;
    xpt2_labelA{ii,4} = a{4};

end
end
接下来,我希望在每个CPNode周围指定一个邻域(例如:请参见图中的红色虚线),然后找到该邻域内的所有x点并对其进行整理

从上面的示例图像中获得的结果如下:

从图中可以看出:

%there are 3 x-points for Vector A (pixels 1|2), hence,
ya1 = 0, ya2 = 0, ya3 = -1
xa1 = +1, xa2 = +1, xa3 = 0

%also, there are 4 x-points for Vector B (pixels 3|2)
yb1 = +1, yb2 = +1, yb3 = 0, yb4 = 0
xb1 = 0, xb2 = 0, xb3 = +1, xb4 = +1

%average vector A:
Va = [(ya1 + ya2 + ya3)/3, (xa1 + xa2 + xa3)/3];
   %ans: 
   Va = [-1/3, 2/3]

%average vector B:
Vb = [(yb1 + yb2 + yb3 + yb4)/4, (xb1 + xb2 + xb3 + xb4)/4];
   %ans: 
   Vb = [0.5, 0.5]

%angle between A and B:
angle = 180 - ( acos( dot(Va, Vb) / (norm(Va) * norm(Vb) ) ) * 180/pi )
   %ans:
   angle = 108.4349
老实说,我不知道该怎么做——这就是为什么我需要帮助的原因。 非常感谢您在这方面的预期帮助/建议/建议


我觉得我以前见过这个问题。您的示例输入所需的输出是什么?您的
A+
B-
标签是输出的一部分,还是只是实现最终结果的一种手段?谢谢@bicker。A+及其类似物是实现最终结果的手段。它们是指定+或-方向上的单位法向量。我将为A和B整理它们,然后使用点积来获得两者之间的角度。最终输出是两个向量A和B之间的角度。整理完x点后,我将为每个A和B向量整理的所有x点获得平均单位法向量。例如,在上图中,
A
的第一个x点具有向量
[y1,x1]=[0,1]红色标记为“x”的第二个x点具有向量
[y2,x2]=[0,1],最后一个x点的向量为
[y3,x3]=[-1,0]
我将计算这些向量的平均值,也将计算
B
的平均值,然后用点积计算它们之间的角度。我实际上是想让问题尽可能简单,以便有人能尽快抓住帖子。尽管如此,我还是会看看我还能添加哪些内容来让它更清晰。非常感谢。我做了一次编辑,在编辑中我将最终输出添加到代码中。预期输出是向量
A和B之间的角度
所有向量
(参见上图后面的含义),尽管从输入图像获得的向量不一定是输出;这只是实现最终产出的一种手段。