2012-11-18 6 views
2

내가 이러한 값의 "그룹화 매트릭스"를 만들고 싶다 "영역"그룹 인접 셀, MATLAB

brk_group = 

    1  1  1  1 
    3  2  2  3 
    3  3  3  3 

의 다음의 행렬을 말한다. 그룹은 동일한 영역 값을 가진 인접한 셀 (위쪽, 오른쪽, 아래쪽, 왼쪽)으로 정의됩니다.

영역 값은 여러 그룹을 가질 수 있습니다. 영역을 공간적으로 조인 할 필요가 없기 때문입니다.

데이터 : 이하 "MY CURRENT CODE"문제 란

brk_group = [1 1 1 1; 3 2 2 3; 3 3 3 3] 

....

해당 영역 값 "3"이 셀 (2,4)이 연결되는 영역 값 셀 (2,1)에서 "3"하지만 현재 응답에있는 루프 (2,4)에 도달하면이 연결이 표시되지 않고 셀 그룹 (2,1)과 셀 그룹 (2)이 잘못 그룹화됩니다. 4) ...

편집 : 기본 MATLAB을 사용하고 싶습니다. Image Processing Toolbox를 사용하지 마십시오. =)

CLARIFICATION : 영역은 동일한 영역 값을 갖는 ALL 셀로 정의됩니다. 그룹은 동일한 영역 값을 갖는 모든 공간적으로 조인 된 셀 (위쪽, 아래쪽, 왼쪽, 오른쪽)으로 정의됩니다.

내 현재 코드 : 난 정말 MATLAB 교환에 대한 발굴했다 코드에 의해 해결

function [groupMat groupIds] = createZoneGroups(zoneMat) 
% 
% groupMat = createZoneGroups(zoneMat) 
% 
% Groups zones spatially. 
% 
% Input: 
%  zoneMat: nxm matrix of "zones". 
% 
% Output: 
%  groupMat: nxm matrix of group ID's 
%  groupIds: An array of unique group ID's 
% 
% Note: A group is defined as spatially connected same zones. Diagonal 
%  cells are not spatially connected. 
% 
% Author: Kyle W. Purdon 
% 
groupIds = []; 
groupMat = nan(size(zoneMat)); 
for rowIdx = 1:size(zoneMat,1) 
    for colIdx = 1:size(zoneMat,2) 

     % Check if the cell is nan, if so group(r,c)=nan 
     if isnan(zoneMat(rowIdx,colIdx)) 
      continue; 
     end 

     % Check if the current cell has a group, if it does, break. 
     if ~isnan(groupMat(rowIdx,colIdx)) 
      continue; 
     end 

     % Check the surrounding cells for groups, if any of the surrounding 
     % cells (1) have a group, and (2) are in the same zone, assighn the 
     % current cell that group. If not assign the current cell a new 
     % group. 

     % Check top cell 
     if rowIdx > 1 && ~isnan(groupMat(rowIdx-1,colIdx)) 
      if isequal(zoneMat(rowIdx,colIdx),zoneMat(rowIdx-1,colIdx)); 
       groupMat(rowIdx,colIdx) = groupMat(rowIdx-1,colIdx); 
       continue; 
      end 
     end 
     % Check right cell 
     if colIdx < size(zoneMat,2) && ~isnan(groupMat(rowIdx,colIdx+1)) 
      if isequal(zoneMat(rowIdx,colIdx),zoneMat(rowIdx,colIdx+1)); 
       groupMat(rowIdx,colIdx) = groupMat(rowIdx,colIdx+1); 
       continue; 
      end 
     end 
     % Check bottom cell 
     if rowIdx < size(zoneMat,1) && ~isnan(groupMat(rowIdx+1,colIdx)) 
      if isequal(zoneMat(rowIdx,colIdx),zoneMat(rowIdx+1,colIdx)); 
       groupMat(rowIdx,colIdx) = groupMat(rowIdx+1,colIdx); 
       continue; 
      end 
     end 
     % Check left cell 
     if colIdx > 1 && ~isnan(groupMat(rowIdx,colIdx-1)) 
      if isequal(zoneMat(rowIdx,colIdx),zoneMat(rowIdx,colIdx-1)); 
       groupMat(rowIdx,colIdx) = groupMat(rowIdx,colIdx-1); 
       continue; 
      end 
     end 

     % If the loop gets to this point, assign a new groupId to the cell. 
     if isempty(groupIds) 
      groupIds = 1; % Start with group #1 
     else 
      groupIds(end+1) = groupIds(end)+1; %Increment the last group by 1. 
     end 

     groupMat(rowIdx,colIdx) = groupIds(end); 

    end 
end 
end 

답변