How to set specific color in an image as transparent?

34 views (last 30 days)
In this image:
I would like to set the blue color rgb = [61 38 168] as transparent and save it to a new png. How can I do this? Thanks a lot
EDIT: additional information
Thanks to your answers. Because you ask yourself, what I want with this transparency I want to give you more information: First, I want to define the blue color as transparent:
to be able to georeference it like this:
Again, thanks for your help (I did this using Photoshop, but I would prefer doing this with Matlab... )

Accepted Answer

Adam Danz
Adam Danz on 28 Dec 2021
Edited: Adam Danz on 28 Dec 2021
Load image
img = 'image.png';
I = imread(img);
figure()
tiledlayout(1,2)
ax1 = nexttile();
imshow(I,'Parent',ax1)
Isolate target color
targetColor = [61 38 168];
isTarget = I(:,:,1) == targetColor(1) & I(:,:,2) == targetColor(2) & I(:,:,3) == targetColor(3);
isTargetArray = repelem(isTarget, 1,1,3);
Updated: Plot isolated section on top of another image
ax2 = nexttile();
hold(ax2, 'on')
imshow(rand(size(I)),'Parent',ax2) % noise image
% Isolate section
img2 = imshow(I,'Parent',ax2);
% Set transparency
set(img2, 'AlphaDataMapping', 'none', 'AlphaData', double(~isTargetArray(:,:,1)));
  5 Comments
Adam Danz
Adam Danz on 28 Dec 2021
I've updated my answer to show how to selectively add transparency to sections of the image.

Sign in to comment.

More Answers (1)

Image Analyst
Image Analyst on 28 Dec 2021
Use imoverlay
maskColor = [61 38 168];
% Create a new image where the mask is the specified color over the original rgb image.
image2 = imoverlay(originalRGBImage, maskImage, 'Color', maskColor);
imshow(image2);
where mask is your image and it's 0 where you want the original image to show through, the mask is 1 where you want the specified color. The color will be solid, 100% opaque.

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!