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

1 visualizzazione (ultimi 30 giorni)
MS il 13 Mar 2022
Commentato: DGM il 14 Mar 2022
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
bodypurple = imtweak(body,'hsl',[-0.28 1 1]);
imshow(bodypurple)
imwrite(bodypurple,'purple.png','alpha',bodya)
##### 2 CommentiMostra NessunoNascondi Nessuno
MS il 13 Mar 2022
Benjamin Thompson il 13 Mar 2022
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?

Accedi per commentare.

### Risposta accettata

DGM il 13 Mar 2022
Modificato: DGM il 13 Mar 2022
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.
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
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():
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:
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.
##### 7 CommentiMostra 5 commenti meno recentiNascondi 5 commenti meno recenti
MS il 14 Mar 2022
Modificato: MS il 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.
DGM il 14 Mar 2022
Yeah, the random 'bomb' blends can be pretty destructive to image continuity. That's kind of why I ended up naming them that way.

Accedi per commentare.

### Categorie

Scopri di più su Convert Image Type in Help Center e File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by