Drawing bounding box onto an image without imshow and hold on ...
Afficher commentaires plus anciens
Hi there. I was wondering if its possible to draw a rectangle, whose coordinates is obtained from regoingprops. I however, am not interested to show the image first. What I intend to do is to get the bounding box coordinates form regionprops, and then just superimpose it onto an image without displaying it. I would then like to save the image (saving is ok... just drawing the rectangle got me a bit tangled up :P ). Would appreciate any advice!
The code I'm currently using is as shown above. (It's in a for loop) where 'l' is the counter.
THANKS in advance!!!
f = figure,imshow(frame, 'Border', 'tight');
hold on;
rectangle('Position',s(l).BoundingBox,'EdgeColor','y','LineWidth',2)
print(f, '-r80', '-djpeg', [dirName 'results' '\' jpegFiles(i).name]);
close all;
Réponse acceptée
Plus de réponses (1)
DGM
le 5 Jan 2025
Just because I felt like doing it my way.
% any image
inpict = imread('peppers.png');
% some 2D logical mask of the same page geometry
% there are only two blobs, one each for the named objects
mask = imread('redpepmask.png')>128 ...
| imread('chilipepmask.png')>128;
% get boundingbox list
S = regionprops(mask,'boundingbox');
R = vertcat(S.BoundingBox);
% generate composite image
fgcolor = [1 1 0]; % an I/RGB color tuple
outpict = overlaybb(inpict,R,fgcolor);
% show the composition
imshow(outpict)

function outpict = overlaybb(inpict,R,fgcolor)
% OUTPICT = OVERLAYBB(INPICT,R,FGCOLOR)
% Draw colored rectangles on an image.
%
% INPICT is an I/IA/RGB/RGBA image of any class
% R is a NBLOBSx4 array of bounding box coordinates
% as given by regionprops().
% FGCOLOR is an I/IA/RGB/RGBA tuple of any class.
% This is typically RGB, expressed as unit-scale float.
% Regardless of class, FGCOLOR must be properly-scaled
% for its class.
%
% Output depth is expanded as needed.
% Output class is inherited from INPICT.
% convert to vertex locations (one row per rectangle)
% these lines are tangential to the blob without any overlap
vy = [floor(R(:,2)) ceil(sum(R(:,[2 4]),2))];
vx = [floor(R(:,1)) ceil(sum(R(:,[1 3]),2))];
% generate a composite mask
sz = imsize(inpict,2); % MIMT; size() can't do this directly in R2015x
Rmask = false(sz);
for k = 1:size(R,1)
V = [vx(k,[1 2 2 1 1]); vy(k,[1 1 2 2 1])].';
Rmask = Rmask | brline(sz,V); % MIMT
end
% do the composition
outpict = replacepixels(fgcolor,inpict,Rmask); % MIMT
end
This example relies on some MIMT tools, and is only partially period-accurate for 2015. While the above code and the current MIMT version will run on R2015x, it wouldn't have in 2015.
While an early version of replacepixels() should have been capable of handling this usage, brline() and imsize() were not yet included in MIMT. That said, other relevant composition options such as imoverlay() and labeloverlay() were also not part of IPT yet either. See other solid-color masked composition options circa 2015:
There are other ways to draw the line masks. There were ROI tools available in R2015x (e.g. imline()). It requires the use of a figure, but at least it wouldn't require figure capture. That still doesn't seem very elegant to me.
If thicker lines are needed, the simple offset of vx,vy and the difference of two poly2mask() calls will get the mask without the need for brline(). ... or you can just do it by direct indexing.
If you wanted each box to inherit a color from a color table (sort of like the later labeloverlay()), that's just a minor rearrangement of the steps. Instead of incrementally generating the mask, incrementally generate the composite image.
Catégories
En savoir plus sur Region and Image Properties dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
