how do I randomly assign an effect to image

Illustration
Nehemiah - 2020-09-28T12:06:19+00:00
Question: how do I randomly assign an effect to image

my code goes something like this: rgbImage = imread ('bunny.png'); %black text image saying "bunny" rgbImage2 = imread ('dragon.png'); %black text image saying dragon theWord = rgbImage; theWord2 = rgbImage2; %% block of code to change the color of bunny image (1) % turn black text image to cyan theWord(:,:,3)=255; theWord(:,:,2)=255; %turn black text to red (2) theWord(:,:,1)=255; theWord(:,:,2)=0; %% repeat for dragon image theWord2(:,:,3)=255; theWord2(:,:,2)=255; theWord2(:,:,1)=255; theWord2(:,:,1) = 0; I want to create a loop using randi where when whatever image is presented, it assigns a random (color) effect to it. I should I go about it so that I can use randi?   I am having a hard time since the images I am using are actually a bit more "messy" so because I have to play around with the colors, the actual text block I wrote to change the color is around 5 lines. I just shortened them a bit for simplicity.

Expert Answer

Profile picture of John Michell John Michell answered . 2025-11-20

Get a mask and then change the colors.

[r, g, b] = imsplit(rgbImage);
mask = any(rgbImage, 3)
randomColor = floor(255 * rand(1,3));
r(mask) = randomColor(1);
g(mask) = randomColor(2);
b(mask) = randomColor(3);
rgbImage = cat(3, r, b, g);
imshow(rgbImage)


Not satisfied with the answer ?? ASK NOW

Get a Free Consultation or a Sample Assignment Review!