is it possible to add/paint multiple colors(more than 100 up to 10000) to an RGBA image in MATLAB?

i need help with changing the colors of an attached RGBA image randomly or cutomized manner (atelast red, orange, yellow, green, blue, purple, pink, brown, gray, black and white). i am attaching a code created by @DGM for the single purple color. i would highly appreicate any help with the code for mutiple colors randomly or customized manner
[body,~,bodya] = imread('Body/maroon.png');
bodypurple = imtweak(body,'hsl',[-0.28 1 1]);
imshow(bodypurple)
imwrite(bodypurple,'purple.png','alpha',bodya)

2 commentaires

You may need to be more specific about what you want and how your current work is not yet working. You can use randi to generate random integer numbers, or rand or randn for double precision random numbers. I can see there could be a problem assigning a unique and useful name to each one of 10,000 colors so how are you looking to do that?

Connectez-vous pour commenter.

 Réponse acceptée

First off, a reminder that this is all using MIMT tools.
One way would be to do a simple hue rotation to create a series of modified images.
[body,~,bodya] = imread('everything/Body/maroon.png');
nhues = 10;
huestep = 1/nhues;
hues = 0:huestep:1-huestep;
for hk = 1:nhues
newbody = imtweak(body,'hsy',[hues(hk) 1 1]);
imwrite(newbody,sprintf('body_h%03d.png',round(hk*360)),'alpha',bodya)
end
You might even do multiple hue sweeps with different saturation or lightness factors
[body,~,bodya] = imread('everything/Body/maroon.png');
nhues = 10;
sats = [0.5 1 2];
huestep = 1/nhues;
hues = 0:huestep:1-huestep;
for sk = 1:numel(sats)
for hk = 1:nhues
newbody = imtweak(body,'hsy',[hues(hk) sats(sk) 1]);
imshow(newbody)
imwrite(newbody,sprintf('body_s%02d_h%03d.png',sk,round(hk*360)),'alpha',bodya)
end
end
For cases where you want to incorporate random colorization or colorization from a table, you might try doing a 'hue' or 'color' blend using imblend():
[body,~,bodya] = imread('everything/Body/maroon.png');
CT = parula(10); % a predefined or random color table
s0 = size(body);
for k = 1:size(CT,1)
cpict = colorpict(s0,CT(k,:));
newbody = imblend(cpict,body,1,'color');
imwrite(newbody,sprintf('body_CT%03d.png',k),'alpha',bodya)
end
... or using a random 10x3 color table
Note that using 'hue' or 'color' blends, even random ones, will tend to (roughly) preserve the lightness of the image regions. If you want something that is random and does not preserve anything, then you might try:
[body,~,bodya] = imread('everything/Body/maroon.png');
nframes = 10;
meshsize = 1; % try [1-4]
for k = 1:nframes
newbody = imblend(body,body,1,'bomb',meshsize);
imwrite(newbody,sprintf('body_bomb%03d.png',k),'alpha',bodya)
end
As the meshsize parameter increases above 1, you can expect to see reduced correlation between the colors in the various image regions. As the source image is flat-colored to begin with, the effects may not be as apparent as meshsize is increased. For meshsize > 1, you may see banding in the transition areas between color regions.
There's probably plenty of other options, but let's start with that.

7 commentaires

Another way to do a randomized colorization might be to exploit indexed images:
[body,~,bodya] = imread('everything/Body/maroon.png');
nlevels = 4; % number of colors in original image
bodyind = rgb2ind(body,nlevels+1); % transparent bg occupies an extra color
bodyind = medfilt2(bodyind,[3 3]); % try to clean up any speckles
nframes = 10;
for k = 1:nframes
newmap = rand(nlevels,3); % random color table
newbody = im2uint8(ind2rgb(bodyind,newmap)); % convert back to RGB
imwrite(newbody,sprintf('body_rand%03d.png',k),'alpha',bodya)
end
This may have the effect of defeating any existing anti-aliasing or creating spurious specks along the transitions between regions, but it is generally faster than a comparable 'bomb' blend.
Another possibility is to use a given color table and use all available permutations of colors. Bear in mind that the number of permutations of (e.g.) 4 colors picked from a color table of only 6 colors is 360. The color table cannot be very long.
[body,~,bodya] = imread('everything/Body/maroon.png');
% need to eliminate zero-opacity colors, otherwise there will be duplicates
nlevels = 4; % number of colors in original image
bgidx = 0; % index of invisible background color
borderidx = 3; % index of character border line
bodyind = rgb2ind(body,nlevels+1); % transparent bg occupies an extra color
bodyind(bodyind==bgidx) = borderidx; % remap invisible bg color
bodyind = medfilt2(bodyind,[3 3]); % filter to remove some speckles
% a short color table at least as long as nlevels
% this cannot be longer than about 10 or 11 rows!
CT = hsv(6);
% create index permutation list from given color table
ncolors = size(CT,1);
indexlist = perms(1:ncolors);
indexlist = unique(indexlist(:,ncolors-nlevels+1:end),'rows');
nframes = size(indexlist,1) % this grows rapidly!
for k = 1:nframes
newmap = CT(indexlist(k,:),:);
newbody = im2uint8(ind2rgb(bodyind,newmap));
imwrite(newbody,sprintf('body_perm%04d.png',k),'alpha',bodya)
end
Of course, I shrunk that image down so far that it's hard to tell that the line color is also changing. Of all the methods given, this one is probably going to be the biggest hassle.
MS
MS le 14 Mar 2022
Modifié(e) : MS le 14 Mar 2022
@DGM Thanks for the efforts. It is amazing. The tansition lines are pixelated/losing its thickness in the randomized method as shown. I will test other methods and update soon.
@DGM i m geting an error when i tried to use MIMT tools 1, rgb2hsy 2, if version as shown here. Please let me know your suggestions.
The main MIMT directory has some subdirectories in it. If when you added MIMT/ to your path, you probably didn't include the subdirectories. Just go back and include add the subdirectories as well.
MS
MS le 14 Mar 2022
Modifié(e) : MS le 14 Mar 2022
@DGM Yes, thanks. It worked well. i like the hue/hue rotaion method for preserving the image quality than randomisation method. Thanks again.
Yeah, the random 'bomb' blends can be pretty destructive to image continuity. That's kind of why I ended up naming them that way.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Convert Image Type dans Centre d'aide et File Exchange

Question posée :

MS
le 13 Mar 2022

Commenté :

DGM
le 14 Mar 2022

Community Treasure Hunt

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

Start Hunting!

Translated by