필터 지우기
필터 지우기

Displaying alpha values written into an image file

조회 수: 20 (최근 30일)
Borna Mahmoudian
Borna Mahmoudian 2022년 8월 4일
편집: DGM 2023년 9월 12일
Background:
I have an image of which a specific ROI (a rectangle) is of interest to me. I would like to make this ROI opaque, while all other pixels transparent. Then I would like to save this image after applying the specific the pixel transparency values, and read the image at a later time.
Problem:
I have done the above, and can visualize my image with the appropriate transparency values. However after I save this image with the transparency values, when I read the image (using imread) I have to explicitly indicate the alpha matrix to apply the transparency values.
Question:
Can the alpha values "baked" in to the image and can they be displayed without being read out every time? I am working in a software that is not allowing my to apply an alpha matrix to the images i'm displaying.
If i read the file and just display it, it does not work (below)
%Read and display your image
image(imread('test.png'))
However if I read it and explicitly indicate the alpha matrix, this presents it correctly.
%Read image and extract alpha values
[test_im,map,alpha] = imread('test.png');
%explicitly indicate alpha values
image(test_im,'AlphaData',alpha)
Code:
%my image
target_img = imread('BlueTargets.png');
%center coordinates of my ROI
top_left_coor = [530 270];
% create the 4 corners of your roi from the center coordinate
center_coor = repmat(top_left_coor,[4 1]);
coord_signs = [-1 -1; 1 -1; 1 1; -1 1];
%making the assumption that the sides/2 of my ROI are 180 pixels
transform_to_corner =repmat(180,[4 2]);
four_coord = center_coor + (transform_to_corner .* coord_signs);
%make region of interest given your coordinates
ROI = roipoly(target_img,four_coord(:,1),four_coord(:,2));
ROI = double(ROI);
%visualize your image with the desired transparency ROI.
image(target_img,'AlphaData',ROI)
% writing the image with the transparency onto a file.
imwrite(target_img, 'test.png', 'Alpha',ROI);
%Read and display your image
image(imread('test.png'))

답변 (2개)

Saarthak Gupta
Saarthak Gupta 2023년 9월 12일
Hi Borna,
I understand you are trying to merge the alpha values of an image into the image file itself, such that alpha is not stored as a separate channel.
By inspecting the ROI matrix, you can observe that it is a binary matrix, i.e., its elements are either 0 or 1
To merge the alpha values with the image data, you can simply do an element-wise multiplication of the ROI matrix with the original image (after correctly casting the data types) and save it subsequently.
The following code can help you achieve the desired result:
%my image
target_img = imread('pears.png');
%center coordinates of my ROI
top_left_coor = [530 270];
% create the 4 corners of your roi from the center coordinate
center_coor = repmat(top_left_coor,[4 1]);
coord_signs = [-1 -1; 1 -1; 1 1; -1 1];
%making the assumption that the sides/2 of my ROI are 180 pixels
transform_to_corner =repmat(180,[4 2]);
four_coord = center_coor + (transform_to_corner .* coord_signs);
%make region of interest given your coordinates
ROI = roipoly(target_img,four_coord(:,1),four_coord(:,2));
ROI = uint8(ROI);
% Element-wise multiplication of the alpha channel with the image
image_with_alpha = target_img.*ROI;
image(image_with_alpha);
imwrite(image_with_alpha, 'test.png');
image(imread('test.png'));
Original image:
Image with alpha values applied (or baked in):
Please refer to the following MATLAB documentation for more details:

DGM
DGM 2023년 9월 12일
편집: DGM 2023년 9월 12일
MATLAB has no image viewer which can conveniently handle isolated display of transparent images. If you try to display the image using the 'alphadata' property of the image object, the result you get will simply be the image composed with whatever the axes background color is (probably white). That usually makes it difficult to impossible to tell the difference between white areas and transparent areas. .
MIMT does have tools and a viewer that support RGBA images. You'll still have to usually work around imread()/imwrite(), though.
That said, if you just want to compose the image to eliminate the alpha channel, then it depends what the background is to be.
% the inputs
[inpict,~,alpha] = imread('peppers_rgba.png'); % an image (uint8)
% do it the old way that doesn't really work anyway
hi = imshow(inpict,'border','tight');
hi.AlphaData = alpha; % image is always composed with whatever is in the figure
Again, doing in-figure composition is problematic and cumbersome. Just do the composition outside the figure.
% do basic multiplicative composition with a solid color background
BG = [0 0.447 0.741]; % a background color (unit-scale float)
mask = im2double(alpha);
outpict1 = (1-mask).*permute(BG,[1 3 2]) + mask.*im2double(inpict);
outpict1 = im2uint8(outpict1);
imshow(outpict1,'border','tight')
Okay, that's more flexible, but it's still cumbersome, and it's still hard to tell the difference between transparent regions and opaque regions with the same color as the background. That's why image editors use some sort of matting to help make transparent regions visually distinct.
% do basic multiplicative composition with a checkerboard background
sout = size(inpict);
squaresize = [10 10];
xx = mod(0:(sout(2)-1),squaresize(2)*2)<squaresize(2);
yy = mod(0:(sout(1)-1),squaresize(1)*2)<squaresize(1);
BG = im2double(0.3 + bsxfun(@xor,xx,yy')*0.4);
mask = im2double(alpha);
outpict2 = (1-mask).*BG + mask.*im2double(inpict);
outpict2 = im2uint8(outpict2);
imshow(outpict2,'border','tight')
Okay, that's helpful, but it's still cumbersome. If adding an extra parameter to an image()/imshow() call is annoying, this is just ridiculous.
Again, MIMT has tools for doing this succinctly. The three examples given can be reduced:
% the inputs
[inpict,~,alpha] = imread('peppers_rgba.png'); % an image (uint8)
% view the image as RGBA with checkerboard matting
rgbapict = joinalpha(inpict,alpha);
imshow2(rgbapict) % imshow2() handles RGBA
% a solid color background
BG = [0 0.447 0.741]; % a background color (unit-scale float)
outpict1 = replacepixels(inpict,BG,alpha); % class-agnostic, all in one line
imshow2(outpict1)
% a checkerboard background
outpict2 = alphasafe(joinalpha(inpict,alpha));
imshow2(outpict2)
There are several threads on the topic. Bear in mind though, that the composed image contains less information than the image with its alpha intact. They are not equivalent, so if you're just doing this for the sake of MATLAB's limited image viewer, then you're probably shooting yourself in the foot.

카테고리

Help CenterFile Exchange에서 Image Processing Toolbox에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by