% extracts the center (cc,cr) and radius of the largest blob
% flag = 0 if failure
function [cc,cr,radius,flag]=extractball(Imwork,Imback,fig1,fig2,fig3,index)
cc = 0;
cr = 0;
radius=0;
flag=0;
[MR,MC,Dim] = size(Imback); %m = size(X,dim) returns the size of the dimension of X specified by scalar dim.
% subtract background & select pixels with a big difference
fore = zeros(MR,MC); %Create array of all zeros,B = zeros(m,n,p,...) or B = zeros([m n p ...]) returns an m-by-n-by-p-by-... array of zeros.
fore = (abs(Imwork(:,:,1)-Imback(:,:,1)) > 10) ...
| (abs(Imwork(:,:,2) - Imback(:,:,2)) > 10) ...
| (abs(Imwork(:,:,3) - Imback(:,:,3)) > 10);
% erode to remove small noise
foremm = bwmorph(fore,'erode',2); %Morphological operations on binary images,即二值图的形态学操作,BW2 = bwmorph(BW,operation,n) applies the operation n times. n can be Inf, in which case the operation is repeated until the image no longer changes.
%'erode' ,function:Performs erosion using the structuring element ones(3).erosion腐蚀, 侵蚀; 磨损。structuring element ones(3)
%Y = ones(n) returns an n-by-n matrix of 1s. An error message appears if n is not a scalar.
if fig2 > 0
figure(fig2)
clf
imshow(foremm)
%eval(['imwrite(uint8(foremm),''BCLEAN/cln',int2str(index),'.jpg'',''jpg'')']);
end
% select largest object
labeled = bwlabel(foremm,4); %Label connected components in 2-D binary image
%Label标签, 符号 connected连接的, 有关系的,有社交〔职业、商业〕关系的 ,有血统〔婚姻〕关系的
%L = bwlabel(BW, n) returns a matrix L, of the same size as BW, containing labels for the connected objects in BW.
%The variable n can have a value of either 4 or 8, where 4 specifies 4-connected objects and 8 specifies 8-connected objects. If the argument is omitted, it defaults to 8.
%specify:详述,指定,具体说明;把…写入说明书;详细列举 ,提出…的条件,明确提出;详细说明
stats = regionprops(labeled,['basic']); %Measure properties of image regions
%STATS = regionprops(BW, properties) measures a set of properties for each connected component (object) in the binary image, BW. The image BW is a logical array; it can have any dimension.
%STATS is a structure array with length equal to the number of objects in BW, CC.NumObjects, or max(L(:)). The fields of the structure array denote different properties for each region, as specified by properties.
%properties can be a comma-separated list of strings, a cell array containing strings, the single string 'all', or the string 'basic'. If properties is the string 'all', regionprops computes all the shape measurements, listed in Shape Measurements.
%If called with a grayscale image, regionprops also returns the pixel value measurements, listed in Pixel Value Measurements.
%If properties is not specified or if it is the string 'basic', regionprops computes only the 'Area', 'Centroid', and 'BoundingBox' measurements. You can calculate the following properties on N-D inputs: 'Area', 'BoundingBox', 'Centroid', 'FilledArea', 'FilledImage', 'Image', 'PixelIdxList', 'PixelList', and 'SubarrayIdx'.
%properties,1.财产,资产;所有物2.房产,地产,房地产3.花园住宅4.特性,性能,属性5.所有权,财产权。comma-separated,comma逗号。centroid,[?sentr?id]质心。
[N,W] = size(stats);
if N < 1
return
end
% do bubble sort (large to small) on regions in case there are more than 1
id = zeros(N);
for i = 1 : N
id(i) = i; %id(N)={1,2,3,4,5,6,7,8,9,...N}
end
for i = 1 : N-1 %i=1,2,3,4,5,...N-1。
for j = i+1 : N %j=i+1,i+2,i+3...N。
if stats(i).Area < stats(j).Area %stats是个结构体数组,stats(k).Aera表示取得结构体数组stats的第k个单元内的域名为为Aera的区域所存储的数据
tmp = stats(i);
stats(i) = stats(j);
stats(j) = tmp;
tmp = id(i);
id(i) = id(j);
id(j) = tmp;
end
end
end
% make sure that there is at least 1 big region
if stats(1).Area < 100
return
end
selected = (labeled==id(1));
if fig3 > 0
figure(fig3)
clf
imshow(selected)
end
% get center of mass and radius of largest
centroid = stats(1).Centroid;
radius = sqrt(stats(1).Area/pi);
cc = centroid(1);
cr = centroid(2);
flag = 1;
return
评论0