%程序代码:test2.m
clear, close all,I = imread('rice.tif');
imshow(I)
background = imopen(I,strel('disk',15));
figure,imshow(background)
figure, surf(double(background(1:8:end,1:8:end))),zlim([0 255]);
(gca,'ydir','reverse');
I2 = imsubtract(I,background); figure, imshow(I2);
I3 = imadjust(I2, stretchlim(I2),[0 1]);figure, imshow(I3);
level = graythresh(I3);bw = im2bw(I3,level); figure, imshow(bw)
[labeled,numObjects] = bwlabel(bw,4);
grain = imcrop(labeled)
RGB_label = label2rgb(labeled, @spring,'c','shuffle');
figure,imshow(RGB_label);
graindata = regionprops(labeled,'basic')
graindata(51).Area, graindata(51).BoundingBox, graindata(51).Centroid
allgrains=[graindata.Area]
max(allgrains)
biggrain = find(allgrains==ans)
mean(allgrains)
hist(allgrains,20)
clear, close all,I = imread('rice.tif');
imshow(I)
background = imopen(I,strel('disk',15));
figure,imshow(background)
figure, surf(double(background(1:8:end,1:8:end))),zlim([0 255]);
(gca,'ydir','reverse');
I2 = imsubtract(I,background); figure, imshow(I2);
I3 = imadjust(I2, stretchlim(I2),[0 1]);figure, imshow(I3);
level = graythresh(I3);bw = im2bw(I3,level); figure, imshow(bw)
[labeled,numObjects] = bwlabel(bw,4);
grain = imcrop(labeled)
RGB_label = label2rgb(labeled, @spring,'c','shuffle');
figure,imshow(RGB_label);
graindata = regionprops(labeled,'basic')
graindata(51).Area, graindata(51).BoundingBox, graindata(51).Centroid
allgrains=[graindata.Area]
max(allgrains)
biggrain = find(allgrains==ans)
mean(allgrains)
hist(allgrains,20)