|
% Correction for uneven illumination
clc; close all; clear all;
I=imread('rice.png');
subplot(3,3,1); imshow(I); title('raw image');
background=imopen(I,strel('disk',15));
% IM2=imopen(IM,SE), gray-scale opening(erosion followed by dilation)
% suppress small bright regions whilst keep the rest relatively unchanged
subplot(3,3,2); imshow(background); title('background');
I2=imsubtract(I,background); % Z=imsubtract(X,Y), the same size and class
% lift out image details independently of background intensity variantion
% Equivalently, I2=imtophat(I,strel('disk',15));
% IM2=imtophat(IM,SE) top hat filtering to correct uneven illumination
subplot(3,3,3); imshow(I2); title('raw with illumination subtracted');
I3=imadjust(I2,stretchlim(I2),[]); % Low_High=stretchlim(I,Tol)
subplot(3,3,4); imshow(I3); title('contrast-enhanced');
BW=im2bw(I3,graythresh(I3));
BW=bwareaopen(BW,50); % BW2=bwareaopen(BW,P)
% remove connected objects have fewer than P pixels, and P is 8 by default
subplot(3,3,5); imshow(BW); title('denoised image');
cc=bwconncomp(BW,4); % identify all connected components
% CC=bwconncomp(BW,CONN); CONN specifies the connectivity, 8 by default
% CC is a structure with fields: ImageSize, NumObjects, PixelIdxList
grain=false(size(BW)); % false array, logcial(0) in effect
grain(cc.PixelIdxList{50})=true; % examine one object
subplot(3,3,6); imshow(grain); title('one grain');
labeled=labelmatrix(cc); % create a label matrix from output of bwconncomp
rgb_label=label2rgb(labeled,@spring,'c','shuffle'); % pseudo-color image
subplot(3,3,7); imshow(rgb_label); title('colormap');
graindata=regionprops(cc,'basic'); % graindata is a structure
% By defuault, the properties are Area, Centroid, BoundingBox
% for the 50th comp., graindata(50).Area..
grain_areas=[graindata.Area];
nbins=20; subplot(3,3,8);
hist(grain_areas,nbins); title('histogram of grain area');
[min_area,idx]=min(grain_areas);
grain=false(size(BW));
grain(cc.PixelIdxList{idx})=true;
subplot(3,3,9); imshow(grain);
title('smallest grain');
Archiver|手机版|科学网 ( 京ICP备07017567号-12 )
GMT+8, 2025-1-10 07:58
Powered by ScienceNet.cn
Copyright © 2007- 中国科学报社