Info
此问题已关闭。 请重新打开它进行编辑或回答。
I want the output of RAG code output to appear as given in the image,but appear something else please help me to get the following output
1 次查看(过去 30 天)
显示 更早的评论
function varargout = imRAG(img, varargin)
%IMRAG Region adjacency graph of a labeled image
%
% Usage:
% ADJ = imRAG(IMG);
% computes region adjacencies graph of labeled 2D or 3D image IMG.
% The result is a N*2 array, containing 2 indices for each couple of
% neighbor regions. Two regions are considered as neighbor if they are
% separated by a black (i. e. with color 0) pixel in the horizontal or
% vertical direction.
% ADJ has the format [LBL1 LBL2], LBL1 and LBL2 being vertical arrays the
% same size.
%
% [NODES, ADJ] = imRAG(IMG);
% Return two arrays: the first one is a [N*2] array containing centroids
% of the N labeled region, and ADJ is the adjacency previously described.
% For 3D images, the nodes array is [N*3].
%
%%Initialisations
% size of image
dim = size(img);
% number of dimensions
nd = length(dim);
% initialize array of neighbor regions
edges = [];
% Number of background pixels or voxels between two regions
% gap = 0 -> regions are contiguous
% gap = 1 -> there is a 1-pixel large line or surface between two adjacent
% pixels, for example the result of a watershed
gap = 1;
if ~isempty(varargin) && isnumeric(varargin{1})
gap = varargin{1};
end
shift = gap + 1;
if nd == 2
%%First direction of 2D image
% identify transitions
[i1 i2] = find(img(1:end-shift,:) ~= img((shift+1):end, :));
% get values of consecutive changes
val1 = img(sub2ind(dim, i1, i2));
val2 = img(sub2ind(dim, i1+shift, i2));
% keep only changes not involving background
inds = val1 ~= 0 & val2 ~= 0 & val1 ~= val2;
edges = unique([val1(inds) val2(inds)], 'rows');
%%Second direction of 2D image
% identify transitions
[i1 i2] = find(img(:, 1:end-shift) ~= img(:, (shift+1):end));
% get values of consecutive changes
val1 = img(sub2ind(dim, i1, i2));
val2 = img(sub2ind(dim, i1, i2+shift));
% keep only changes not involving background
inds = val1 ~= 0 & val2 ~= 0 & val1 ~= val2;
edges = [edges; unique([val1(inds) val2(inds)], 'rows')];
elseif nd == 3
%%First direction of 3D image
% identify transitions
[i1 i2 i3] = ind2sub(dim-[shift 0 0], ...
find(img(1:end-shift,:,:) ~= img((shift+1):end,:,:)));
% get values of consecutive changes
val1 = img(sub2ind(dim, i1, i2, i3));
val2 = img(sub2ind(dim, i1+shift, i2, i3));
% keep only changes not involving background
inds = val1 ~= 0 & val2 ~= 0 & val1 ~= val2;
edges = unique([val1(inds) val2(inds)], 'rows');
%%Second direction of 3D image
% identify transitions
[i1 i2 i3] = ind2sub(dim-[0 shift 0], ...
find(img(:,1:end-shift,:) ~= img(:,(shift+1):end,:)));
% get values of consecutive changes
val1 = img(sub2ind(dim, i1, i2, i3));
val2 = img(sub2ind(dim, i1, i2+shift, i3));
% keep only changes not involving background
inds = val1 ~= 0 & val2 ~= 0 & val1 ~= val2;
edges = [edges; unique([val1(inds) val2(inds)], 'rows')];
%%Third direction of 3D image
% identify transitions
[i1 i2 i3] = ind2sub(dim-[0 0 shift], ...
find(img(:,:,1:end-shift) ~= img(:,:,(shift+1):end)));
% get values of consecutive changes
val1 = img(sub2ind(dim, i1, i2, i3));
val2 = img(sub2ind(dim, i1, i2, i3+shift));
% keep only changes not involving background
inds = val1 ~= 0 & val2 ~= 0 & val1 ~= val2;
edges = [edges; unique([val1(inds) val2(inds)], 'rows')];
end
% format output to have increasing order of n1, n1<n2, and
% increasing order of n2 for n1=constant.
edges = sortrows(sort(edges, 2));
% remove eventual double edges
edges = unique(edges, 'rows');
%%Output processing
if nargout == 1
varargout{1} = edges;
elseif nargout == 2
% Also compute region centroids
N = max(img(:));
points = zeros(N, nd);
labels = unique(img);
labels(labels==0) = [];
if nd == 2
% compute 2D centroids
for i = 1:length(labels)
label = labels(i);
[iy ix] = ind2sub(dim, find(img==label));
points(label, 1) = mean(ix);
points(label, 2) = mean(iy);
end
else
% compute 3D centroids
for i = 1:length(labels)
label = labels(i);
[iy ix iz] = ind2sub(dim, find(img==label));
points(label, 1) = mean(ix);
points(label, 2) = mean(iy);
points(label, 3) = mean(iz);
<<
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/148001/image.png)
>>
end
end
% setup output arguments
varargout{1} = points;
varargout{2} = edges;
end
This is what Im calling from my main function
[n e]=imRAG(coloredLabels);
for i = 1:size(e, 1) plot(n(e(i,:), 1), n(e(i,:), 2), 'linewidth', 4, 'color', 'g'); end plot(n(:,1), n(:,2), 'bo', 'markerfacecolor', 'b');
getting following output
</matlabcentral/answers/uploaded_files/27330/my.png> but want output to be like this
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/148004/image.png)
%
1 个评论
回答(0 个)
此问题已关闭。
另请参阅
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!