MATLAB中regiongrow函数怎么用?

如题所述

 Matlab中开发一个名为regiongrow的M函数来完成基本的区域生长。

该函数为 [g,NR,SI,TI]=regiongrow(f,S,T) 输入中:f为输入图像,S为种子,T为阈值(标量时为全 局阈值) 输出中:g为分割后的图像,NR为连通区域的数目,SI为一幅 包含有种子点的图像。SI也为一幅图像,包含在连通性处理前, 通过阈值检测的像素。 


Matlab程序举例如下:(程序使用时候,regiongrow一定要先定义,这个我不用交吧?)

i=imread('eight.tif');

figure(1);imshow(i);

% i=doulbe(i);

[m,n]=size(i);

[y1,x1]=getpts;

x1=round(x1);y1=round(y1);

seed=[x1,y1];

th_mean=40;

yout=regiongrow(i,seed,th_mean);

figure(2);imshow(yout);title('区域增长');

%原图:

%增长之后的:

追问

我切切实实菜鸟一个,所以还真不知道怎么定义,regiongrow?
若你有空,请帮帮我!
真心感谢,毕设急用!

追答

好吧。
%%%%%%%%%%%%%%regiongrow区域增长
function yout=regiongrow(I,seed,th_mean)
[m,n]=size(I);
[I h]=size(seed);
yout=zeros(m,n);
for i=1:I
yout(seed(i,1),seed(i,2))=1;
end
for i=1:I
sum(seed(i,1),seed(i,2));
end
seed_mean=mean(sum);
ok=true;
s_star=1;
s_end=1;
while ok
ok=false;
for i=s_star:s_end
x=seed(i,1);
y=seed(i,2);
if x>2&&(x+1)2&&(y+1)<n
for u=-1:1
for v=-1:1
if yout(x+u,y+v)==0
avs(I(x+u,y+v)-seed_mean)<=th_mean
yout(x+u,y+v)=I
ok=true;
seed=[seed;[x+u,y+v]];
end
end
end
end
end
s_star=s_end+1;
[I h]=size(seed);
s_end=1
end

温馨提示:答案为网友推荐,仅供参考
第1个回答  推荐于2016-07-06
使用方法:
clc;
A=importdata('D:/seedpoint.txt');
str = A.textdata{1};
str1 = strrep(str,'/','\');
I = im2double(imread( str1));
x = A.data(1,1);
y = A.data(2,1);
reg_maxdist = A.data(3,1);
%function J=regiongrowing(I,x,y,reg_maxdist)
% This function performs "region growing" in an image from a specified
% seedpoint (x,y)
%
% J = regiongrowing(I,x,y,t)
%
% I : input image
% J : logical output image of region
% x,y : the position of the seedpoint (if not given uses function getpts)
% t : maximum intensity distance (defaults to 0.2)
%
% The region is iteratively grown by comparing all unallocated neighbouring pixels to the region.
% The difference between a pixel's intensity value and the region's mean,
% is used as a measure of similarity. The pixel with the smallest difference
% measured this way is allocated to the respective region.
% This process stops when the intensity difference between region mean and
% new pixel become larger than a certain treshold (t)
%
% Example:
%
% I = im2double(imread('medtest.png'));
% x=198; y=359;
% J = regiongrowing(I,x,y,0.2);
% figure, imshow(I+J);
%
% Author: D. Kroon, University of Twente
%if(exist('reg_maxdist','var')==0), reg_maxdist=0.2; end
%if(exist('y','var')==0), figure, imshow(I,[]); [y,x]=getpts; y=round(y(1)); x=round(x(1)); end
J = zeros(size(I)); % Output
Isizes = size(I); % Dimensions of input image
reg_mean = I(x,y); % The mean of the segmented region
reg_size = 1; % Number of pixels in region
% Free memory to store neighbours of the (segmented) region
neg_free = 10000; neg_pos=0;
neg_list = zeros(neg_free,3);
pixdist=0; % Distance of the region newest pixel to the regio mean
% Neighbor locations (footprint)
neigb=[-1 0; 1 0; 0 -1;0 1];
% Start regiogrowing until distance between regio and posible new pixels become
% higher than a certain treshold
while(pixdist
% Add new neighbors pixels
for j=1:4,
% Calculate the neighbour coordinate
xn = x +neigb(j,1); yn = y +neigb(j,2);

% Check if neighbour is inside or outside the image
ins=(xn>=1)&&(yn>=1)&&(xn<=Isizes(1))&&(yn<=Isizes(2));

% Add neighbor if inside and not already part of the segmented area
if(ins&&(J(xn,yn)==0))
neg_pos = neg_pos+1;
neg_list(neg_pos,:) = [xn yn I(xn,yn)]; J(xn,yn)=1;
end
end
% Add a new block of free memory
if(neg_pos+10>neg_free), neg_free=neg_free+10000; neg_list((neg_pos+1):neg_free,:)=0; end

% Add pixel with intensity nearest to the mean of the region, to the region
dist = abs(neg_list(1:neg_pos,3)-reg_mean);
[pixdist, index] = min(dist);
J(x,y)=2; reg_size=reg_size+1;

% Calculate the new mean of the region
reg_mean= (reg_mean*reg_size + neg_list(index,3))/(reg_size+1);

% Save the x and y coordinates of the pixel (for the neighbour add proccess)
x = neg_list(index,1); y = neg_list(index,2);

% Remove the pixel from the neighbour (check) list
neg_list(index,:)=neg_list(neg_pos,:); neg_pos=neg_pos-1;
end
imwrite(J,'j.png');
J2gray=rgb2gray(imread('j.png'));
%imwrite(L,'J2gray.png')
Isize=size(J2gray);
for i=1:Isize(1)
for j=1:Isize(2)
if(J2gray(i,j)==76) J2gray(i,j)=0;
else J2gray(i,j)=3;
end;
end;
end;
imwrite(J2gray,'J2gray_0_3.png');
% Return the segmented area as logical matrix
J=J>1;