I think the statistical region merging algorithm must have include the extraction part from the original image. You may need to improve your smr code to achieve that. Here I just proposed an extraction method based on the estimation of pixel values. The region with the closest average pixel values to the template will be extracted. I added something in your function, just showing you the example of yellow box extraction. Hopefully you can do the rest yourself.
for i= 2:2 % yellow box in your image
mapi = reshape(iu == largestLabels(i),size(I,1),size(I,2));
[L,Total] = bwlabel(mapi);
Sdata=regionprops(L,'BoundingBox');
for j=1:Total
Img=imcrop(I,Sdata(j).BoundingBox);
im1=double(Img(:,:,1));
im2=double(Img(:,:,2));
im3=double(Img(:,:,3));
R=mode(im1(:)); % pixel values in the template
G=mode(im2(:));
B=mode(im3(:));
end
% same procedure on your original image
bw=im2bw(originalImage); % please note that sometimes a threshold is needed to include the items you are interested but with a dark intensity
L= regionprops(bw,'BoundingBox');
N=length(L);
di = Inf;
for t=1:N
tmp=imcrop(originalImage,L(t).BoundingBox);
tmp1=tmp(:,:,1);
tmp2=tmp(:,:,2);
tmp3=tmp(:,:,3);
R1=mean(tmp1(:));
G1=mean(tmp2(:));
B1=mean(tmp3(:));
if sqrt((R-R1)^2+(G-G1)^2+(B-B1)^2)<di
di=sqrt((R-R1)^2+(G-G1)^2+(B-B1)^2);
best=t;
end
end
tmp=imcrop(originalImage,L(best).BoundingBox);
figure,imagesc(tmp)
end
The result: