How to set specific color in an image as transparent?
20 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Harald von der Osten
le 28 Déc 2021
Commenté : Harald von der Osten
le 29 Déc 2021
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... )
0 commentaires
Réponse acceptée
Adam Danz
le 28 Déc 2021
Modifié(e) : Adam Danz
le 28 Déc 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 commentaires
Adam Danz
le 28 Déc 2021
I've updated my answer to show how to selectively add transparency to sections of the image.
Plus de réponses (1)
Image Analyst
le 28 Déc 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.
0 commentaires
Voir également
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!