% load image (colorful fabric acquired with frame grabber and camera)
rgb = imread('UnitedArabEmirates_Dubai_05_006.jpg'); % Matrox frame grabber + Pulnix camera
% imshow(rgb)
% explore color content using image viewer
% imview(rgb)
% smooth image (reduce noise/color variation)
rgb = imfilter(rgb,ones(3,3)/9);
% imview(rgb)
% view image and RGB layers (nonuniform illumination)
% figure(1), set(1,'position',[99 79 826 589])
% subplot(2,2,1), subimage(rgb), title('fabric image'), axis off
% subplot(2,2,2), map=gray(256); map(:,2:3)=0; subimage(rgb(:,:,1),map), title('red layer'), axis off
% subplot(2,2,3), map=gray(256); map(:,[1 3])=0; subimage(rgb(:,:,2),map), title('green layer'), axis off
% subplot(2,2,4), map=gray(256); map(:,1:2)=0; subimage(rgb(:,:,3),map), title('blue layer'), axis off
% RGB histograms (poor separability)
% figure(1), set(1,'position',[452 68 560 420])
% figure(2), set(2,'position',[16 269 560 420])
c='rgb';
for i=1:3
n=hist(reshape(double(rgb(:,:,i)),[size(rgb,1)*size(rgb,2) 1]),0.5:256);
line(0:255,n,'color',c(i))
end
axis tight, xlim([0 255]), box on
xlabel intensity, ylabel population, title histograms
% convert image to L*a*b* color space (transform)
cform = makecform('srgb2lab');
lab = applycform(rgb,cform);
% view components (note illumination free)
% figure(1), figure(2)
% subplot(2,2,1), subimage(rgb), title('fabric image'), axis off
% subplot(2,2,2), subimage(lab(:,:,1)), title('L* layer'), axis off
% subplot(2,2,3), map=gray(256); map(:,3)=0; map(:,2)=map(end:-1:1,2); subimage(lab(:,:,2),map), title('a* layer'), axis off
% subplot(2,2,4), map=gray(256); map(:,3)=map(end:-1:1,3); subimage(lab(:,:,3),map), title('b* layer'), axis off
figure;
mask=mask_generator(rgb);
bw=repmat(logical(sum(mask,3)),[1 1 3]);
im=rgb; im(~bw)=nan;
figure(3), imshow(im)
% color markers (region average values of L*, a* and b*)
l=lab(:,:,1); a=lab(:,:,2); b=lab(:,:,3);
for i = 1:size(mask,3)
cMark(i,1) = mean2(l(mask(:,:,i)));
cMark(i,2) = mean2(a(mask(:,:,i)));
cMark(i,3) = mean2(b(mask(:,:,i)));
end
% classify every pixel using nearest neighbor rule
distance = zeros([size(rgb,1) size(rgb,2) size(mask,3)]);
for i=1:size(mask,3)
dx=double(a)-cMark(i,2); dy=double(b)-cMark(i,3);
distance(:,:,i) = sqrt(dx.^2 + dy.^2);
end
cLabels=0:(size(mask,3)-1); [value,index]=min(distance,[],3);
label=cLabels(index);
% display segmented image
cMap=double(applycform(uint8(cMark),makecform('lab2srgb')))/255;
figure('position',[9 344 1005 334])
subplot(121), subimage(rgb), title('Original Image'), axis off,
subplot(122), subimage(label+1,cMap), title('Segmented Image'), axis off
%----------------------------------------------------------------------
% Step 4. Perform statistical analysis on distinct objects
%----------------------------------------------------------------------
% compute histograms & statistics
% bins=0.5:20; pop=zeros(length(bins),5);
% for i=1:5 %loop thru each color
% mask = (label==i); %segment distinct regions
% L = bwlabel(mask); %label region
% stats = regionprops(L,'Area'); %extract areas of each
% A=[stats.Area]; r=sqrt(A/pi); %convert areas to radii
% pop(:,i) = hist(r,bins)'; %generate histogram
% mu(i)=mean(r); sigma(i)=std(r); %first order stats
% end
% display histograms for each color
% figure('position',[303 65 461 241]), h=plot(bins,pop);
% for i=1:5, set(h(i),'color',cMap(i+1,:),'linewidth',2), end
% xlabel radius, ylabel pop, title histogram
% set(gca,'position',[0.13 0.2 0.775 0.65],'color',cMap(1,:)), shg
% zoom Y axis to see scarce, large contributions better
% ylim([0 50])
% display first order stats for each color
% [dummy,order]=sort(mu);
% for index=1:5
% i=order(index);
% text(10,10+7*index,sprintf('\\mu=%.1f, \\sigma=%.1f',mu(i),sigma(i)),...
% 'color',cMap(i+1,:),'fontsize',15)
% end
评论30
最新资源