How to create a new image with average color of input image in the segmentation region?

2 vues (au cours des 30 derniers jours)
I have an image and a segmentation mask. How can I generate a new image with each segmented region containing the average color of that region in the original image?

Réponses (1)

DGM
DGM le 13 Oct 2024
There are several ways to approach this. I'm going to assume "color" implies that the image may either be RGB or grayscale.
Consider the example:
% a color image
inpict = imread('triad.png');
imshow(inpict)
% a mask selecting objects in the image
mk = max(inpict,[],3) > 32;
% isolate mask blobs
[L nblobs] = bwlabel(mk);
% compose the output image
outpict = inpict;
for k = 1:nblobs
thismask = L == k;
for c = 1:size(inpict,3)
thischannel = outpict(:,:,c);
thischannel(thismask) = mean(thischannel(thismask));
outpict(:,:,c) = thischannel;
end
end
imshow(outpict)
Alternatively, this can be done a bit quicker with bwconncomp() instead, though perhaps the composition is a bit less obvious. Conveniences like cc2bw() didn't exist until recently.
% a color image
inpict = imread('triad.png');
% a mask selecting objects in the image
mk = max(inpict,[],3) > 32;
% isolate mask blobs
CC = bwconncomp(mk);
% compose the output image
outpict = reshape(inpict,[],size(inpict,3));
for k = 1:CC.NumObjects
idx = CC.PixelIdxList{k};
thiscolor = mean(outpict(idx,:),1);
outpict(idx,:) = repmat(thiscolor,[numel(idx) 1]);
end
outpict = reshape(outpict,size(inpict));
imshow(outpict)
The composition could probably also be done using labeloverlay(), but you'd still need something similar to the above examples in order to construct a colortable of mean blob colors. Also, labeloverlay() would be an anachronism here.
For a strictly grayscale image, the mean blob colors could be obtained from regionprops(), though composition would still require extra work.
  1 commentaire
DGM
DGM le 13 Oct 2024
If we want to go down the anachronism path, then:
% a color image
inpict = imread('triad.png');
% a mask selecting objects in the image
mask = max(inpict,[],3) > 32;
% MIMT blobcolor and IPT labeloverlay()
CT = blobcolor_forum(inpict,mask,'mean');
outpict = labeloverlay(inpict,bwlabel(mask),'colormap',CT,'transparency',0);
imshow(outpict)
Like I said, labeloverlay() didn't exist until R2017b, and MIMT blobcolor() didn't exist until about a month ago. The attached version didn't exist until today.

Connectez-vous pour commenter.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by