is it possible to add/paint multiple colors(more than 100 up to 10000) to an RGBA image in MATLAB?
2 views (last 30 days)
Show older comments
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
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/925724/image.png)
[body,~,bodya] = imread('Body/maroon.png');
bodypurple = imtweak(body,'hsl',[-0.28 1 1]);
imshow(bodypurple)
imwrite(bodypurple,'purple.png','alpha',bodya)
2 Comments
Benjamin Thompson
on 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?
Accepted Answer
DGM
on 13 Mar 2022
Edited: DGM
on 13 Mar 2022
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
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/925914/image.png)
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
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/925919/image.png)
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
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/925879/image.png)
... or using a random 10x3 color table
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/925884/image.png)
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
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/925909/image.png)
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 Comments
DGM
on 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.
More Answers (0)
See Also
Categories
Find more on Convert Image Type in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!