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

1 view (last 30 days)
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]);
Benjamin Thompson
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?

Sign in to comment.

Accepted Answer

DGM on 13 Mar 2022
Edited: DGM on 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.
[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]);
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]);
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');
... 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);
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.
MS on 14 Mar 2022
Edited: MS on 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 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.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!