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