How to set specific color in an image as transparent?

22 ビュー (過去 30 日間)
Harald von der Osten
Harald von der Osten 2021 年 12 月 28 日
コメント済み: Harald von der Osten 2021 年 12 月 29 日
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... )

採用された回答

Adam Danz
Adam Danz 2021 年 12 月 28 日
編集済み: Adam Danz 2021 年 12 月 28 日
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 件のコメント
Adam Danz
Adam Danz 2021 年 12 月 28 日
I've updated my answer to show how to selectively add transparency to sections of the image.
Harald von der Osten
Harald von der Osten 2021 年 12 月 29 日
thanks a lot, dear Adam Danz

サインインしてコメントする。

その他の回答 (1 件)

Image Analyst
Image Analyst 2021 年 12 月 28 日
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.

製品


リリース

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by