how do I randomly assign an effect to image

N
Nehemiah · Sep 28, 2020 · 1.9K views
Question
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 PhD Expert
Answered Aug 24, 2026

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)
100% Run Guarantee 3-Hour Fast-Track Delivery

Need a Custom Version or Complete Simulation for This Problem?

Our 500+ PhD engineers build, debug, and optimize working MATLAB scripts and Simulink (.slx) models tailored to your exact assignment rubrics with zero plagiarism.

Tested on MATLAB R2024b / R2026a
Turnitin 0% Plagiarism Report
Free 7-Day Revisions Guarantee
Have a different question? Ask here

Get a Free Consultation or a Sample Assignment Review!