How to place pregenerated image (double) on top of an opened image with transparency?

1 visualización (últimos 30 días)
Dear All,
I have a question how to add a scale bar (rooler) on top of the image (the example of result image is below).
In my code i have created the below image by:
1) drawing a black and white rooler and saving it as png file
2) I open both the required image and the rooler image and place one on top of another with appropriate "AlphaData" to get transparency.
Here is the question. How I can omit saving/opening the rooler??? The entire code is beelow the image.
Many thanks for the hepl and ideas
% the script draw a roller(scale bar) in micromiters (um) on imported image
% Options:
% 1) the scale of the rooler is CHANGING depending on the magnification.
% 2) The scale size remains as requested, but the size of the rooler changes
% input arguments:
% image fileName,
% coordinates of a defect,
% magnification
clear all;close all
folderName2save='..\export_with_rooles';
Input arguments
% ID of the frame
frameID="226456454";
% the image on which the rooles has to be placed
fileName='C:\Users\sns\Documents\P-Frames\Defektenanerkenung\MatlabScripts\rooler\image0007.jpg';
%position where the rooler has to beplaces on the image
position=[600,300];% has to be an input argument)
% maginificaitons value used in a microscope to inspect the frames. Relation coeficients um/px are stored in the file Re-scale.excel
magn=5;
% select an option with fixed rooles: "yes" or not "no"
RoolerScale="yes";
% fixed scales of the roole
widthFixed=[100, 200, 300]
TfontSize=14;
Exporting data
filePathR='..\export_with_rooles\temp\'; % file path for the rooler
FileNameR=sprintf("rooler_ID%s_X%d_%s.png",frameID,magn,string(datestr(now,'mm-dd-yyyy HH-MM')));
filePath='..\export_with_rooles\'; % file path for the images with rooler
FileName=sprintf("ID%s_X%d_%s_defects.png",frameID,magn,string(datestr(now,'mm-dd-yyyy HH-MM')))
Reading the magnification factor
my_excel=readtable('C:\Users\sns\Documents\P-Frames\Defektenanerkenung\MatlabScripts\rooler\Re-scale.xlsx',VariableNamingRule='modify');
idx=(my_excel.Magnification==magn); % finding the correct correction factor according to magnification
correctFactor=my_excel.AverageValueOfPixelSize_um_px_(idx) % um/px
clear my_excel idx
Drawing the rooler with changeble scale
%parameters of the rooler
heightR=[15]; % height of the rooler
widthR=[25,50,100,150]; % width in pixels and number of elements of the rooler
TextcorrectFactor=correctFactor;
% if the scale of the rooler has to be fixed:
if RoolerScale=="yes"
widthR=round(widthFixed./correctFactor,0);
TfontSize=12;
end
TextcorrectFactor=correctFactor*0.2;
borders=[5]; % border on top/bottom of the rooler
% generation of the horisontal rooler
rooler=[];
for i=1:length(widthR)
rooleri=[zeros(heightR,widthR(1,i));ones(heightR,widthR(1,i))];
rooleri(end-borders:end,:)=0;
if rem(i,2)==0
rooler=[rooler,flip(rooleri)];
else
rooler=[rooler,rooleri];
end
end
clear i idx my_excel
rooler=[ones(size(rooler));rooler];
roolerHeight=size(rooler,1);
HVrooler=ones(sum(size(rooler))); % rooler canvas
HVrooler(1:roolerHeight,roolerHeight+1:end)=rooler ;
HVrooler(roolerHeight+1:end,1:roolerHeight)=rooler';
% text on the rooler with correction according to the magnification
my_text=string(fix(round(widthR.*correctFactor,0)));
if RoolerScale=="yes"
my_text=string(widthFixed)
end
%rooler saving as a png file and transparency
A=double(HVrooler==0);
f=fullfile(filePathR,FileNameR);
imwrite(HVrooler,f, 'Alpha',A);
f2=fullfile(filePathR,'Alphachanel');
save(f2,'A');
placing the rooler on an image
% reading the image for analysis
Image =imread(fileName);
imshow(Image);
hold on
% loading the pregenerated rooles and the alpha chennel
roolerImage=imread(f);
load(f2,"A")
% combining the rooler and the images
image(position(1,1),position(1,2),roolerImage,"AlphaData",A);
% placeing the velues for the rooler
for i=1:length(my_text)
X=roolerHeight+widthR(1,i)*(i-1)/2+position(1,1);
Y=0+position(1,2);
if i==length(my_text)
my_text(1,i)=append(num2str(my_text(1,i))," (um)");
text(X, Y,my_text(1,i),'Color','b','FontSize',TfontSize)
else
text(X, Y,my_text(1,i),'Color','b','FontSize',TfontSize)
end
end
saving the resulted image with the rooler on top
f=fullfile(folderName2save,FileName);
saveas(gcf,f)

