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

时间:2017-03-15 01:18:55

标签: matlab image-processing

这可能有点长,但看看你是否有耐心会很有趣。

考虑一下我有下面的图片。 enter image description here

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

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

xpoint1 是一个包含所有接触点每个面上所有向量的单元格数组。

x-point1是具有标签1和2的相邻像素的面,使得矢量方向为“A +”或“A-”,具体取决于标签2的方向。这对于像素标签3是相同的和2,使得矢量方向是“B +”或“B - ”。

到目前为止我尝试了什么:

我能够找到尽可能多的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

展望未来,我想要的是在每个cpNodes周围指定一个邻域(例如:参见图中的红色虚线),然后查找该邻域内的所有x点以及整理它们。

从我上面的示例图片中获得的结果如下: enter image description here

从图中可以看出:

%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

老实说,我不知道如何解决这个问题 - 这就是我在这里需要帮助的原因。 非常感谢您对此的预期帮助/建议/意见。

enter image description here

enter image description here

0 个答案:

没有答案
相关问题