Overlay two images of different type
Mostrar comentarios más antiguos
I have a uint8 image and another logical image of the same dimensions, containing edges found from the first one. I want to make the white pixels (edges) of the logical image red (so must be converted to rgb) and then superimpose it over the original uint8 image. Transparency is not a requirement, I want the simplest code possible! Thanks everyone.
Respuesta aceptada
Más respuestas (5)
Matt J
el 15 de En. de 2015
0 votos
This FEX file can do that,
I don't know if there are simpler ways.
Image Analyst
el 15 de En. de 2015
0 votos
See these two links for demos and code:
Also check out the functions imshowpair() and imfuse() in the Image Processing Toolbox.
2 comentarios
Xen
el 17 de En. de 2015
Image Analyst
el 18 de En. de 2015
Just call export_fig after you've displayed your image and plotted dots over it. Check the file exchange for export_fig().
Image Analyst
el 15 de En. de 2015
Another simple way is to just get the coordinates of your binary image and then call plot to lay down a dot.
[rows, columns] = find(binaryImage); % Returns [y, x] since rows = y.
hold on;
plot(columns, rows, 'r.', 'MarkerSize', 1); % Increase marker size if you want.
4 comentarios
Matt J
el 15 de En. de 2015
Or scatter()
Image Analyst
el 18 de En. de 2015
Try
plot(x, y, 'r.'); % Plot a single dot.
DGM
el 13 de Feb. de 2023
The apparent size of a rendered '.' marker may be 1px, but they will only be displayed correctly if their spacing is also 1px. This requires the image is to be rendered at 1:1. If it's not scaled 1:1, the colored area will be filled with interference patterns. You'd have to use truesize() to set that. That also means that this won't work for large images or docked figures.
Likewise, trying to save the image+scatter is likely going to end up with a result that's unpredictably sized and/or padded depending on version, environment, and the method used.
This is also relatively very slow. While the difference between 6ms and 600ms might not be a big deal for something that has to happen once or twice, these compromises are unnecessary.
It's pretty clever though.
Haripriya Sharma
el 15 de Abr. de 2016
0 votos
MATLAB R2016a introduced "imoverlay" function in Image Processing Toolbox to accomplish this. For more information please refer to the following documentation page: http://www.mathworks.com/help/images/ref/imoverlay.html
2 comentarios
Xen
el 15 de Abr. de 2016
Hmmm. That's one of the unfortunate hazards of the FEX, I suppose. I've been using a function called imoverlay from the FEX for a few years now and my mfile library is now replete with it. If I now want access to R2016a's native imoverlay, or if I give my code to someone who does, I'll have to do a lot of search/replacing :-(
This was never a complicated question, even in 2015. This is concise and works for logical and numeric masks. The background can be I/RGB, and so can the foreground color tuple.
% an image presumed to be uint8 (I or RGB works)
inpict = imread('peppers.png'); % uint8 (RGB)
%inpict = rgb2gray(inpict); % convert to gray (I)
% a mask and foreground color tuple
% mask can be numeric or logical
mask = imread('redpepmask.png');
fgcolor = [0.5 0 1]; % an I/RGB tuple in unit-scale
% do basic multiplicative composition
% since we're living in 2015, this uses bsxfun()
mask = im2double(mask);
fgc = bsxfun(@times,mask,permute(fgcolor,[1 3 2]));
bgc = bsxfun(@times,(1-mask),im2double(inpict));
outpict = im2uint8(bsxfun(@plus,fgc,bgc));
imshow(outpict)
If you really absolutely wanted code that works on nothing but logical masks, you can do that too! Believe it or not, this isn't significantly faster than doing all that multiplication.
% an image presumed to be uint8 (I or RGB works)
inpict = imread('peppers.png'); % uint8 (RGB)
%inpict = rgb2gray(inpict); % convert to gray (I)
% a mask and foreground color tuple
% mask must be logical
mask = imread('redpepmask.png')>128;
fgcolor = [0.5 0 1]; % an I/RGB tuple in unit-scale
fgchans = numel(fgcolor);
bgchans = size(inpict,3);
outpict = zeros(size(inpict));
for c = 1:max(fgchans,bgchans)
thischan = im2double(inpict(:,:,min(c,bgchans)));
thischan(mask) = fgcolor(min(c,fgchans));
outpict(:,:,c) = thischan;
end
outpict = im2uint8(outpict);
imshow(outpict)
While FEX imoverlay() existed, that's basically a simple scalar alpha composition, so it wouldn't easily work for a masked task. The contemporary IPT imoverlay() would probably satisfy OP's needs, but it didn't exist then.
Circa 2015, MIMT did have tools to do this, though I don't remember if everything had been consolidated into replacepixels() yet. If nothing else, it should have at least supported logical masks. That said, even the current version of replacepixels() works at least back to R2009b. So if you wanted the "simplest" (easiest to use, fewest lines needed, fewest restrictions to accomodate), then MIMT replacepixels() would take the cake in 2015, same as it does today.
% an image of any class (I or RGB works)
inpict = imread('peppers.png'); % uint8 (RGB)
%inpict = rgb2gray(inpict); % convert to gray (I)
% a mask and foreground color tuple
% mask can be numeric or logical
mask = imread('redpepmask.png');
fgcolor = [0.5 0 1]; % an I/RGB tuple in unit-scale
% use MIMT replacepixels(FG,BG,mask)
% FG and BG can be an I/IA/RGB/RGBA/RGBAAA image or tuple
% mask can be an I/RGB image or tuple
% mask can be logical or numeric
% output class is inherited from BG
outpict = replacepixels(fgcolor,inpict,mask); % one line
imshow(outpict)
IPT imoverlay is limited in that it really can only do logical operations (no smooth masks, no transparency), and can only do so with (at most) a color tuple as the foreground. So even though IPT imoverlay() exists today, I can't help but see it as a tiny fraction of an answer to general masked composition.
Categorías
Más información sobre Image Processing Toolbox en Centro de ayuda y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

