Image enhancement actual combat of MATLAB image processing II

I = imread('rice.png');% read the rice grain image
figure(1); subplot(221)
imshow(I);% display the original image
background = imopen(I,strel('disk',15)); % Morphology open operation
figure,
surf(double(background(1:8:end,1:8:end))),...
zlim([0 255]);% display background change
set(gca,'ydir', 'reverse');
I2 = imsubtract(I,background);% subtract the background
figure(1); subplot(222)
imshow(I2)% display the image after removing the background
I3 = imadjust(I2);% adjust the contrast of the image
figure(1); subplot(223),% display the image after contrast adjustment
imshow(I3);
level = graythresh(I3);% set threshold
bw = im2bw(I3,level);% generate binary image
figure(1 ); subplot(224)
imshow(bw)% Display binary image
[labeled,numObjects] = bwlabel(bw,4);% Generate label matrix
numObjects% Calculate the number of target objects in the image
rect = [105 125 10 10] ;%Fixed image area
grain = imcrop(labeled,rect)% Determine a part of the labeling matrix
RGB_label = label2rgb(labeled, @spring,'c','shuffle');% pseudo-color image
figure,
imshow(RGB_label)% display pseudo-color image
graindata = regionprops (labeled,'basic')% returns the basic attributes of the image
graindata(50).Area% the area of ​​the 50th target object
allgrains = [graindata.Area];% generates the area matrix of all target objects
max_area = max(allgrains)% Find the rice grain with the largest area
biggrain = find(allgrains==max_area)% Find the label of the largest rice grain
mean(allgrains)% Find the average value of all rice grains
nbins = 20;
figure,
hist(allgrains,nbins)% shows the area of ​​all rice grains Histogram
Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here

Guess you like

Origin blog.csdn.net/m0_38127487/article/details/115259543