bio_img_matlab

The MATLAB Blog

Practical Advice for People on the Leading Edge

Add a “Swiftie” touch to your images with MATLAB

Today’s post is a bit different from my regular content over on the Startups, Accelerators, & Entrepreneurs Blog. I’m Madeline Carleton, and I’m the marketing lead for MathWorks Startup Program. Usually, I write about the amazing things startups are building with MATLAB and Simulink, but this time, I'm excited to be in the command window.
Its the color seen around the world! Or at least, by the millions of fans who follow Taylor Swift. The artist recently announced her upcoming album, this one with a sparkly orange aesthetic to it. It didn’t take long for the internet to run wild, with brands from Elmo to FedEx reimaging their own logos and photos with the vibrant theme.
As a millennial on social media, I couldn’t resist jumping on this trend. My first thought? The MathWorks logo, our signature L-shaped membrane - a place where I already see a splash of orange every day. Could I update the orange hues to match Taylor’s new shade?
I wondered if there was a simple, automated way to do this using the image processing capabilities within MATLAB. I’m not exactly a very experienced coder, so I turned to MATLAB Copilot, built right into my MATLAB Online window, for help. My initial prompt: “I want to write a script that replaces part of a logo image that is orange with a different orange color.” In response, it gave me sufficient code that could swap out a specific orange hue with another color hue. However, I wanted to replace the original orange hue with something more graphic, not flat; I wanted the sparkle details.
After a quick clarification, MATLAB Copilot generated a revised code, which I copied into the MATLAB Online command window. After a few tweaks of finding the RGB values and adjusting the tolerance for the right amount of coverage, I ended up with an L-shaped membrane that’s both socially relevant and unmistakably “Swiftie”!
% Read the original image and the replacement image
[originalImg, ~,alpha] = imread('membrane.png');
replacementImg = imread('sparkle.png');
 
 
% Define the RGB values for the orange color you want to replace
targetOrange = [205, 82, 50]; % Example RGB for orange
tolerance = 35; % Tolerance for color matching
 
% Create a mask for the orange color
mask = (abs(double(originalImg(:,:,1)) - targetOrange(1)) < tolerance) & ...
(abs(double(originalImg(:,:,2)) - targetOrange(2)) < tolerance) & ...
(abs(double(originalImg(:,:,3)) - targetOrange(3)) < tolerance);
 
% Resize the replacement image to match the size of the mask
replacementImgResized = imresize(replacementImg, size(originalImg(:,:,1)));
 
% Replace the orange regions with the replacement image
modifiedImage = originalImg;
for channel = 1:3
modifiedImage(:,:,channel) = modifiedImage(:,:,channel) .* uint8(~mask) + ...
replacementImgResized(:,:,channel) .* uint8(mask);
end
 
% Display images
figure('Position', [0 0 1024 512])
tiledlayout(1,2)
 
nexttile
origImgh = imshow(originalImg);
origImgh.AlphaData = alpha;
title('Original Image')
 
% Display the modified image
nexttile
img = imshow(modifiedImage);
img.AlphaData = alpha;
title('Image with Replaced Orange Regions');
Want to jump on the trend and create your own Taylor Swift-inspired image? Here’s the step-by-step:
1. Prepare Your Files
  • Original Image: I used MathWorks' L-shaped logo (e.g., membrane.png) but you could find your own image.
  • Orange Sparkle Texture: An image file (I grabbed mine from a Google image search) with the desired sparkly orange texture (e.g., sparkle.png), ideally matching or a bit larger than the orange area in the logo.
% Read the original image and the replacement image
originalImg = imread('membrane.png');
replacementImg = imread('sparkle.png');
2. Get the RGB value for the color in your image that you want to replace
You can use a free hue finder online to find the RBG value. You can adjust the tolerance to cover a larger area of slightly varying color – my sweet spot was 35%.
% Define the RGB values for the orange color you want to replace
targetOrange = [205, 82, 50]; % Example RGB for orange
tolerance = 35; % Tolerance for color matching
3.Create the mask and do the replacement
Our image has three color channels so we need to loop over all of them
% Create a mask for the orange color
mask = (abs(double(originalImg(:,:,1)) - targetOrange(1)) < tolerance) & ...
(abs(double(originalImg(:,:,2)) - targetOrange(2)) < tolerance) & ...
(abs(double(originalImg(:,:,3)) - targetOrange(3)) < tolerance);
 
% Resize the replacement image to match the size of the mask
replacementImgResized = imresize(replacementImg, size(originalImg(:,:,1)));
 
% Replace the orange regions with the replacement image
for channel = 1:3
originalImg(:,:,channel) = originalImg(:,:,channel) .* uint8(~mask) + ...
replacementImgResized(:,:,channel) .* uint8(mask);
end
4. Display the images
We want to display the images side by side so we set up a tiledlayout.
% Display images
figure('Position', [0 0 1024 512])
tiledlayout(1,2)
 
nexttile
origImgh = imshow(originalImg);
origImgh.AlphaData = alpha;
title('Original Image')
 
% Display the modified image
nexttile
img = imshow(modifiedImage);
img.AlphaData = alpha;
title('Image with Replaced Orange Regions');
|
  • print

Comments

To leave a comment, please click here to sign in to your MathWorks Account or create a new one.