Respuestas (1)

DGM
DGM el 19 de Oct. de 2022
If you're generating a ruler image each time and don't need to save a copy, then just don't save a copy.
folderName2save='';
% ID of the frame
frameID="226456454";
% the image on which the rooles has to be placed
%fileName='C:\Users\sns\Documents\P-Frames\Defektenanerkenung\MatlabScripts\rooler\image0007.jpg';
fileName = 'peppers.png';
%position where the rooler has to beplaces on the image
position=[50,10];% has to be an input argument (adjusted for test image)
% maginificaitons value used in a microscope to inspect the frames. Relation coeficients um/px are stored in the file Re-scale.excel
magn=5;
% select an option with fixed rooles: "yes" or not "no"
RoolerScale="yes";
% fixed scales of the roole
widthFixed=[100, 200, 300]
widthFixed = 1×3
100 200 300
TfontSize=14;
%Exporting data
filePathR='./'; % file path for the rooler
FileNameR=sprintf("rooler_ID%s_X%d_%s.png",frameID,magn,string(datestr(now,'mm-dd-yyyy HH-MM')));
filePath='./'; % file path for the images with rooler
FileName=sprintf("ID%s_X%d_%s_defects.png",frameID,magn,string(datestr(now,'mm-dd-yyyy HH-MM')))
FileName = "ID226456454_X5_10-19-2022 19-22_defects.png"
%Reading the magnification factor
%my_excel=readtable('C:\Users\sns\Documents\P-Frames\Defektenanerkenung\MatlabScripts\rooler\Re-scale.xlsx',VariableNamingRule='modify');
%idx=(my_excel.Magnification==magn); % finding the correct correction factor according to magnification
%correctFactor=my_excel.AverageValueOfPixelSize_um_px_(idx) % um/px
%clear my_excel idx
correctFactor = 2; % idk what's in that file, so i'm just going to guess
%Drawing the rooler with changeble scale
%parameters of the rooler
heightR=[15]; % height of the rooler
widthR=[25,50,100,150]; % width in pixels and number of elements of the rooler
TextcorrectFactor=correctFactor;
% if the scale of the rooler has to be fixed:
if RoolerScale=="yes"
widthR=round(widthFixed./correctFactor,0);
TfontSize=12;
end
TextcorrectFactor=correctFactor*0.2;
borders=[5]; % border on top/bottom of the rooler
% generation of the horisontal rooler
rooler=[];
for i=1:length(widthR)
rooleri=[zeros(heightR,widthR(1,i));ones(heightR,widthR(1,i))];
rooleri(end-borders:end,:)=0;
if rem(i,2)==0
rooler=[rooler,flip(rooleri)];
else
rooler=[rooler,rooleri];
end
end
clear i idx my_excel
rooler=[ones(size(rooler)); rooler];
roolerHeight=size(rooler,1);
HVrooler=ones(sum(size(rooler))); % rooler canvas
HVrooler(1:roolerHeight,roolerHeight+1:end)=rooler ;
HVrooler(roolerHeight+1:end,1:roolerHeight)=rooler';
% text on the rooler with correction according to the magnification
my_text=string(fix(round(widthR.*correctFactor,0)));
if RoolerScale=="yes"
my_text=string(widthFixed)
end
my_text = 1×3 string array
"100" "200" "300"
% create transparency
A = double(HVrooler==0); % this is the alpha
%placing the rooler on an image
% reading the image for analysis
Image = imread(fileName);
imshow(Image);
hold on
% combining the rooler and the images
% just use the generated images
image(position(1,1),position(1,2),HVrooler,"AlphaData",A);
% placeing the velues for the rooler
for i=1:length(my_text)
X=roolerHeight+widthR(1,i)*(i-1)/2+position(1,1);
Y=0+position(1,2);
if i==length(my_text)
my_text(1,i)=append(num2str(my_text(1,i))," (um)");
text(X, Y,my_text(1,i),'Color','b','FontSize',TfontSize)
else
text(X, Y,my_text(1,i),'Color','b','FontSize',TfontSize)
end
end
%saving the resulted image with the rooler on top
f=fullfile(folderName2save,FileName);
saveas(gcf,f)
Bear in mind that the color of the ruler overlay now depends on the current colormap. The same goes for any single-channel image.
If you wanted to save the images with transparency, you don't need to save alpha as a .mat file. PNG supports alpha content; you'd just have to read it when you call imread().

Categorías

Más información sobre Lighting, Transparency, and Shading en Help Center y File Exchange.

Productos


Versión

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by