create a grid and label on the images Using for loop
Show older comments
% Using label contour
I = imread('people2.jpg');
position = [0 0 330 400;
350 0 329 400;
690 0 330 400;
1032 0 330 400;
1370 0 330 400;
1720 0 330 400;
1370 0 330 400;
1370 0 330 400;
1370 0 330 400;
1370 0 330 400;
1370 0 330 400;
1370 0 330 400;
1370 0 330 400;
1370 0 330 400;
1370 0 330 400;
1370 0 330 400;
1370 0 330 400;
1370 0 330 400];
label_str = cell(3,18);
conf_val = [1 2 3 4 5 6;
7 8 9 10 11 12;
13 14 15 16 17 18];
for ii=3:18
label_str{ii} = ['Label: ' num2str(conf_val(ii),'%0.0f') ''];
ahist=rgb2gray(I);
end
RGB = insertObjectAnnotation(I,'rectangle',position,label_str,'TextBoxOpacity',0.9,'FontSize',28,'Color',{'Red'});
imshow(RGB)
2 Comments
DGM
on 24 Apr 2021
You did provide a picture and some code, but you might want to actually provide an explanation so that we know the context. Is the picture an example of the intended output? Is it the output of the attached code? Is it the source image that you're starting with? Is there a problem with the code? What is ahist, and why is it being recalculated repeatedly in a loop and then never used?
Dawit Abay
on 24 Apr 2021
Answers (1)
DGM
on 24 Apr 2021
Well okay, if you don't want to explain anything, I'll just make my own assumptions. You said you want to create a grid, but the image already has a grid. I'll assume that you want to replace it. Of course, this image grid isn't exactly regular, so it would be easier to start with the source images and tile them uniformly to begin with. I'll remove it anyway, but the recommendation still stands. If you have the source images, it'd be better to use them.
I'm going to use nonstandard tools to do this, because I can.
% you said you want to create a grid, so i'm going to assume you want to get rid of the old one
faces = imread('faaaaaces.jpg'); % read the image
faces = addborder(faces,[10 5 3 3],255); % add an outer border to try to align the tiling as close as possible
% detile into a 4D stack so i can work on tiles instead of dealing with x/y offsets
% crop off the remaining borders so that we now have nothing but a clean stack of faces
facestack = imdetile(faces,[3 6]);
facestack = cropborder(facestack,[15 5]);
s = size(facestack);
% i'm going to assume that the ideal goal is to not overlay labels onto the existing image data
% pad the stack to make room for labels beneath each image
facestack = padarray(facestack,[16 0],0,'post'); % height equal to font
% add labels to the bottom of each image
for f=1:s(4)
labelpict = textim(sprintf('This is Face #%d',f),'ibm-iso-16x8')*255;
facestack(s(1)+1:end,10+(1:size(labelpict,2)),:,f) = repmat(labelpict,[1 1 size(facestack,3)]);
end
% add a colored border around frames
bordercolor = [0.585 0.498 0.974];
borderwidth = 10;
facestack = addborder(facestack,floor(borderwidth/2),bordercolor,'normalized');
% retile the images
outpict = imtile(facestack,[3 6]);
% add an extra border thickness so that the borders are uniform
outpict = addborder(outpict,floor(borderwidth/2),bordercolor,'normalized');
% show the image
imshow2(outpict,'tools')

The images are now on a regular grid spacing, the grid is of user-defined color and width, and there are unique labels for each image.
If you wanted different label colors or text sizes, consider the example edit:
fgc = [0 0 0];
bgc = [0.698 0.71475 0.989];
facestack = padarray(facestack,[32 0],0,'post'); % height equal to font
% add labels to the bottom of each image
for f=1:s(4)
tm = textim(sprintf('This is Face #%d',f),'wyse-700b');
labelpict = repmat(tm,[1 1 size(facestack,3)]);
labelpict = replacepixels(fgc,labelpict,tm);
labelpict = replacepixels(bgc,labelpict,~tm);
labelpict = cat(2,colorpict([32 10 3],bgc),labelpict,colorpict([32 s(2)-size(labelpict,2)-10 3],bgc));
facestack(s(1)+1:end,:,:,f) = labelpict*255;
end

Many of these functions are from the MIMT. This task can certainly be accomplished without them, but I have no reason to put forth that effort.
Categories
Find more on Face Detection in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!